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