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