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