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.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.OrderByComparator;
31  import com.liferay.portal.kernel.util.StringMaker;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.ModelListener;
35  import com.liferay.portal.model.PortletPreferences;
36  import com.liferay.portal.model.impl.PortletPreferencesImpl;
37  import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
38  import com.liferay.portal.spring.hibernate.FinderCache;
39  import com.liferay.portal.spring.hibernate.HibernateUtil;
40  import com.liferay.portal.util.PropsUtil;
41  
42  import com.liferay.util.dao.hibernate.QueryUtil;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.hibernate.Query;
48  import org.hibernate.Session;
49  
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  /**
55   * <a href="PortletPreferencesPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class PortletPreferencesPersistenceImpl extends BasePersistence
61      implements PortletPreferencesPersistence {
62      public PortletPreferences create(long portletPreferencesId) {
63          PortletPreferences portletPreferences = new PortletPreferencesImpl();
64  
65          portletPreferences.setNew(true);
66          portletPreferences.setPrimaryKey(portletPreferencesId);
67  
68          return portletPreferences;
69      }
70  
71      public PortletPreferences remove(long portletPreferencesId)
72          throws NoSuchPortletPreferencesException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              PortletPreferences portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
79                      new Long(portletPreferencesId));
80  
81              if (portletPreferences == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn(
84                          "No PortletPreferences exists with the primary key " +
85                          portletPreferencesId);
86                  }
87  
88                  throw new NoSuchPortletPreferencesException(
89                      "No PortletPreferences exists with the primary key " +
90                      portletPreferencesId);
91              }
92  
93              return remove(portletPreferences);
94          }
95          catch (NoSuchPortletPreferencesException nsee) {
96              throw nsee;
97          }
98          catch (Exception e) {
99              throw HibernateUtil.processException(e);
100         }
101         finally {
102             closeSession(session);
103         }
104     }
105 
106     public PortletPreferences remove(PortletPreferences portletPreferences)
107         throws SystemException {
108         ModelListener listener = _getListener();
109 
110         if (listener != null) {
111             listener.onBeforeRemove(portletPreferences);
112         }
113 
114         portletPreferences = removeImpl(portletPreferences);
115 
116         if (listener != null) {
117             listener.onAfterRemove(portletPreferences);
118         }
119 
120         return portletPreferences;
121     }
122 
123     protected PortletPreferences removeImpl(
124         PortletPreferences portletPreferences) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             session.delete(portletPreferences);
131 
132             session.flush();
133 
134             return portletPreferences;
135         }
136         catch (Exception e) {
137             throw HibernateUtil.processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCache.clearCache(PortletPreferences.class.getName());
143         }
144     }
145 
146     public PortletPreferences update(PortletPreferences portletPreferences)
147         throws SystemException {
148         return update(portletPreferences, false);
149     }
150 
151     public PortletPreferences update(PortletPreferences portletPreferences,
152         boolean merge) throws SystemException {
153         ModelListener listener = _getListener();
154 
155         boolean isNew = portletPreferences.isNew();
156 
157         if (listener != null) {
158             if (isNew) {
159                 listener.onBeforeCreate(portletPreferences);
160             }
161             else {
162                 listener.onBeforeUpdate(portletPreferences);
163             }
164         }
165 
166         portletPreferences = updateImpl(portletPreferences, merge);
167 
168         if (listener != null) {
169             if (isNew) {
170                 listener.onAfterCreate(portletPreferences);
171             }
172             else {
173                 listener.onAfterUpdate(portletPreferences);
174             }
175         }
176 
177         return portletPreferences;
178     }
179 
180     public PortletPreferences updateImpl(
181         com.liferay.portal.model.PortletPreferences portletPreferences,
182         boolean merge) throws SystemException {
183         Session session = null;
184 
185         try {
186             session = openSession();
187 
188             if (merge) {
189                 session.merge(portletPreferences);
190             }
191             else {
192                 if (portletPreferences.isNew()) {
193                     session.save(portletPreferences);
194                 }
195             }
196 
197             session.flush();
198 
199             portletPreferences.setNew(false);
200 
201             return portletPreferences;
202         }
203         catch (Exception e) {
204             throw HibernateUtil.processException(e);
205         }
206         finally {
207             closeSession(session);
208 
209             FinderCache.clearCache(PortletPreferences.class.getName());
210         }
211     }
212 
213     public PortletPreferences findByPrimaryKey(long portletPreferencesId)
214         throws NoSuchPortletPreferencesException, SystemException {
215         PortletPreferences portletPreferences = fetchByPrimaryKey(portletPreferencesId);
216 
217         if (portletPreferences == null) {
218             if (_log.isWarnEnabled()) {
219                 _log.warn("No PortletPreferences exists with the primary key " +
220                     portletPreferencesId);
221             }
222 
223             throw new NoSuchPortletPreferencesException(
224                 "No PortletPreferences exists with the primary key " +
225                 portletPreferencesId);
226         }
227 
228         return portletPreferences;
229     }
230 
231     public PortletPreferences fetchByPrimaryKey(long portletPreferencesId)
232         throws SystemException {
233         Session session = null;
234 
235         try {
236             session = openSession();
237 
238             return (PortletPreferences)session.get(PortletPreferencesImpl.class,
239                 new Long(portletPreferencesId));
240         }
241         catch (Exception e) {
242             throw HibernateUtil.processException(e);
243         }
244         finally {
245             closeSession(session);
246         }
247     }
248 
249     public List findByPlid(long plid) throws SystemException {
250         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
251         String finderClassName = PortletPreferences.class.getName();
252         String finderMethodName = "findByPlid";
253         String[] finderParams = new String[] { Long.class.getName() };
254         Object[] finderArgs = new Object[] { new Long(plid) };
255 
256         Object result = null;
257 
258         if (finderClassNameCacheEnabled) {
259             result = FinderCache.getResult(finderClassName, finderMethodName,
260                     finderParams, finderArgs, getSessionFactory());
261         }
262 
263         if (result == null) {
264             Session session = null;
265 
266             try {
267                 session = openSession();
268 
269                 StringMaker query = new StringMaker();
270 
271                 query.append(
272                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
273 
274                 query.append("plid = ?");
275 
276                 query.append(" ");
277 
278                 Query q = session.createQuery(query.toString());
279 
280                 int queryPos = 0;
281 
282                 q.setLong(queryPos++, plid);
283 
284                 List list = q.list();
285 
286                 FinderCache.putResult(finderClassNameCacheEnabled,
287                     finderClassName, finderMethodName, finderParams,
288                     finderArgs, list);
289 
290                 return list;
291             }
292             catch (Exception e) {
293                 throw HibernateUtil.processException(e);
294             }
295             finally {
296                 closeSession(session);
297             }
298         }
299         else {
300             return (List)result;
301         }
302     }
303 
304     public List findByPlid(long plid, int begin, int end)
305         throws SystemException {
306         return findByPlid(plid, begin, end, null);
307     }
308 
309     public List findByPlid(long plid, int begin, int end, OrderByComparator obc)
310         throws SystemException {
311         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
312         String finderClassName = PortletPreferences.class.getName();
313         String finderMethodName = "findByPlid";
314         String[] finderParams = new String[] {
315                 Long.class.getName(),
316                 
317                 "java.lang.Integer", "java.lang.Integer",
318                 "com.liferay.portal.kernel.util.OrderByComparator"
319             };
320         Object[] finderArgs = new Object[] {
321                 new Long(plid),
322                 
323                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
324             };
325 
326         Object result = null;
327 
328         if (finderClassNameCacheEnabled) {
329             result = FinderCache.getResult(finderClassName, finderMethodName,
330                     finderParams, finderArgs, getSessionFactory());
331         }
332 
333         if (result == null) {
334             Session session = null;
335 
336             try {
337                 session = openSession();
338 
339                 StringMaker query = new StringMaker();
340 
341                 query.append(
342                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
343 
344                 query.append("plid = ?");
345 
346                 query.append(" ");
347 
348                 if (obc != null) {
349                     query.append("ORDER BY ");
350                     query.append(obc.getOrderBy());
351                 }
352 
353                 Query q = session.createQuery(query.toString());
354 
355                 int queryPos = 0;
356 
357                 q.setLong(queryPos++, plid);
358 
359                 List list = QueryUtil.list(q, getDialect(), begin, end);
360 
361                 FinderCache.putResult(finderClassNameCacheEnabled,
362                     finderClassName, finderMethodName, finderParams,
363                     finderArgs, list);
364 
365                 return list;
366             }
367             catch (Exception e) {
368                 throw HibernateUtil.processException(e);
369             }
370             finally {
371                 closeSession(session);
372             }
373         }
374         else {
375             return (List)result;
376         }
377     }
378 
379     public PortletPreferences findByPlid_First(long plid, OrderByComparator obc)
380         throws NoSuchPortletPreferencesException, SystemException {
381         List list = findByPlid(plid, 0, 1, obc);
382 
383         if (list.size() == 0) {
384             StringMaker msg = new StringMaker();
385 
386             msg.append("No PortletPreferences exists with the key {");
387 
388             msg.append("plid=" + plid);
389 
390             msg.append(StringPool.CLOSE_CURLY_BRACE);
391 
392             throw new NoSuchPortletPreferencesException(msg.toString());
393         }
394         else {
395             return (PortletPreferences)list.get(0);
396         }
397     }
398 
399     public PortletPreferences findByPlid_Last(long plid, OrderByComparator obc)
400         throws NoSuchPortletPreferencesException, SystemException {
401         int count = countByPlid(plid);
402 
403         List list = findByPlid(plid, count - 1, count, obc);
404 
405         if (list.size() == 0) {
406             StringMaker msg = new StringMaker();
407 
408             msg.append("No PortletPreferences exists with the key {");
409 
410             msg.append("plid=" + plid);
411 
412             msg.append(StringPool.CLOSE_CURLY_BRACE);
413 
414             throw new NoSuchPortletPreferencesException(msg.toString());
415         }
416         else {
417             return (PortletPreferences)list.get(0);
418         }
419     }
420 
421     public PortletPreferences[] findByPlid_PrevAndNext(
422         long portletPreferencesId, long plid, OrderByComparator obc)
423         throws NoSuchPortletPreferencesException, SystemException {
424         PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
425 
426         int count = countByPlid(plid);
427 
428         Session session = null;
429 
430         try {
431             session = openSession();
432 
433             StringMaker query = new StringMaker();
434 
435             query.append(
436                 "FROM com.liferay.portal.model.PortletPreferences WHERE ");
437 
438             query.append("plid = ?");
439 
440             query.append(" ");
441 
442             if (obc != null) {
443                 query.append("ORDER BY ");
444                 query.append(obc.getOrderBy());
445             }
446 
447             Query q = session.createQuery(query.toString());
448 
449             int queryPos = 0;
450 
451             q.setLong(queryPos++, plid);
452 
453             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
454                     portletPreferences);
455 
456             PortletPreferences[] array = new PortletPreferencesImpl[3];
457 
458             array[0] = (PortletPreferences)objArray[0];
459             array[1] = (PortletPreferences)objArray[1];
460             array[2] = (PortletPreferences)objArray[2];
461 
462             return array;
463         }
464         catch (Exception e) {
465             throw HibernateUtil.processException(e);
466         }
467         finally {
468             closeSession(session);
469         }
470     }
471 
472     public List findByO_O_P(long ownerId, int ownerType, long plid)
473         throws SystemException {
474         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
475         String finderClassName = PortletPreferences.class.getName();
476         String finderMethodName = "findByO_O_P";
477         String[] finderParams = new String[] {
478                 Long.class.getName(), Integer.class.getName(),
479                 Long.class.getName()
480             };
481         Object[] finderArgs = new Object[] {
482                 new Long(ownerId), new Integer(ownerType), new Long(plid)
483             };
484 
485         Object result = null;
486 
487         if (finderClassNameCacheEnabled) {
488             result = FinderCache.getResult(finderClassName, finderMethodName,
489                     finderParams, finderArgs, getSessionFactory());
490         }
491 
492         if (result == null) {
493             Session session = null;
494 
495             try {
496                 session = openSession();
497 
498                 StringMaker query = new StringMaker();
499 
500                 query.append(
501                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
502 
503                 query.append("ownerId = ?");
504 
505                 query.append(" AND ");
506 
507                 query.append("ownerType = ?");
508 
509                 query.append(" AND ");
510 
511                 query.append("plid = ?");
512 
513                 query.append(" ");
514 
515                 Query q = session.createQuery(query.toString());
516 
517                 int queryPos = 0;
518 
519                 q.setLong(queryPos++, ownerId);
520 
521                 q.setInteger(queryPos++, ownerType);
522 
523                 q.setLong(queryPos++, plid);
524 
525                 List list = q.list();
526 
527                 FinderCache.putResult(finderClassNameCacheEnabled,
528                     finderClassName, finderMethodName, finderParams,
529                     finderArgs, list);
530 
531                 return list;
532             }
533             catch (Exception e) {
534                 throw HibernateUtil.processException(e);
535             }
536             finally {
537                 closeSession(session);
538             }
539         }
540         else {
541             return (List)result;
542         }
543     }
544 
545     public List findByO_O_P(long ownerId, int ownerType, long plid, int begin,
546         int end) throws SystemException {
547         return findByO_O_P(ownerId, ownerType, plid, begin, end, null);
548     }
549 
550     public List findByO_O_P(long ownerId, int ownerType, long plid, int begin,
551         int end, OrderByComparator obc) throws SystemException {
552         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
553         String finderClassName = PortletPreferences.class.getName();
554         String finderMethodName = "findByO_O_P";
555         String[] finderParams = new String[] {
556                 Long.class.getName(), Integer.class.getName(),
557                 Long.class.getName(),
558                 
559                 "java.lang.Integer", "java.lang.Integer",
560                 "com.liferay.portal.kernel.util.OrderByComparator"
561             };
562         Object[] finderArgs = new Object[] {
563                 new Long(ownerId), new Integer(ownerType), new Long(plid),
564                 
565                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
566             };
567 
568         Object result = null;
569 
570         if (finderClassNameCacheEnabled) {
571             result = FinderCache.getResult(finderClassName, finderMethodName,
572                     finderParams, finderArgs, getSessionFactory());
573         }
574 
575         if (result == null) {
576             Session session = null;
577 
578             try {
579                 session = openSession();
580 
581                 StringMaker query = new StringMaker();
582 
583                 query.append(
584                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
585 
586                 query.append("ownerId = ?");
587 
588                 query.append(" AND ");
589 
590                 query.append("ownerType = ?");
591 
592                 query.append(" AND ");
593 
594                 query.append("plid = ?");
595 
596                 query.append(" ");
597 
598                 if (obc != null) {
599                     query.append("ORDER BY ");
600                     query.append(obc.getOrderBy());
601                 }
602 
603                 Query q = session.createQuery(query.toString());
604 
605                 int queryPos = 0;
606 
607                 q.setLong(queryPos++, ownerId);
608 
609                 q.setInteger(queryPos++, ownerType);
610 
611                 q.setLong(queryPos++, plid);
612 
613                 List list = QueryUtil.list(q, getDialect(), begin, end);
614 
615                 FinderCache.putResult(finderClassNameCacheEnabled,
616                     finderClassName, finderMethodName, finderParams,
617                     finderArgs, list);
618 
619                 return list;
620             }
621             catch (Exception e) {
622                 throw HibernateUtil.processException(e);
623             }
624             finally {
625                 closeSession(session);
626             }
627         }
628         else {
629             return (List)result;
630         }
631     }
632 
633     public PortletPreferences findByO_O_P_First(long ownerId, int ownerType,
634         long plid, OrderByComparator obc)
635         throws NoSuchPortletPreferencesException, SystemException {
636         List list = findByO_O_P(ownerId, ownerType, plid, 0, 1, obc);
637 
638         if (list.size() == 0) {
639             StringMaker msg = new StringMaker();
640 
641             msg.append("No PortletPreferences exists with the key {");
642 
643             msg.append("ownerId=" + ownerId);
644 
645             msg.append(", ");
646             msg.append("ownerType=" + ownerType);
647 
648             msg.append(", ");
649             msg.append("plid=" + plid);
650 
651             msg.append(StringPool.CLOSE_CURLY_BRACE);
652 
653             throw new NoSuchPortletPreferencesException(msg.toString());
654         }
655         else {
656             return (PortletPreferences)list.get(0);
657         }
658     }
659 
660     public PortletPreferences findByO_O_P_Last(long ownerId, int ownerType,
661         long plid, OrderByComparator obc)
662         throws NoSuchPortletPreferencesException, SystemException {
663         int count = countByO_O_P(ownerId, ownerType, plid);
664 
665         List list = findByO_O_P(ownerId, ownerType, plid, count - 1, count, obc);
666 
667         if (list.size() == 0) {
668             StringMaker msg = new StringMaker();
669 
670             msg.append("No PortletPreferences exists with the key {");
671 
672             msg.append("ownerId=" + ownerId);
673 
674             msg.append(", ");
675             msg.append("ownerType=" + ownerType);
676 
677             msg.append(", ");
678             msg.append("plid=" + plid);
679 
680             msg.append(StringPool.CLOSE_CURLY_BRACE);
681 
682             throw new NoSuchPortletPreferencesException(msg.toString());
683         }
684         else {
685             return (PortletPreferences)list.get(0);
686         }
687     }
688 
689     public PortletPreferences[] findByO_O_P_PrevAndNext(
690         long portletPreferencesId, long ownerId, int ownerType, long plid,
691         OrderByComparator obc)
692         throws NoSuchPortletPreferencesException, SystemException {
693         PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
694 
695         int count = countByO_O_P(ownerId, ownerType, plid);
696 
697         Session session = null;
698 
699         try {
700             session = openSession();
701 
702             StringMaker query = new StringMaker();
703 
704             query.append(
705                 "FROM com.liferay.portal.model.PortletPreferences WHERE ");
706 
707             query.append("ownerId = ?");
708 
709             query.append(" AND ");
710 
711             query.append("ownerType = ?");
712 
713             query.append(" AND ");
714 
715             query.append("plid = ?");
716 
717             query.append(" ");
718 
719             if (obc != null) {
720                 query.append("ORDER BY ");
721                 query.append(obc.getOrderBy());
722             }
723 
724             Query q = session.createQuery(query.toString());
725 
726             int queryPos = 0;
727 
728             q.setLong(queryPos++, ownerId);
729 
730             q.setInteger(queryPos++, ownerType);
731 
732             q.setLong(queryPos++, plid);
733 
734             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
735                     portletPreferences);
736 
737             PortletPreferences[] array = new PortletPreferencesImpl[3];
738 
739             array[0] = (PortletPreferences)objArray[0];
740             array[1] = (PortletPreferences)objArray[1];
741             array[2] = (PortletPreferences)objArray[2];
742 
743             return array;
744         }
745         catch (Exception e) {
746             throw HibernateUtil.processException(e);
747         }
748         finally {
749             closeSession(session);
750         }
751     }
752 
753     public PortletPreferences findByO_O_P_P(long ownerId, int ownerType,
754         long plid, String portletId)
755         throws NoSuchPortletPreferencesException, SystemException {
756         PortletPreferences portletPreferences = fetchByO_O_P_P(ownerId,
757                 ownerType, plid, portletId);
758 
759         if (portletPreferences == null) {
760             StringMaker msg = new StringMaker();
761 
762             msg.append("No PortletPreferences exists with the key {");
763 
764             msg.append("ownerId=" + ownerId);
765 
766             msg.append(", ");
767             msg.append("ownerType=" + ownerType);
768 
769             msg.append(", ");
770             msg.append("plid=" + plid);
771 
772             msg.append(", ");
773             msg.append("portletId=" + portletId);
774 
775             msg.append(StringPool.CLOSE_CURLY_BRACE);
776 
777             if (_log.isWarnEnabled()) {
778                 _log.warn(msg.toString());
779             }
780 
781             throw new NoSuchPortletPreferencesException(msg.toString());
782         }
783 
784         return portletPreferences;
785     }
786 
787     public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
788         long plid, String portletId) throws SystemException {
789         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
790         String finderClassName = PortletPreferences.class.getName();
791         String finderMethodName = "fetchByO_O_P_P";
792         String[] finderParams = new String[] {
793                 Long.class.getName(), Integer.class.getName(),
794                 Long.class.getName(), String.class.getName()
795             };
796         Object[] finderArgs = new Object[] {
797                 new Long(ownerId), new Integer(ownerType), new Long(plid),
798                 
799                 portletId
800             };
801 
802         Object result = null;
803 
804         if (finderClassNameCacheEnabled) {
805             result = FinderCache.getResult(finderClassName, finderMethodName,
806                     finderParams, finderArgs, getSessionFactory());
807         }
808 
809         if (result == null) {
810             Session session = null;
811 
812             try {
813                 session = openSession();
814 
815                 StringMaker query = new StringMaker();
816 
817                 query.append(
818                     "FROM com.liferay.portal.model.PortletPreferences WHERE ");
819 
820                 query.append("ownerId = ?");
821 
822                 query.append(" AND ");
823 
824                 query.append("ownerType = ?");
825 
826                 query.append(" AND ");
827 
828                 query.append("plid = ?");
829 
830                 query.append(" AND ");
831 
832                 if (portletId == null) {
833                     query.append("portletId IS NULL");
834                 }
835                 else {
836                     query.append("portletId = ?");
837                 }
838 
839                 query.append(" ");
840 
841                 Query q = session.createQuery(query.toString());
842 
843                 int queryPos = 0;
844 
845                 q.setLong(queryPos++, ownerId);
846 
847                 q.setInteger(queryPos++, ownerType);
848 
849                 q.setLong(queryPos++, plid);
850 
851                 if (portletId != null) {
852                     q.setString(queryPos++, portletId);
853                 }
854 
855                 List list = q.list();
856 
857                 FinderCache.putResult(finderClassNameCacheEnabled,
858                     finderClassName, finderMethodName, finderParams,
859                     finderArgs, list);
860 
861                 if (list.size() == 0) {
862                     return null;
863                 }
864                 else {
865                     return (PortletPreferences)list.get(0);
866                 }
867             }
868             catch (Exception e) {
869                 throw HibernateUtil.processException(e);
870             }
871             finally {
872                 closeSession(session);
873             }
874         }
875         else {
876             List list = (List)result;
877 
878             if (list.size() == 0) {
879                 return null;
880             }
881             else {
882                 return (PortletPreferences)list.get(0);
883             }
884         }
885     }
886 
887     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
888         throws SystemException {
889         Session session = null;
890 
891         try {
892             session = openSession();
893 
894             DynamicQuery query = queryInitializer.initialize(session);
895 
896             return query.list();
897         }
898         catch (Exception e) {
899             throw HibernateUtil.processException(e);
900         }
901         finally {
902             closeSession(session);
903         }
904     }
905 
906     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
907         int begin, int end) throws SystemException {
908         Session session = null;
909 
910         try {
911             session = openSession();
912 
913             DynamicQuery query = queryInitializer.initialize(session);
914 
915             query.setLimit(begin, end);
916 
917             return query.list();
918         }
919         catch (Exception e) {
920             throw HibernateUtil.processException(e);
921         }
922         finally {
923             closeSession(session);
924         }
925     }
926 
927     public List findAll() throws SystemException {
928         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
929     }
930 
931     public List findAll(int begin, int end) throws SystemException {
932         return findAll(begin, end, null);
933     }
934 
935     public List findAll(int begin, int end, OrderByComparator obc)
936         throws SystemException {
937         boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
938         String finderClassName = PortletPreferences.class.getName();
939         String finderMethodName = "findAll";
940         String[] finderParams = new String[] {
941                 "java.lang.Integer", "java.lang.Integer",
942                 "com.liferay.portal.kernel.util.OrderByComparator"
943             };
944         Object[] finderArgs = new Object[] {
945                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
946             };
947 
948         Object result = null;
949 
950         if (finderClassNameCacheEnabled) {
951             result = FinderCache.getResult(finderClassName, finderMethodName,
952                     finderParams, finderArgs, getSessionFactory());
953         }
954 
955         if (result == null) {
956             Session session = null;
957 
958             try {
959                 session = openSession();
960 
961                 StringMaker query = new StringMaker();
962 
963                 query.append(
964                     "FROM com.liferay.portal.model.PortletPreferences ");
965 
966                 if (obc != null) {
967                     query.append("ORDER BY ");
968                     query.append(obc.getOrderBy());
969                 }
970 
971                 Query q = session.createQuery(query.toString());
972 
973                 List list = QueryUtil.list(q, getDialect(), begin, end);
974 
975                 if (obc == null) {
976                     Collections.sort(list);
977                 }
978 
979                 FinderCache.putResult(finderClassNameCacheEnabled,
980                     finderClassName, finderMethodName, finderParams,
981                     finderArgs, list);
982 
983                 return list;
984             }
985             catch (Exception e) {
986                 throw HibernateUtil.processException(e);
987             }
988             finally {
989                 closeSession(session);
990             }
991         }
992         else {
993             return (List)result;
994         }
995     }
996 
997     public void removeByPlid(long plid) throws SystemException {
998         Iterator itr = findByPlid(plid).iterator();
999 
1000        while (itr.hasNext()) {
1001            PortletPreferences portletPreferences = (PortletPreferences)itr.next();
1002
1003            remove(portletPreferences);
1004        }
1005    }
1006
1007    public void removeByO_O_P(long ownerId, int ownerType, long plid)
1008        throws SystemException {
1009        Iterator itr = findByO_O_P(ownerId, ownerType, plid).iterator();
1010
1011        while (itr.hasNext()) {
1012            PortletPreferences portletPreferences = (PortletPreferences)itr.next();
1013
1014            remove(portletPreferences);
1015        }
1016    }
1017
1018    public void removeByO_O_P_P(long ownerId, int ownerType, long plid,
1019        String portletId)
1020        throws NoSuchPortletPreferencesException, SystemException {
1021        PortletPreferences portletPreferences = findByO_O_P_P(ownerId,
1022                ownerType, plid, portletId);
1023
1024        remove(portletPreferences);
1025    }
1026
1027    public void removeAll() throws SystemException {
1028        Iterator itr = findAll().iterator();
1029
1030        while (itr.hasNext()) {
1031            remove((PortletPreferences)itr.next());
1032        }
1033    }
1034
1035    public int countByPlid(long plid) throws SystemException {
1036        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1037        String finderClassName = PortletPreferences.class.getName();
1038        String finderMethodName = "countByPlid";
1039        String[] finderParams = new String[] { Long.class.getName() };
1040        Object[] finderArgs = new Object[] { new Long(plid) };
1041
1042        Object result = null;
1043
1044        if (finderClassNameCacheEnabled) {
1045            result = FinderCache.getResult(finderClassName, finderMethodName,
1046                    finderParams, finderArgs, getSessionFactory());
1047        }
1048
1049        if (result == null) {
1050            Session session = null;
1051
1052            try {
1053                session = openSession();
1054
1055                StringMaker query = new StringMaker();
1056
1057                query.append("SELECT COUNT(*) ");
1058                query.append(
1059                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1060
1061                query.append("plid = ?");
1062
1063                query.append(" ");
1064
1065                Query q = session.createQuery(query.toString());
1066
1067                int queryPos = 0;
1068
1069                q.setLong(queryPos++, plid);
1070
1071                Long count = null;
1072
1073                Iterator itr = q.list().iterator();
1074
1075                if (itr.hasNext()) {
1076                    count = (Long)itr.next();
1077                }
1078
1079                if (count == null) {
1080                    count = new Long(0);
1081                }
1082
1083                FinderCache.putResult(finderClassNameCacheEnabled,
1084                    finderClassName, finderMethodName, finderParams,
1085                    finderArgs, count);
1086
1087                return count.intValue();
1088            }
1089            catch (Exception e) {
1090                throw HibernateUtil.processException(e);
1091            }
1092            finally {
1093                closeSession(session);
1094            }
1095        }
1096        else {
1097            return ((Long)result).intValue();
1098        }
1099    }
1100
1101    public int countByO_O_P(long ownerId, int ownerType, long plid)
1102        throws SystemException {
1103        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1104        String finderClassName = PortletPreferences.class.getName();
1105        String finderMethodName = "countByO_O_P";
1106        String[] finderParams = new String[] {
1107                Long.class.getName(), Integer.class.getName(),
1108                Long.class.getName()
1109            };
1110        Object[] finderArgs = new Object[] {
1111                new Long(ownerId), new Integer(ownerType), new Long(plid)
1112            };
1113
1114        Object result = null;
1115
1116        if (finderClassNameCacheEnabled) {
1117            result = FinderCache.getResult(finderClassName, finderMethodName,
1118                    finderParams, finderArgs, getSessionFactory());
1119        }
1120
1121        if (result == null) {
1122            Session session = null;
1123
1124            try {
1125                session = openSession();
1126
1127                StringMaker query = new StringMaker();
1128
1129                query.append("SELECT COUNT(*) ");
1130                query.append(
1131                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1132
1133                query.append("ownerId = ?");
1134
1135                query.append(" AND ");
1136
1137                query.append("ownerType = ?");
1138
1139                query.append(" AND ");
1140
1141                query.append("plid = ?");
1142
1143                query.append(" ");
1144
1145                Query q = session.createQuery(query.toString());
1146
1147                int queryPos = 0;
1148
1149                q.setLong(queryPos++, ownerId);
1150
1151                q.setInteger(queryPos++, ownerType);
1152
1153                q.setLong(queryPos++, plid);
1154
1155                Long count = null;
1156
1157                Iterator itr = q.list().iterator();
1158
1159                if (itr.hasNext()) {
1160                    count = (Long)itr.next();
1161                }
1162
1163                if (count == null) {
1164                    count = new Long(0);
1165                }
1166
1167                FinderCache.putResult(finderClassNameCacheEnabled,
1168                    finderClassName, finderMethodName, finderParams,
1169                    finderArgs, count);
1170
1171                return count.intValue();
1172            }
1173            catch (Exception e) {
1174                throw HibernateUtil.processException(e);
1175            }
1176            finally {
1177                closeSession(session);
1178            }
1179        }
1180        else {
1181            return ((Long)result).intValue();
1182        }
1183    }
1184
1185    public int countByO_O_P_P(long ownerId, int ownerType, long plid,
1186        String portletId) throws SystemException {
1187        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1188        String finderClassName = PortletPreferences.class.getName();
1189        String finderMethodName = "countByO_O_P_P";
1190        String[] finderParams = new String[] {
1191                Long.class.getName(), Integer.class.getName(),
1192                Long.class.getName(), String.class.getName()
1193            };
1194        Object[] finderArgs = new Object[] {
1195                new Long(ownerId), new Integer(ownerType), new Long(plid),
1196                
1197                portletId
1198            };
1199
1200        Object result = null;
1201
1202        if (finderClassNameCacheEnabled) {
1203            result = FinderCache.getResult(finderClassName, finderMethodName,
1204                    finderParams, finderArgs, getSessionFactory());
1205        }
1206
1207        if (result == null) {
1208            Session session = null;
1209
1210            try {
1211                session = openSession();
1212
1213                StringMaker query = new StringMaker();
1214
1215                query.append("SELECT COUNT(*) ");
1216                query.append(
1217                    "FROM com.liferay.portal.model.PortletPreferences WHERE ");
1218
1219                query.append("ownerId = ?");
1220
1221                query.append(" AND ");
1222
1223                query.append("ownerType = ?");
1224
1225                query.append(" AND ");
1226
1227                query.append("plid = ?");
1228
1229                query.append(" AND ");
1230
1231                if (portletId == null) {
1232                    query.append("portletId IS NULL");
1233                }
1234                else {
1235                    query.append("portletId = ?");
1236                }
1237
1238                query.append(" ");
1239
1240                Query q = session.createQuery(query.toString());
1241
1242                int queryPos = 0;
1243
1244                q.setLong(queryPos++, ownerId);
1245
1246                q.setInteger(queryPos++, ownerType);
1247
1248                q.setLong(queryPos++, plid);
1249
1250                if (portletId != null) {
1251                    q.setString(queryPos++, portletId);
1252                }
1253
1254                Long count = null;
1255
1256                Iterator itr = q.list().iterator();
1257
1258                if (itr.hasNext()) {
1259                    count = (Long)itr.next();
1260                }
1261
1262                if (count == null) {
1263                    count = new Long(0);
1264                }
1265
1266                FinderCache.putResult(finderClassNameCacheEnabled,
1267                    finderClassName, finderMethodName, finderParams,
1268                    finderArgs, count);
1269
1270                return count.intValue();
1271            }
1272            catch (Exception e) {
1273                throw HibernateUtil.processException(e);
1274            }
1275            finally {
1276                closeSession(session);
1277            }
1278        }
1279        else {
1280            return ((Long)result).intValue();
1281        }
1282    }
1283
1284    public int countAll() throws SystemException {
1285        boolean finderClassNameCacheEnabled = PortletPreferencesModelImpl.CACHE_ENABLED;
1286        String finderClassName = PortletPreferences.class.getName();
1287        String finderMethodName = "countAll";
1288        String[] finderParams = new String[] {  };
1289        Object[] finderArgs = new Object[] {  };
1290
1291        Object result = null;
1292
1293        if (finderClassNameCacheEnabled) {
1294            result = FinderCache.getResult(finderClassName, finderMethodName,
1295                    finderParams, finderArgs, getSessionFactory());
1296        }
1297
1298        if (result == null) {
1299            Session session = null;
1300
1301            try {
1302                session = openSession();
1303
1304                Query q = session.createQuery(
1305                        "SELECT COUNT(*) FROM com.liferay.portal.model.PortletPreferences");
1306
1307                Long count = null;
1308
1309                Iterator itr = q.list().iterator();
1310
1311                if (itr.hasNext()) {
1312                    count = (Long)itr.next();
1313                }
1314
1315                if (count == null) {
1316                    count = new Long(0);
1317                }
1318
1319                FinderCache.putResult(finderClassNameCacheEnabled,
1320                    finderClassName, finderMethodName, finderParams,
1321                    finderArgs, count);
1322
1323                return count.intValue();
1324            }
1325            catch (Exception e) {
1326                throw HibernateUtil.processException(e);
1327            }
1328            finally {
1329                closeSession(session);
1330            }
1331        }
1332        else {
1333            return ((Long)result).intValue();
1334        }
1335    }
1336
1337    protected void initDao() {
1338    }
1339
1340    private static ModelListener _getListener() {
1341        if (Validator.isNotNull(_LISTENER)) {
1342            try {
1343                return (ModelListener)Class.forName(_LISTENER).newInstance();
1344            }
1345            catch (Exception e) {
1346                _log.error(e);
1347            }
1348        }
1349
1350        return null;
1351    }
1352
1353    private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
1354                "value.object.listener.com.liferay.portal.model.PortletPreferences"));
1355    private static Log _log = LogFactory.getLog(PortletPreferencesPersistenceImpl.class);
1356}