001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.announcements.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.InstanceFactory;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.UnmodifiableList;
036    import com.liferay.portal.model.CacheModel;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
039    
040    import com.liferay.portlet.announcements.NoSuchFlagException;
041    import com.liferay.portlet.announcements.model.AnnouncementsFlag;
042    import com.liferay.portlet.announcements.model.impl.AnnouncementsFlagImpl;
043    import com.liferay.portlet.announcements.model.impl.AnnouncementsFlagModelImpl;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Collections;
049    import java.util.List;
050    
051    /**
052     * The persistence implementation for the announcements flag service.
053     *
054     * <p>
055     * Caching information and settings can be found in <code>portal.properties</code>
056     * </p>
057     *
058     * @author Brian Wing Shun Chan
059     * @see AnnouncementsFlagPersistence
060     * @see AnnouncementsFlagUtil
061     * @generated
062     */
063    public class AnnouncementsFlagPersistenceImpl extends BasePersistenceImpl<AnnouncementsFlag>
064            implements AnnouncementsFlagPersistence {
065            /*
066             * NOTE FOR DEVELOPERS:
067             *
068             * Never modify or reference this class directly. Always use {@link AnnouncementsFlagUtil} to access the announcements flag persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
069             */
070            public static final String FINDER_CLASS_NAME_ENTITY = AnnouncementsFlagImpl.class.getName();
071            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072                    ".List1";
073            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074                    ".List2";
075            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
076                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED,
077                            AnnouncementsFlagImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
079            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
080                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED,
081                            AnnouncementsFlagImpl.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
083            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
084                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED, Long.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ENTRYID = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
087                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED,
088                            AnnouncementsFlagImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByEntryId",
090                            new String[] {
091                                    Long.class.getName(),
092                                    
093                            Integer.class.getName(), Integer.class.getName(),
094                                    OrderByComparator.class.getName()
095                            });
096            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID =
097                    new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
098                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED,
099                            AnnouncementsFlagImpl.class,
100                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByEntryId",
101                            new String[] { Long.class.getName() },
102                            AnnouncementsFlagModelImpl.ENTRYID_COLUMN_BITMASK |
103                            AnnouncementsFlagModelImpl.USERID_COLUMN_BITMASK |
104                            AnnouncementsFlagModelImpl.CREATEDATE_COLUMN_BITMASK);
105            public static final FinderPath FINDER_PATH_COUNT_BY_ENTRYID = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
106                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED, Long.class,
107                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByEntryId",
108                            new String[] { Long.class.getName() });
109    
110            /**
111             * Returns all the announcements flags where entryId = &#63;.
112             *
113             * @param entryId the entry ID
114             * @return the matching announcements flags
115             * @throws SystemException if a system exception occurred
116             */
117            public List<AnnouncementsFlag> findByEntryId(long entryId)
118                    throws SystemException {
119                    return findByEntryId(entryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
120            }
121    
122            /**
123             * Returns a range of all the announcements flags where entryId = &#63;.
124             *
125             * <p>
126             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.announcements.model.impl.AnnouncementsFlagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
127             * </p>
128             *
129             * @param entryId the entry ID
130             * @param start the lower bound of the range of announcements flags
131             * @param end the upper bound of the range of announcements flags (not inclusive)
132             * @return the range of matching announcements flags
133             * @throws SystemException if a system exception occurred
134             */
135            public List<AnnouncementsFlag> findByEntryId(long entryId, int start,
136                    int end) throws SystemException {
137                    return findByEntryId(entryId, start, end, null);
138            }
139    
140            /**
141             * Returns an ordered range of all the announcements flags where entryId = &#63;.
142             *
143             * <p>
144             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.announcements.model.impl.AnnouncementsFlagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
145             * </p>
146             *
147             * @param entryId the entry ID
148             * @param start the lower bound of the range of announcements flags
149             * @param end the upper bound of the range of announcements flags (not inclusive)
150             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
151             * @return the ordered range of matching announcements flags
152             * @throws SystemException if a system exception occurred
153             */
154            public List<AnnouncementsFlag> findByEntryId(long entryId, int start,
155                    int end, OrderByComparator orderByComparator) throws SystemException {
156                    boolean pagination = true;
157                    FinderPath finderPath = null;
158                    Object[] finderArgs = null;
159    
160                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
161                                    (orderByComparator == null)) {
162                            pagination = false;
163                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID;
164                            finderArgs = new Object[] { entryId };
165                    }
166                    else {
167                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ENTRYID;
168                            finderArgs = new Object[] { entryId, start, end, orderByComparator };
169                    }
170    
171                    List<AnnouncementsFlag> list = (List<AnnouncementsFlag>)FinderCacheUtil.getResult(finderPath,
172                                    finderArgs, this);
173    
174                    if ((list != null) && !list.isEmpty()) {
175                            for (AnnouncementsFlag announcementsFlag : list) {
176                                    if ((entryId != announcementsFlag.getEntryId())) {
177                                            list = null;
178    
179                                            break;
180                                    }
181                            }
182                    }
183    
184                    if (list == null) {
185                            StringBundler query = null;
186    
187                            if (orderByComparator != null) {
188                                    query = new StringBundler(3 +
189                                                    (orderByComparator.getOrderByFields().length * 3));
190                            }
191                            else {
192                                    query = new StringBundler(3);
193                            }
194    
195                            query.append(_SQL_SELECT_ANNOUNCEMENTSFLAG_WHERE);
196    
197                            query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);
198    
199                            if (orderByComparator != null) {
200                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
201                                            orderByComparator);
202                            }
203                            else
204                             if (pagination) {
205                                    query.append(AnnouncementsFlagModelImpl.ORDER_BY_JPQL);
206                            }
207    
208                            String sql = query.toString();
209    
210                            Session session = null;
211    
212                            try {
213                                    session = openSession();
214    
215                                    Query q = session.createQuery(sql);
216    
217                                    QueryPos qPos = QueryPos.getInstance(q);
218    
219                                    qPos.add(entryId);
220    
221                                    if (!pagination) {
222                                            list = (List<AnnouncementsFlag>)QueryUtil.list(q,
223                                                            getDialect(), start, end, false);
224    
225                                            Collections.sort(list);
226    
227                                            list = new UnmodifiableList<AnnouncementsFlag>(list);
228                                    }
229                                    else {
230                                            list = (List<AnnouncementsFlag>)QueryUtil.list(q,
231                                                            getDialect(), start, end);
232                                    }
233    
234                                    cacheResult(list);
235    
236                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
237                            }
238                            catch (Exception e) {
239                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
240    
241                                    throw processException(e);
242                            }
243                            finally {
244                                    closeSession(session);
245                            }
246                    }
247    
248                    return list;
249            }
250    
251            /**
252             * Returns the first announcements flag in the ordered set where entryId = &#63;.
253             *
254             * @param entryId the entry ID
255             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
256             * @return the first matching announcements flag
257             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a matching announcements flag could not be found
258             * @throws SystemException if a system exception occurred
259             */
260            public AnnouncementsFlag findByEntryId_First(long entryId,
261                    OrderByComparator orderByComparator)
262                    throws NoSuchFlagException, SystemException {
263                    AnnouncementsFlag announcementsFlag = fetchByEntryId_First(entryId,
264                                    orderByComparator);
265    
266                    if (announcementsFlag != null) {
267                            return announcementsFlag;
268                    }
269    
270                    StringBundler msg = new StringBundler(4);
271    
272                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
273    
274                    msg.append("entryId=");
275                    msg.append(entryId);
276    
277                    msg.append(StringPool.CLOSE_CURLY_BRACE);
278    
279                    throw new NoSuchFlagException(msg.toString());
280            }
281    
282            /**
283             * Returns the first announcements flag in the ordered set where entryId = &#63;.
284             *
285             * @param entryId the entry ID
286             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
287             * @return the first matching announcements flag, or <code>null</code> if a matching announcements flag could not be found
288             * @throws SystemException if a system exception occurred
289             */
290            public AnnouncementsFlag fetchByEntryId_First(long entryId,
291                    OrderByComparator orderByComparator) throws SystemException {
292                    List<AnnouncementsFlag> list = findByEntryId(entryId, 0, 1,
293                                    orderByComparator);
294    
295                    if (!list.isEmpty()) {
296                            return list.get(0);
297                    }
298    
299                    return null;
300            }
301    
302            /**
303             * Returns the last announcements flag in the ordered set where entryId = &#63;.
304             *
305             * @param entryId the entry ID
306             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
307             * @return the last matching announcements flag
308             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a matching announcements flag could not be found
309             * @throws SystemException if a system exception occurred
310             */
311            public AnnouncementsFlag findByEntryId_Last(long entryId,
312                    OrderByComparator orderByComparator)
313                    throws NoSuchFlagException, SystemException {
314                    AnnouncementsFlag announcementsFlag = fetchByEntryId_Last(entryId,
315                                    orderByComparator);
316    
317                    if (announcementsFlag != null) {
318                            return announcementsFlag;
319                    }
320    
321                    StringBundler msg = new StringBundler(4);
322    
323                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
324    
325                    msg.append("entryId=");
326                    msg.append(entryId);
327    
328                    msg.append(StringPool.CLOSE_CURLY_BRACE);
329    
330                    throw new NoSuchFlagException(msg.toString());
331            }
332    
333            /**
334             * Returns the last announcements flag in the ordered set where entryId = &#63;.
335             *
336             * @param entryId the entry ID
337             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
338             * @return the last matching announcements flag, or <code>null</code> if a matching announcements flag could not be found
339             * @throws SystemException if a system exception occurred
340             */
341            public AnnouncementsFlag fetchByEntryId_Last(long entryId,
342                    OrderByComparator orderByComparator) throws SystemException {
343                    int count = countByEntryId(entryId);
344    
345                    List<AnnouncementsFlag> list = findByEntryId(entryId, count - 1, count,
346                                    orderByComparator);
347    
348                    if (!list.isEmpty()) {
349                            return list.get(0);
350                    }
351    
352                    return null;
353            }
354    
355            /**
356             * Returns the announcements flags before and after the current announcements flag in the ordered set where entryId = &#63;.
357             *
358             * @param flagId the primary key of the current announcements flag
359             * @param entryId the entry ID
360             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
361             * @return the previous, current, and next announcements flag
362             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a announcements flag with the primary key could not be found
363             * @throws SystemException if a system exception occurred
364             */
365            public AnnouncementsFlag[] findByEntryId_PrevAndNext(long flagId,
366                    long entryId, OrderByComparator orderByComparator)
367                    throws NoSuchFlagException, SystemException {
368                    AnnouncementsFlag announcementsFlag = findByPrimaryKey(flagId);
369    
370                    Session session = null;
371    
372                    try {
373                            session = openSession();
374    
375                            AnnouncementsFlag[] array = new AnnouncementsFlagImpl[3];
376    
377                            array[0] = getByEntryId_PrevAndNext(session, announcementsFlag,
378                                            entryId, orderByComparator, true);
379    
380                            array[1] = announcementsFlag;
381    
382                            array[2] = getByEntryId_PrevAndNext(session, announcementsFlag,
383                                            entryId, orderByComparator, false);
384    
385                            return array;
386                    }
387                    catch (Exception e) {
388                            throw processException(e);
389                    }
390                    finally {
391                            closeSession(session);
392                    }
393            }
394    
395            protected AnnouncementsFlag getByEntryId_PrevAndNext(Session session,
396                    AnnouncementsFlag announcementsFlag, long entryId,
397                    OrderByComparator orderByComparator, boolean previous) {
398                    StringBundler query = null;
399    
400                    if (orderByComparator != null) {
401                            query = new StringBundler(6 +
402                                            (orderByComparator.getOrderByFields().length * 6));
403                    }
404                    else {
405                            query = new StringBundler(3);
406                    }
407    
408                    query.append(_SQL_SELECT_ANNOUNCEMENTSFLAG_WHERE);
409    
410                    query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);
411    
412                    if (orderByComparator != null) {
413                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
414    
415                            if (orderByConditionFields.length > 0) {
416                                    query.append(WHERE_AND);
417                            }
418    
419                            for (int i = 0; i < orderByConditionFields.length; i++) {
420                                    query.append(_ORDER_BY_ENTITY_ALIAS);
421                                    query.append(orderByConditionFields[i]);
422    
423                                    if ((i + 1) < orderByConditionFields.length) {
424                                            if (orderByComparator.isAscending() ^ previous) {
425                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
426                                            }
427                                            else {
428                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
429                                            }
430                                    }
431                                    else {
432                                            if (orderByComparator.isAscending() ^ previous) {
433                                                    query.append(WHERE_GREATER_THAN);
434                                            }
435                                            else {
436                                                    query.append(WHERE_LESSER_THAN);
437                                            }
438                                    }
439                            }
440    
441                            query.append(ORDER_BY_CLAUSE);
442    
443                            String[] orderByFields = orderByComparator.getOrderByFields();
444    
445                            for (int i = 0; i < orderByFields.length; i++) {
446                                    query.append(_ORDER_BY_ENTITY_ALIAS);
447                                    query.append(orderByFields[i]);
448    
449                                    if ((i + 1) < orderByFields.length) {
450                                            if (orderByComparator.isAscending() ^ previous) {
451                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
452                                            }
453                                            else {
454                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
455                                            }
456                                    }
457                                    else {
458                                            if (orderByComparator.isAscending() ^ previous) {
459                                                    query.append(ORDER_BY_ASC);
460                                            }
461                                            else {
462                                                    query.append(ORDER_BY_DESC);
463                                            }
464                                    }
465                            }
466                    }
467                    else {
468                            query.append(AnnouncementsFlagModelImpl.ORDER_BY_JPQL);
469                    }
470    
471                    String sql = query.toString();
472    
473                    Query q = session.createQuery(sql);
474    
475                    q.setFirstResult(0);
476                    q.setMaxResults(2);
477    
478                    QueryPos qPos = QueryPos.getInstance(q);
479    
480                    qPos.add(entryId);
481    
482                    if (orderByComparator != null) {
483                            Object[] values = orderByComparator.getOrderByConditionValues(announcementsFlag);
484    
485                            for (Object value : values) {
486                                    qPos.add(value);
487                            }
488                    }
489    
490                    List<AnnouncementsFlag> list = q.list();
491    
492                    if (list.size() == 2) {
493                            return list.get(1);
494                    }
495                    else {
496                            return null;
497                    }
498            }
499    
500            /**
501             * Removes all the announcements flags where entryId = &#63; from the database.
502             *
503             * @param entryId the entry ID
504             * @throws SystemException if a system exception occurred
505             */
506            public void removeByEntryId(long entryId) throws SystemException {
507                    for (AnnouncementsFlag announcementsFlag : findByEntryId(entryId,
508                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
509                            remove(announcementsFlag);
510                    }
511            }
512    
513            /**
514             * Returns the number of announcements flags where entryId = &#63;.
515             *
516             * @param entryId the entry ID
517             * @return the number of matching announcements flags
518             * @throws SystemException if a system exception occurred
519             */
520            public int countByEntryId(long entryId) throws SystemException {
521                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ENTRYID;
522    
523                    Object[] finderArgs = new Object[] { entryId };
524    
525                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
526                                    this);
527    
528                    if (count == null) {
529                            StringBundler query = new StringBundler(2);
530    
531                            query.append(_SQL_COUNT_ANNOUNCEMENTSFLAG_WHERE);
532    
533                            query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);
534    
535                            String sql = query.toString();
536    
537                            Session session = null;
538    
539                            try {
540                                    session = openSession();
541    
542                                    Query q = session.createQuery(sql);
543    
544                                    QueryPos qPos = QueryPos.getInstance(q);
545    
546                                    qPos.add(entryId);
547    
548                                    count = (Long)q.uniqueResult();
549    
550                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
551                            }
552                            catch (Exception e) {
553                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
554    
555                                    throw processException(e);
556                            }
557                            finally {
558                                    closeSession(session);
559                            }
560                    }
561    
562                    return count.intValue();
563            }
564    
565            private static final String _FINDER_COLUMN_ENTRYID_ENTRYID_2 = "announcementsFlag.entryId = ?";
566            public static final FinderPath FINDER_PATH_FETCH_BY_U_E_V = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
567                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED,
568                            AnnouncementsFlagImpl.class, FINDER_CLASS_NAME_ENTITY,
569                            "fetchByU_E_V",
570                            new String[] {
571                                    Long.class.getName(), Long.class.getName(),
572                                    Integer.class.getName()
573                            },
574                            AnnouncementsFlagModelImpl.USERID_COLUMN_BITMASK |
575                            AnnouncementsFlagModelImpl.ENTRYID_COLUMN_BITMASK |
576                            AnnouncementsFlagModelImpl.VALUE_COLUMN_BITMASK);
577            public static final FinderPath FINDER_PATH_COUNT_BY_U_E_V = new FinderPath(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
578                            AnnouncementsFlagModelImpl.FINDER_CACHE_ENABLED, Long.class,
579                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByU_E_V",
580                            new String[] {
581                                    Long.class.getName(), Long.class.getName(),
582                                    Integer.class.getName()
583                            });
584    
585            /**
586             * Returns the announcements flag where userId = &#63; and entryId = &#63; and value = &#63; or throws a {@link com.liferay.portlet.announcements.NoSuchFlagException} if it could not be found.
587             *
588             * @param userId the user ID
589             * @param entryId the entry ID
590             * @param value the value
591             * @return the matching announcements flag
592             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a matching announcements flag could not be found
593             * @throws SystemException if a system exception occurred
594             */
595            public AnnouncementsFlag findByU_E_V(long userId, long entryId, int value)
596                    throws NoSuchFlagException, SystemException {
597                    AnnouncementsFlag announcementsFlag = fetchByU_E_V(userId, entryId,
598                                    value);
599    
600                    if (announcementsFlag == null) {
601                            StringBundler msg = new StringBundler(8);
602    
603                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
604    
605                            msg.append("userId=");
606                            msg.append(userId);
607    
608                            msg.append(", entryId=");
609                            msg.append(entryId);
610    
611                            msg.append(", value=");
612                            msg.append(value);
613    
614                            msg.append(StringPool.CLOSE_CURLY_BRACE);
615    
616                            if (_log.isWarnEnabled()) {
617                                    _log.warn(msg.toString());
618                            }
619    
620                            throw new NoSuchFlagException(msg.toString());
621                    }
622    
623                    return announcementsFlag;
624            }
625    
626            /**
627             * Returns the announcements flag where userId = &#63; and entryId = &#63; and value = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
628             *
629             * @param userId the user ID
630             * @param entryId the entry ID
631             * @param value the value
632             * @return the matching announcements flag, or <code>null</code> if a matching announcements flag could not be found
633             * @throws SystemException if a system exception occurred
634             */
635            public AnnouncementsFlag fetchByU_E_V(long userId, long entryId, int value)
636                    throws SystemException {
637                    return fetchByU_E_V(userId, entryId, value, true);
638            }
639    
640            /**
641             * Returns the announcements flag where userId = &#63; and entryId = &#63; and value = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
642             *
643             * @param userId the user ID
644             * @param entryId the entry ID
645             * @param value the value
646             * @param retrieveFromCache whether to use the finder cache
647             * @return the matching announcements flag, or <code>null</code> if a matching announcements flag could not be found
648             * @throws SystemException if a system exception occurred
649             */
650            public AnnouncementsFlag fetchByU_E_V(long userId, long entryId, int value,
651                    boolean retrieveFromCache) throws SystemException {
652                    Object[] finderArgs = new Object[] { userId, entryId, value };
653    
654                    Object result = null;
655    
656                    if (retrieveFromCache) {
657                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_E_V,
658                                            finderArgs, this);
659                    }
660    
661                    if (result instanceof AnnouncementsFlag) {
662                            AnnouncementsFlag announcementsFlag = (AnnouncementsFlag)result;
663    
664                            if ((userId != announcementsFlag.getUserId()) ||
665                                            (entryId != announcementsFlag.getEntryId()) ||
666                                            (value != announcementsFlag.getValue())) {
667                                    result = null;
668                            }
669                    }
670    
671                    if (result == null) {
672                            StringBundler query = new StringBundler(5);
673    
674                            query.append(_SQL_SELECT_ANNOUNCEMENTSFLAG_WHERE);
675    
676                            query.append(_FINDER_COLUMN_U_E_V_USERID_2);
677    
678                            query.append(_FINDER_COLUMN_U_E_V_ENTRYID_2);
679    
680                            query.append(_FINDER_COLUMN_U_E_V_VALUE_2);
681    
682                            String sql = query.toString();
683    
684                            Session session = null;
685    
686                            try {
687                                    session = openSession();
688    
689                                    Query q = session.createQuery(sql);
690    
691                                    QueryPos qPos = QueryPos.getInstance(q);
692    
693                                    qPos.add(userId);
694    
695                                    qPos.add(entryId);
696    
697                                    qPos.add(value);
698    
699                                    List<AnnouncementsFlag> list = q.list();
700    
701                                    if (list.isEmpty()) {
702                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V,
703                                                    finderArgs, list);
704                                    }
705                                    else {
706                                            AnnouncementsFlag announcementsFlag = list.get(0);
707    
708                                            result = announcementsFlag;
709    
710                                            cacheResult(announcementsFlag);
711    
712                                            if ((announcementsFlag.getUserId() != userId) ||
713                                                            (announcementsFlag.getEntryId() != entryId) ||
714                                                            (announcementsFlag.getValue() != value)) {
715                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V,
716                                                            finderArgs, announcementsFlag);
717                                            }
718                                    }
719                            }
720                            catch (Exception e) {
721                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_E_V,
722                                            finderArgs);
723    
724                                    throw processException(e);
725                            }
726                            finally {
727                                    closeSession(session);
728                            }
729                    }
730    
731                    if (result instanceof List<?>) {
732                            return null;
733                    }
734                    else {
735                            return (AnnouncementsFlag)result;
736                    }
737            }
738    
739            /**
740             * Removes the announcements flag where userId = &#63; and entryId = &#63; and value = &#63; from the database.
741             *
742             * @param userId the user ID
743             * @param entryId the entry ID
744             * @param value the value
745             * @return the announcements flag that was removed
746             * @throws SystemException if a system exception occurred
747             */
748            public AnnouncementsFlag removeByU_E_V(long userId, long entryId, int value)
749                    throws NoSuchFlagException, SystemException {
750                    AnnouncementsFlag announcementsFlag = findByU_E_V(userId, entryId, value);
751    
752                    return remove(announcementsFlag);
753            }
754    
755            /**
756             * Returns the number of announcements flags where userId = &#63; and entryId = &#63; and value = &#63;.
757             *
758             * @param userId the user ID
759             * @param entryId the entry ID
760             * @param value the value
761             * @return the number of matching announcements flags
762             * @throws SystemException if a system exception occurred
763             */
764            public int countByU_E_V(long userId, long entryId, int value)
765                    throws SystemException {
766                    FinderPath finderPath = FINDER_PATH_COUNT_BY_U_E_V;
767    
768                    Object[] finderArgs = new Object[] { userId, entryId, value };
769    
770                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
771                                    this);
772    
773                    if (count == null) {
774                            StringBundler query = new StringBundler(4);
775    
776                            query.append(_SQL_COUNT_ANNOUNCEMENTSFLAG_WHERE);
777    
778                            query.append(_FINDER_COLUMN_U_E_V_USERID_2);
779    
780                            query.append(_FINDER_COLUMN_U_E_V_ENTRYID_2);
781    
782                            query.append(_FINDER_COLUMN_U_E_V_VALUE_2);
783    
784                            String sql = query.toString();
785    
786                            Session session = null;
787    
788                            try {
789                                    session = openSession();
790    
791                                    Query q = session.createQuery(sql);
792    
793                                    QueryPos qPos = QueryPos.getInstance(q);
794    
795                                    qPos.add(userId);
796    
797                                    qPos.add(entryId);
798    
799                                    qPos.add(value);
800    
801                                    count = (Long)q.uniqueResult();
802    
803                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
804                            }
805                            catch (Exception e) {
806                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
807    
808                                    throw processException(e);
809                            }
810                            finally {
811                                    closeSession(session);
812                            }
813                    }
814    
815                    return count.intValue();
816            }
817    
818            private static final String _FINDER_COLUMN_U_E_V_USERID_2 = "announcementsFlag.userId = ? AND ";
819            private static final String _FINDER_COLUMN_U_E_V_ENTRYID_2 = "announcementsFlag.entryId = ? AND ";
820            private static final String _FINDER_COLUMN_U_E_V_VALUE_2 = "announcementsFlag.value = ?";
821    
822            /**
823             * Caches the announcements flag in the entity cache if it is enabled.
824             *
825             * @param announcementsFlag the announcements flag
826             */
827            public void cacheResult(AnnouncementsFlag announcementsFlag) {
828                    EntityCacheUtil.putResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
829                            AnnouncementsFlagImpl.class, announcementsFlag.getPrimaryKey(),
830                            announcementsFlag);
831    
832                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V,
833                            new Object[] {
834                                    Long.valueOf(announcementsFlag.getUserId()),
835                                    Long.valueOf(announcementsFlag.getEntryId()),
836                                    Integer.valueOf(announcementsFlag.getValue())
837                            }, announcementsFlag);
838    
839                    announcementsFlag.resetOriginalValues();
840            }
841    
842            /**
843             * Caches the announcements flags in the entity cache if it is enabled.
844             *
845             * @param announcementsFlags the announcements flags
846             */
847            public void cacheResult(List<AnnouncementsFlag> announcementsFlags) {
848                    for (AnnouncementsFlag announcementsFlag : announcementsFlags) {
849                            if (EntityCacheUtil.getResult(
850                                                    AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
851                                                    AnnouncementsFlagImpl.class,
852                                                    announcementsFlag.getPrimaryKey()) == null) {
853                                    cacheResult(announcementsFlag);
854                            }
855                            else {
856                                    announcementsFlag.resetOriginalValues();
857                            }
858                    }
859            }
860    
861            /**
862             * Clears the cache for all announcements flags.
863             *
864             * <p>
865             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
866             * </p>
867             */
868            @Override
869            public void clearCache() {
870                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
871                            CacheRegistryUtil.clear(AnnouncementsFlagImpl.class.getName());
872                    }
873    
874                    EntityCacheUtil.clearCache(AnnouncementsFlagImpl.class.getName());
875    
876                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
877                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
878                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
879            }
880    
881            /**
882             * Clears the cache for the announcements flag.
883             *
884             * <p>
885             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
886             * </p>
887             */
888            @Override
889            public void clearCache(AnnouncementsFlag announcementsFlag) {
890                    EntityCacheUtil.removeResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
891                            AnnouncementsFlagImpl.class, announcementsFlag.getPrimaryKey());
892    
893                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
894                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
895    
896                    clearUniqueFindersCache(announcementsFlag);
897            }
898    
899            @Override
900            public void clearCache(List<AnnouncementsFlag> announcementsFlags) {
901                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
902                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
903    
904                    for (AnnouncementsFlag announcementsFlag : announcementsFlags) {
905                            EntityCacheUtil.removeResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
906                                    AnnouncementsFlagImpl.class, announcementsFlag.getPrimaryKey());
907    
908                            clearUniqueFindersCache(announcementsFlag);
909                    }
910            }
911    
912            protected void cacheUniqueFindersCache(AnnouncementsFlag announcementsFlag) {
913                    if (announcementsFlag.isNew()) {
914                            Object[] args = new Object[] {
915                                            Long.valueOf(announcementsFlag.getUserId()),
916                                            Long.valueOf(announcementsFlag.getEntryId()),
917                                            Integer.valueOf(announcementsFlag.getValue())
918                                    };
919    
920                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_E_V, args,
921                                    Long.valueOf(1));
922                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V, args,
923                                    announcementsFlag);
924                    }
925                    else {
926                            AnnouncementsFlagModelImpl announcementsFlagModelImpl = (AnnouncementsFlagModelImpl)announcementsFlag;
927    
928                            if ((announcementsFlagModelImpl.getColumnBitmask() &
929                                            FINDER_PATH_FETCH_BY_U_E_V.getColumnBitmask()) != 0) {
930                                    Object[] args = new Object[] {
931                                                    Long.valueOf(announcementsFlag.getUserId()),
932                                                    Long.valueOf(announcementsFlag.getEntryId()),
933                                                    Integer.valueOf(announcementsFlag.getValue())
934                                            };
935    
936                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_E_V, args,
937                                            Long.valueOf(1));
938                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_E_V, args,
939                                            announcementsFlag);
940                            }
941                    }
942            }
943    
944            protected void clearUniqueFindersCache(AnnouncementsFlag announcementsFlag) {
945                    AnnouncementsFlagModelImpl announcementsFlagModelImpl = (AnnouncementsFlagModelImpl)announcementsFlag;
946    
947                    Object[] args = new Object[] {
948                                    Long.valueOf(announcementsFlag.getUserId()),
949                                    Long.valueOf(announcementsFlag.getEntryId()),
950                                    Integer.valueOf(announcementsFlag.getValue())
951                            };
952    
953                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_E_V, args);
954                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_E_V, args);
955    
956                    if ((announcementsFlagModelImpl.getColumnBitmask() &
957                                    FINDER_PATH_FETCH_BY_U_E_V.getColumnBitmask()) != 0) {
958                            args = new Object[] {
959                                            Long.valueOf(announcementsFlagModelImpl.getOriginalUserId()),
960                                            Long.valueOf(announcementsFlagModelImpl.getOriginalEntryId()),
961                                            Integer.valueOf(announcementsFlagModelImpl.getOriginalValue())
962                                    };
963    
964                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_E_V, args);
965                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_E_V, args);
966                    }
967            }
968    
969            /**
970             * Creates a new announcements flag with the primary key. Does not add the announcements flag to the database.
971             *
972             * @param flagId the primary key for the new announcements flag
973             * @return the new announcements flag
974             */
975            public AnnouncementsFlag create(long flagId) {
976                    AnnouncementsFlag announcementsFlag = new AnnouncementsFlagImpl();
977    
978                    announcementsFlag.setNew(true);
979                    announcementsFlag.setPrimaryKey(flagId);
980    
981                    return announcementsFlag;
982            }
983    
984            /**
985             * Removes the announcements flag with the primary key from the database. Also notifies the appropriate model listeners.
986             *
987             * @param flagId the primary key of the announcements flag
988             * @return the announcements flag that was removed
989             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a announcements flag with the primary key could not be found
990             * @throws SystemException if a system exception occurred
991             */
992            public AnnouncementsFlag remove(long flagId)
993                    throws NoSuchFlagException, SystemException {
994                    return remove(Long.valueOf(flagId));
995            }
996    
997            /**
998             * Removes the announcements flag with the primary key from the database. Also notifies the appropriate model listeners.
999             *
1000             * @param primaryKey the primary key of the announcements flag
1001             * @return the announcements flag that was removed
1002             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a announcements flag with the primary key could not be found
1003             * @throws SystemException if a system exception occurred
1004             */
1005            @Override
1006            public AnnouncementsFlag remove(Serializable primaryKey)
1007                    throws NoSuchFlagException, SystemException {
1008                    Session session = null;
1009    
1010                    try {
1011                            session = openSession();
1012    
1013                            AnnouncementsFlag announcementsFlag = (AnnouncementsFlag)session.get(AnnouncementsFlagImpl.class,
1014                                            primaryKey);
1015    
1016                            if (announcementsFlag == null) {
1017                                    if (_log.isWarnEnabled()) {
1018                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1019                                    }
1020    
1021                                    throw new NoSuchFlagException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1022                                            primaryKey);
1023                            }
1024    
1025                            return remove(announcementsFlag);
1026                    }
1027                    catch (NoSuchFlagException nsee) {
1028                            throw nsee;
1029                    }
1030                    catch (Exception e) {
1031                            throw processException(e);
1032                    }
1033                    finally {
1034                            closeSession(session);
1035                    }
1036            }
1037    
1038            @Override
1039            protected AnnouncementsFlag removeImpl(AnnouncementsFlag announcementsFlag)
1040                    throws SystemException {
1041                    announcementsFlag = toUnwrappedModel(announcementsFlag);
1042    
1043                    Session session = null;
1044    
1045                    try {
1046                            session = openSession();
1047    
1048                            if (!session.contains(announcementsFlag)) {
1049                                    announcementsFlag = (AnnouncementsFlag)session.get(AnnouncementsFlagImpl.class,
1050                                                    announcementsFlag.getPrimaryKeyObj());
1051                            }
1052    
1053                            if (announcementsFlag != null) {
1054                                    session.delete(announcementsFlag);
1055                            }
1056                    }
1057                    catch (Exception e) {
1058                            throw processException(e);
1059                    }
1060                    finally {
1061                            closeSession(session);
1062                    }
1063    
1064                    if (announcementsFlag != null) {
1065                            clearCache(announcementsFlag);
1066                    }
1067    
1068                    return announcementsFlag;
1069            }
1070    
1071            @Override
1072            public AnnouncementsFlag updateImpl(
1073                    com.liferay.portlet.announcements.model.AnnouncementsFlag announcementsFlag)
1074                    throws SystemException {
1075                    announcementsFlag = toUnwrappedModel(announcementsFlag);
1076    
1077                    boolean isNew = announcementsFlag.isNew();
1078    
1079                    AnnouncementsFlagModelImpl announcementsFlagModelImpl = (AnnouncementsFlagModelImpl)announcementsFlag;
1080    
1081                    Session session = null;
1082    
1083                    try {
1084                            session = openSession();
1085    
1086                            if (announcementsFlag.isNew()) {
1087                                    session.save(announcementsFlag);
1088    
1089                                    announcementsFlag.setNew(false);
1090                            }
1091                            else {
1092                                    session.merge(announcementsFlag);
1093                            }
1094                    }
1095                    catch (Exception e) {
1096                            throw processException(e);
1097                    }
1098                    finally {
1099                            closeSession(session);
1100                    }
1101    
1102                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1103    
1104                    if (isNew || !AnnouncementsFlagModelImpl.COLUMN_BITMASK_ENABLED) {
1105                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1106                    }
1107    
1108                    else {
1109                            if ((announcementsFlagModelImpl.getColumnBitmask() &
1110                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID.getColumnBitmask()) != 0) {
1111                                    Object[] args = new Object[] {
1112                                                    Long.valueOf(announcementsFlagModelImpl.getOriginalEntryId())
1113                                            };
1114    
1115                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ENTRYID, args);
1116                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID,
1117                                            args);
1118    
1119                                    args = new Object[] {
1120                                                    Long.valueOf(announcementsFlagModelImpl.getEntryId())
1121                                            };
1122    
1123                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ENTRYID, args);
1124                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID,
1125                                            args);
1126                            }
1127                    }
1128    
1129                    EntityCacheUtil.putResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
1130                            AnnouncementsFlagImpl.class, announcementsFlag.getPrimaryKey(),
1131                            announcementsFlag);
1132    
1133                    clearUniqueFindersCache(announcementsFlag);
1134                    cacheUniqueFindersCache(announcementsFlag);
1135    
1136                    return announcementsFlag;
1137            }
1138    
1139            protected AnnouncementsFlag toUnwrappedModel(
1140                    AnnouncementsFlag announcementsFlag) {
1141                    if (announcementsFlag instanceof AnnouncementsFlagImpl) {
1142                            return announcementsFlag;
1143                    }
1144    
1145                    AnnouncementsFlagImpl announcementsFlagImpl = new AnnouncementsFlagImpl();
1146    
1147                    announcementsFlagImpl.setNew(announcementsFlag.isNew());
1148                    announcementsFlagImpl.setPrimaryKey(announcementsFlag.getPrimaryKey());
1149    
1150                    announcementsFlagImpl.setFlagId(announcementsFlag.getFlagId());
1151                    announcementsFlagImpl.setUserId(announcementsFlag.getUserId());
1152                    announcementsFlagImpl.setCreateDate(announcementsFlag.getCreateDate());
1153                    announcementsFlagImpl.setEntryId(announcementsFlag.getEntryId());
1154                    announcementsFlagImpl.setValue(announcementsFlag.getValue());
1155    
1156                    return announcementsFlagImpl;
1157            }
1158    
1159            /**
1160             * Returns the announcements flag with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1161             *
1162             * @param primaryKey the primary key of the announcements flag
1163             * @return the announcements flag
1164             * @throws com.liferay.portal.NoSuchModelException if a announcements flag with the primary key could not be found
1165             * @throws SystemException if a system exception occurred
1166             */
1167            @Override
1168            public AnnouncementsFlag findByPrimaryKey(Serializable primaryKey)
1169                    throws NoSuchModelException, SystemException {
1170                    return findByPrimaryKey(((Long)primaryKey).longValue());
1171            }
1172    
1173            /**
1174             * Returns the announcements flag with the primary key or throws a {@link com.liferay.portlet.announcements.NoSuchFlagException} if it could not be found.
1175             *
1176             * @param flagId the primary key of the announcements flag
1177             * @return the announcements flag
1178             * @throws com.liferay.portlet.announcements.NoSuchFlagException if a announcements flag with the primary key could not be found
1179             * @throws SystemException if a system exception occurred
1180             */
1181            public AnnouncementsFlag findByPrimaryKey(long flagId)
1182                    throws NoSuchFlagException, SystemException {
1183                    AnnouncementsFlag announcementsFlag = fetchByPrimaryKey(flagId);
1184    
1185                    if (announcementsFlag == null) {
1186                            if (_log.isWarnEnabled()) {
1187                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + flagId);
1188                            }
1189    
1190                            throw new NoSuchFlagException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1191                                    flagId);
1192                    }
1193    
1194                    return announcementsFlag;
1195            }
1196    
1197            /**
1198             * Returns the announcements flag with the primary key or returns <code>null</code> if it could not be found.
1199             *
1200             * @param primaryKey the primary key of the announcements flag
1201             * @return the announcements flag, or <code>null</code> if a announcements flag with the primary key could not be found
1202             * @throws SystemException if a system exception occurred
1203             */
1204            @Override
1205            public AnnouncementsFlag fetchByPrimaryKey(Serializable primaryKey)
1206                    throws SystemException {
1207                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
1208            }
1209    
1210            /**
1211             * Returns the announcements flag with the primary key or returns <code>null</code> if it could not be found.
1212             *
1213             * @param flagId the primary key of the announcements flag
1214             * @return the announcements flag, or <code>null</code> if a announcements flag with the primary key could not be found
1215             * @throws SystemException if a system exception occurred
1216             */
1217            public AnnouncementsFlag fetchByPrimaryKey(long flagId)
1218                    throws SystemException {
1219                    AnnouncementsFlag announcementsFlag = (AnnouncementsFlag)EntityCacheUtil.getResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
1220                                    AnnouncementsFlagImpl.class, flagId);
1221    
1222                    if (announcementsFlag == _nullAnnouncementsFlag) {
1223                            return null;
1224                    }
1225    
1226                    if (announcementsFlag == null) {
1227                            Session session = null;
1228    
1229                            try {
1230                                    session = openSession();
1231    
1232                                    announcementsFlag = (AnnouncementsFlag)session.get(AnnouncementsFlagImpl.class,
1233                                                    Long.valueOf(flagId));
1234    
1235                                    if (announcementsFlag != null) {
1236                                            cacheResult(announcementsFlag);
1237                                    }
1238                                    else {
1239                                            EntityCacheUtil.putResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
1240                                                    AnnouncementsFlagImpl.class, flagId,
1241                                                    _nullAnnouncementsFlag);
1242                                    }
1243                            }
1244                            catch (Exception e) {
1245                                    EntityCacheUtil.removeResult(AnnouncementsFlagModelImpl.ENTITY_CACHE_ENABLED,
1246                                            AnnouncementsFlagImpl.class, flagId);
1247    
1248                                    throw processException(e);
1249                            }
1250                            finally {
1251                                    closeSession(session);
1252                            }
1253                    }
1254    
1255                    return announcementsFlag;
1256            }
1257    
1258            /**
1259             * Returns all the announcements flags.
1260             *
1261             * @return the announcements flags
1262             * @throws SystemException if a system exception occurred
1263             */
1264            public List<AnnouncementsFlag> findAll() throws SystemException {
1265                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1266            }
1267    
1268            /**
1269             * Returns a range of all the announcements flags.
1270             *
1271             * <p>
1272             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.announcements.model.impl.AnnouncementsFlagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1273             * </p>
1274             *
1275             * @param start the lower bound of the range of announcements flags
1276             * @param end the upper bound of the range of announcements flags (not inclusive)
1277             * @return the range of announcements flags
1278             * @throws SystemException if a system exception occurred
1279             */
1280            public List<AnnouncementsFlag> findAll(int start, int end)
1281                    throws SystemException {
1282                    return findAll(start, end, null);
1283            }
1284    
1285            /**
1286             * Returns an ordered range of all the announcements flags.
1287             *
1288             * <p>
1289             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.announcements.model.impl.AnnouncementsFlagModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1290             * </p>
1291             *
1292             * @param start the lower bound of the range of announcements flags
1293             * @param end the upper bound of the range of announcements flags (not inclusive)
1294             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1295             * @return the ordered range of announcements flags
1296             * @throws SystemException if a system exception occurred
1297             */
1298            public List<AnnouncementsFlag> findAll(int start, int end,
1299                    OrderByComparator orderByComparator) throws SystemException {
1300                    boolean pagination = true;
1301                    FinderPath finderPath = null;
1302                    Object[] finderArgs = null;
1303    
1304                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1305                                    (orderByComparator == null)) {
1306                            pagination = false;
1307                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1308                            finderArgs = FINDER_ARGS_EMPTY;
1309                    }
1310                    else {
1311                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1312                            finderArgs = new Object[] { start, end, orderByComparator };
1313                    }
1314    
1315                    List<AnnouncementsFlag> list = (List<AnnouncementsFlag>)FinderCacheUtil.getResult(finderPath,
1316                                    finderArgs, this);
1317    
1318                    if (list == null) {
1319                            StringBundler query = null;
1320                            String sql = null;
1321    
1322                            if (orderByComparator != null) {
1323                                    query = new StringBundler(2 +
1324                                                    (orderByComparator.getOrderByFields().length * 3));
1325    
1326                                    query.append(_SQL_SELECT_ANNOUNCEMENTSFLAG);
1327    
1328                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1329                                            orderByComparator);
1330    
1331                                    sql = query.toString();
1332                            }
1333                            else {
1334                                    sql = _SQL_SELECT_ANNOUNCEMENTSFLAG;
1335    
1336                                    if (pagination) {
1337                                            sql = sql.concat(AnnouncementsFlagModelImpl.ORDER_BY_JPQL);
1338                                    }
1339                            }
1340    
1341                            Session session = null;
1342    
1343                            try {
1344                                    session = openSession();
1345    
1346                                    Query q = session.createQuery(sql);
1347    
1348                                    if (!pagination) {
1349                                            list = (List<AnnouncementsFlag>)QueryUtil.list(q,
1350                                                            getDialect(), start, end, false);
1351    
1352                                            Collections.sort(list);
1353    
1354                                            list = new UnmodifiableList<AnnouncementsFlag>(list);
1355                                    }
1356                                    else {
1357                                            list = (List<AnnouncementsFlag>)QueryUtil.list(q,
1358                                                            getDialect(), start, end);
1359                                    }
1360    
1361                                    cacheResult(list);
1362    
1363                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1364                            }
1365                            catch (Exception e) {
1366                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1367    
1368                                    throw processException(e);
1369                            }
1370                            finally {
1371                                    closeSession(session);
1372                            }
1373                    }
1374    
1375                    return list;
1376            }
1377    
1378            /**
1379             * Removes all the announcements flags from the database.
1380             *
1381             * @throws SystemException if a system exception occurred
1382             */
1383            public void removeAll() throws SystemException {
1384                    for (AnnouncementsFlag announcementsFlag : findAll()) {
1385                            remove(announcementsFlag);
1386                    }
1387            }
1388    
1389            /**
1390             * Returns the number of announcements flags.
1391             *
1392             * @return the number of announcements flags
1393             * @throws SystemException if a system exception occurred
1394             */
1395            public int countAll() throws SystemException {
1396                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1397                                    FINDER_ARGS_EMPTY, this);
1398    
1399                    if (count == null) {
1400                            Session session = null;
1401    
1402                            try {
1403                                    session = openSession();
1404    
1405                                    Query q = session.createQuery(_SQL_COUNT_ANNOUNCEMENTSFLAG);
1406    
1407                                    count = (Long)q.uniqueResult();
1408    
1409                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1410                                            FINDER_ARGS_EMPTY, count);
1411                            }
1412                            catch (Exception e) {
1413                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1414                                            FINDER_ARGS_EMPTY);
1415    
1416                                    throw processException(e);
1417                            }
1418                            finally {
1419                                    closeSession(session);
1420                            }
1421                    }
1422    
1423                    return count.intValue();
1424            }
1425    
1426            /**
1427             * Initializes the announcements flag persistence.
1428             */
1429            public void afterPropertiesSet() {
1430                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1431                                            com.liferay.portal.util.PropsUtil.get(
1432                                                    "value.object.listener.com.liferay.portlet.announcements.model.AnnouncementsFlag")));
1433    
1434                    if (listenerClassNames.length > 0) {
1435                            try {
1436                                    List<ModelListener<AnnouncementsFlag>> listenersList = new ArrayList<ModelListener<AnnouncementsFlag>>();
1437    
1438                                    for (String listenerClassName : listenerClassNames) {
1439                                            listenersList.add((ModelListener<AnnouncementsFlag>)InstanceFactory.newInstance(
1440                                                            listenerClassName));
1441                                    }
1442    
1443                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1444                            }
1445                            catch (Exception e) {
1446                                    _log.error(e);
1447                            }
1448                    }
1449            }
1450    
1451            public void destroy() {
1452                    EntityCacheUtil.removeCache(AnnouncementsFlagImpl.class.getName());
1453                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1454                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1455                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1456            }
1457    
1458            private static final String _SQL_SELECT_ANNOUNCEMENTSFLAG = "SELECT announcementsFlag FROM AnnouncementsFlag announcementsFlag";
1459            private static final String _SQL_SELECT_ANNOUNCEMENTSFLAG_WHERE = "SELECT announcementsFlag FROM AnnouncementsFlag announcementsFlag WHERE ";
1460            private static final String _SQL_COUNT_ANNOUNCEMENTSFLAG = "SELECT COUNT(announcementsFlag) FROM AnnouncementsFlag announcementsFlag";
1461            private static final String _SQL_COUNT_ANNOUNCEMENTSFLAG_WHERE = "SELECT COUNT(announcementsFlag) FROM AnnouncementsFlag announcementsFlag WHERE ";
1462            private static final String _ORDER_BY_ENTITY_ALIAS = "announcementsFlag.";
1463            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No AnnouncementsFlag exists with the primary key ";
1464            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No AnnouncementsFlag exists with the key {";
1465            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1466            private static Log _log = LogFactoryUtil.getLog(AnnouncementsFlagPersistenceImpl.class);
1467            private static AnnouncementsFlag _nullAnnouncementsFlag = new AnnouncementsFlagImpl() {
1468                            @Override
1469                            public Object clone() {
1470                                    return this;
1471                            }
1472    
1473                            @Override
1474                            public CacheModel<AnnouncementsFlag> toCacheModel() {
1475                                    return _nullAnnouncementsFlagCacheModel;
1476                            }
1477                    };
1478    
1479            private static CacheModel<AnnouncementsFlag> _nullAnnouncementsFlagCacheModel =
1480                    new CacheModel<AnnouncementsFlag>() {
1481                            public AnnouncementsFlag toEntityModel() {
1482                                    return _nullAnnouncementsFlag;
1483                            }
1484                    };
1485    }