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