1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchPortletPreferencesException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.bean.InitializingBean;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.Session;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portal.kernel.util.ListUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.model.PortletPreferences;
41  import com.liferay.portal.model.impl.PortletPreferencesImpl;
42  import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
43  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  /**
54   * <a href="PortletPreferencesPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class PortletPreferencesPersistenceImpl extends BasePersistenceImpl
60      implements PortletPreferencesPersistence, InitializingBean {
61      public PortletPreferences create(long portletPreferencesId) {
62          PortletPreferences portletPreferences = new PortletPreferencesImpl();
63  
64          portletPreferences.setNew(true);
65          portletPreferences.setPrimaryKey(portletPreferencesId);
66  
67          return portletPreferences;
68      }
69  
70      public PortletPreferences remove(long portletPreferencesId)
71          throws NoSuchPortletPreferencesException, SystemException {
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              PortletPreferences portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
78                      new Long(portletPreferencesId));
79  
80              if (portletPreferences == null) {
81                  if (_log.isWarnEnabled()) {
82                      _log.warn(
83                          "No PortletPreferences exists with the primary key " +
84                          portletPreferencesId);
85                  }
86  
87                  throw new NoSuchPortletPreferencesException(
88                      "No PortletPreferences exists with the primary key " +
89                      portletPreferencesId);
90              }
91  
92              return remove(portletPreferences);
93          }
94          catch (NoSuchPortletPreferencesException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public PortletPreferences remove(PortletPreferences portletPreferences)
106         throws SystemException {
107         if (_listeners.length > 0) {
108             for (ModelListener listener : _listeners) {
109                 listener.onBeforeRemove(portletPreferences);
110             }
111         }
112 
113         portletPreferences = removeImpl(portletPreferences);
114 
115         if (_listeners.length > 0) {
116             for (ModelListener listener : _listeners) {
117                 listener.onAfterRemove(portletPreferences);
118             }
119         }
120 
121         return portletPreferences;
122     }
123 
124     protected PortletPreferences removeImpl(
125         PortletPreferences portletPreferences) throws SystemException {
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             session.delete(portletPreferences);
132 
133             session.flush();
134 
135             return portletPreferences;
136         }
137         catch (Exception e) {
138             throw processException(e);
139         }
140         finally {
141             closeSession(session);
142 
143             FinderCacheUtil.clearCache(PortletPreferences.class.getName());
144         }
145     }
146 
147     /**
148      * @deprecated Use <code>update(PortletPreferences portletPreferences, boolean merge)</code>.
149      */
150     public PortletPreferences update(PortletPreferences portletPreferences)
151         throws SystemException {
152         if (_log.isWarnEnabled()) {
153             _log.warn(
154                 "Using the deprecated update(PortletPreferences portletPreferences) method. Use update(PortletPreferences portletPreferences, boolean merge) instead.");
155         }
156 
157         return update(portletPreferences, false);
158     }
159 
160     /**
161      * Add, update, or merge, the entity. This method also calls the model
162      * listeners to trigger the proper events associated with adding, deleting,
163      * or updating an entity.
164      *
165      * @param        portletPreferences the entity to add, update, or merge
166      * @param        merge boolean value for whether to merge the entity. The
167      *                default value is false. Setting merge to true is more
168      *                expensive and should only be true when portletPreferences is
169      *                transient. See LEP-5473 for a detailed discussion of this
170      *                method.
171      * @return        true if the portlet can be displayed via Ajax
172      */
173     public PortletPreferences update(PortletPreferences portletPreferences,
174         boolean merge) throws SystemException {
175         boolean isNew = portletPreferences.isNew();
176 
177         if (_listeners.length > 0) {
178             for (ModelListener listener : _listeners) {
179                 if (isNew) {
180                     listener.onBeforeCreate(portletPreferences);
181                 }
182                 else {
183                     listener.onBeforeUpdate(portletPreferences);
184                 }
185             }
186         }
187 
188         portletPreferences = updateImpl(portletPreferences, merge);
189 
190         if (_listeners.length > 0) {
191             for (ModelListener listener : _listeners) {
192                 if (isNew) {
193                     listener.onAfterCreate(portletPreferences);
194                 }
195                 else {
196                     listener.onAfterUpdate(portletPreferences);
197                 }
198             }
199         }
200 
201         return portletPreferences;
202     }
203 
204     public PortletPreferences updateImpl(
205         com.liferay.portal.model.PortletPreferences portletPreferences,
206         boolean merge) throws SystemException {
207         Session session = null;
208 
209         try {
210             session = openSession();
211 
212             if (merge) {
213                 session.merge(portletPreferences);
214             }
215             else {
216                 if (portletPreferences.isNew()) {
217                     session.save(portletPreferences);
218                 }
219             }
220 
221             session.flush();
222 
223             portletPreferences.setNew(false);
224 
225             return portletPreferences;
226         }
227         catch (Exception e) {
228             throw processException(e);
229         }
230         finally {
231             closeSession(session);
232 
233             FinderCacheUtil.clearCache(PortletPreferences.class.getName());
234         }
235     }
236 
237     public PortletPreferences findByPrimaryKey(long portletPreferencesId)
238         throws NoSuchPortletPreferencesException, SystemException {
239         PortletPreferences portletPreferences = fetchByPrimaryKey(portletPreferencesId);
240 
241         if (portletPreferences == null) {
242             if (_log.isWarnEnabled()) {
243                 _log.warn("No PortletPreferences exists with the primary key " +
244                     portletPreferencesId);
245             }
246 
247             throw new NoSuchPortletPreferencesException(
248                 "No PortletPreferences exists with the primary key " +
249                 portletPreferencesId);
250         }
251 
252         return portletPreferences;
253     }
254 
255     public PortletPreferences fetchByPrimaryKey(long portletPreferencesId)
256         throws SystemException {
257         Session session = null;
258 
259         try {
260             session = openSession();
261 
262             return (PortletPreferences)session.get(PortletPreferencesImpl.class,
263                 new Long(portletPreferencesId));
264         }
265         catch (Exception e) {
266             throw processException(e);
267         }
268         finally {
269             closeSession(session);
270         }
271     }
272 
273     public List<PortletPreferences> findByPlid(long plid)
274         throws SystemException {
275         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
276         String finderClassName = PortletPreferences.class.getName();
277         String finderMethodName = "findByPlid";
278         String[] finderParams = new String[] { Long.class.getName() };
279         Object[] finderArgs = new Object[] { new Long(plid) };
280 
281         Object result = null;
282 
283         if (finderClassNameCacheEnabled) {
284             result = FinderCacheUtil.getResult(finderClassName,
285                     finderMethodName, finderParams, finderArgs, this);
286         }
287 
288         if (result == null) {
289             Session session = null;
290 
291             try {
292                 session = openSession();
293 
294                 StringBuilder query = new StringBuilder();
295 
296                 query.append(
297                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
298 
299                 query.append("plid = ?");
300 
301                 query.append(" ");
302 
303                 Query q = session.createQuery(query.toString());
304 
305                 QueryPos qPos = QueryPos.getInstance(q);
306 
307                 qPos.add(plid);
308 
309                 List<PortletPreferences> list = q.list();
310 
311                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
312                     finderClassName, finderMethodName, finderParams,
313                     finderArgs, list);
314 
315                 return list;
316             }
317             catch (Exception e) {
318                 throw processException(e);
319             }
320             finally {
321                 closeSession(session);
322             }
323         }
324         else {
325             return (List<PortletPreferences>)result;
326         }
327     }
328 
329     public List<PortletPreferences> findByPlid(long plid, int start, int end)
330         throws SystemException {
331         return findByPlid(plid, start, end, null);
332     }
333 
334     public List<PortletPreferences> findByPlid(long plid, int start, int end,
335         OrderByComparator obc) throws SystemException {
336         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
337         String finderClassName = PortletPreferences.class.getName();
338         String finderMethodName = "findByPlid";
339         String[] finderParams = new String[] {
340                 Long.class.getName(),
341                 
342                 "java.lang.Integer", "java.lang.Integer",
343                 "com.liferay.portal.kernel.util.OrderByComparator"
344             };
345         Object[] finderArgs = new Object[] {
346                 new Long(plid),
347                 
348                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
349             };
350 
351         Object result = null;
352 
353         if (finderClassNameCacheEnabled) {
354             result = FinderCacheUtil.getResult(finderClassName,
355                     finderMethodName, finderParams, finderArgs, this);
356         }
357 
358         if (result == null) {
359             Session session = null;
360 
361             try {
362                 session = openSession();
363 
364                 StringBuilder query = new StringBuilder();
365 
366                 query.append(
367                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
368 
369                 query.append("plid = ?");
370 
371                 query.append(" ");
372 
373                 if (obc != null) {
374                     query.append("ORDER BY ");
375                     query.append(obc.getOrderBy());
376                 }
377 
378                 Query q = session.createQuery(query.toString());
379 
380                 QueryPos qPos = QueryPos.getInstance(q);
381 
382                 qPos.add(plid);
383 
384                 List<PortletPreferences> list = (List<PortletPreferences>)QueryUtil.list(q,
385                         getDialect(), start, end);
386 
387                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
388                     finderClassName, finderMethodName, finderParams,
389                     finderArgs, list);
390 
391                 return list;
392             }
393             catch (Exception e) {
394                 throw processException(e);
395             }
396             finally {
397                 closeSession(session);
398             }
399         }
400         else {
401             return (List<PortletPreferences>)result;
402         }
403     }
404 
405     public PortletPreferences findByPlid_First(long plid, OrderByComparator obc)
406         throws NoSuchPortletPreferencesException, SystemException {
407         List<PortletPreferences> list = findByPlid(plid, 0, 1, obc);
408 
409         if (list.size() == 0) {
410             StringBuilder msg = new StringBuilder();
411 
412             msg.append("No PortletPreferences exists with the key {");
413 
414             msg.append("plid=" + plid);
415 
416             msg.append(StringPool.CLOSE_CURLY_BRACE);
417 
418             throw new NoSuchPortletPreferencesException(msg.toString());
419         }
420         else {
421             return list.get(0);
422         }
423     }
424 
425     public PortletPreferences findByPlid_Last(long plid, OrderByComparator obc)
426         throws NoSuchPortletPreferencesException, SystemException {
427         int count = countByPlid(plid);
428 
429         List<PortletPreferences> list = findByPlid(plid, count - 1, count, obc);
430 
431         if (list.size() == 0) {
432             StringBuilder msg = new StringBuilder();
433 
434             msg.append("No PortletPreferences exists with the key {");
435 
436             msg.append("plid=" + plid);
437 
438             msg.append(StringPool.CLOSE_CURLY_BRACE);
439 
440             throw new NoSuchPortletPreferencesException(msg.toString());
441         }
442         else {
443             return list.get(0);
444         }
445     }
446 
447     public PortletPreferences[] findByPlid_PrevAndNext(
448         long portletPreferencesId, long plid, OrderByComparator obc)
449         throws NoSuchPortletPreferencesException, SystemException {
450         PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
451 
452         int count = countByPlid(plid);
453 
454         Session session = null;
455 
456         try {
457             session = openSession();
458 
459             StringBuilder query = new StringBuilder();
460 
461             query.append(
462                 "FROM com.liferay.portal.model.PortletPreferences WHERE ");
463 
464             query.append("plid = ?");
465 
466             query.append(" ");
467 
468             if (obc != null) {
469                 query.append("ORDER BY ");
470                 query.append(obc.getOrderBy());
471             }
472 
473             Query q = session.createQuery(query.toString());
474 
475             QueryPos qPos = QueryPos.getInstance(q);
476 
477             qPos.add(plid);
478 
479             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
480                     portletPreferences);
481 
482             PortletPreferences[] array = new PortletPreferencesImpl[3];
483 
484             array[0] = (PortletPreferences)objArray[0];
485             array[1] = (PortletPreferences)objArray[1];
486             array[2] = (PortletPreferences)objArray[2];
487 
488             return array;
489         }
490         catch (Exception e) {
491             throw processException(e);
492         }
493         finally {
494             closeSession(session);
495         }
496     }
497 
498     public List<PortletPreferences> findByP_P(long plid, String portletId)
499         throws SystemException {
500         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
501         String finderClassName = PortletPreferences.class.getName();
502         String finderMethodName = "findByP_P";
503         String[] finderParams = new String[] {
504                 Long.class.getName(), String.class.getName()
505             };
506         Object[] finderArgs = new Object[] { new Long(plid), portletId };
507 
508         Object result = null;
509 
510         if (finderClassNameCacheEnabled) {
511             result = FinderCacheUtil.getResult(finderClassName,
512                     finderMethodName, finderParams, finderArgs, this);
513         }
514 
515         if (result == null) {
516             Session session = null;
517 
518             try {
519                 session = openSession();
520 
521                 StringBuilder query = new StringBuilder();
522 
523                 query.append(
524                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
525 
526                 query.append("plid = ?");
527 
528                 query.append(" AND ");
529 
530                 if (portletId == null) {
531                     query.append("portletId IS NULL");
532                 }
533                 else {
534                     query.append("portletId = ?");
535                 }
536 
537                 query.append(" ");
538 
539                 Query q = session.createQuery(query.toString());
540 
541                 QueryPos qPos = QueryPos.getInstance(q);
542 
543                 qPos.add(plid);
544 
545                 if (portletId != null) {
546                     qPos.add(portletId);
547                 }
548 
549                 List<PortletPreferences> list = q.list();
550 
551                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
552                     finderClassName, finderMethodName, finderParams,
553                     finderArgs, list);
554 
555                 return list;
556             }
557             catch (Exception e) {
558                 throw processException(e);
559             }
560             finally {
561                 closeSession(session);
562             }
563         }
564         else {
565             return (List<PortletPreferences>)result;
566         }
567     }
568 
569     public List<PortletPreferences> findByP_P(long plid, String portletId,
570         int start, int end) throws SystemException {
571         return findByP_P(plid, portletId, start, end, null);
572     }
573 
574     public List<PortletPreferences> findByP_P(long plid, String portletId,
575         int start, int end, OrderByComparator obc) throws SystemException {
576         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
577         String finderClassName = PortletPreferences.class.getName();
578         String finderMethodName = "findByP_P";
579         String[] finderParams = new String[] {
580                 Long.class.getName(), String.class.getName(),
581                 
582                 "java.lang.Integer", "java.lang.Integer",
583                 "com.liferay.portal.kernel.util.OrderByComparator"
584             };
585         Object[] finderArgs = new Object[] {
586                 new Long(plid),
587                 
588                 portletId,
589                 
590                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
591             };
592 
593         Object result = null;
594 
595         if (finderClassNameCacheEnabled) {
596             result = FinderCacheUtil.getResult(finderClassName,
597                     finderMethodName, finderParams, finderArgs, this);
598         }
599 
600         if (result == null) {
601             Session session = null;
602 
603             try {
604                 session = openSession();
605 
606                 StringBuilder query = new StringBuilder();
607 
608                 query.append(
609                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
610 
611                 query.append("plid = ?");
612 
613                 query.append(" AND ");
614 
615                 if (portletId == null) {
616                     query.append("portletId IS NULL");
617                 }
618                 else {
619                     query.append("portletId = ?");
620                 }
621 
622                 query.append(" ");
623 
624                 if (obc != null) {
625                     query.append("ORDER BY ");
626                     query.append(obc.getOrderBy());
627                 }
628 
629                 Query q = session.createQuery(query.toString());
630 
631                 QueryPos qPos = QueryPos.getInstance(q);
632 
633                 qPos.add(plid);
634 
635                 if (portletId != null) {
636                     qPos.add(portletId);
637                 }
638 
639                 List<PortletPreferences> list = (List<PortletPreferences>)QueryUtil.list(q,
640                         getDialect(), start, end);
641 
642                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
643                     finderClassName, finderMethodName, finderParams,
644                     finderArgs, list);
645 
646                 return list;
647             }
648             catch (Exception e) {
649                 throw processException(e);
650             }
651             finally {
652                 closeSession(session);
653             }
654         }
655         else {
656             return (List<PortletPreferences>)result;
657         }
658     }
659 
660     public PortletPreferences findByP_P_First(long plid, String portletId,
661         OrderByComparator obc)
662         throws NoSuchPortletPreferencesException, SystemException {
663         List<PortletPreferences> list = findByP_P(plid, portletId, 0, 1, obc);
664 
665         if (list.size() == 0) {
666             StringBuilder msg = new StringBuilder();
667 
668             msg.append("No PortletPreferences exists with the key {");
669 
670             msg.append("plid=" + plid);
671 
672             msg.append(", ");
673             msg.append("portletId=" + portletId);
674 
675             msg.append(StringPool.CLOSE_CURLY_BRACE);
676 
677             throw new NoSuchPortletPreferencesException(msg.toString());
678         }
679         else {
680             return list.get(0);
681         }
682     }
683 
684     public PortletPreferences findByP_P_Last(long plid, String portletId,
685         OrderByComparator obc)
686         throws NoSuchPortletPreferencesException, SystemException {
687         int count = countByP_P(plid, portletId);
688 
689         List<PortletPreferences> list = findByP_P(plid, portletId, count - 1,
690                 count, obc);
691 
692         if (list.size() == 0) {
693             StringBuilder msg = new StringBuilder();
694 
695             msg.append("No PortletPreferences exists with the key {");
696 
697             msg.append("plid=" + plid);
698 
699             msg.append(", ");
700             msg.append("portletId=" + portletId);
701 
702             msg.append(StringPool.CLOSE_CURLY_BRACE);
703 
704             throw new NoSuchPortletPreferencesException(msg.toString());
705         }
706         else {
707             return list.get(0);
708         }
709     }
710 
711     public PortletPreferences[] findByP_P_PrevAndNext(
712         long portletPreferencesId, long plid, String portletId,
713         OrderByComparator obc)
714         throws NoSuchPortletPreferencesException, SystemException {
715         PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
716 
717         int count = countByP_P(plid, portletId);
718 
719         Session session = null;
720 
721         try {
722             session = openSession();
723 
724             StringBuilder query = new StringBuilder();
725 
726             query.append(
727                 "FROM com.liferay.portal.model.PortletPreferences WHERE ");
728 
729             query.append("plid = ?");
730 
731             query.append(" AND ");
732 
733             if (portletId == null) {
734                 query.append("portletId IS NULL");
735             }
736             else {
737                 query.append("portletId = ?");
738             }
739 
740             query.append(" ");
741 
742             if (obc != null) {
743                 query.append("ORDER BY ");
744                 query.append(obc.getOrderBy());
745             }
746 
747             Query q = session.createQuery(query.toString());
748 
749             QueryPos qPos = QueryPos.getInstance(q);
750 
751             qPos.add(plid);
752 
753             if (portletId != null) {
754                 qPos.add(portletId);
755             }
756 
757             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
758                     portletPreferences);
759 
760             PortletPreferences[] array = new PortletPreferencesImpl[3];
761 
762             array[0] = (PortletPreferences)objArray[0];
763             array[1] = (PortletPreferences)objArray[1];
764             array[2] = (PortletPreferences)objArray[2];
765 
766             return array;
767         }
768         catch (Exception e) {
769             throw processException(e);
770         }
771         finally {
772             closeSession(session);
773         }
774     }
775 
776     public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
777         long plid) throws SystemException {
778         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
779         String finderClassName = PortletPreferences.class.getName();
780         String finderMethodName = "findByO_O_P";
781         String[] finderParams = new String[] {
782                 Long.class.getName(), Integer.class.getName(),
783                 Long.class.getName()
784             };
785         Object[] finderArgs = new Object[] {
786                 new Long(ownerId), new Integer(ownerType), new Long(plid)
787             };
788 
789         Object result = null;
790 
791         if (finderClassNameCacheEnabled) {
792             result = FinderCacheUtil.getResult(finderClassName,
793                     finderMethodName, finderParams, finderArgs, this);
794         }
795 
796         if (result == null) {
797             Session session = null;
798 
799             try {
800                 session = openSession();
801 
802                 StringBuilder query = new StringBuilder();
803 
804                 query.append(
805                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
806 
807                 query.append("ownerId = ?");
808 
809                 query.append(" AND ");
810 
811                 query.append("ownerType = ?");
812 
813                 query.append(" AND ");
814 
815                 query.append("plid = ?");
816 
817                 query.append(" ");
818 
819                 Query q = session.createQuery(query.toString());
820 
821                 QueryPos qPos = QueryPos.getInstance(q);
822 
823                 qPos.add(ownerId);
824 
825                 qPos.add(ownerType);
826 
827                 qPos.add(plid);
828 
829                 List<PortletPreferences> list = q.list();
830 
831                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
832                     finderClassName, finderMethodName, finderParams,
833                     finderArgs, list);
834 
835                 return list;
836             }
837             catch (Exception e) {
838                 throw processException(e);
839             }
840             finally {
841                 closeSession(session);
842             }
843         }
844         else {
845             return (List<PortletPreferences>)result;
846         }
847     }
848 
849     public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
850         long plid, int start, int end) throws SystemException {
851         return findByO_O_P(ownerId, ownerType, plid, start, end, null);
852     }
853 
854     public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
855         long plid, int start, int end, OrderByComparator obc)
856         throws SystemException {
857         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
858         String finderClassName = PortletPreferences.class.getName();
859         String finderMethodName = "findByO_O_P";
860         String[] finderParams = new String[] {
861                 Long.class.getName(), Integer.class.getName(),
862                 Long.class.getName(),
863                 
864                 "java.lang.Integer", "java.lang.Integer",
865                 "com.liferay.portal.kernel.util.OrderByComparator"
866             };
867         Object[] finderArgs = new Object[] {
868                 new Long(ownerId), new Integer(ownerType), new Long(plid),
869                 
870                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
871             };
872 
873         Object result = null;
874 
875         if (finderClassNameCacheEnabled) {
876             result = FinderCacheUtil.getResult(finderClassName,
877                     finderMethodName, finderParams, finderArgs, this);
878         }
879 
880         if (result == null) {
881             Session session = null;
882 
883             try {
884                 session = openSession();
885 
886                 StringBuilder query = new StringBuilder();
887 
888                 query.append(
889                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
890 
891                 query.append("ownerId = ?");
892 
893                 query.append(" AND ");
894 
895                 query.append("ownerType = ?");
896 
897                 query.append(" AND ");
898 
899                 query.append("plid = ?");
900 
901                 query.append(" ");
902 
903                 if (obc != null) {
904                     query.append("ORDER BY ");
905                     query.append(obc.getOrderBy());
906                 }
907 
908                 Query q = session.createQuery(query.toString());
909 
910                 QueryPos qPos = QueryPos.getInstance(q);
911 
912                 qPos.add(ownerId);
913 
914                 qPos.add(ownerType);
915 
916                 qPos.add(plid);
917 
918                 List<PortletPreferences> list = (List<PortletPreferences>)QueryUtil.list(q,
919                         getDialect(), start, end);
920 
921                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
922                     finderClassName, finderMethodName, finderParams,
923                     finderArgs, list);
924 
925                 return list;
926             }
927             catch (Exception e) {
928                 throw processException(e);
929             }
930             finally {
931                 closeSession(session);
932             }
933         }
934         else {
935             return (List<PortletPreferences>)result;
936         }
937     }
938 
939     public PortletPreferences findByO_O_P_First(long ownerId, int ownerType,
940         long plid, OrderByComparator obc)
941         throws NoSuchPortletPreferencesException, SystemException {
942         List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
943                 0, 1, obc);
944 
945         if (list.size() == 0) {
946             StringBuilder msg = new StringBuilder();
947 
948             msg.append("No PortletPreferences exists with the key {");
949 
950             msg.append("ownerId=" + ownerId);
951 
952             msg.append(", ");
953             msg.append("ownerType=" + ownerType);
954 
955             msg.append(", ");
956             msg.append("plid=" + plid);
957 
958             msg.append(StringPool.CLOSE_CURLY_BRACE);
959 
960             throw new NoSuchPortletPreferencesException(msg.toString());
961         }
962         else {
963             return list.get(0);
964         }
965     }
966 
967     public PortletPreferences findByO_O_P_Last(long ownerId, int ownerType,
968         long plid, OrderByComparator obc)
969         throws NoSuchPortletPreferencesException, SystemException {
970         int count = countByO_O_P(ownerId, ownerType, plid);
971 
972         List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
973                 count - 1, count, obc);
974 
975         if (list.size() == 0) {
976             StringBuilder msg = new StringBuilder();
977 
978             msg.append("No PortletPreferences exists with the key {");
979 
980             msg.append("ownerId=" + ownerId);
981 
982             msg.append(", ");
983             msg.append("ownerType=" + ownerType);
984 
985             msg.append(", ");
986             msg.append("plid=" + plid);
987 
988             msg.append(StringPool.CLOSE_CURLY_BRACE);
989 
990             throw new NoSuchPortletPreferencesException(msg.toString());
991         }
992         else {
993             return list.get(0);
994         }
995     }
996 
997     public PortletPreferences[] findByO_O_P_PrevAndNext(
998         long portletPreferencesId, long ownerId, int ownerType, long plid,
999         OrderByComparator obc)
1000        throws NoSuchPortletPreferencesException, SystemException {
1001        PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
1002
1003        int count = countByO_O_P(ownerId, ownerType, plid);
1004
1005        Session session = null;
1006
1007        try {
1008            session = openSession();
1009
1010            StringBuilder query = new StringBuilder();
1011
1012            query.append(
1013                "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1014
1015            query.append("ownerId = ?");
1016
1017            query.append(" AND ");
1018
1019            query.append("ownerType = ?");
1020
1021            query.append(" AND ");
1022
1023            query.append("plid = ?");
1024
1025            query.append(" ");
1026
1027            if (obc != null) {
1028                query.append("ORDER BY ");
1029                query.append(obc.getOrderBy());
1030            }
1031
1032            Query q = session.createQuery(query.toString());
1033
1034            QueryPos qPos = QueryPos.getInstance(q);
1035
1036            qPos.add(ownerId);
1037
1038            qPos.add(ownerType);
1039
1040            qPos.add(plid);
1041
1042            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
1043                    portletPreferences);
1044
1045            PortletPreferences[] array = new PortletPreferencesImpl[3];
1046
1047            array[0] = (PortletPreferences)objArray[0];
1048            array[1] = (PortletPreferences)objArray[1];
1049            array[2] = (PortletPreferences)objArray[2];
1050
1051            return array;
1052        }
1053        catch (Exception e) {
1054            throw processException(e);
1055        }
1056        finally {
1057            closeSession(session);
1058        }
1059    }
1060
1061    public PortletPreferences findByO_O_P_P(long ownerId, int ownerType,
1062        long plid, String portletId)
1063        throws NoSuchPortletPreferencesException, SystemException {
1064        PortletPreferences portletPreferences = fetchByO_O_P_P(ownerId,
1065                ownerType, plid, portletId);
1066
1067        if (portletPreferences == null) {
1068            StringBuilder msg = new StringBuilder();
1069
1070            msg.append("No PortletPreferences exists with the key {");
1071
1072            msg.append("ownerId=" + ownerId);
1073
1074            msg.append(", ");
1075            msg.append("ownerType=" + ownerType);
1076
1077            msg.append(", ");
1078            msg.append("plid=" + plid);
1079
1080            msg.append(", ");
1081            msg.append("portletId=" + portletId);
1082
1083            msg.append(StringPool.CLOSE_CURLY_BRACE);
1084
1085            if (_log.isWarnEnabled()) {
1086                _log.warn(msg.toString());
1087            }
1088
1089            throw new NoSuchPortletPreferencesException(msg.toString());
1090        }
1091
1092        return portletPreferences;
1093    }
1094
1095    public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
1096        long plid, String portletId) throws SystemException {
1097        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1098        String finderClassName = PortletPreferences.class.getName();
1099        String finderMethodName = "fetchByO_O_P_P";
1100        String[] finderParams = new String[] {
1101                Long.class.getName(), Integer.class.getName(),
1102                Long.class.getName(), String.class.getName()
1103            };
1104        Object[] finderArgs = new Object[] {
1105                new Long(ownerId), new Integer(ownerType), new Long(plid),
1106                
1107                portletId
1108            };
1109
1110        Object result = null;
1111
1112        if (finderClassNameCacheEnabled) {
1113            result = FinderCacheUtil.getResult(finderClassName,
1114                    finderMethodName, finderParams, finderArgs, this);
1115        }
1116
1117        if (result == null) {
1118            Session session = null;
1119
1120            try {
1121                session = openSession();
1122
1123                StringBuilder query = new StringBuilder();
1124
1125                query.append(
1126                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1127
1128                query.append("ownerId = ?");
1129
1130                query.append(" AND ");
1131
1132                query.append("ownerType = ?");
1133
1134                query.append(" AND ");
1135
1136                query.append("plid = ?");
1137
1138                query.append(" AND ");
1139
1140                if (portletId == null) {
1141                    query.append("portletId IS NULL");
1142                }
1143                else {
1144                    query.append("portletId = ?");
1145                }
1146
1147                query.append(" ");
1148
1149                Query q = session.createQuery(query.toString());
1150
1151                QueryPos qPos = QueryPos.getInstance(q);
1152
1153                qPos.add(ownerId);
1154
1155                qPos.add(ownerType);
1156
1157                qPos.add(plid);
1158
1159                if (portletId != null) {
1160                    qPos.add(portletId);
1161                }
1162
1163                List<PortletPreferences> list = q.list();
1164
1165                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1166                    finderClassName, finderMethodName, finderParams,
1167                    finderArgs, list);
1168
1169                if (list.size() == 0) {
1170                    return null;
1171                }
1172                else {
1173                    return list.get(0);
1174                }
1175            }
1176            catch (Exception e) {
1177                throw processException(e);
1178            }
1179            finally {
1180                closeSession(session);
1181            }
1182        }
1183        else {
1184            List<PortletPreferences> list = (List<PortletPreferences>)result;
1185
1186            if (list.size() == 0) {
1187                return null;
1188            }
1189            else {
1190                return list.get(0);
1191            }
1192        }
1193    }
1194
1195    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1196        throws SystemException {
1197        Session session = null;
1198
1199        try {
1200            session = openSession();
1201
1202            dynamicQuery.compile(session);
1203
1204            return dynamicQuery.list();
1205        }
1206        catch (Exception e) {
1207            throw processException(e);
1208        }
1209        finally {
1210            closeSession(session);
1211        }
1212    }
1213
1214    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1215        int start, int end) throws SystemException {
1216        Session session = null;
1217
1218        try {
1219            session = openSession();
1220
1221            dynamicQuery.setLimit(start, end);
1222
1223            dynamicQuery.compile(session);
1224
1225            return dynamicQuery.list();
1226        }
1227        catch (Exception e) {
1228            throw processException(e);
1229        }
1230        finally {
1231            closeSession(session);
1232        }
1233    }
1234
1235    public List<PortletPreferences> findAll() throws SystemException {
1236        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1237    }
1238
1239    public List<PortletPreferences> findAll(int start, int end)
1240        throws SystemException {
1241        return findAll(start, end, null);
1242    }
1243
1244    public List<PortletPreferences> findAll(int start, int end,
1245        OrderByComparator obc) throws SystemException {
1246        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1247        String finderClassName = PortletPreferences.class.getName();
1248        String finderMethodName = "findAll";
1249        String[] finderParams = new String[] {
1250                "java.lang.Integer", "java.lang.Integer",
1251                "com.liferay.portal.kernel.util.OrderByComparator"
1252            };
1253        Object[] finderArgs = new Object[] {
1254                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1255            };
1256
1257        Object result = null;
1258
1259        if (finderClassNameCacheEnabled) {
1260            result = FinderCacheUtil.getResult(finderClassName,
1261                    finderMethodName, finderParams, finderArgs, this);
1262        }
1263
1264        if (result == null) {
1265            Session session = null;
1266
1267            try {
1268                session = openSession();
1269
1270                StringBuilder query = new StringBuilder();
1271
1272                query.append(
1273                    "FROM com.liferay.portal.model.PortletPreferences ");
1274
1275                if (obc != null) {
1276                    query.append("ORDER BY ");
1277                    query.append(obc.getOrderBy());
1278                }
1279
1280                Query q = session.createQuery(query.toString());
1281
1282                List<PortletPreferences> list = (List<PortletPreferences>)QueryUtil.list(q,
1283                        getDialect(), start, end);
1284
1285                if (obc == null) {
1286                    Collections.sort(list);
1287                }
1288
1289                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1290                    finderClassName, finderMethodName, finderParams,
1291                    finderArgs, list);
1292
1293                return list;
1294            }
1295            catch (Exception e) {
1296                throw processException(e);
1297            }
1298            finally {
1299                closeSession(session);
1300            }
1301        }
1302        else {
1303            return (List<PortletPreferences>)result;
1304        }
1305    }
1306
1307    public void removeByPlid(long plid) throws SystemException {
1308        for (PortletPreferences portletPreferences : findByPlid(plid)) {
1309            remove(portletPreferences);
1310        }
1311    }
1312
1313    public void removeByP_P(long plid, String portletId)
1314        throws SystemException {
1315        for (PortletPreferences portletPreferences : findByP_P(plid, portletId)) {
1316            remove(portletPreferences);
1317        }
1318    }
1319
1320    public void removeByO_O_P(long ownerId, int ownerType, long plid)
1321        throws SystemException {
1322        for (PortletPreferences portletPreferences : findByO_O_P(ownerId,
1323                ownerType, plid)) {
1324            remove(portletPreferences);
1325        }
1326    }
1327
1328    public void removeByO_O_P_P(long ownerId, int ownerType, long plid,
1329        String portletId)
1330        throws NoSuchPortletPreferencesException, SystemException {
1331        PortletPreferences portletPreferences = findByO_O_P_P(ownerId,
1332                ownerType, plid, portletId);
1333
1334        remove(portletPreferences);
1335    }
1336
1337    public void removeAll() throws SystemException {
1338        for (PortletPreferences portletPreferences : findAll()) {
1339            remove(portletPreferences);
1340        }
1341    }
1342
1343    public int countByPlid(long plid) throws SystemException {
1344        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1345        String finderClassName = PortletPreferences.class.getName();
1346        String finderMethodName = "countByPlid";
1347        String[] finderParams = new String[] { Long.class.getName() };
1348        Object[] finderArgs = new Object[] { new Long(plid) };
1349
1350        Object result = null;
1351
1352        if (finderClassNameCacheEnabled) {
1353            result = FinderCacheUtil.getResult(finderClassName,
1354                    finderMethodName, finderParams, finderArgs, this);
1355        }
1356
1357        if (result == null) {
1358            Session session = null;
1359
1360            try {
1361                session = openSession();
1362
1363                StringBuilder query = new StringBuilder();
1364
1365                query.append("SELECT COUNT(*) ");
1366                query.append(
1367                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1368
1369                query.append("plid = ?");
1370
1371                query.append(" ");
1372
1373                Query q = session.createQuery(query.toString());
1374
1375                QueryPos qPos = QueryPos.getInstance(q);
1376
1377                qPos.add(plid);
1378
1379                Long count = null;
1380
1381                Iterator<Long> itr = q.list().iterator();
1382
1383                if (itr.hasNext()) {
1384                    count = itr.next();
1385                }
1386
1387                if (count == null) {
1388                    count = new Long(0);
1389                }
1390
1391                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1392                    finderClassName, finderMethodName, finderParams,
1393                    finderArgs, count);
1394
1395                return count.intValue();
1396            }
1397            catch (Exception e) {
1398                throw processException(e);
1399            }
1400            finally {
1401                closeSession(session);
1402            }
1403        }
1404        else {
1405            return ((Long)result).intValue();
1406        }
1407    }
1408
1409    public int countByP_P(long plid, String portletId)
1410        throws SystemException {
1411        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1412        String finderClassName = PortletPreferences.class.getName();
1413        String finderMethodName = "countByP_P";
1414        String[] finderParams = new String[] {
1415                Long.class.getName(), String.class.getName()
1416            };
1417        Object[] finderArgs = new Object[] { new Long(plid), portletId };
1418
1419        Object result = null;
1420
1421        if (finderClassNameCacheEnabled) {
1422            result = FinderCacheUtil.getResult(finderClassName,
1423                    finderMethodName, finderParams, finderArgs, this);
1424        }
1425
1426        if (result == null) {
1427            Session session = null;
1428
1429            try {
1430                session = openSession();
1431
1432                StringBuilder query = new StringBuilder();
1433
1434                query.append("SELECT COUNT(*) ");
1435                query.append(
1436                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1437
1438                query.append("plid = ?");
1439
1440                query.append(" AND ");
1441
1442                if (portletId == null) {
1443                    query.append("portletId IS NULL");
1444                }
1445                else {
1446                    query.append("portletId = ?");
1447                }
1448
1449                query.append(" ");
1450
1451                Query q = session.createQuery(query.toString());
1452
1453                QueryPos qPos = QueryPos.getInstance(q);
1454
1455                qPos.add(plid);
1456
1457                if (portletId != null) {
1458                    qPos.add(portletId);
1459                }
1460
1461                Long count = null;
1462
1463                Iterator<Long> itr = q.list().iterator();
1464
1465                if (itr.hasNext()) {
1466                    count = itr.next();
1467                }
1468
1469                if (count == null) {
1470                    count = new Long(0);
1471                }
1472
1473                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1474                    finderClassName, finderMethodName, finderParams,
1475                    finderArgs, count);
1476
1477                return count.intValue();
1478            }
1479            catch (Exception e) {
1480                throw processException(e);
1481            }
1482            finally {
1483                closeSession(session);
1484            }
1485        }
1486        else {
1487            return ((Long)result).intValue();
1488        }
1489    }
1490
1491    public int countByO_O_P(long ownerId, int ownerType, long plid)
1492        throws SystemException {
1493        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1494        String finderClassName = PortletPreferences.class.getName();
1495        String finderMethodName = "countByO_O_P";
1496        String[] finderParams = new String[] {
1497                Long.class.getName(), Integer.class.getName(),
1498                Long.class.getName()
1499            };
1500        Object[] finderArgs = new Object[] {
1501                new Long(ownerId), new Integer(ownerType), new Long(plid)
1502            };
1503
1504        Object result = null;
1505
1506        if (finderClassNameCacheEnabled) {
1507            result = FinderCacheUtil.getResult(finderClassName,
1508                    finderMethodName, finderParams, finderArgs, this);
1509        }
1510
1511        if (result == null) {
1512            Session session = null;
1513
1514            try {
1515                session = openSession();
1516
1517                StringBuilder query = new StringBuilder();
1518
1519                query.append("SELECT COUNT(*) ");
1520                query.append(
1521                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1522
1523                query.append("ownerId = ?");
1524
1525                query.append(" AND ");
1526
1527                query.append("ownerType = ?");
1528
1529                query.append(" AND ");
1530
1531                query.append("plid = ?");
1532
1533                query.append(" ");
1534
1535                Query q = session.createQuery(query.toString());
1536
1537                QueryPos qPos = QueryPos.getInstance(q);
1538
1539                qPos.add(ownerId);
1540
1541                qPos.add(ownerType);
1542
1543                qPos.add(plid);
1544
1545                Long count = null;
1546
1547                Iterator<Long> itr = q.list().iterator();
1548
1549                if (itr.hasNext()) {
1550                    count = itr.next();
1551                }
1552
1553                if (count == null) {
1554                    count = new Long(0);
1555                }
1556
1557                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1558                    finderClassName, finderMethodName, finderParams,
1559                    finderArgs, count);
1560
1561                return count.intValue();
1562            }
1563            catch (Exception e) {
1564                throw processException(e);
1565            }
1566            finally {
1567                closeSession(session);
1568            }
1569        }
1570        else {
1571            return ((Long)result).intValue();
1572        }
1573    }
1574
1575    public int countByO_O_P_P(long ownerId, int ownerType, long plid,
1576        String portletId) throws SystemException {
1577        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1578        String finderClassName = PortletPreferences.class.getName();
1579        String finderMethodName = "countByO_O_P_P";
1580        String[] finderParams = new String[] {
1581                Long.class.getName(), Integer.class.getName(),
1582                Long.class.getName(), String.class.getName()
1583            };
1584        Object[] finderArgs = new Object[] {
1585                new Long(ownerId), new Integer(ownerType), new Long(plid),
1586                
1587                portletId
1588            };
1589
1590        Object result = null;
1591
1592        if (finderClassNameCacheEnabled) {
1593            result = FinderCacheUtil.getResult(finderClassName,
1594                    finderMethodName, finderParams, finderArgs, this);
1595        }
1596
1597        if (result == null) {
1598            Session session = null;
1599
1600            try {
1601                session = openSession();
1602
1603                StringBuilder query = new StringBuilder();
1604
1605                query.append("SELECT COUNT(*) ");
1606                query.append(
1607                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1608
1609                query.append("ownerId = ?");
1610
1611                query.append(" AND ");
1612
1613                query.append("ownerType = ?");
1614
1615                query.append(" AND ");
1616
1617                query.append("plid = ?");
1618
1619                query.append(" AND ");
1620
1621                if (portletId == null) {
1622                    query.append("portletId IS NULL");
1623                }
1624                else {
1625                    query.append("portletId = ?");
1626                }
1627
1628                query.append(" ");
1629
1630                Query q = session.createQuery(query.toString());
1631
1632                QueryPos qPos = QueryPos.getInstance(q);
1633
1634                qPos.add(ownerId);
1635
1636                qPos.add(ownerType);
1637
1638                qPos.add(plid);
1639
1640                if (portletId != null) {
1641                    qPos.add(portletId);
1642                }
1643
1644                Long count = null;
1645
1646                Iterator<Long> itr = q.list().iterator();
1647
1648                if (itr.hasNext()) {
1649                    count = itr.next();
1650                }
1651
1652                if (count == null) {
1653                    count = new Long(0);
1654                }
1655
1656                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1657                    finderClassName, finderMethodName, finderParams,
1658                    finderArgs, count);
1659
1660                return count.intValue();
1661            }
1662            catch (Exception e) {
1663                throw processException(e);
1664            }
1665            finally {
1666                closeSession(session);
1667            }
1668        }
1669        else {
1670            return ((Long)result).intValue();
1671        }
1672    }
1673
1674    public int countAll() throws SystemException {
1675        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1676        String finderClassName = PortletPreferences.class.getName();
1677        String finderMethodName = "countAll";
1678        String[] finderParams = new String[] {  };
1679        Object[] finderArgs = new Object[] {  };
1680
1681        Object result = null;
1682
1683        if (finderClassNameCacheEnabled) {
1684            result = FinderCacheUtil.getResult(finderClassName,
1685                    finderMethodName, finderParams, finderArgs, this);
1686        }
1687
1688        if (result == null) {
1689            Session session = null;
1690
1691            try {
1692                session = openSession();
1693
1694                Query q = session.createQuery(
1695                        "SELECT COUNT(*) FROM com.liferay.portal.model.PortletPreferences");
1696
1697                Long count = null;
1698
1699                Iterator<Long> itr = q.list().iterator();
1700
1701                if (itr.hasNext()) {
1702                    count = itr.next();
1703                }
1704
1705                if (count == null) {
1706                    count = new Long(0);
1707                }
1708
1709                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1710                    finderClassName, finderMethodName, finderParams,
1711                    finderArgs, count);
1712
1713                return count.intValue();
1714            }
1715            catch (Exception e) {
1716                throw processException(e);
1717            }
1718            finally {
1719                closeSession(session);
1720            }
1721        }
1722        else {
1723            return ((Long)result).intValue();
1724        }
1725    }
1726
1727    public void registerListener(ModelListener listener) {
1728        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1729
1730        listeners.add(listener);
1731
1732        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1733    }
1734
1735    public void unregisterListener(ModelListener listener) {
1736        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1737
1738        listeners.remove(listener);
1739
1740        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1741    }
1742
1743    public void afterPropertiesSet() {
1744        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1745                    com.liferay.portal.util.PropsUtil.get(
1746                        "value.object.listener.com.liferay.portal.model.PortletPreferences")));
1747
1748        if (listenerClassNames.length > 0) {
1749            try {
1750                List<ModelListener> listeners = new ArrayList<ModelListener>();
1751
1752                for (String listenerClassName : listenerClassNames) {
1753                    listeners.add((ModelListener)Class.forName(
1754                            listenerClassName).newInstance());
1755                }
1756
1757                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1758            }
1759            catch (Exception e) {
1760                _log.error(e);
1761            }
1762        }
1763    }
1764
1765    private static Log _log = LogFactory.getLog(PortletPreferencesPersistenceImpl.class);
1766    private ModelListener[] _listeners = new ModelListener[0];
1767}