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