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