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