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