1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ModelListener;
37  import com.liferay.portal.service.persistence.BatchSessionUtil;
38  import com.liferay.portal.service.persistence.ResourcePersistence;
39  import com.liferay.portal.service.persistence.UserPersistence;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
43  import com.liferay.portlet.messageboards.model.MBMessageFlag;
44  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
45  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagModelImpl;
46  
47  import java.io.Serializable;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  
53  /**
54   * <a href="MBMessageFlagPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * <p>
57   * ServiceBuilder generated this class. Modifications in this class will be
58   * overwritten the next time is generated.
59   * </p>
60   *
61   * @author    Brian Wing Shun Chan
62   * @see       MBMessageFlagPersistence
63   * @see       MBMessageFlagUtil
64   * @generated
65   */
66  public class MBMessageFlagPersistenceImpl extends BasePersistenceImpl<MBMessageFlag>
67      implements MBMessageFlagPersistence {
68      public static final String FINDER_CLASS_NAME_ENTITY = MBMessageFlagImpl.class.getName();
69      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
70          ".List";
71      public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
72              MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
73              FINDER_CLASS_NAME_LIST, "findByUserId",
74              new String[] {
75                  Long.class.getName(),
76                  
77              "java.lang.Integer", "java.lang.Integer",
78                  "com.liferay.portal.kernel.util.OrderByComparator"
79              });
80      public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
81              MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
82              FINDER_CLASS_NAME_LIST, "countByUserId",
83              new String[] { Long.class.getName() });
84      public static final FinderPath FINDER_PATH_FIND_BY_THREADID = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
85              MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
86              FINDER_CLASS_NAME_LIST, "findByThreadId",
87              new String[] {
88                  Long.class.getName(),
89                  
90              "java.lang.Integer", "java.lang.Integer",
91                  "com.liferay.portal.kernel.util.OrderByComparator"
92              });
93      public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
94              MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
95              FINDER_CLASS_NAME_LIST, "countByThreadId",
96              new String[] { Long.class.getName() });
97      public static final FinderPath FINDER_PATH_FIND_BY_MESSAGEID = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
98              MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
99              FINDER_CLASS_NAME_LIST, "findByMessageId",
100             new String[] {
101                 Long.class.getName(),
102                 
103             "java.lang.Integer", "java.lang.Integer",
104                 "com.liferay.portal.kernel.util.OrderByComparator"
105             });
106     public static final FinderPath FINDER_PATH_COUNT_BY_MESSAGEID = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
107             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
108             FINDER_CLASS_NAME_LIST, "countByMessageId",
109             new String[] { Long.class.getName() });
110     public static final FinderPath FINDER_PATH_FIND_BY_T_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
111             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
112             FINDER_CLASS_NAME_LIST, "findByT_F",
113             new String[] {
114                 Long.class.getName(), Integer.class.getName(),
115                 
116             "java.lang.Integer", "java.lang.Integer",
117                 "com.liferay.portal.kernel.util.OrderByComparator"
118             });
119     public static final FinderPath FINDER_PATH_COUNT_BY_T_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
120             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
121             FINDER_CLASS_NAME_LIST, "countByT_F",
122             new String[] { Long.class.getName(), Integer.class.getName() });
123     public static final FinderPath FINDER_PATH_FIND_BY_M_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
124             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
125             FINDER_CLASS_NAME_LIST, "findByM_F",
126             new String[] {
127                 Long.class.getName(), Integer.class.getName(),
128                 
129             "java.lang.Integer", "java.lang.Integer",
130                 "com.liferay.portal.kernel.util.OrderByComparator"
131             });
132     public static final FinderPath FINDER_PATH_COUNT_BY_M_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
133             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
134             FINDER_CLASS_NAME_LIST, "countByM_F",
135             new String[] { Long.class.getName(), Integer.class.getName() });
136     public static final FinderPath FINDER_PATH_FIND_BY_U_T_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
137             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
138             FINDER_CLASS_NAME_LIST, "findByU_T_F",
139             new String[] {
140                 Long.class.getName(), Long.class.getName(),
141                 Integer.class.getName(),
142                 
143             "java.lang.Integer", "java.lang.Integer",
144                 "com.liferay.portal.kernel.util.OrderByComparator"
145             });
146     public static final FinderPath FINDER_PATH_COUNT_BY_U_T_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
147             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
148             FINDER_CLASS_NAME_LIST, "countByU_T_F",
149             new String[] {
150                 Long.class.getName(), Long.class.getName(),
151                 Integer.class.getName()
152             });
153     public static final FinderPath FINDER_PATH_FETCH_BY_U_M_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
154             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
155             FINDER_CLASS_NAME_ENTITY, "fetchByU_M_F",
156             new String[] {
157                 Long.class.getName(), Long.class.getName(),
158                 Integer.class.getName()
159             });
160     public static final FinderPath FINDER_PATH_COUNT_BY_U_M_F = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
161             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
162             FINDER_CLASS_NAME_LIST, "countByU_M_F",
163             new String[] {
164                 Long.class.getName(), Long.class.getName(),
165                 Integer.class.getName()
166             });
167     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
168             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
169             FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
170     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
171             MBMessageFlagModelImpl.FINDER_CACHE_ENABLED,
172             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
173 
174     public void cacheResult(MBMessageFlag mbMessageFlag) {
175         EntityCacheUtil.putResult(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
176             MBMessageFlagImpl.class, mbMessageFlag.getPrimaryKey(),
177             mbMessageFlag);
178 
179         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_M_F,
180             new Object[] {
181                 new Long(mbMessageFlag.getUserId()),
182                 new Long(mbMessageFlag.getMessageId()),
183                 new Integer(mbMessageFlag.getFlag())
184             }, mbMessageFlag);
185     }
186 
187     public void cacheResult(List<MBMessageFlag> mbMessageFlags) {
188         for (MBMessageFlag mbMessageFlag : mbMessageFlags) {
189             if (EntityCacheUtil.getResult(
190                         MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
191                         MBMessageFlagImpl.class, mbMessageFlag.getPrimaryKey(),
192                         this) == null) {
193                 cacheResult(mbMessageFlag);
194             }
195         }
196     }
197 
198     public void clearCache() {
199         CacheRegistry.clear(MBMessageFlagImpl.class.getName());
200         EntityCacheUtil.clearCache(MBMessageFlagImpl.class.getName());
201         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
202         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
203     }
204 
205     public void clearCache(MBMessageFlag mbMessageFlag) {
206         EntityCacheUtil.removeResult(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
207             MBMessageFlagImpl.class, mbMessageFlag.getPrimaryKey());
208 
209         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_M_F,
210             new Object[] {
211                 new Long(mbMessageFlag.getUserId()),
212                 new Long(mbMessageFlag.getMessageId()),
213                 new Integer(mbMessageFlag.getFlag())
214             });
215     }
216 
217     public MBMessageFlag create(long messageFlagId) {
218         MBMessageFlag mbMessageFlag = new MBMessageFlagImpl();
219 
220         mbMessageFlag.setNew(true);
221         mbMessageFlag.setPrimaryKey(messageFlagId);
222 
223         return mbMessageFlag;
224     }
225 
226     public MBMessageFlag remove(Serializable primaryKey)
227         throws NoSuchModelException, SystemException {
228         return remove(((Long)primaryKey).longValue());
229     }
230 
231     public MBMessageFlag remove(long messageFlagId)
232         throws NoSuchMessageFlagException, SystemException {
233         Session session = null;
234 
235         try {
236             session = openSession();
237 
238             MBMessageFlag mbMessageFlag = (MBMessageFlag)session.get(MBMessageFlagImpl.class,
239                     new Long(messageFlagId));
240 
241             if (mbMessageFlag == null) {
242                 if (_log.isWarnEnabled()) {
243                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + messageFlagId);
244                 }
245 
246                 throw new NoSuchMessageFlagException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
247                     messageFlagId);
248             }
249 
250             return remove(mbMessageFlag);
251         }
252         catch (NoSuchMessageFlagException nsee) {
253             throw nsee;
254         }
255         catch (Exception e) {
256             throw processException(e);
257         }
258         finally {
259             closeSession(session);
260         }
261     }
262 
263     protected MBMessageFlag removeImpl(MBMessageFlag mbMessageFlag)
264         throws SystemException {
265         mbMessageFlag = toUnwrappedModel(mbMessageFlag);
266 
267         Session session = null;
268 
269         try {
270             session = openSession();
271 
272             BatchSessionUtil.delete(session, mbMessageFlag);
273         }
274         catch (Exception e) {
275             throw processException(e);
276         }
277         finally {
278             closeSession(session);
279         }
280 
281         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
282 
283         MBMessageFlagModelImpl mbMessageFlagModelImpl = (MBMessageFlagModelImpl)mbMessageFlag;
284 
285         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_M_F,
286             new Object[] {
287                 new Long(mbMessageFlagModelImpl.getOriginalUserId()),
288                 new Long(mbMessageFlagModelImpl.getOriginalMessageId()),
289                 new Integer(mbMessageFlagModelImpl.getOriginalFlag())
290             });
291 
292         EntityCacheUtil.removeResult(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
293             MBMessageFlagImpl.class, mbMessageFlag.getPrimaryKey());
294 
295         return mbMessageFlag;
296     }
297 
298     /**
299      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
300      */
301     public MBMessageFlag update(MBMessageFlag mbMessageFlag)
302         throws SystemException {
303         if (_log.isWarnEnabled()) {
304             _log.warn(
305                 "Using the deprecated update(MBMessageFlag mbMessageFlag) method. Use update(MBMessageFlag mbMessageFlag, boolean merge) instead.");
306         }
307 
308         return update(mbMessageFlag, false);
309     }
310 
311     public MBMessageFlag updateImpl(
312         com.liferay.portlet.messageboards.model.MBMessageFlag mbMessageFlag,
313         boolean merge) throws SystemException {
314         mbMessageFlag = toUnwrappedModel(mbMessageFlag);
315 
316         boolean isNew = mbMessageFlag.isNew();
317 
318         MBMessageFlagModelImpl mbMessageFlagModelImpl = (MBMessageFlagModelImpl)mbMessageFlag;
319 
320         Session session = null;
321 
322         try {
323             session = openSession();
324 
325             BatchSessionUtil.update(session, mbMessageFlag, merge);
326 
327             mbMessageFlag.setNew(false);
328         }
329         catch (Exception e) {
330             throw processException(e);
331         }
332         finally {
333             closeSession(session);
334         }
335 
336         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
337 
338         EntityCacheUtil.putResult(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
339             MBMessageFlagImpl.class, mbMessageFlag.getPrimaryKey(),
340             mbMessageFlag);
341 
342         if (!isNew &&
343                 ((mbMessageFlag.getUserId() != mbMessageFlagModelImpl.getOriginalUserId()) ||
344                 (mbMessageFlag.getMessageId() != mbMessageFlagModelImpl.getOriginalMessageId()) ||
345                 (mbMessageFlag.getFlag() != mbMessageFlagModelImpl.getOriginalFlag()))) {
346             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_M_F,
347                 new Object[] {
348                     new Long(mbMessageFlagModelImpl.getOriginalUserId()),
349                     new Long(mbMessageFlagModelImpl.getOriginalMessageId()),
350                     new Integer(mbMessageFlagModelImpl.getOriginalFlag())
351                 });
352         }
353 
354         if (isNew ||
355                 ((mbMessageFlag.getUserId() != mbMessageFlagModelImpl.getOriginalUserId()) ||
356                 (mbMessageFlag.getMessageId() != mbMessageFlagModelImpl.getOriginalMessageId()) ||
357                 (mbMessageFlag.getFlag() != mbMessageFlagModelImpl.getOriginalFlag()))) {
358             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_M_F,
359                 new Object[] {
360                     new Long(mbMessageFlag.getUserId()),
361                     new Long(mbMessageFlag.getMessageId()),
362                     new Integer(mbMessageFlag.getFlag())
363                 }, mbMessageFlag);
364         }
365 
366         return mbMessageFlag;
367     }
368 
369     protected MBMessageFlag toUnwrappedModel(MBMessageFlag mbMessageFlag) {
370         if (mbMessageFlag instanceof MBMessageFlagImpl) {
371             return mbMessageFlag;
372         }
373 
374         MBMessageFlagImpl mbMessageFlagImpl = new MBMessageFlagImpl();
375 
376         mbMessageFlagImpl.setNew(mbMessageFlag.isNew());
377         mbMessageFlagImpl.setPrimaryKey(mbMessageFlag.getPrimaryKey());
378 
379         mbMessageFlagImpl.setMessageFlagId(mbMessageFlag.getMessageFlagId());
380         mbMessageFlagImpl.setUserId(mbMessageFlag.getUserId());
381         mbMessageFlagImpl.setModifiedDate(mbMessageFlag.getModifiedDate());
382         mbMessageFlagImpl.setThreadId(mbMessageFlag.getThreadId());
383         mbMessageFlagImpl.setMessageId(mbMessageFlag.getMessageId());
384         mbMessageFlagImpl.setFlag(mbMessageFlag.getFlag());
385 
386         return mbMessageFlagImpl;
387     }
388 
389     public MBMessageFlag findByPrimaryKey(Serializable primaryKey)
390         throws NoSuchModelException, SystemException {
391         return findByPrimaryKey(((Long)primaryKey).longValue());
392     }
393 
394     public MBMessageFlag findByPrimaryKey(long messageFlagId)
395         throws NoSuchMessageFlagException, SystemException {
396         MBMessageFlag mbMessageFlag = fetchByPrimaryKey(messageFlagId);
397 
398         if (mbMessageFlag == null) {
399             if (_log.isWarnEnabled()) {
400                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + messageFlagId);
401             }
402 
403             throw new NoSuchMessageFlagException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
404                 messageFlagId);
405         }
406 
407         return mbMessageFlag;
408     }
409 
410     public MBMessageFlag fetchByPrimaryKey(Serializable primaryKey)
411         throws SystemException {
412         return fetchByPrimaryKey(((Long)primaryKey).longValue());
413     }
414 
415     public MBMessageFlag fetchByPrimaryKey(long messageFlagId)
416         throws SystemException {
417         MBMessageFlag mbMessageFlag = (MBMessageFlag)EntityCacheUtil.getResult(MBMessageFlagModelImpl.ENTITY_CACHE_ENABLED,
418                 MBMessageFlagImpl.class, messageFlagId, this);
419 
420         if (mbMessageFlag == null) {
421             Session session = null;
422 
423             try {
424                 session = openSession();
425 
426                 mbMessageFlag = (MBMessageFlag)session.get(MBMessageFlagImpl.class,
427                         new Long(messageFlagId));
428             }
429             catch (Exception e) {
430                 throw processException(e);
431             }
432             finally {
433                 if (mbMessageFlag != null) {
434                     cacheResult(mbMessageFlag);
435                 }
436 
437                 closeSession(session);
438             }
439         }
440 
441         return mbMessageFlag;
442     }
443 
444     public List<MBMessageFlag> findByUserId(long userId)
445         throws SystemException {
446         return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
447     }
448 
449     public List<MBMessageFlag> findByUserId(long userId, int start, int end)
450         throws SystemException {
451         return findByUserId(userId, start, end, null);
452     }
453 
454     public List<MBMessageFlag> findByUserId(long userId, int start, int end,
455         OrderByComparator orderByComparator) throws SystemException {
456         Object[] finderArgs = new Object[] {
457                 userId,
458                 
459                 String.valueOf(start), String.valueOf(end),
460                 String.valueOf(orderByComparator)
461             };
462 
463         List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
464                 finderArgs, this);
465 
466         if (list == null) {
467             StringBundler query = null;
468 
469             if (orderByComparator != null) {
470                 query = new StringBundler(3 +
471                         (orderByComparator.getOrderByFields().length * 3));
472             }
473             else {
474                 query = new StringBundler(2);
475             }
476 
477             query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
478 
479             query.append(_FINDER_COLUMN_USERID_USERID_2);
480 
481             if (orderByComparator != null) {
482                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
483                     orderByComparator);
484             }
485 
486             String sql = query.toString();
487 
488             Session session = null;
489 
490             try {
491                 session = openSession();
492 
493                 Query q = session.createQuery(sql);
494 
495                 QueryPos qPos = QueryPos.getInstance(q);
496 
497                 qPos.add(userId);
498 
499                 list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
500                         start, end);
501             }
502             catch (Exception e) {
503                 throw processException(e);
504             }
505             finally {
506                 if (list == null) {
507                     list = new ArrayList<MBMessageFlag>();
508                 }
509 
510                 cacheResult(list);
511 
512                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
513                     finderArgs, list);
514 
515                 closeSession(session);
516             }
517         }
518 
519         return list;
520     }
521 
522     public MBMessageFlag findByUserId_First(long userId,
523         OrderByComparator orderByComparator)
524         throws NoSuchMessageFlagException, SystemException {
525         List<MBMessageFlag> list = findByUserId(userId, 0, 1, orderByComparator);
526 
527         if (list.isEmpty()) {
528             StringBundler msg = new StringBundler(4);
529 
530             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
531 
532             msg.append("userId=");
533             msg.append(userId);
534 
535             msg.append(StringPool.CLOSE_CURLY_BRACE);
536 
537             throw new NoSuchMessageFlagException(msg.toString());
538         }
539         else {
540             return list.get(0);
541         }
542     }
543 
544     public MBMessageFlag findByUserId_Last(long userId,
545         OrderByComparator orderByComparator)
546         throws NoSuchMessageFlagException, SystemException {
547         int count = countByUserId(userId);
548 
549         List<MBMessageFlag> list = findByUserId(userId, count - 1, count,
550                 orderByComparator);
551 
552         if (list.isEmpty()) {
553             StringBundler msg = new StringBundler(4);
554 
555             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
556 
557             msg.append("userId=");
558             msg.append(userId);
559 
560             msg.append(StringPool.CLOSE_CURLY_BRACE);
561 
562             throw new NoSuchMessageFlagException(msg.toString());
563         }
564         else {
565             return list.get(0);
566         }
567     }
568 
569     public MBMessageFlag[] findByUserId_PrevAndNext(long messageFlagId,
570         long userId, OrderByComparator orderByComparator)
571         throws NoSuchMessageFlagException, SystemException {
572         MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
573 
574         Session session = null;
575 
576         try {
577             session = openSession();
578 
579             MBMessageFlag[] array = new MBMessageFlagImpl[3];
580 
581             array[0] = getByUserId_PrevAndNext(session, mbMessageFlag, userId,
582                     orderByComparator, true);
583 
584             array[1] = mbMessageFlag;
585 
586             array[2] = getByUserId_PrevAndNext(session, mbMessageFlag, userId,
587                     orderByComparator, false);
588 
589             return array;
590         }
591         catch (Exception e) {
592             throw processException(e);
593         }
594         finally {
595             closeSession(session);
596         }
597     }
598 
599     protected MBMessageFlag getByUserId_PrevAndNext(Session session,
600         MBMessageFlag mbMessageFlag, long userId,
601         OrderByComparator orderByComparator, boolean previous) {
602         StringBundler query = null;
603 
604         if (orderByComparator != null) {
605             query = new StringBundler(6 +
606                     (orderByComparator.getOrderByFields().length * 6));
607         }
608         else {
609             query = new StringBundler(3);
610         }
611 
612         query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
613 
614         query.append(_FINDER_COLUMN_USERID_USERID_2);
615 
616         if (orderByComparator != null) {
617             String[] orderByFields = orderByComparator.getOrderByFields();
618 
619             if (orderByFields.length > 0) {
620                 query.append(WHERE_AND);
621             }
622 
623             for (int i = 0; i < orderByFields.length; i++) {
624                 query.append(_ORDER_BY_ENTITY_ALIAS);
625                 query.append(orderByFields[i]);
626 
627                 if ((i + 1) < orderByFields.length) {
628                     if (orderByComparator.isAscending() ^ previous) {
629                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
630                     }
631                     else {
632                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
633                     }
634                 }
635                 else {
636                     if (orderByComparator.isAscending() ^ previous) {
637                         query.append(WHERE_GREATER_THAN);
638                     }
639                     else {
640                         query.append(WHERE_LESSER_THAN);
641                     }
642                 }
643             }
644 
645             query.append(ORDER_BY_CLAUSE);
646 
647             for (int i = 0; i < orderByFields.length; i++) {
648                 query.append(_ORDER_BY_ENTITY_ALIAS);
649                 query.append(orderByFields[i]);
650 
651                 if ((i + 1) < orderByFields.length) {
652                     if (orderByComparator.isAscending() ^ previous) {
653                         query.append(ORDER_BY_ASC_HAS_NEXT);
654                     }
655                     else {
656                         query.append(ORDER_BY_DESC_HAS_NEXT);
657                     }
658                 }
659                 else {
660                     if (orderByComparator.isAscending() ^ previous) {
661                         query.append(ORDER_BY_ASC);
662                     }
663                     else {
664                         query.append(ORDER_BY_DESC);
665                     }
666                 }
667             }
668         }
669 
670         String sql = query.toString();
671 
672         Query q = session.createQuery(sql);
673 
674         q.setFirstResult(0);
675         q.setMaxResults(2);
676 
677         QueryPos qPos = QueryPos.getInstance(q);
678 
679         qPos.add(userId);
680 
681         if (orderByComparator != null) {
682             Object[] values = orderByComparator.getOrderByValues(mbMessageFlag);
683 
684             for (Object value : values) {
685                 qPos.add(value);
686             }
687         }
688 
689         List<MBMessageFlag> list = q.list();
690 
691         if (list.size() == 2) {
692             return list.get(1);
693         }
694         else {
695             return null;
696         }
697     }
698 
699     public List<MBMessageFlag> findByThreadId(long threadId)
700         throws SystemException {
701         return findByThreadId(threadId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
702             null);
703     }
704 
705     public List<MBMessageFlag> findByThreadId(long threadId, int start, int end)
706         throws SystemException {
707         return findByThreadId(threadId, start, end, null);
708     }
709 
710     public List<MBMessageFlag> findByThreadId(long threadId, int start,
711         int end, OrderByComparator orderByComparator) throws SystemException {
712         Object[] finderArgs = new Object[] {
713                 threadId,
714                 
715                 String.valueOf(start), String.valueOf(end),
716                 String.valueOf(orderByComparator)
717             };
718 
719         List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_THREADID,
720                 finderArgs, this);
721 
722         if (list == null) {
723             StringBundler query = null;
724 
725             if (orderByComparator != null) {
726                 query = new StringBundler(3 +
727                         (orderByComparator.getOrderByFields().length * 3));
728             }
729             else {
730                 query = new StringBundler(2);
731             }
732 
733             query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
734 
735             query.append(_FINDER_COLUMN_THREADID_THREADID_2);
736 
737             if (orderByComparator != null) {
738                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
739                     orderByComparator);
740             }
741 
742             String sql = query.toString();
743 
744             Session session = null;
745 
746             try {
747                 session = openSession();
748 
749                 Query q = session.createQuery(sql);
750 
751                 QueryPos qPos = QueryPos.getInstance(q);
752 
753                 qPos.add(threadId);
754 
755                 list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
756                         start, end);
757             }
758             catch (Exception e) {
759                 throw processException(e);
760             }
761             finally {
762                 if (list == null) {
763                     list = new ArrayList<MBMessageFlag>();
764                 }
765 
766                 cacheResult(list);
767 
768                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_THREADID,
769                     finderArgs, list);
770 
771                 closeSession(session);
772             }
773         }
774 
775         return list;
776     }
777 
778     public MBMessageFlag findByThreadId_First(long threadId,
779         OrderByComparator orderByComparator)
780         throws NoSuchMessageFlagException, SystemException {
781         List<MBMessageFlag> list = findByThreadId(threadId, 0, 1,
782                 orderByComparator);
783 
784         if (list.isEmpty()) {
785             StringBundler msg = new StringBundler(4);
786 
787             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
788 
789             msg.append("threadId=");
790             msg.append(threadId);
791 
792             msg.append(StringPool.CLOSE_CURLY_BRACE);
793 
794             throw new NoSuchMessageFlagException(msg.toString());
795         }
796         else {
797             return list.get(0);
798         }
799     }
800 
801     public MBMessageFlag findByThreadId_Last(long threadId,
802         OrderByComparator orderByComparator)
803         throws NoSuchMessageFlagException, SystemException {
804         int count = countByThreadId(threadId);
805 
806         List<MBMessageFlag> list = findByThreadId(threadId, count - 1, count,
807                 orderByComparator);
808 
809         if (list.isEmpty()) {
810             StringBundler msg = new StringBundler(4);
811 
812             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
813 
814             msg.append("threadId=");
815             msg.append(threadId);
816 
817             msg.append(StringPool.CLOSE_CURLY_BRACE);
818 
819             throw new NoSuchMessageFlagException(msg.toString());
820         }
821         else {
822             return list.get(0);
823         }
824     }
825 
826     public MBMessageFlag[] findByThreadId_PrevAndNext(long messageFlagId,
827         long threadId, OrderByComparator orderByComparator)
828         throws NoSuchMessageFlagException, SystemException {
829         MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
830 
831         Session session = null;
832 
833         try {
834             session = openSession();
835 
836             MBMessageFlag[] array = new MBMessageFlagImpl[3];
837 
838             array[0] = getByThreadId_PrevAndNext(session, mbMessageFlag,
839                     threadId, orderByComparator, true);
840 
841             array[1] = mbMessageFlag;
842 
843             array[2] = getByThreadId_PrevAndNext(session, mbMessageFlag,
844                     threadId, orderByComparator, false);
845 
846             return array;
847         }
848         catch (Exception e) {
849             throw processException(e);
850         }
851         finally {
852             closeSession(session);
853         }
854     }
855 
856     protected MBMessageFlag getByThreadId_PrevAndNext(Session session,
857         MBMessageFlag mbMessageFlag, long threadId,
858         OrderByComparator orderByComparator, boolean previous) {
859         StringBundler query = null;
860 
861         if (orderByComparator != null) {
862             query = new StringBundler(6 +
863                     (orderByComparator.getOrderByFields().length * 6));
864         }
865         else {
866             query = new StringBundler(3);
867         }
868 
869         query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
870 
871         query.append(_FINDER_COLUMN_THREADID_THREADID_2);
872 
873         if (orderByComparator != null) {
874             String[] orderByFields = orderByComparator.getOrderByFields();
875 
876             if (orderByFields.length > 0) {
877                 query.append(WHERE_AND);
878             }
879 
880             for (int i = 0; i < orderByFields.length; i++) {
881                 query.append(_ORDER_BY_ENTITY_ALIAS);
882                 query.append(orderByFields[i]);
883 
884                 if ((i + 1) < orderByFields.length) {
885                     if (orderByComparator.isAscending() ^ previous) {
886                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
887                     }
888                     else {
889                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
890                     }
891                 }
892                 else {
893                     if (orderByComparator.isAscending() ^ previous) {
894                         query.append(WHERE_GREATER_THAN);
895                     }
896                     else {
897                         query.append(WHERE_LESSER_THAN);
898                     }
899                 }
900             }
901 
902             query.append(ORDER_BY_CLAUSE);
903 
904             for (int i = 0; i < orderByFields.length; i++) {
905                 query.append(_ORDER_BY_ENTITY_ALIAS);
906                 query.append(orderByFields[i]);
907 
908                 if ((i + 1) < orderByFields.length) {
909                     if (orderByComparator.isAscending() ^ previous) {
910                         query.append(ORDER_BY_ASC_HAS_NEXT);
911                     }
912                     else {
913                         query.append(ORDER_BY_DESC_HAS_NEXT);
914                     }
915                 }
916                 else {
917                     if (orderByComparator.isAscending() ^ previous) {
918                         query.append(ORDER_BY_ASC);
919                     }
920                     else {
921                         query.append(ORDER_BY_DESC);
922                     }
923                 }
924             }
925         }
926 
927         String sql = query.toString();
928 
929         Query q = session.createQuery(sql);
930 
931         q.setFirstResult(0);
932         q.setMaxResults(2);
933 
934         QueryPos qPos = QueryPos.getInstance(q);
935 
936         qPos.add(threadId);
937 
938         if (orderByComparator != null) {
939             Object[] values = orderByComparator.getOrderByValues(mbMessageFlag);
940 
941             for (Object value : values) {
942                 qPos.add(value);
943             }
944         }
945 
946         List<MBMessageFlag> list = q.list();
947 
948         if (list.size() == 2) {
949             return list.get(1);
950         }
951         else {
952             return null;
953         }
954     }
955 
956     public List<MBMessageFlag> findByMessageId(long messageId)
957         throws SystemException {
958         return findByMessageId(messageId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
959             null);
960     }
961 
962     public List<MBMessageFlag> findByMessageId(long messageId, int start,
963         int end) throws SystemException {
964         return findByMessageId(messageId, start, end, null);
965     }
966 
967     public List<MBMessageFlag> findByMessageId(long messageId, int start,
968         int end, OrderByComparator orderByComparator) throws SystemException {
969         Object[] finderArgs = new Object[] {
970                 messageId,
971                 
972                 String.valueOf(start), String.valueOf(end),
973                 String.valueOf(orderByComparator)
974             };
975 
976         List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_MESSAGEID,
977                 finderArgs, this);
978 
979         if (list == null) {
980             StringBundler query = null;
981 
982             if (orderByComparator != null) {
983                 query = new StringBundler(3 +
984                         (orderByComparator.getOrderByFields().length * 3));
985             }
986             else {
987                 query = new StringBundler(2);
988             }
989 
990             query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
991 
992             query.append(_FINDER_COLUMN_MESSAGEID_MESSAGEID_2);
993 
994             if (orderByComparator != null) {
995                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
996                     orderByComparator);
997             }
998 
999             String sql = query.toString();
1000
1001            Session session = null;
1002
1003            try {
1004                session = openSession();
1005
1006                Query q = session.createQuery(sql);
1007
1008                QueryPos qPos = QueryPos.getInstance(q);
1009
1010                qPos.add(messageId);
1011
1012                list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
1013                        start, end);
1014            }
1015            catch (Exception e) {
1016                throw processException(e);
1017            }
1018            finally {
1019                if (list == null) {
1020                    list = new ArrayList<MBMessageFlag>();
1021                }
1022
1023                cacheResult(list);
1024
1025                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_MESSAGEID,
1026                    finderArgs, list);
1027
1028                closeSession(session);
1029            }
1030        }
1031
1032        return list;
1033    }
1034
1035    public MBMessageFlag findByMessageId_First(long messageId,
1036        OrderByComparator orderByComparator)
1037        throws NoSuchMessageFlagException, SystemException {
1038        List<MBMessageFlag> list = findByMessageId(messageId, 0, 1,
1039                orderByComparator);
1040
1041        if (list.isEmpty()) {
1042            StringBundler msg = new StringBundler(4);
1043
1044            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1045
1046            msg.append("messageId=");
1047            msg.append(messageId);
1048
1049            msg.append(StringPool.CLOSE_CURLY_BRACE);
1050
1051            throw new NoSuchMessageFlagException(msg.toString());
1052        }
1053        else {
1054            return list.get(0);
1055        }
1056    }
1057
1058    public MBMessageFlag findByMessageId_Last(long messageId,
1059        OrderByComparator orderByComparator)
1060        throws NoSuchMessageFlagException, SystemException {
1061        int count = countByMessageId(messageId);
1062
1063        List<MBMessageFlag> list = findByMessageId(messageId, count - 1, count,
1064                orderByComparator);
1065
1066        if (list.isEmpty()) {
1067            StringBundler msg = new StringBundler(4);
1068
1069            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1070
1071            msg.append("messageId=");
1072            msg.append(messageId);
1073
1074            msg.append(StringPool.CLOSE_CURLY_BRACE);
1075
1076            throw new NoSuchMessageFlagException(msg.toString());
1077        }
1078        else {
1079            return list.get(0);
1080        }
1081    }
1082
1083    public MBMessageFlag[] findByMessageId_PrevAndNext(long messageFlagId,
1084        long messageId, OrderByComparator orderByComparator)
1085        throws NoSuchMessageFlagException, SystemException {
1086        MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
1087
1088        Session session = null;
1089
1090        try {
1091            session = openSession();
1092
1093            MBMessageFlag[] array = new MBMessageFlagImpl[3];
1094
1095            array[0] = getByMessageId_PrevAndNext(session, mbMessageFlag,
1096                    messageId, orderByComparator, true);
1097
1098            array[1] = mbMessageFlag;
1099
1100            array[2] = getByMessageId_PrevAndNext(session, mbMessageFlag,
1101                    messageId, orderByComparator, false);
1102
1103            return array;
1104        }
1105        catch (Exception e) {
1106            throw processException(e);
1107        }
1108        finally {
1109            closeSession(session);
1110        }
1111    }
1112
1113    protected MBMessageFlag getByMessageId_PrevAndNext(Session session,
1114        MBMessageFlag mbMessageFlag, long messageId,
1115        OrderByComparator orderByComparator, boolean previous) {
1116        StringBundler query = null;
1117
1118        if (orderByComparator != null) {
1119            query = new StringBundler(6 +
1120                    (orderByComparator.getOrderByFields().length * 6));
1121        }
1122        else {
1123            query = new StringBundler(3);
1124        }
1125
1126        query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1127
1128        query.append(_FINDER_COLUMN_MESSAGEID_MESSAGEID_2);
1129
1130        if (orderByComparator != null) {
1131            String[] orderByFields = orderByComparator.getOrderByFields();
1132
1133            if (orderByFields.length > 0) {
1134                query.append(WHERE_AND);
1135            }
1136
1137            for (int i = 0; i < orderByFields.length; i++) {
1138                query.append(_ORDER_BY_ENTITY_ALIAS);
1139                query.append(orderByFields[i]);
1140
1141                if ((i + 1) < orderByFields.length) {
1142                    if (orderByComparator.isAscending() ^ previous) {
1143                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1144                    }
1145                    else {
1146                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1147                    }
1148                }
1149                else {
1150                    if (orderByComparator.isAscending() ^ previous) {
1151                        query.append(WHERE_GREATER_THAN);
1152                    }
1153                    else {
1154                        query.append(WHERE_LESSER_THAN);
1155                    }
1156                }
1157            }
1158
1159            query.append(ORDER_BY_CLAUSE);
1160
1161            for (int i = 0; i < orderByFields.length; i++) {
1162                query.append(_ORDER_BY_ENTITY_ALIAS);
1163                query.append(orderByFields[i]);
1164
1165                if ((i + 1) < orderByFields.length) {
1166                    if (orderByComparator.isAscending() ^ previous) {
1167                        query.append(ORDER_BY_ASC_HAS_NEXT);
1168                    }
1169                    else {
1170                        query.append(ORDER_BY_DESC_HAS_NEXT);
1171                    }
1172                }
1173                else {
1174                    if (orderByComparator.isAscending() ^ previous) {
1175                        query.append(ORDER_BY_ASC);
1176                    }
1177                    else {
1178                        query.append(ORDER_BY_DESC);
1179                    }
1180                }
1181            }
1182        }
1183
1184        String sql = query.toString();
1185
1186        Query q = session.createQuery(sql);
1187
1188        q.setFirstResult(0);
1189        q.setMaxResults(2);
1190
1191        QueryPos qPos = QueryPos.getInstance(q);
1192
1193        qPos.add(messageId);
1194
1195        if (orderByComparator != null) {
1196            Object[] values = orderByComparator.getOrderByValues(mbMessageFlag);
1197
1198            for (Object value : values) {
1199                qPos.add(value);
1200            }
1201        }
1202
1203        List<MBMessageFlag> list = q.list();
1204
1205        if (list.size() == 2) {
1206            return list.get(1);
1207        }
1208        else {
1209            return null;
1210        }
1211    }
1212
1213    public List<MBMessageFlag> findByT_F(long threadId, int flag)
1214        throws SystemException {
1215        return findByT_F(threadId, flag, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1216            null);
1217    }
1218
1219    public List<MBMessageFlag> findByT_F(long threadId, int flag, int start,
1220        int end) throws SystemException {
1221        return findByT_F(threadId, flag, start, end, null);
1222    }
1223
1224    public List<MBMessageFlag> findByT_F(long threadId, int flag, int start,
1225        int end, OrderByComparator orderByComparator) throws SystemException {
1226        Object[] finderArgs = new Object[] {
1227                threadId, flag,
1228                
1229                String.valueOf(start), String.valueOf(end),
1230                String.valueOf(orderByComparator)
1231            };
1232
1233        List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_T_F,
1234                finderArgs, this);
1235
1236        if (list == null) {
1237            StringBundler query = null;
1238
1239            if (orderByComparator != null) {
1240                query = new StringBundler(4 +
1241                        (orderByComparator.getOrderByFields().length * 3));
1242            }
1243            else {
1244                query = new StringBundler(3);
1245            }
1246
1247            query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1248
1249            query.append(_FINDER_COLUMN_T_F_THREADID_2);
1250
1251            query.append(_FINDER_COLUMN_T_F_FLAG_2);
1252
1253            if (orderByComparator != null) {
1254                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1255                    orderByComparator);
1256            }
1257
1258            String sql = query.toString();
1259
1260            Session session = null;
1261
1262            try {
1263                session = openSession();
1264
1265                Query q = session.createQuery(sql);
1266
1267                QueryPos qPos = QueryPos.getInstance(q);
1268
1269                qPos.add(threadId);
1270
1271                qPos.add(flag);
1272
1273                list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
1274                        start, end);
1275            }
1276            catch (Exception e) {
1277                throw processException(e);
1278            }
1279            finally {
1280                if (list == null) {
1281                    list = new ArrayList<MBMessageFlag>();
1282                }
1283
1284                cacheResult(list);
1285
1286                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_T_F, finderArgs,
1287                    list);
1288
1289                closeSession(session);
1290            }
1291        }
1292
1293        return list;
1294    }
1295
1296    public MBMessageFlag findByT_F_First(long threadId, int flag,
1297        OrderByComparator orderByComparator)
1298        throws NoSuchMessageFlagException, SystemException {
1299        List<MBMessageFlag> list = findByT_F(threadId, flag, 0, 1,
1300                orderByComparator);
1301
1302        if (list.isEmpty()) {
1303            StringBundler msg = new StringBundler(6);
1304
1305            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1306
1307            msg.append("threadId=");
1308            msg.append(threadId);
1309
1310            msg.append(", flag=");
1311            msg.append(flag);
1312
1313            msg.append(StringPool.CLOSE_CURLY_BRACE);
1314
1315            throw new NoSuchMessageFlagException(msg.toString());
1316        }
1317        else {
1318            return list.get(0);
1319        }
1320    }
1321
1322    public MBMessageFlag findByT_F_Last(long threadId, int flag,
1323        OrderByComparator orderByComparator)
1324        throws NoSuchMessageFlagException, SystemException {
1325        int count = countByT_F(threadId, flag);
1326
1327        List<MBMessageFlag> list = findByT_F(threadId, flag, count - 1, count,
1328                orderByComparator);
1329
1330        if (list.isEmpty()) {
1331            StringBundler msg = new StringBundler(6);
1332
1333            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1334
1335            msg.append("threadId=");
1336            msg.append(threadId);
1337
1338            msg.append(", flag=");
1339            msg.append(flag);
1340
1341            msg.append(StringPool.CLOSE_CURLY_BRACE);
1342
1343            throw new NoSuchMessageFlagException(msg.toString());
1344        }
1345        else {
1346            return list.get(0);
1347        }
1348    }
1349
1350    public MBMessageFlag[] findByT_F_PrevAndNext(long messageFlagId,
1351        long threadId, int flag, OrderByComparator orderByComparator)
1352        throws NoSuchMessageFlagException, SystemException {
1353        MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
1354
1355        Session session = null;
1356
1357        try {
1358            session = openSession();
1359
1360            MBMessageFlag[] array = new MBMessageFlagImpl[3];
1361
1362            array[0] = getByT_F_PrevAndNext(session, mbMessageFlag, threadId,
1363                    flag, orderByComparator, true);
1364
1365            array[1] = mbMessageFlag;
1366
1367            array[2] = getByT_F_PrevAndNext(session, mbMessageFlag, threadId,
1368                    flag, orderByComparator, false);
1369
1370            return array;
1371        }
1372        catch (Exception e) {
1373            throw processException(e);
1374        }
1375        finally {
1376            closeSession(session);
1377        }
1378    }
1379
1380    protected MBMessageFlag getByT_F_PrevAndNext(Session session,
1381        MBMessageFlag mbMessageFlag, long threadId, int flag,
1382        OrderByComparator orderByComparator, boolean previous) {
1383        StringBundler query = null;
1384
1385        if (orderByComparator != null) {
1386            query = new StringBundler(6 +
1387                    (orderByComparator.getOrderByFields().length * 6));
1388        }
1389        else {
1390            query = new StringBundler(3);
1391        }
1392
1393        query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1394
1395        query.append(_FINDER_COLUMN_T_F_THREADID_2);
1396
1397        query.append(_FINDER_COLUMN_T_F_FLAG_2);
1398
1399        if (orderByComparator != null) {
1400            String[] orderByFields = orderByComparator.getOrderByFields();
1401
1402            if (orderByFields.length > 0) {
1403                query.append(WHERE_AND);
1404            }
1405
1406            for (int i = 0; i < orderByFields.length; i++) {
1407                query.append(_ORDER_BY_ENTITY_ALIAS);
1408                query.append(orderByFields[i]);
1409
1410                if ((i + 1) < orderByFields.length) {
1411                    if (orderByComparator.isAscending() ^ previous) {
1412                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1413                    }
1414                    else {
1415                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1416                    }
1417                }
1418                else {
1419                    if (orderByComparator.isAscending() ^ previous) {
1420                        query.append(WHERE_GREATER_THAN);
1421                    }
1422                    else {
1423                        query.append(WHERE_LESSER_THAN);
1424                    }
1425                }
1426            }
1427
1428            query.append(ORDER_BY_CLAUSE);
1429
1430            for (int i = 0; i < orderByFields.length; i++) {
1431                query.append(_ORDER_BY_ENTITY_ALIAS);
1432                query.append(orderByFields[i]);
1433
1434                if ((i + 1) < orderByFields.length) {
1435                    if (orderByComparator.isAscending() ^ previous) {
1436                        query.append(ORDER_BY_ASC_HAS_NEXT);
1437                    }
1438                    else {
1439                        query.append(ORDER_BY_DESC_HAS_NEXT);
1440                    }
1441                }
1442                else {
1443                    if (orderByComparator.isAscending() ^ previous) {
1444                        query.append(ORDER_BY_ASC);
1445                    }
1446                    else {
1447                        query.append(ORDER_BY_DESC);
1448                    }
1449                }
1450            }
1451        }
1452
1453        String sql = query.toString();
1454
1455        Query q = session.createQuery(sql);
1456
1457        q.setFirstResult(0);
1458        q.setMaxResults(2);
1459
1460        QueryPos qPos = QueryPos.getInstance(q);
1461
1462        qPos.add(threadId);
1463
1464        qPos.add(flag);
1465
1466        if (orderByComparator != null) {
1467            Object[] values = orderByComparator.getOrderByValues(mbMessageFlag);
1468
1469            for (Object value : values) {
1470                qPos.add(value);
1471            }
1472        }
1473
1474        List<MBMessageFlag> list = q.list();
1475
1476        if (list.size() == 2) {
1477            return list.get(1);
1478        }
1479        else {
1480            return null;
1481        }
1482    }
1483
1484    public List<MBMessageFlag> findByM_F(long messageId, int flag)
1485        throws SystemException {
1486        return findByM_F(messageId, flag, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1487            null);
1488    }
1489
1490    public List<MBMessageFlag> findByM_F(long messageId, int flag, int start,
1491        int end) throws SystemException {
1492        return findByM_F(messageId, flag, start, end, null);
1493    }
1494
1495    public List<MBMessageFlag> findByM_F(long messageId, int flag, int start,
1496        int end, OrderByComparator orderByComparator) throws SystemException {
1497        Object[] finderArgs = new Object[] {
1498                messageId, flag,
1499                
1500                String.valueOf(start), String.valueOf(end),
1501                String.valueOf(orderByComparator)
1502            };
1503
1504        List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_M_F,
1505                finderArgs, this);
1506
1507        if (list == null) {
1508            StringBundler query = null;
1509
1510            if (orderByComparator != null) {
1511                query = new StringBundler(4 +
1512                        (orderByComparator.getOrderByFields().length * 3));
1513            }
1514            else {
1515                query = new StringBundler(3);
1516            }
1517
1518            query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1519
1520            query.append(_FINDER_COLUMN_M_F_MESSAGEID_2);
1521
1522            query.append(_FINDER_COLUMN_M_F_FLAG_2);
1523
1524            if (orderByComparator != null) {
1525                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1526                    orderByComparator);
1527            }
1528
1529            String sql = query.toString();
1530
1531            Session session = null;
1532
1533            try {
1534                session = openSession();
1535
1536                Query q = session.createQuery(sql);
1537
1538                QueryPos qPos = QueryPos.getInstance(q);
1539
1540                qPos.add(messageId);
1541
1542                qPos.add(flag);
1543
1544                list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
1545                        start, end);
1546            }
1547            catch (Exception e) {
1548                throw processException(e);
1549            }
1550            finally {
1551                if (list == null) {
1552                    list = new ArrayList<MBMessageFlag>();
1553                }
1554
1555                cacheResult(list);
1556
1557                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_M_F, finderArgs,
1558                    list);
1559
1560                closeSession(session);
1561            }
1562        }
1563
1564        return list;
1565    }
1566
1567    public MBMessageFlag findByM_F_First(long messageId, int flag,
1568        OrderByComparator orderByComparator)
1569        throws NoSuchMessageFlagException, SystemException {
1570        List<MBMessageFlag> list = findByM_F(messageId, flag, 0, 1,
1571                orderByComparator);
1572
1573        if (list.isEmpty()) {
1574            StringBundler msg = new StringBundler(6);
1575
1576            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1577
1578            msg.append("messageId=");
1579            msg.append(messageId);
1580
1581            msg.append(", flag=");
1582            msg.append(flag);
1583
1584            msg.append(StringPool.CLOSE_CURLY_BRACE);
1585
1586            throw new NoSuchMessageFlagException(msg.toString());
1587        }
1588        else {
1589            return list.get(0);
1590        }
1591    }
1592
1593    public MBMessageFlag findByM_F_Last(long messageId, int flag,
1594        OrderByComparator orderByComparator)
1595        throws NoSuchMessageFlagException, SystemException {
1596        int count = countByM_F(messageId, flag);
1597
1598        List<MBMessageFlag> list = findByM_F(messageId, flag, count - 1, count,
1599                orderByComparator);
1600
1601        if (list.isEmpty()) {
1602            StringBundler msg = new StringBundler(6);
1603
1604            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1605
1606            msg.append("messageId=");
1607            msg.append(messageId);
1608
1609            msg.append(", flag=");
1610            msg.append(flag);
1611
1612            msg.append(StringPool.CLOSE_CURLY_BRACE);
1613
1614            throw new NoSuchMessageFlagException(msg.toString());
1615        }
1616        else {
1617            return list.get(0);
1618        }
1619    }
1620
1621    public MBMessageFlag[] findByM_F_PrevAndNext(long messageFlagId,
1622        long messageId, int flag, OrderByComparator orderByComparator)
1623        throws NoSuchMessageFlagException, SystemException {
1624        MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
1625
1626        Session session = null;
1627
1628        try {
1629            session = openSession();
1630
1631            MBMessageFlag[] array = new MBMessageFlagImpl[3];
1632
1633            array[0] = getByM_F_PrevAndNext(session, mbMessageFlag, messageId,
1634                    flag, orderByComparator, true);
1635
1636            array[1] = mbMessageFlag;
1637
1638            array[2] = getByM_F_PrevAndNext(session, mbMessageFlag, messageId,
1639                    flag, orderByComparator, false);
1640
1641            return array;
1642        }
1643        catch (Exception e) {
1644            throw processException(e);
1645        }
1646        finally {
1647            closeSession(session);
1648        }
1649    }
1650
1651    protected MBMessageFlag getByM_F_PrevAndNext(Session session,
1652        MBMessageFlag mbMessageFlag, long messageId, int flag,
1653        OrderByComparator orderByComparator, boolean previous) {
1654        StringBundler query = null;
1655
1656        if (orderByComparator != null) {
1657            query = new StringBundler(6 +
1658                    (orderByComparator.getOrderByFields().length * 6));
1659        }
1660        else {
1661            query = new StringBundler(3);
1662        }
1663
1664        query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1665
1666        query.append(_FINDER_COLUMN_M_F_MESSAGEID_2);
1667
1668        query.append(_FINDER_COLUMN_M_F_FLAG_2);
1669
1670        if (orderByComparator != null) {
1671            String[] orderByFields = orderByComparator.getOrderByFields();
1672
1673            if (orderByFields.length > 0) {
1674                query.append(WHERE_AND);
1675            }
1676
1677            for (int i = 0; i < orderByFields.length; i++) {
1678                query.append(_ORDER_BY_ENTITY_ALIAS);
1679                query.append(orderByFields[i]);
1680
1681                if ((i + 1) < orderByFields.length) {
1682                    if (orderByComparator.isAscending() ^ previous) {
1683                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1684                    }
1685                    else {
1686                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1687                    }
1688                }
1689                else {
1690                    if (orderByComparator.isAscending() ^ previous) {
1691                        query.append(WHERE_GREATER_THAN);
1692                    }
1693                    else {
1694                        query.append(WHERE_LESSER_THAN);
1695                    }
1696                }
1697            }
1698
1699            query.append(ORDER_BY_CLAUSE);
1700
1701            for (int i = 0; i < orderByFields.length; i++) {
1702                query.append(_ORDER_BY_ENTITY_ALIAS);
1703                query.append(orderByFields[i]);
1704
1705                if ((i + 1) < orderByFields.length) {
1706                    if (orderByComparator.isAscending() ^ previous) {
1707                        query.append(ORDER_BY_ASC_HAS_NEXT);
1708                    }
1709                    else {
1710                        query.append(ORDER_BY_DESC_HAS_NEXT);
1711                    }
1712                }
1713                else {
1714                    if (orderByComparator.isAscending() ^ previous) {
1715                        query.append(ORDER_BY_ASC);
1716                    }
1717                    else {
1718                        query.append(ORDER_BY_DESC);
1719                    }
1720                }
1721            }
1722        }
1723
1724        String sql = query.toString();
1725
1726        Query q = session.createQuery(sql);
1727
1728        q.setFirstResult(0);
1729        q.setMaxResults(2);
1730
1731        QueryPos qPos = QueryPos.getInstance(q);
1732
1733        qPos.add(messageId);
1734
1735        qPos.add(flag);
1736
1737        if (orderByComparator != null) {
1738            Object[] values = orderByComparator.getOrderByValues(mbMessageFlag);
1739
1740            for (Object value : values) {
1741                qPos.add(value);
1742            }
1743        }
1744
1745        List<MBMessageFlag> list = q.list();
1746
1747        if (list.size() == 2) {
1748            return list.get(1);
1749        }
1750        else {
1751            return null;
1752        }
1753    }
1754
1755    public List<MBMessageFlag> findByU_T_F(long userId, long threadId, int flag)
1756        throws SystemException {
1757        return findByU_T_F(userId, threadId, flag, QueryUtil.ALL_POS,
1758            QueryUtil.ALL_POS, null);
1759    }
1760
1761    public List<MBMessageFlag> findByU_T_F(long userId, long threadId,
1762        int flag, int start, int end) throws SystemException {
1763        return findByU_T_F(userId, threadId, flag, start, end, null);
1764    }
1765
1766    public List<MBMessageFlag> findByU_T_F(long userId, long threadId,
1767        int flag, int start, int end, OrderByComparator orderByComparator)
1768        throws SystemException {
1769        Object[] finderArgs = new Object[] {
1770                userId, threadId, flag,
1771                
1772                String.valueOf(start), String.valueOf(end),
1773                String.valueOf(orderByComparator)
1774            };
1775
1776        List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_U_T_F,
1777                finderArgs, this);
1778
1779        if (list == null) {
1780            StringBundler query = null;
1781
1782            if (orderByComparator != null) {
1783                query = new StringBundler(5 +
1784                        (orderByComparator.getOrderByFields().length * 3));
1785            }
1786            else {
1787                query = new StringBundler(4);
1788            }
1789
1790            query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1791
1792            query.append(_FINDER_COLUMN_U_T_F_USERID_2);
1793
1794            query.append(_FINDER_COLUMN_U_T_F_THREADID_2);
1795
1796            query.append(_FINDER_COLUMN_U_T_F_FLAG_2);
1797
1798            if (orderByComparator != null) {
1799                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1800                    orderByComparator);
1801            }
1802
1803            String sql = query.toString();
1804
1805            Session session = null;
1806
1807            try {
1808                session = openSession();
1809
1810                Query q = session.createQuery(sql);
1811
1812                QueryPos qPos = QueryPos.getInstance(q);
1813
1814                qPos.add(userId);
1815
1816                qPos.add(threadId);
1817
1818                qPos.add(flag);
1819
1820                list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
1821                        start, end);
1822            }
1823            catch (Exception e) {
1824                throw processException(e);
1825            }
1826            finally {
1827                if (list == null) {
1828                    list = new ArrayList<MBMessageFlag>();
1829                }
1830
1831                cacheResult(list);
1832
1833                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_U_T_F,
1834                    finderArgs, list);
1835
1836                closeSession(session);
1837            }
1838        }
1839
1840        return list;
1841    }
1842
1843    public MBMessageFlag findByU_T_F_First(long userId, long threadId,
1844        int flag, OrderByComparator orderByComparator)
1845        throws NoSuchMessageFlagException, SystemException {
1846        List<MBMessageFlag> list = findByU_T_F(userId, threadId, flag, 0, 1,
1847                orderByComparator);
1848
1849        if (list.isEmpty()) {
1850            StringBundler msg = new StringBundler(8);
1851
1852            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1853
1854            msg.append("userId=");
1855            msg.append(userId);
1856
1857            msg.append(", threadId=");
1858            msg.append(threadId);
1859
1860            msg.append(", flag=");
1861            msg.append(flag);
1862
1863            msg.append(StringPool.CLOSE_CURLY_BRACE);
1864
1865            throw new NoSuchMessageFlagException(msg.toString());
1866        }
1867        else {
1868            return list.get(0);
1869        }
1870    }
1871
1872    public MBMessageFlag findByU_T_F_Last(long userId, long threadId, int flag,
1873        OrderByComparator orderByComparator)
1874        throws NoSuchMessageFlagException, SystemException {
1875        int count = countByU_T_F(userId, threadId, flag);
1876
1877        List<MBMessageFlag> list = findByU_T_F(userId, threadId, flag,
1878                count - 1, count, orderByComparator);
1879
1880        if (list.isEmpty()) {
1881            StringBundler msg = new StringBundler(8);
1882
1883            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1884
1885            msg.append("userId=");
1886            msg.append(userId);
1887
1888            msg.append(", threadId=");
1889            msg.append(threadId);
1890
1891            msg.append(", flag=");
1892            msg.append(flag);
1893
1894            msg.append(StringPool.CLOSE_CURLY_BRACE);
1895
1896            throw new NoSuchMessageFlagException(msg.toString());
1897        }
1898        else {
1899            return list.get(0);
1900        }
1901    }
1902
1903    public MBMessageFlag[] findByU_T_F_PrevAndNext(long messageFlagId,
1904        long userId, long threadId, int flag,
1905        OrderByComparator orderByComparator)
1906        throws NoSuchMessageFlagException, SystemException {
1907        MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
1908
1909        Session session = null;
1910
1911        try {
1912            session = openSession();
1913
1914            MBMessageFlag[] array = new MBMessageFlagImpl[3];
1915
1916            array[0] = getByU_T_F_PrevAndNext(session, mbMessageFlag, userId,
1917                    threadId, flag, orderByComparator, true);
1918
1919            array[1] = mbMessageFlag;
1920
1921            array[2] = getByU_T_F_PrevAndNext(session, mbMessageFlag, userId,
1922                    threadId, flag, orderByComparator, false);
1923
1924            return array;
1925        }
1926        catch (Exception e) {
1927            throw processException(e);
1928        }
1929        finally {
1930            closeSession(session);
1931        }
1932    }
1933
1934    protected MBMessageFlag getByU_T_F_PrevAndNext(Session session,
1935        MBMessageFlag mbMessageFlag, long userId, long threadId, int flag,
1936        OrderByComparator orderByComparator, boolean previous) {
1937        StringBundler query = null;
1938
1939        if (orderByComparator != null) {
1940            query = new StringBundler(6 +
1941                    (orderByComparator.getOrderByFields().length * 6));
1942        }
1943        else {
1944            query = new StringBundler(3);
1945        }
1946
1947        query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
1948
1949        query.append(_FINDER_COLUMN_U_T_F_USERID_2);
1950
1951        query.append(_FINDER_COLUMN_U_T_F_THREADID_2);
1952
1953        query.append(_FINDER_COLUMN_U_T_F_FLAG_2);
1954
1955        if (orderByComparator != null) {
1956            String[] orderByFields = orderByComparator.getOrderByFields();
1957
1958            if (orderByFields.length > 0) {
1959                query.append(WHERE_AND);
1960            }
1961
1962            for (int i = 0; i < orderByFields.length; i++) {
1963                query.append(_ORDER_BY_ENTITY_ALIAS);
1964                query.append(orderByFields[i]);
1965
1966                if ((i + 1) < orderByFields.length) {
1967                    if (orderByComparator.isAscending() ^ previous) {
1968                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1969                    }
1970                    else {
1971                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1972                    }
1973                }
1974                else {
1975                    if (orderByComparator.isAscending() ^ previous) {
1976                        query.append(WHERE_GREATER_THAN);
1977                    }
1978                    else {
1979                        query.append(WHERE_LESSER_THAN);
1980                    }
1981                }
1982            }
1983
1984            query.append(ORDER_BY_CLAUSE);
1985
1986            for (int i = 0; i < orderByFields.length; i++) {
1987                query.append(_ORDER_BY_ENTITY_ALIAS);
1988                query.append(orderByFields[i]);
1989
1990                if ((i + 1) < orderByFields.length) {
1991                    if (orderByComparator.isAscending() ^ previous) {
1992                        query.append(ORDER_BY_ASC_HAS_NEXT);
1993                    }
1994                    else {
1995                        query.append(ORDER_BY_DESC_HAS_NEXT);
1996                    }
1997                }
1998                else {
1999                    if (orderByComparator.isAscending() ^ previous) {
2000                        query.append(ORDER_BY_ASC);
2001                    }
2002                    else {
2003                        query.append(ORDER_BY_DESC);
2004                    }
2005                }
2006            }
2007        }
2008
2009        String sql = query.toString();
2010
2011        Query q = session.createQuery(sql);
2012
2013        q.setFirstResult(0);
2014        q.setMaxResults(2);
2015
2016        QueryPos qPos = QueryPos.getInstance(q);
2017
2018        qPos.add(userId);
2019
2020        qPos.add(threadId);
2021
2022        qPos.add(flag);
2023
2024        if (orderByComparator != null) {
2025            Object[] values = orderByComparator.getOrderByValues(mbMessageFlag);
2026
2027            for (Object value : values) {
2028                qPos.add(value);
2029            }
2030        }
2031
2032        List<MBMessageFlag> list = q.list();
2033
2034        if (list.size() == 2) {
2035            return list.get(1);
2036        }
2037        else {
2038            return null;
2039        }
2040    }
2041
2042    public MBMessageFlag findByU_M_F(long userId, long messageId, int flag)
2043        throws NoSuchMessageFlagException, SystemException {
2044        MBMessageFlag mbMessageFlag = fetchByU_M_F(userId, messageId, flag);
2045
2046        if (mbMessageFlag == null) {
2047            StringBundler msg = new StringBundler(8);
2048
2049            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2050
2051            msg.append("userId=");
2052            msg.append(userId);
2053
2054            msg.append(", messageId=");
2055            msg.append(messageId);
2056
2057            msg.append(", flag=");
2058            msg.append(flag);
2059
2060            msg.append(StringPool.CLOSE_CURLY_BRACE);
2061
2062            if (_log.isWarnEnabled()) {
2063                _log.warn(msg.toString());
2064            }
2065
2066            throw new NoSuchMessageFlagException(msg.toString());
2067        }
2068
2069        return mbMessageFlag;
2070    }
2071
2072    public MBMessageFlag fetchByU_M_F(long userId, long messageId, int flag)
2073        throws SystemException {
2074        return fetchByU_M_F(userId, messageId, flag, true);
2075    }
2076
2077    public MBMessageFlag fetchByU_M_F(long userId, long messageId, int flag,
2078        boolean retrieveFromCache) throws SystemException {
2079        Object[] finderArgs = new Object[] { userId, messageId, flag };
2080
2081        Object result = null;
2082
2083        if (retrieveFromCache) {
2084            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_M_F,
2085                    finderArgs, this);
2086        }
2087
2088        if (result == null) {
2089            StringBundler query = new StringBundler(4);
2090
2091            query.append(_SQL_SELECT_MBMESSAGEFLAG_WHERE);
2092
2093            query.append(_FINDER_COLUMN_U_M_F_USERID_2);
2094
2095            query.append(_FINDER_COLUMN_U_M_F_MESSAGEID_2);
2096
2097            query.append(_FINDER_COLUMN_U_M_F_FLAG_2);
2098
2099            String sql = query.toString();
2100
2101            Session session = null;
2102
2103            try {
2104                session = openSession();
2105
2106                Query q = session.createQuery(sql);
2107
2108                QueryPos qPos = QueryPos.getInstance(q);
2109
2110                qPos.add(userId);
2111
2112                qPos.add(messageId);
2113
2114                qPos.add(flag);
2115
2116                List<MBMessageFlag> list = q.list();
2117
2118                result = list;
2119
2120                MBMessageFlag mbMessageFlag = null;
2121
2122                if (list.isEmpty()) {
2123                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_M_F,
2124                        finderArgs, list);
2125                }
2126                else {
2127                    mbMessageFlag = list.get(0);
2128
2129                    cacheResult(mbMessageFlag);
2130
2131                    if ((mbMessageFlag.getUserId() != userId) ||
2132                            (mbMessageFlag.getMessageId() != messageId) ||
2133                            (mbMessageFlag.getFlag() != flag)) {
2134                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_M_F,
2135                            finderArgs, mbMessageFlag);
2136                    }
2137                }
2138
2139                return mbMessageFlag;
2140            }
2141            catch (Exception e) {
2142                throw processException(e);
2143            }
2144            finally {
2145                if (result == null) {
2146                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_M_F,
2147                        finderArgs, new ArrayList<MBMessageFlag>());
2148                }
2149
2150                closeSession(session);
2151            }
2152        }
2153        else {
2154            if (result instanceof List<?>) {
2155                return null;
2156            }
2157            else {
2158                return (MBMessageFlag)result;
2159            }
2160        }
2161    }
2162
2163    public List<MBMessageFlag> findAll() throws SystemException {
2164        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2165    }
2166
2167    public List<MBMessageFlag> findAll(int start, int end)
2168        throws SystemException {
2169        return findAll(start, end, null);
2170    }
2171
2172    public List<MBMessageFlag> findAll(int start, int end,
2173        OrderByComparator orderByComparator) throws SystemException {
2174        Object[] finderArgs = new Object[] {
2175                String.valueOf(start), String.valueOf(end),
2176                String.valueOf(orderByComparator)
2177            };
2178
2179        List<MBMessageFlag> list = (List<MBMessageFlag>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
2180                finderArgs, this);
2181
2182        if (list == null) {
2183            StringBundler query = null;
2184            String sql = null;
2185
2186            if (orderByComparator != null) {
2187                query = new StringBundler(2 +
2188                        (orderByComparator.getOrderByFields().length * 3));
2189
2190                query.append(_SQL_SELECT_MBMESSAGEFLAG);
2191
2192                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2193                    orderByComparator);
2194
2195                sql = query.toString();
2196            }
2197            else {
2198                sql = _SQL_SELECT_MBMESSAGEFLAG;
2199            }
2200
2201            Session session = null;
2202
2203            try {
2204                session = openSession();
2205
2206                Query q = session.createQuery(sql);
2207
2208                if (orderByComparator == null) {
2209                    list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
2210                            start, end, false);
2211
2212                    Collections.sort(list);
2213                }
2214                else {
2215                    list = (List<MBMessageFlag>)QueryUtil.list(q, getDialect(),
2216                            start, end);
2217                }
2218            }
2219            catch (Exception e) {
2220                throw processException(e);
2221            }
2222            finally {
2223                if (list == null) {
2224                    list = new ArrayList<MBMessageFlag>();
2225                }
2226
2227                cacheResult(list);
2228
2229                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
2230
2231                closeSession(session);
2232            }
2233        }
2234
2235        return list;
2236    }
2237
2238    public void removeByUserId(long userId) throws SystemException {
2239        for (MBMessageFlag mbMessageFlag : findByUserId(userId)) {
2240            remove(mbMessageFlag);
2241        }
2242    }
2243
2244    public void removeByThreadId(long threadId) throws SystemException {
2245        for (MBMessageFlag mbMessageFlag : findByThreadId(threadId)) {
2246            remove(mbMessageFlag);
2247        }
2248    }
2249
2250    public void removeByMessageId(long messageId) throws SystemException {
2251        for (MBMessageFlag mbMessageFlag : findByMessageId(messageId)) {
2252            remove(mbMessageFlag);
2253        }
2254    }
2255
2256    public void removeByT_F(long threadId, int flag) throws SystemException {
2257        for (MBMessageFlag mbMessageFlag : findByT_F(threadId, flag)) {
2258            remove(mbMessageFlag);
2259        }
2260    }
2261
2262    public void removeByM_F(long messageId, int flag) throws SystemException {
2263        for (MBMessageFlag mbMessageFlag : findByM_F(messageId, flag)) {
2264            remove(mbMessageFlag);
2265        }
2266    }
2267
2268    public void removeByU_T_F(long userId, long threadId, int flag)
2269        throws SystemException {
2270        for (MBMessageFlag mbMessageFlag : findByU_T_F(userId, threadId, flag)) {
2271            remove(mbMessageFlag);
2272        }
2273    }
2274
2275    public void removeByU_M_F(long userId, long messageId, int flag)
2276        throws NoSuchMessageFlagException, SystemException {
2277        MBMessageFlag mbMessageFlag = findByU_M_F(userId, messageId, flag);
2278
2279        remove(mbMessageFlag);
2280    }
2281
2282    public void removeAll() throws SystemException {
2283        for (MBMessageFlag mbMessageFlag : findAll()) {
2284            remove(mbMessageFlag);
2285        }
2286    }
2287
2288    public int countByUserId(long userId) throws SystemException {
2289        Object[] finderArgs = new Object[] { userId };
2290
2291        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
2292                finderArgs, this);
2293
2294        if (count == null) {
2295            StringBundler query = new StringBundler(2);
2296
2297            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2298
2299            query.append(_FINDER_COLUMN_USERID_USERID_2);
2300
2301            String sql = query.toString();
2302
2303            Session session = null;
2304
2305            try {
2306                session = openSession();
2307
2308                Query q = session.createQuery(sql);
2309
2310                QueryPos qPos = QueryPos.getInstance(q);
2311
2312                qPos.add(userId);
2313
2314                count = (Long)q.uniqueResult();
2315            }
2316            catch (Exception e) {
2317                throw processException(e);
2318            }
2319            finally {
2320                if (count == null) {
2321                    count = Long.valueOf(0);
2322                }
2323
2324                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
2325                    finderArgs, count);
2326
2327                closeSession(session);
2328            }
2329        }
2330
2331        return count.intValue();
2332    }
2333
2334    public int countByThreadId(long threadId) throws SystemException {
2335        Object[] finderArgs = new Object[] { threadId };
2336
2337        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
2338                finderArgs, this);
2339
2340        if (count == null) {
2341            StringBundler query = new StringBundler(2);
2342
2343            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2344
2345            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
2346
2347            String sql = query.toString();
2348
2349            Session session = null;
2350
2351            try {
2352                session = openSession();
2353
2354                Query q = session.createQuery(sql);
2355
2356                QueryPos qPos = QueryPos.getInstance(q);
2357
2358                qPos.add(threadId);
2359
2360                count = (Long)q.uniqueResult();
2361            }
2362            catch (Exception e) {
2363                throw processException(e);
2364            }
2365            finally {
2366                if (count == null) {
2367                    count = Long.valueOf(0);
2368                }
2369
2370                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
2371                    finderArgs, count);
2372
2373                closeSession(session);
2374            }
2375        }
2376
2377        return count.intValue();
2378    }
2379
2380    public int countByMessageId(long messageId) throws SystemException {
2381        Object[] finderArgs = new Object[] { messageId };
2382
2383        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_MESSAGEID,
2384                finderArgs, this);
2385
2386        if (count == null) {
2387            StringBundler query = new StringBundler(2);
2388
2389            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2390
2391            query.append(_FINDER_COLUMN_MESSAGEID_MESSAGEID_2);
2392
2393            String sql = query.toString();
2394
2395            Session session = null;
2396
2397            try {
2398                session = openSession();
2399
2400                Query q = session.createQuery(sql);
2401
2402                QueryPos qPos = QueryPos.getInstance(q);
2403
2404                qPos.add(messageId);
2405
2406                count = (Long)q.uniqueResult();
2407            }
2408            catch (Exception e) {
2409                throw processException(e);
2410            }
2411            finally {
2412                if (count == null) {
2413                    count = Long.valueOf(0);
2414                }
2415
2416                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_MESSAGEID,
2417                    finderArgs, count);
2418
2419                closeSession(session);
2420            }
2421        }
2422
2423        return count.intValue();
2424    }
2425
2426    public int countByT_F(long threadId, int flag) throws SystemException {
2427        Object[] finderArgs = new Object[] { threadId, flag };
2428
2429        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_F,
2430                finderArgs, this);
2431
2432        if (count == null) {
2433            StringBundler query = new StringBundler(3);
2434
2435            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2436
2437            query.append(_FINDER_COLUMN_T_F_THREADID_2);
2438
2439            query.append(_FINDER_COLUMN_T_F_FLAG_2);
2440
2441            String sql = query.toString();
2442
2443            Session session = null;
2444
2445            try {
2446                session = openSession();
2447
2448                Query q = session.createQuery(sql);
2449
2450                QueryPos qPos = QueryPos.getInstance(q);
2451
2452                qPos.add(threadId);
2453
2454                qPos.add(flag);
2455
2456                count = (Long)q.uniqueResult();
2457            }
2458            catch (Exception e) {
2459                throw processException(e);
2460            }
2461            finally {
2462                if (count == null) {
2463                    count = Long.valueOf(0);
2464                }
2465
2466                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_F, finderArgs,
2467                    count);
2468
2469                closeSession(session);
2470            }
2471        }
2472
2473        return count.intValue();
2474    }
2475
2476    public int countByM_F(long messageId, int flag) throws SystemException {
2477        Object[] finderArgs = new Object[] { messageId, flag };
2478
2479        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_M_F,
2480                finderArgs, this);
2481
2482        if (count == null) {
2483            StringBundler query = new StringBundler(3);
2484
2485            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2486
2487            query.append(_FINDER_COLUMN_M_F_MESSAGEID_2);
2488
2489            query.append(_FINDER_COLUMN_M_F_FLAG_2);
2490
2491            String sql = query.toString();
2492
2493            Session session = null;
2494
2495            try {
2496                session = openSession();
2497
2498                Query q = session.createQuery(sql);
2499
2500                QueryPos qPos = QueryPos.getInstance(q);
2501
2502                qPos.add(messageId);
2503
2504                qPos.add(flag);
2505
2506                count = (Long)q.uniqueResult();
2507            }
2508            catch (Exception e) {
2509                throw processException(e);
2510            }
2511            finally {
2512                if (count == null) {
2513                    count = Long.valueOf(0);
2514                }
2515
2516                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_M_F, finderArgs,
2517                    count);
2518
2519                closeSession(session);
2520            }
2521        }
2522
2523        return count.intValue();
2524    }
2525
2526    public int countByU_T_F(long userId, long threadId, int flag)
2527        throws SystemException {
2528        Object[] finderArgs = new Object[] { userId, threadId, flag };
2529
2530        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_T_F,
2531                finderArgs, this);
2532
2533        if (count == null) {
2534            StringBundler query = new StringBundler(4);
2535
2536            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2537
2538            query.append(_FINDER_COLUMN_U_T_F_USERID_2);
2539
2540            query.append(_FINDER_COLUMN_U_T_F_THREADID_2);
2541
2542            query.append(_FINDER_COLUMN_U_T_F_FLAG_2);
2543
2544            String sql = query.toString();
2545
2546            Session session = null;
2547
2548            try {
2549                session = openSession();
2550
2551                Query q = session.createQuery(sql);
2552
2553                QueryPos qPos = QueryPos.getInstance(q);
2554
2555                qPos.add(userId);
2556
2557                qPos.add(threadId);
2558
2559                qPos.add(flag);
2560
2561                count = (Long)q.uniqueResult();
2562            }
2563            catch (Exception e) {
2564                throw processException(e);
2565            }
2566            finally {
2567                if (count == null) {
2568                    count = Long.valueOf(0);
2569                }
2570
2571                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_T_F,
2572                    finderArgs, count);
2573
2574                closeSession(session);
2575            }
2576        }
2577
2578        return count.intValue();
2579    }
2580
2581    public int countByU_M_F(long userId, long messageId, int flag)
2582        throws SystemException {
2583        Object[] finderArgs = new Object[] { userId, messageId, flag };
2584
2585        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_M_F,
2586                finderArgs, this);
2587
2588        if (count == null) {
2589            StringBundler query = new StringBundler(4);
2590
2591            query.append(_SQL_COUNT_MBMESSAGEFLAG_WHERE);
2592
2593            query.append(_FINDER_COLUMN_U_M_F_USERID_2);
2594
2595            query.append(_FINDER_COLUMN_U_M_F_MESSAGEID_2);
2596
2597            query.append(_FINDER_COLUMN_U_M_F_FLAG_2);
2598
2599            String sql = query.toString();
2600
2601            Session session = null;
2602
2603            try {
2604                session = openSession();
2605
2606                Query q = session.createQuery(sql);
2607
2608                QueryPos qPos = QueryPos.getInstance(q);
2609
2610                qPos.add(userId);
2611
2612                qPos.add(messageId);
2613
2614                qPos.add(flag);
2615
2616                count = (Long)q.uniqueResult();
2617            }
2618            catch (Exception e) {
2619                throw processException(e);
2620            }
2621            finally {
2622                if (count == null) {
2623                    count = Long.valueOf(0);
2624                }
2625
2626                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_M_F,
2627                    finderArgs, count);
2628
2629                closeSession(session);
2630            }
2631        }
2632
2633        return count.intValue();
2634    }
2635
2636    public int countAll() throws SystemException {
2637        Object[] finderArgs = new Object[0];
2638
2639        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2640                finderArgs, this);
2641
2642        if (count == null) {
2643            Session session = null;
2644
2645            try {
2646                session = openSession();
2647
2648                Query q = session.createQuery(_SQL_COUNT_MBMESSAGEFLAG);
2649
2650                count = (Long)q.uniqueResult();
2651            }
2652            catch (Exception e) {
2653                throw processException(e);
2654            }
2655            finally {
2656                if (count == null) {
2657                    count = Long.valueOf(0);
2658                }
2659
2660                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
2661                    count);
2662
2663                closeSession(session);
2664            }
2665        }
2666
2667        return count.intValue();
2668    }
2669
2670    public void afterPropertiesSet() {
2671        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2672                    com.liferay.portal.util.PropsUtil.get(
2673                        "value.object.listener.com.liferay.portlet.messageboards.model.MBMessageFlag")));
2674
2675        if (listenerClassNames.length > 0) {
2676            try {
2677                List<ModelListener<MBMessageFlag>> listenersList = new ArrayList<ModelListener<MBMessageFlag>>();
2678
2679                for (String listenerClassName : listenerClassNames) {
2680                    listenersList.add((ModelListener<MBMessageFlag>)InstanceFactory.newInstance(
2681                            listenerClassName));
2682                }
2683
2684                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2685            }
2686            catch (Exception e) {
2687                _log.error(e);
2688            }
2689        }
2690    }
2691
2692    public void destroy() {
2693        EntityCacheUtil.removeCache(MBMessageFlagImpl.class.getName());
2694        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2695        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
2696    }
2697
2698    @BeanReference(type = MBBanPersistence.class)
2699    protected MBBanPersistence mbBanPersistence;
2700    @BeanReference(type = MBCategoryPersistence.class)
2701    protected MBCategoryPersistence mbCategoryPersistence;
2702    @BeanReference(type = MBDiscussionPersistence.class)
2703    protected MBDiscussionPersistence mbDiscussionPersistence;
2704    @BeanReference(type = MBMailingListPersistence.class)
2705    protected MBMailingListPersistence mbMailingListPersistence;
2706    @BeanReference(type = MBMessagePersistence.class)
2707    protected MBMessagePersistence mbMessagePersistence;
2708    @BeanReference(type = MBMessageFlagPersistence.class)
2709    protected MBMessageFlagPersistence mbMessageFlagPersistence;
2710    @BeanReference(type = MBStatsUserPersistence.class)
2711    protected MBStatsUserPersistence mbStatsUserPersistence;
2712    @BeanReference(type = MBThreadPersistence.class)
2713    protected MBThreadPersistence mbThreadPersistence;
2714    @BeanReference(type = ResourcePersistence.class)
2715    protected ResourcePersistence resourcePersistence;
2716    @BeanReference(type = UserPersistence.class)
2717    protected UserPersistence userPersistence;
2718    private static final String _SQL_SELECT_MBMESSAGEFLAG = "SELECT mbMessageFlag FROM MBMessageFlag mbMessageFlag";
2719    private static final String _SQL_SELECT_MBMESSAGEFLAG_WHERE = "SELECT mbMessageFlag FROM MBMessageFlag mbMessageFlag WHERE ";
2720    private static final String _SQL_COUNT_MBMESSAGEFLAG = "SELECT COUNT(mbMessageFlag) FROM MBMessageFlag mbMessageFlag";
2721    private static final String _SQL_COUNT_MBMESSAGEFLAG_WHERE = "SELECT COUNT(mbMessageFlag) FROM MBMessageFlag mbMessageFlag WHERE ";
2722    private static final String _FINDER_COLUMN_USERID_USERID_2 = "mbMessageFlag.userId = ?";
2723    private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbMessageFlag.threadId = ?";
2724    private static final String _FINDER_COLUMN_MESSAGEID_MESSAGEID_2 = "mbMessageFlag.messageId = ?";
2725    private static final String _FINDER_COLUMN_T_F_THREADID_2 = "mbMessageFlag.threadId = ? AND ";
2726    private static final String _FINDER_COLUMN_T_F_FLAG_2 = "mbMessageFlag.flag = ?";
2727    private static final String _FINDER_COLUMN_M_F_MESSAGEID_2 = "mbMessageFlag.messageId = ? AND ";
2728    private static final String _FINDER_COLUMN_M_F_FLAG_2 = "mbMessageFlag.flag = ?";
2729    private static final String _FINDER_COLUMN_U_T_F_USERID_2 = "mbMessageFlag.userId = ? AND ";
2730    private static final String _FINDER_COLUMN_U_T_F_THREADID_2 = "mbMessageFlag.threadId = ? AND ";
2731    private static final String _FINDER_COLUMN_U_T_F_FLAG_2 = "mbMessageFlag.flag = ?";
2732    private static final String _FINDER_COLUMN_U_M_F_USERID_2 = "mbMessageFlag.userId = ? AND ";
2733    private static final String _FINDER_COLUMN_U_M_F_MESSAGEID_2 = "mbMessageFlag.messageId = ? AND ";
2734    private static final String _FINDER_COLUMN_U_M_F_FLAG_2 = "mbMessageFlag.flag = ?";
2735    private static final String _ORDER_BY_ENTITY_ALIAS = "mbMessageFlag.";
2736    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBMessageFlag exists with the primary key ";
2737    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBMessageFlag exists with the key {";
2738    private static Log _log = LogFactoryUtil.getLog(MBMessageFlagPersistenceImpl.class);
2739}