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