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.portlet.announcements.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  import com.liferay.portal.util.PropsUtil;
38  
39  import com.liferay.portlet.announcements.NoSuchFlagException;
40  import com.liferay.portlet.announcements.model.AnnouncementsFlag;
41  import com.liferay.portlet.announcements.model.impl.AnnouncementsFlagImpl;
42  import com.liferay.portlet.announcements.model.impl.AnnouncementsFlagModelImpl;
43  
44  import com.liferay.util.dao.hibernate.QueryUtil;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.hibernate.Query;
50  import org.hibernate.Session;
51  
52  import java.util.ArrayList;
53  import java.util.Collections;
54  import java.util.Iterator;
55  import java.util.List;
56  
57  /**
58   * <a href="AnnouncementsFlagPersistenceImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class AnnouncementsFlagPersistenceImpl extends BasePersistence
64      implements AnnouncementsFlagPersistence {
65      public AnnouncementsFlag create(long flagId) {
66          AnnouncementsFlag announcementsFlag = new AnnouncementsFlagImpl();
67  
68          announcementsFlag.setNew(true);
69          announcementsFlag.setPrimaryKey(flagId);
70  
71          return announcementsFlag;
72      }
73  
74      public AnnouncementsFlag remove(long flagId)
75          throws NoSuchFlagException, SystemException {
76          Session session = null;
77  
78          try {
79              session = openSession();
80  
81              AnnouncementsFlag announcementsFlag = (AnnouncementsFlag)session.get(AnnouncementsFlagImpl.class,
82                      new Long(flagId));
83  
84              if (announcementsFlag == null) {
85                  if (_log.isWarnEnabled()) {
86                      _log.warn(
87                          "No AnnouncementsFlag exists with the primary key " +
88                          flagId);
89                  }
90  
91                  throw new NoSuchFlagException(
92                      "No AnnouncementsFlag exists with the primary key " +
93                      flagId);
94              }
95  
96              return remove(announcementsFlag);
97          }
98          catch (NoSuchFlagException nsee) {
99              throw nsee;
100         }
101         catch (Exception e) {
102             throw HibernateUtil.processException(e);
103         }
104         finally {
105             closeSession(session);
106         }
107     }
108 
109     public AnnouncementsFlag remove(AnnouncementsFlag announcementsFlag)
110         throws SystemException {
111         if (_listeners != null) {
112             for (ModelListener listener : _listeners) {
113                 listener.onBeforeRemove(announcementsFlag);
114             }
115         }
116 
117         announcementsFlag = removeImpl(announcementsFlag);
118 
119         if (_listeners != null) {
120             for (ModelListener listener : _listeners) {
121                 listener.onAfterRemove(announcementsFlag);
122             }
123         }
124 
125         return announcementsFlag;
126     }
127 
128     protected AnnouncementsFlag removeImpl(AnnouncementsFlag announcementsFlag)
129         throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             session.delete(announcementsFlag);
136 
137             session.flush();
138 
139             return announcementsFlag;
140         }
141         catch (Exception e) {
142             throw HibernateUtil.processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCache.clearCache(AnnouncementsFlag.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(AnnouncementsFlag announcementsFlag, boolean merge)</code>.
153      */
154     public AnnouncementsFlag update(AnnouncementsFlag announcementsFlag)
155         throws SystemException {
156         if (_log.isWarnEnabled()) {
157             _log.warn(
158                 "Using the deprecated update(AnnouncementsFlag announcementsFlag) method. Use update(AnnouncementsFlag announcementsFlag, boolean merge) instead.");
159         }
160 
161         return update(announcementsFlag, false);
162     }
163 
164     /**
165      * Add, update, or merge, the entity. This method also calls the model
166      * listeners to trigger the proper events associated with adding, deleting,
167      * or updating an entity.
168      *
169      * @param        announcementsFlag the entity to add, update, or merge
170      * @param        merge boolean value for whether to merge the entity. The
171      *                default value is false. Setting merge to true is more
172      *                expensive and should only be true when announcementsFlag is
173      *                transient. See LEP-5473 for a detailed discussion of this
174      *                method.
175      * @return        true if the portlet can be displayed via Ajax
176      */
177     public AnnouncementsFlag update(AnnouncementsFlag announcementsFlag,
178         boolean merge) throws SystemException {
179         boolean isNew = announcementsFlag.isNew();
180 
181         if (_listeners != null) {
182             for (ModelListener listener : _listeners) {
183                 if (isNew) {
184                     listener.onBeforeCreate(announcementsFlag);
185                 }
186                 else {
187                     listener.onBeforeUpdate(announcementsFlag);
188                 }
189             }
190         }
191 
192         announcementsFlag = updateImpl(announcementsFlag, merge);
193 
194         if (_listeners != null) {
195             for (ModelListener listener : _listeners) {
196                 if (isNew) {
197                     listener.onAfterCreate(announcementsFlag);
198                 }
199                 else {
200                     listener.onAfterUpdate(announcementsFlag);
201                 }
202             }
203         }
204 
205         return announcementsFlag;
206     }
207 
208     public AnnouncementsFlag updateImpl(
209         com.liferay.portlet.announcements.model.AnnouncementsFlag announcementsFlag,
210         boolean merge) throws SystemException {
211         Session session = null;
212 
213         try {
214             session = openSession();
215 
216             if (merge) {
217                 session.merge(announcementsFlag);
218             }
219             else {
220                 if (announcementsFlag.isNew()) {
221                     session.save(announcementsFlag);
222                 }
223             }
224 
225             session.flush();
226 
227             announcementsFlag.setNew(false);
228 
229             return announcementsFlag;
230         }
231         catch (Exception e) {
232             throw HibernateUtil.processException(e);
233         }
234         finally {
235             closeSession(session);
236 
237             FinderCache.clearCache(AnnouncementsFlag.class.getName());
238         }
239     }
240 
241     public AnnouncementsFlag findByPrimaryKey(long flagId)
242         throws NoSuchFlagException, SystemException {
243         AnnouncementsFlag announcementsFlag = fetchByPrimaryKey(flagId);
244 
245         if (announcementsFlag == null) {
246             if (_log.isWarnEnabled()) {
247                 _log.warn("No AnnouncementsFlag exists with the primary key " +
248                     flagId);
249             }
250 
251             throw new NoSuchFlagException(
252                 "No AnnouncementsFlag exists with the primary key " + flagId);
253         }
254 
255         return announcementsFlag;
256     }
257 
258     public AnnouncementsFlag fetchByPrimaryKey(long flagId)
259         throws SystemException {
260         Session session = null;
261 
262         try {
263             session = openSession();
264 
265             return (AnnouncementsFlag)session.get(AnnouncementsFlagImpl.class,
266                 new Long(flagId));
267         }
268         catch (Exception e) {
269             throw HibernateUtil.processException(e);
270         }
271         finally {
272             closeSession(session);
273         }
274     }
275 
276     public List<AnnouncementsFlag> findByEntryId(long entryId)
277         throws SystemException {
278         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
279         String finderClassName = AnnouncementsFlag.class.getName();
280         String finderMethodName = "findByEntryId";
281         String[] finderParams = new String[] { Long.class.getName() };
282         Object[] finderArgs = new Object[] { new Long(entryId) };
283 
284         Object result = null;
285 
286         if (finderClassNameCacheEnabled) {
287             result = FinderCache.getResult(finderClassName, finderMethodName,
288                     finderParams, finderArgs, getSessionFactory());
289         }
290 
291         if (result == null) {
292             Session session = null;
293 
294             try {
295                 session = openSession();
296 
297                 StringMaker query = new StringMaker();
298 
299                 query.append(
300                     "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag WHERE ");
301 
302                 query.append("entryId = ?");
303 
304                 query.append(" ");
305 
306                 query.append("ORDER BY ");
307 
308                 query.append("userId ASC, ");
309                 query.append("createDate ASC");
310 
311                 Query q = session.createQuery(query.toString());
312 
313                 int queryPos = 0;
314 
315                 q.setLong(queryPos++, entryId);
316 
317                 List<AnnouncementsFlag> list = q.list();
318 
319                 FinderCache.putResult(finderClassNameCacheEnabled,
320                     finderClassName, finderMethodName, finderParams,
321                     finderArgs, list);
322 
323                 return list;
324             }
325             catch (Exception e) {
326                 throw HibernateUtil.processException(e);
327             }
328             finally {
329                 closeSession(session);
330             }
331         }
332         else {
333             return (List<AnnouncementsFlag>)result;
334         }
335     }
336 
337     public List<AnnouncementsFlag> findByEntryId(long entryId, int begin,
338         int end) throws SystemException {
339         return findByEntryId(entryId, begin, end, null);
340     }
341 
342     public List<AnnouncementsFlag> findByEntryId(long entryId, int begin,
343         int end, OrderByComparator obc) throws SystemException {
344         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
345         String finderClassName = AnnouncementsFlag.class.getName();
346         String finderMethodName = "findByEntryId";
347         String[] finderParams = new String[] {
348                 Long.class.getName(),
349                 
350                 "java.lang.Integer", "java.lang.Integer",
351                 "com.liferay.portal.kernel.util.OrderByComparator"
352             };
353         Object[] finderArgs = new Object[] {
354                 new Long(entryId),
355                 
356                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
357             };
358 
359         Object result = null;
360 
361         if (finderClassNameCacheEnabled) {
362             result = FinderCache.getResult(finderClassName, finderMethodName,
363                     finderParams, finderArgs, getSessionFactory());
364         }
365 
366         if (result == null) {
367             Session session = null;
368 
369             try {
370                 session = openSession();
371 
372                 StringMaker query = new StringMaker();
373 
374                 query.append(
375                     "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag WHERE ");
376 
377                 query.append("entryId = ?");
378 
379                 query.append(" ");
380 
381                 if (obc != null) {
382                     query.append("ORDER BY ");
383                     query.append(obc.getOrderBy());
384                 }
385 
386                 else {
387                     query.append("ORDER BY ");
388 
389                     query.append("userId ASC, ");
390                     query.append("createDate ASC");
391                 }
392 
393                 Query q = session.createQuery(query.toString());
394 
395                 int queryPos = 0;
396 
397                 q.setLong(queryPos++, entryId);
398 
399                 List<AnnouncementsFlag> list = (List<AnnouncementsFlag>)QueryUtil.list(q,
400                         getDialect(), begin, end);
401 
402                 FinderCache.putResult(finderClassNameCacheEnabled,
403                     finderClassName, finderMethodName, finderParams,
404                     finderArgs, list);
405 
406                 return list;
407             }
408             catch (Exception e) {
409                 throw HibernateUtil.processException(e);
410             }
411             finally {
412                 closeSession(session);
413             }
414         }
415         else {
416             return (List<AnnouncementsFlag>)result;
417         }
418     }
419 
420     public AnnouncementsFlag findByEntryId_First(long entryId,
421         OrderByComparator obc) throws NoSuchFlagException, SystemException {
422         List<AnnouncementsFlag> list = findByEntryId(entryId, 0, 1, obc);
423 
424         if (list.size() == 0) {
425             StringMaker msg = new StringMaker();
426 
427             msg.append("No AnnouncementsFlag exists with the key {");
428 
429             msg.append("entryId=" + entryId);
430 
431             msg.append(StringPool.CLOSE_CURLY_BRACE);
432 
433             throw new NoSuchFlagException(msg.toString());
434         }
435         else {
436             return list.get(0);
437         }
438     }
439 
440     public AnnouncementsFlag findByEntryId_Last(long entryId,
441         OrderByComparator obc) throws NoSuchFlagException, SystemException {
442         int count = countByEntryId(entryId);
443 
444         List<AnnouncementsFlag> list = findByEntryId(entryId, count - 1, count,
445                 obc);
446 
447         if (list.size() == 0) {
448             StringMaker msg = new StringMaker();
449 
450             msg.append("No AnnouncementsFlag exists with the key {");
451 
452             msg.append("entryId=" + entryId);
453 
454             msg.append(StringPool.CLOSE_CURLY_BRACE);
455 
456             throw new NoSuchFlagException(msg.toString());
457         }
458         else {
459             return list.get(0);
460         }
461     }
462 
463     public AnnouncementsFlag[] findByEntryId_PrevAndNext(long flagId,
464         long entryId, OrderByComparator obc)
465         throws NoSuchFlagException, SystemException {
466         AnnouncementsFlag announcementsFlag = findByPrimaryKey(flagId);
467 
468         int count = countByEntryId(entryId);
469 
470         Session session = null;
471 
472         try {
473             session = openSession();
474 
475             StringMaker query = new StringMaker();
476 
477             query.append(
478                 "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag WHERE ");
479 
480             query.append("entryId = ?");
481 
482             query.append(" ");
483 
484             if (obc != null) {
485                 query.append("ORDER BY ");
486                 query.append(obc.getOrderBy());
487             }
488 
489             else {
490                 query.append("ORDER BY ");
491 
492                 query.append("userId ASC, ");
493                 query.append("createDate ASC");
494             }
495 
496             Query q = session.createQuery(query.toString());
497 
498             int queryPos = 0;
499 
500             q.setLong(queryPos++, entryId);
501 
502             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
503                     announcementsFlag);
504 
505             AnnouncementsFlag[] array = new AnnouncementsFlagImpl[3];
506 
507             array[0] = (AnnouncementsFlag)objArray[0];
508             array[1] = (AnnouncementsFlag)objArray[1];
509             array[2] = (AnnouncementsFlag)objArray[2];
510 
511             return array;
512         }
513         catch (Exception e) {
514             throw HibernateUtil.processException(e);
515         }
516         finally {
517             closeSession(session);
518         }
519     }
520 
521     public AnnouncementsFlag findByU_E_V(long userId, long entryId, int value)
522         throws NoSuchFlagException, SystemException {
523         AnnouncementsFlag announcementsFlag = fetchByU_E_V(userId, entryId,
524                 value);
525 
526         if (announcementsFlag == null) {
527             StringMaker msg = new StringMaker();
528 
529             msg.append("No AnnouncementsFlag exists with the key {");
530 
531             msg.append("userId=" + userId);
532 
533             msg.append(", ");
534             msg.append("entryId=" + entryId);
535 
536             msg.append(", ");
537             msg.append("value=" + value);
538 
539             msg.append(StringPool.CLOSE_CURLY_BRACE);
540 
541             if (_log.isWarnEnabled()) {
542                 _log.warn(msg.toString());
543             }
544 
545             throw new NoSuchFlagException(msg.toString());
546         }
547 
548         return announcementsFlag;
549     }
550 
551     public AnnouncementsFlag fetchByU_E_V(long userId, long entryId, int value)
552         throws SystemException {
553         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
554         String finderClassName = AnnouncementsFlag.class.getName();
555         String finderMethodName = "fetchByU_E_V";
556         String[] finderParams = new String[] {
557                 Long.class.getName(), Long.class.getName(),
558                 Integer.class.getName()
559             };
560         Object[] finderArgs = new Object[] {
561                 new Long(userId), new Long(entryId), new Integer(value)
562             };
563 
564         Object result = null;
565 
566         if (finderClassNameCacheEnabled) {
567             result = FinderCache.getResult(finderClassName, finderMethodName,
568                     finderParams, finderArgs, getSessionFactory());
569         }
570 
571         if (result == null) {
572             Session session = null;
573 
574             try {
575                 session = openSession();
576 
577                 StringMaker query = new StringMaker();
578 
579                 query.append(
580                     "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag WHERE ");
581 
582                 query.append("userId = ?");
583 
584                 query.append(" AND ");
585 
586                 query.append("entryId = ?");
587 
588                 query.append(" AND ");
589 
590                 query.append("value = ?");
591 
592                 query.append(" ");
593 
594                 query.append("ORDER BY ");
595 
596                 query.append("userId ASC, ");
597                 query.append("createDate ASC");
598 
599                 Query q = session.createQuery(query.toString());
600 
601                 int queryPos = 0;
602 
603                 q.setLong(queryPos++, userId);
604 
605                 q.setLong(queryPos++, entryId);
606 
607                 q.setInteger(queryPos++, value);
608 
609                 List<AnnouncementsFlag> list = q.list();
610 
611                 FinderCache.putResult(finderClassNameCacheEnabled,
612                     finderClassName, finderMethodName, finderParams,
613                     finderArgs, list);
614 
615                 if (list.size() == 0) {
616                     return null;
617                 }
618                 else {
619                     return list.get(0);
620                 }
621             }
622             catch (Exception e) {
623                 throw HibernateUtil.processException(e);
624             }
625             finally {
626                 closeSession(session);
627             }
628         }
629         else {
630             List<AnnouncementsFlag> list = (List<AnnouncementsFlag>)result;
631 
632             if (list.size() == 0) {
633                 return null;
634             }
635             else {
636                 return list.get(0);
637             }
638         }
639     }
640 
641     public List<AnnouncementsFlag> findWithDynamicQuery(
642         DynamicQueryInitializer queryInitializer) throws SystemException {
643         Session session = null;
644 
645         try {
646             session = openSession();
647 
648             DynamicQuery query = queryInitializer.initialize(session);
649 
650             return query.list();
651         }
652         catch (Exception e) {
653             throw HibernateUtil.processException(e);
654         }
655         finally {
656             closeSession(session);
657         }
658     }
659 
660     public List<AnnouncementsFlag> findWithDynamicQuery(
661         DynamicQueryInitializer queryInitializer, int begin, int end)
662         throws SystemException {
663         Session session = null;
664 
665         try {
666             session = openSession();
667 
668             DynamicQuery query = queryInitializer.initialize(session);
669 
670             query.setLimit(begin, end);
671 
672             return query.list();
673         }
674         catch (Exception e) {
675             throw HibernateUtil.processException(e);
676         }
677         finally {
678             closeSession(session);
679         }
680     }
681 
682     public List<AnnouncementsFlag> findAll() throws SystemException {
683         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
684     }
685 
686     public List<AnnouncementsFlag> findAll(int begin, int end)
687         throws SystemException {
688         return findAll(begin, end, null);
689     }
690 
691     public List<AnnouncementsFlag> findAll(int begin, int end,
692         OrderByComparator obc) throws SystemException {
693         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
694         String finderClassName = AnnouncementsFlag.class.getName();
695         String finderMethodName = "findAll";
696         String[] finderParams = new String[] {
697                 "java.lang.Integer", "java.lang.Integer",
698                 "com.liferay.portal.kernel.util.OrderByComparator"
699             };
700         Object[] finderArgs = new Object[] {
701                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
702             };
703 
704         Object result = null;
705 
706         if (finderClassNameCacheEnabled) {
707             result = FinderCache.getResult(finderClassName, finderMethodName,
708                     finderParams, finderArgs, getSessionFactory());
709         }
710 
711         if (result == null) {
712             Session session = null;
713 
714             try {
715                 session = openSession();
716 
717                 StringMaker query = new StringMaker();
718 
719                 query.append(
720                     "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag ");
721 
722                 if (obc != null) {
723                     query.append("ORDER BY ");
724                     query.append(obc.getOrderBy());
725                 }
726 
727                 else {
728                     query.append("ORDER BY ");
729 
730                     query.append("userId ASC, ");
731                     query.append("createDate ASC");
732                 }
733 
734                 Query q = session.createQuery(query.toString());
735 
736                 List<AnnouncementsFlag> list = (List<AnnouncementsFlag>)QueryUtil.list(q,
737                         getDialect(), begin, end);
738 
739                 if (obc == null) {
740                     Collections.sort(list);
741                 }
742 
743                 FinderCache.putResult(finderClassNameCacheEnabled,
744                     finderClassName, finderMethodName, finderParams,
745                     finderArgs, list);
746 
747                 return list;
748             }
749             catch (Exception e) {
750                 throw HibernateUtil.processException(e);
751             }
752             finally {
753                 closeSession(session);
754             }
755         }
756         else {
757             return (List<AnnouncementsFlag>)result;
758         }
759     }
760 
761     public void removeByEntryId(long entryId) throws SystemException {
762         for (AnnouncementsFlag announcementsFlag : findByEntryId(entryId)) {
763             remove(announcementsFlag);
764         }
765     }
766 
767     public void removeByU_E_V(long userId, long entryId, int value)
768         throws NoSuchFlagException, SystemException {
769         AnnouncementsFlag announcementsFlag = findByU_E_V(userId, entryId, value);
770 
771         remove(announcementsFlag);
772     }
773 
774     public void removeAll() throws SystemException {
775         for (AnnouncementsFlag announcementsFlag : findAll()) {
776             remove(announcementsFlag);
777         }
778     }
779 
780     public int countByEntryId(long entryId) throws SystemException {
781         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
782         String finderClassName = AnnouncementsFlag.class.getName();
783         String finderMethodName = "countByEntryId";
784         String[] finderParams = new String[] { Long.class.getName() };
785         Object[] finderArgs = new Object[] { new Long(entryId) };
786 
787         Object result = null;
788 
789         if (finderClassNameCacheEnabled) {
790             result = FinderCache.getResult(finderClassName, finderMethodName,
791                     finderParams, finderArgs, getSessionFactory());
792         }
793 
794         if (result == null) {
795             Session session = null;
796 
797             try {
798                 session = openSession();
799 
800                 StringMaker query = new StringMaker();
801 
802                 query.append("SELECT COUNT(*) ");
803                 query.append(
804                     "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag WHERE ");
805 
806                 query.append("entryId = ?");
807 
808                 query.append(" ");
809 
810                 Query q = session.createQuery(query.toString());
811 
812                 int queryPos = 0;
813 
814                 q.setLong(queryPos++, entryId);
815 
816                 Long count = null;
817 
818                 Iterator<Long> itr = q.list().iterator();
819 
820                 if (itr.hasNext()) {
821                     count = itr.next();
822                 }
823 
824                 if (count == null) {
825                     count = new Long(0);
826                 }
827 
828                 FinderCache.putResult(finderClassNameCacheEnabled,
829                     finderClassName, finderMethodName, finderParams,
830                     finderArgs, count);
831 
832                 return count.intValue();
833             }
834             catch (Exception e) {
835                 throw HibernateUtil.processException(e);
836             }
837             finally {
838                 closeSession(session);
839             }
840         }
841         else {
842             return ((Long)result).intValue();
843         }
844     }
845 
846     public int countByU_E_V(long userId, long entryId, int value)
847         throws SystemException {
848         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
849         String finderClassName = AnnouncementsFlag.class.getName();
850         String finderMethodName = "countByU_E_V";
851         String[] finderParams = new String[] {
852                 Long.class.getName(), Long.class.getName(),
853                 Integer.class.getName()
854             };
855         Object[] finderArgs = new Object[] {
856                 new Long(userId), new Long(entryId), new Integer(value)
857             };
858 
859         Object result = null;
860 
861         if (finderClassNameCacheEnabled) {
862             result = FinderCache.getResult(finderClassName, finderMethodName,
863                     finderParams, finderArgs, getSessionFactory());
864         }
865 
866         if (result == null) {
867             Session session = null;
868 
869             try {
870                 session = openSession();
871 
872                 StringMaker query = new StringMaker();
873 
874                 query.append("SELECT COUNT(*) ");
875                 query.append(
876                     "FROM com.liferay.portlet.announcements.model.AnnouncementsFlag WHERE ");
877 
878                 query.append("userId = ?");
879 
880                 query.append(" AND ");
881 
882                 query.append("entryId = ?");
883 
884                 query.append(" AND ");
885 
886                 query.append("value = ?");
887 
888                 query.append(" ");
889 
890                 Query q = session.createQuery(query.toString());
891 
892                 int queryPos = 0;
893 
894                 q.setLong(queryPos++, userId);
895 
896                 q.setLong(queryPos++, entryId);
897 
898                 q.setInteger(queryPos++, value);
899 
900                 Long count = null;
901 
902                 Iterator<Long> itr = q.list().iterator();
903 
904                 if (itr.hasNext()) {
905                     count = itr.next();
906                 }
907 
908                 if (count == null) {
909                     count = new Long(0);
910                 }
911 
912                 FinderCache.putResult(finderClassNameCacheEnabled,
913                     finderClassName, finderMethodName, finderParams,
914                     finderArgs, count);
915 
916                 return count.intValue();
917             }
918             catch (Exception e) {
919                 throw HibernateUtil.processException(e);
920             }
921             finally {
922                 closeSession(session);
923             }
924         }
925         else {
926             return ((Long)result).intValue();
927         }
928     }
929 
930     public int countAll() throws SystemException {
931         boolean finderClassNameCacheEnabled = AnnouncementsFlagModelImpl.CACHE_ENABLED;
932         String finderClassName = AnnouncementsFlag.class.getName();
933         String finderMethodName = "countAll";
934         String[] finderParams = new String[] {  };
935         Object[] finderArgs = new Object[] {  };
936 
937         Object result = null;
938 
939         if (finderClassNameCacheEnabled) {
940             result = FinderCache.getResult(finderClassName, finderMethodName,
941                     finderParams, finderArgs, getSessionFactory());
942         }
943 
944         if (result == null) {
945             Session session = null;
946 
947             try {
948                 session = openSession();
949 
950                 Query q = session.createQuery(
951                         "SELECT COUNT(*) FROM com.liferay.portlet.announcements.model.AnnouncementsFlag");
952 
953                 Long count = null;
954 
955                 Iterator<Long> itr = q.list().iterator();
956 
957                 if (itr.hasNext()) {
958                     count = itr.next();
959                 }
960 
961                 if (count == null) {
962                     count = new Long(0);
963                 }
964 
965                 FinderCache.putResult(finderClassNameCacheEnabled,
966                     finderClassName, finderMethodName, finderParams,
967                     finderArgs, count);
968 
969                 return count.intValue();
970             }
971             catch (Exception e) {
972                 throw HibernateUtil.processException(e);
973             }
974             finally {
975                 closeSession(session);
976             }
977         }
978         else {
979             return ((Long)result).intValue();
980         }
981     }
982 
983     protected void initDao() {
984         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
985                     PropsUtil.get(
986                         "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsFlag")));
987 
988         if (listenerClassNames.length > 0) {
989             try {
990                 List<ModelListener> listeners = new ArrayList<ModelListener>();
991 
992                 for (String listenerClassName : listenerClassNames) {
993                     listeners.add((ModelListener)Class.forName(
994                             listenerClassName).newInstance());
995                 }
996 
997                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
998             }
999             catch (Exception e) {
1000                _log.error(e);
1001            }
1002        }
1003    }
1004
1005    private static Log _log = LogFactory.getLog(AnnouncementsFlagPersistenceImpl.class);
1006    private ModelListener[] _listeners;
1007}