1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  import com.liferay.portal.util.PropsUtil;
38  
39  import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
40  import com.liferay.portlet.messageboards.model.MBMessageFlag;
41  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
42  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagModelImpl;
43  
44  import com.liferay.util.dao.hibernate.QueryUtil;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.hibernate.Query;
50  import org.hibernate.Session;
51  
52  import java.util.Collections;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  /**
57   * <a href="MBMessageFlagPersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class MBMessageFlagPersistenceImpl extends BasePersistence
63      implements MBMessageFlagPersistence {
64      public MBMessageFlag create(long messageFlagId) {
65          MBMessageFlag mbMessageFlag = new MBMessageFlagImpl();
66  
67          mbMessageFlag.setNew(true);
68          mbMessageFlag.setPrimaryKey(messageFlagId);
69  
70          return mbMessageFlag;
71      }
72  
73      public MBMessageFlag remove(long messageFlagId)
74          throws NoSuchMessageFlagException, SystemException {
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              MBMessageFlag mbMessageFlag = (MBMessageFlag)session.get(MBMessageFlagImpl.class,
81                      new Long(messageFlagId));
82  
83              if (mbMessageFlag == null) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn("No MBMessageFlag exists with the primary key " +
86                          messageFlagId);
87                  }
88  
89                  throw new NoSuchMessageFlagException(
90                      "No MBMessageFlag exists with the primary key " +
91                      messageFlagId);
92              }
93  
94              return remove(mbMessageFlag);
95          }
96          catch (NoSuchMessageFlagException nsee) {
97              throw nsee;
98          }
99          catch (Exception e) {
100             throw HibernateUtil.processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public MBMessageFlag remove(MBMessageFlag mbMessageFlag)
108         throws SystemException {
109         ModelListener listener = _getListener();
110 
111         if (listener != null) {
112             listener.onBeforeRemove(mbMessageFlag);
113         }
114 
115         mbMessageFlag = removeImpl(mbMessageFlag);
116 
117         if (listener != null) {
118             listener.onAfterRemove(mbMessageFlag);
119         }
120 
121         return mbMessageFlag;
122     }
123 
124     protected MBMessageFlag removeImpl(MBMessageFlag mbMessageFlag)
125         throws SystemException {
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             session.delete(mbMessageFlag);
132 
133             session.flush();
134 
135             return mbMessageFlag;
136         }
137         catch (Exception e) {
138             throw HibernateUtil.processException(e);
139         }
140         finally {
141             closeSession(session);
142 
143             FinderCache.clearCache(MBMessageFlag.class.getName());
144         }
145     }
146 
147     public MBMessageFlag update(MBMessageFlag mbMessageFlag)
148         throws SystemException {
149         return update(mbMessageFlag, false);
150     }
151 
152     public MBMessageFlag update(MBMessageFlag mbMessageFlag, boolean merge)
153         throws SystemException {
154         ModelListener listener = _getListener();
155 
156         boolean isNew = mbMessageFlag.isNew();
157 
158         if (listener != null) {
159             if (isNew) {
160                 listener.onBeforeCreate(mbMessageFlag);
161             }
162             else {
163                 listener.onBeforeUpdate(mbMessageFlag);
164             }
165         }
166 
167         mbMessageFlag = updateImpl(mbMessageFlag, merge);
168 
169         if (listener != null) {
170             if (isNew) {
171                 listener.onAfterCreate(mbMessageFlag);
172             }
173             else {
174                 listener.onAfterUpdate(mbMessageFlag);
175             }
176         }
177 
178         return mbMessageFlag;
179     }
180 
181     public MBMessageFlag updateImpl(
182         com.liferay.portlet.messageboards.model.MBMessageFlag mbMessageFlag,
183         boolean merge) throws SystemException {
184         Session session = null;
185 
186         try {
187             session = openSession();
188 
189             if (merge) {
190                 session.merge(mbMessageFlag);
191             }
192             else {
193                 if (mbMessageFlag.isNew()) {
194                     session.save(mbMessageFlag);
195                 }
196             }
197 
198             session.flush();
199 
200             mbMessageFlag.setNew(false);
201 
202             return mbMessageFlag;
203         }
204         catch (Exception e) {
205             throw HibernateUtil.processException(e);
206         }
207         finally {
208             closeSession(session);
209 
210             FinderCache.clearCache(MBMessageFlag.class.getName());
211         }
212     }
213 
214     public MBMessageFlag findByPrimaryKey(long messageFlagId)
215         throws NoSuchMessageFlagException, SystemException {
216         MBMessageFlag mbMessageFlag = fetchByPrimaryKey(messageFlagId);
217 
218         if (mbMessageFlag == null) {
219             if (_log.isWarnEnabled()) {
220                 _log.warn("No MBMessageFlag exists with the primary key " +
221                     messageFlagId);
222             }
223 
224             throw new NoSuchMessageFlagException(
225                 "No MBMessageFlag exists with the primary key " +
226                 messageFlagId);
227         }
228 
229         return mbMessageFlag;
230     }
231 
232     public MBMessageFlag fetchByPrimaryKey(long messageFlagId)
233         throws SystemException {
234         Session session = null;
235 
236         try {
237             session = openSession();
238 
239             return (MBMessageFlag)session.get(MBMessageFlagImpl.class,
240                 new Long(messageFlagId));
241         }
242         catch (Exception e) {
243             throw HibernateUtil.processException(e);
244         }
245         finally {
246             closeSession(session);
247         }
248     }
249 
250     public List findByUserId(long userId) throws SystemException {
251         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
252         String finderClassName = MBMessageFlag.class.getName();
253         String finderMethodName = "findByUserId";
254         String[] finderParams = new String[] { Long.class.getName() };
255         Object[] finderArgs = new Object[] { new Long(userId) };
256 
257         Object result = null;
258 
259         if (finderClassNameCacheEnabled) {
260             result = FinderCache.getResult(finderClassName, finderMethodName,
261                     finderParams, finderArgs, getSessionFactory());
262         }
263 
264         if (result == null) {
265             Session session = null;
266 
267             try {
268                 session = openSession();
269 
270                 StringMaker query = new StringMaker();
271 
272                 query.append(
273                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
274 
275                 query.append("userId = ?");
276 
277                 query.append(" ");
278 
279                 Query q = session.createQuery(query.toString());
280 
281                 int queryPos = 0;
282 
283                 q.setLong(queryPos++, userId);
284 
285                 List list = q.list();
286 
287                 FinderCache.putResult(finderClassNameCacheEnabled,
288                     finderClassName, finderMethodName, finderParams,
289                     finderArgs, list);
290 
291                 return list;
292             }
293             catch (Exception e) {
294                 throw HibernateUtil.processException(e);
295             }
296             finally {
297                 closeSession(session);
298             }
299         }
300         else {
301             return (List)result;
302         }
303     }
304 
305     public List findByUserId(long userId, int begin, int end)
306         throws SystemException {
307         return findByUserId(userId, begin, end, null);
308     }
309 
310     public List findByUserId(long userId, int begin, int end,
311         OrderByComparator obc) throws SystemException {
312         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
313         String finderClassName = MBMessageFlag.class.getName();
314         String finderMethodName = "findByUserId";
315         String[] finderParams = new String[] {
316                 Long.class.getName(),
317                 
318                 "java.lang.Integer", "java.lang.Integer",
319                 "com.liferay.portal.kernel.util.OrderByComparator"
320             };
321         Object[] finderArgs = new Object[] {
322                 new Long(userId),
323                 
324                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
325             };
326 
327         Object result = null;
328 
329         if (finderClassNameCacheEnabled) {
330             result = FinderCache.getResult(finderClassName, finderMethodName,
331                     finderParams, finderArgs, getSessionFactory());
332         }
333 
334         if (result == null) {
335             Session session = null;
336 
337             try {
338                 session = openSession();
339 
340                 StringMaker query = new StringMaker();
341 
342                 query.append(
343                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
344 
345                 query.append("userId = ?");
346 
347                 query.append(" ");
348 
349                 if (obc != null) {
350                     query.append("ORDER BY ");
351                     query.append(obc.getOrderBy());
352                 }
353 
354                 Query q = session.createQuery(query.toString());
355 
356                 int queryPos = 0;
357 
358                 q.setLong(queryPos++, userId);
359 
360                 List list = QueryUtil.list(q, getDialect(), begin, end);
361 
362                 FinderCache.putResult(finderClassNameCacheEnabled,
363                     finderClassName, finderMethodName, finderParams,
364                     finderArgs, list);
365 
366                 return list;
367             }
368             catch (Exception e) {
369                 throw HibernateUtil.processException(e);
370             }
371             finally {
372                 closeSession(session);
373             }
374         }
375         else {
376             return (List)result;
377         }
378     }
379 
380     public MBMessageFlag findByUserId_First(long userId, OrderByComparator obc)
381         throws NoSuchMessageFlagException, SystemException {
382         List list = findByUserId(userId, 0, 1, obc);
383 
384         if (list.size() == 0) {
385             StringMaker msg = new StringMaker();
386 
387             msg.append("No MBMessageFlag exists with the key {");
388 
389             msg.append("userId=" + userId);
390 
391             msg.append(StringPool.CLOSE_CURLY_BRACE);
392 
393             throw new NoSuchMessageFlagException(msg.toString());
394         }
395         else {
396             return (MBMessageFlag)list.get(0);
397         }
398     }
399 
400     public MBMessageFlag findByUserId_Last(long userId, OrderByComparator obc)
401         throws NoSuchMessageFlagException, SystemException {
402         int count = countByUserId(userId);
403 
404         List list = findByUserId(userId, count - 1, count, obc);
405 
406         if (list.size() == 0) {
407             StringMaker msg = new StringMaker();
408 
409             msg.append("No MBMessageFlag exists with the key {");
410 
411             msg.append("userId=" + userId);
412 
413             msg.append(StringPool.CLOSE_CURLY_BRACE);
414 
415             throw new NoSuchMessageFlagException(msg.toString());
416         }
417         else {
418             return (MBMessageFlag)list.get(0);
419         }
420     }
421 
422     public MBMessageFlag[] findByUserId_PrevAndNext(long messageFlagId,
423         long userId, OrderByComparator obc)
424         throws NoSuchMessageFlagException, SystemException {
425         MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
426 
427         int count = countByUserId(userId);
428 
429         Session session = null;
430 
431         try {
432             session = openSession();
433 
434             StringMaker query = new StringMaker();
435 
436             query.append(
437                 "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
438 
439             query.append("userId = ?");
440 
441             query.append(" ");
442 
443             if (obc != null) {
444                 query.append("ORDER BY ");
445                 query.append(obc.getOrderBy());
446             }
447 
448             Query q = session.createQuery(query.toString());
449 
450             int queryPos = 0;
451 
452             q.setLong(queryPos++, userId);
453 
454             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
455                     mbMessageFlag);
456 
457             MBMessageFlag[] array = new MBMessageFlagImpl[3];
458 
459             array[0] = (MBMessageFlag)objArray[0];
460             array[1] = (MBMessageFlag)objArray[1];
461             array[2] = (MBMessageFlag)objArray[2];
462 
463             return array;
464         }
465         catch (Exception e) {
466             throw HibernateUtil.processException(e);
467         }
468         finally {
469             closeSession(session);
470         }
471     }
472 
473     public List findByMessageId(long messageId) throws SystemException {
474         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
475         String finderClassName = MBMessageFlag.class.getName();
476         String finderMethodName = "findByMessageId";
477         String[] finderParams = new String[] { Long.class.getName() };
478         Object[] finderArgs = new Object[] { new Long(messageId) };
479 
480         Object result = null;
481 
482         if (finderClassNameCacheEnabled) {
483             result = FinderCache.getResult(finderClassName, finderMethodName,
484                     finderParams, finderArgs, getSessionFactory());
485         }
486 
487         if (result == null) {
488             Session session = null;
489 
490             try {
491                 session = openSession();
492 
493                 StringMaker query = new StringMaker();
494 
495                 query.append(
496                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
497 
498                 query.append("messageId = ?");
499 
500                 query.append(" ");
501 
502                 Query q = session.createQuery(query.toString());
503 
504                 int queryPos = 0;
505 
506                 q.setLong(queryPos++, messageId);
507 
508                 List list = q.list();
509 
510                 FinderCache.putResult(finderClassNameCacheEnabled,
511                     finderClassName, finderMethodName, finderParams,
512                     finderArgs, list);
513 
514                 return list;
515             }
516             catch (Exception e) {
517                 throw HibernateUtil.processException(e);
518             }
519             finally {
520                 closeSession(session);
521             }
522         }
523         else {
524             return (List)result;
525         }
526     }
527 
528     public List findByMessageId(long messageId, int begin, int end)
529         throws SystemException {
530         return findByMessageId(messageId, begin, end, null);
531     }
532 
533     public List findByMessageId(long messageId, int begin, int end,
534         OrderByComparator obc) throws SystemException {
535         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
536         String finderClassName = MBMessageFlag.class.getName();
537         String finderMethodName = "findByMessageId";
538         String[] finderParams = new String[] {
539                 Long.class.getName(),
540                 
541                 "java.lang.Integer", "java.lang.Integer",
542                 "com.liferay.portal.kernel.util.OrderByComparator"
543             };
544         Object[] finderArgs = new Object[] {
545                 new Long(messageId),
546                 
547                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
548             };
549 
550         Object result = null;
551 
552         if (finderClassNameCacheEnabled) {
553             result = FinderCache.getResult(finderClassName, finderMethodName,
554                     finderParams, finderArgs, getSessionFactory());
555         }
556 
557         if (result == null) {
558             Session session = null;
559 
560             try {
561                 session = openSession();
562 
563                 StringMaker query = new StringMaker();
564 
565                 query.append(
566                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
567 
568                 query.append("messageId = ?");
569 
570                 query.append(" ");
571 
572                 if (obc != null) {
573                     query.append("ORDER BY ");
574                     query.append(obc.getOrderBy());
575                 }
576 
577                 Query q = session.createQuery(query.toString());
578 
579                 int queryPos = 0;
580 
581                 q.setLong(queryPos++, messageId);
582 
583                 List list = QueryUtil.list(q, getDialect(), begin, end);
584 
585                 FinderCache.putResult(finderClassNameCacheEnabled,
586                     finderClassName, finderMethodName, finderParams,
587                     finderArgs, list);
588 
589                 return list;
590             }
591             catch (Exception e) {
592                 throw HibernateUtil.processException(e);
593             }
594             finally {
595                 closeSession(session);
596             }
597         }
598         else {
599             return (List)result;
600         }
601     }
602 
603     public MBMessageFlag findByMessageId_First(long messageId,
604         OrderByComparator obc)
605         throws NoSuchMessageFlagException, SystemException {
606         List list = findByMessageId(messageId, 0, 1, obc);
607 
608         if (list.size() == 0) {
609             StringMaker msg = new StringMaker();
610 
611             msg.append("No MBMessageFlag exists with the key {");
612 
613             msg.append("messageId=" + messageId);
614 
615             msg.append(StringPool.CLOSE_CURLY_BRACE);
616 
617             throw new NoSuchMessageFlagException(msg.toString());
618         }
619         else {
620             return (MBMessageFlag)list.get(0);
621         }
622     }
623 
624     public MBMessageFlag findByMessageId_Last(long messageId,
625         OrderByComparator obc)
626         throws NoSuchMessageFlagException, SystemException {
627         int count = countByMessageId(messageId);
628 
629         List list = findByMessageId(messageId, count - 1, count, obc);
630 
631         if (list.size() == 0) {
632             StringMaker msg = new StringMaker();
633 
634             msg.append("No MBMessageFlag exists with the key {");
635 
636             msg.append("messageId=" + messageId);
637 
638             msg.append(StringPool.CLOSE_CURLY_BRACE);
639 
640             throw new NoSuchMessageFlagException(msg.toString());
641         }
642         else {
643             return (MBMessageFlag)list.get(0);
644         }
645     }
646 
647     public MBMessageFlag[] findByMessageId_PrevAndNext(long messageFlagId,
648         long messageId, OrderByComparator obc)
649         throws NoSuchMessageFlagException, SystemException {
650         MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
651 
652         int count = countByMessageId(messageId);
653 
654         Session session = null;
655 
656         try {
657             session = openSession();
658 
659             StringMaker query = new StringMaker();
660 
661             query.append(
662                 "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
663 
664             query.append("messageId = ?");
665 
666             query.append(" ");
667 
668             if (obc != null) {
669                 query.append("ORDER BY ");
670                 query.append(obc.getOrderBy());
671             }
672 
673             Query q = session.createQuery(query.toString());
674 
675             int queryPos = 0;
676 
677             q.setLong(queryPos++, messageId);
678 
679             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
680                     mbMessageFlag);
681 
682             MBMessageFlag[] array = new MBMessageFlagImpl[3];
683 
684             array[0] = (MBMessageFlag)objArray[0];
685             array[1] = (MBMessageFlag)objArray[1];
686             array[2] = (MBMessageFlag)objArray[2];
687 
688             return array;
689         }
690         catch (Exception e) {
691             throw HibernateUtil.processException(e);
692         }
693         finally {
694             closeSession(session);
695         }
696     }
697 
698     public MBMessageFlag findByU_M(long userId, long messageId)
699         throws NoSuchMessageFlagException, SystemException {
700         MBMessageFlag mbMessageFlag = fetchByU_M(userId, messageId);
701 
702         if (mbMessageFlag == null) {
703             StringMaker msg = new StringMaker();
704 
705             msg.append("No MBMessageFlag exists with the key {");
706 
707             msg.append("userId=" + userId);
708 
709             msg.append(", ");
710             msg.append("messageId=" + messageId);
711 
712             msg.append(StringPool.CLOSE_CURLY_BRACE);
713 
714             if (_log.isWarnEnabled()) {
715                 _log.warn(msg.toString());
716             }
717 
718             throw new NoSuchMessageFlagException(msg.toString());
719         }
720 
721         return mbMessageFlag;
722     }
723 
724     public MBMessageFlag fetchByU_M(long userId, long messageId)
725         throws SystemException {
726         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
727         String finderClassName = MBMessageFlag.class.getName();
728         String finderMethodName = "fetchByU_M";
729         String[] finderParams = new String[] {
730                 Long.class.getName(), Long.class.getName()
731             };
732         Object[] finderArgs = new Object[] { new Long(userId), new Long(messageId) };
733 
734         Object result = null;
735 
736         if (finderClassNameCacheEnabled) {
737             result = FinderCache.getResult(finderClassName, finderMethodName,
738                     finderParams, finderArgs, getSessionFactory());
739         }
740 
741         if (result == null) {
742             Session session = null;
743 
744             try {
745                 session = openSession();
746 
747                 StringMaker query = new StringMaker();
748 
749                 query.append(
750                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
751 
752                 query.append("userId = ?");
753 
754                 query.append(" AND ");
755 
756                 query.append("messageId = ?");
757 
758                 query.append(" ");
759 
760                 Query q = session.createQuery(query.toString());
761 
762                 int queryPos = 0;
763 
764                 q.setLong(queryPos++, userId);
765 
766                 q.setLong(queryPos++, messageId);
767 
768                 List list = q.list();
769 
770                 FinderCache.putResult(finderClassNameCacheEnabled,
771                     finderClassName, finderMethodName, finderParams,
772                     finderArgs, list);
773 
774                 if (list.size() == 0) {
775                     return null;
776                 }
777                 else {
778                     return (MBMessageFlag)list.get(0);
779                 }
780             }
781             catch (Exception e) {
782                 throw HibernateUtil.processException(e);
783             }
784             finally {
785                 closeSession(session);
786             }
787         }
788         else {
789             List list = (List)result;
790 
791             if (list.size() == 0) {
792                 return null;
793             }
794             else {
795                 return (MBMessageFlag)list.get(0);
796             }
797         }
798     }
799 
800     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
801         throws SystemException {
802         Session session = null;
803 
804         try {
805             session = openSession();
806 
807             DynamicQuery query = queryInitializer.initialize(session);
808 
809             return query.list();
810         }
811         catch (Exception e) {
812             throw HibernateUtil.processException(e);
813         }
814         finally {
815             closeSession(session);
816         }
817     }
818 
819     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
820         int begin, int end) throws SystemException {
821         Session session = null;
822 
823         try {
824             session = openSession();
825 
826             DynamicQuery query = queryInitializer.initialize(session);
827 
828             query.setLimit(begin, end);
829 
830             return query.list();
831         }
832         catch (Exception e) {
833             throw HibernateUtil.processException(e);
834         }
835         finally {
836             closeSession(session);
837         }
838     }
839 
840     public List findAll() throws SystemException {
841         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
842     }
843 
844     public List findAll(int begin, int end) throws SystemException {
845         return findAll(begin, end, null);
846     }
847 
848     public List findAll(int begin, int end, OrderByComparator obc)
849         throws SystemException {
850         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
851         String finderClassName = MBMessageFlag.class.getName();
852         String finderMethodName = "findAll";
853         String[] finderParams = new String[] {
854                 "java.lang.Integer", "java.lang.Integer",
855                 "com.liferay.portal.kernel.util.OrderByComparator"
856             };
857         Object[] finderArgs = new Object[] {
858                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
859             };
860 
861         Object result = null;
862 
863         if (finderClassNameCacheEnabled) {
864             result = FinderCache.getResult(finderClassName, finderMethodName,
865                     finderParams, finderArgs, getSessionFactory());
866         }
867 
868         if (result == null) {
869             Session session = null;
870 
871             try {
872                 session = openSession();
873 
874                 StringMaker query = new StringMaker();
875 
876                 query.append(
877                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag ");
878 
879                 if (obc != null) {
880                     query.append("ORDER BY ");
881                     query.append(obc.getOrderBy());
882                 }
883 
884                 Query q = session.createQuery(query.toString());
885 
886                 List list = QueryUtil.list(q, getDialect(), begin, end);
887 
888                 if (obc == null) {
889                     Collections.sort(list);
890                 }
891 
892                 FinderCache.putResult(finderClassNameCacheEnabled,
893                     finderClassName, finderMethodName, finderParams,
894                     finderArgs, list);
895 
896                 return list;
897             }
898             catch (Exception e) {
899                 throw HibernateUtil.processException(e);
900             }
901             finally {
902                 closeSession(session);
903             }
904         }
905         else {
906             return (List)result;
907         }
908     }
909 
910     public void removeByUserId(long userId) throws SystemException {
911         Iterator itr = findByUserId(userId).iterator();
912 
913         while (itr.hasNext()) {
914             MBMessageFlag mbMessageFlag = (MBMessageFlag)itr.next();
915 
916             remove(mbMessageFlag);
917         }
918     }
919 
920     public void removeByMessageId(long messageId) throws SystemException {
921         Iterator itr = findByMessageId(messageId).iterator();
922 
923         while (itr.hasNext()) {
924             MBMessageFlag mbMessageFlag = (MBMessageFlag)itr.next();
925 
926             remove(mbMessageFlag);
927         }
928     }
929 
930     public void removeByU_M(long userId, long messageId)
931         throws NoSuchMessageFlagException, SystemException {
932         MBMessageFlag mbMessageFlag = findByU_M(userId, messageId);
933 
934         remove(mbMessageFlag);
935     }
936 
937     public void removeAll() throws SystemException {
938         Iterator itr = findAll().iterator();
939 
940         while (itr.hasNext()) {
941             remove((MBMessageFlag)itr.next());
942         }
943     }
944 
945     public int countByUserId(long userId) throws SystemException {
946         boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
947         String finderClassName = MBMessageFlag.class.getName();
948         String finderMethodName = "countByUserId";
949         String[] finderParams = new String[] { Long.class.getName() };
950         Object[] finderArgs = new Object[] { new Long(userId) };
951 
952         Object result = null;
953 
954         if (finderClassNameCacheEnabled) {
955             result = FinderCache.getResult(finderClassName, finderMethodName,
956                     finderParams, finderArgs, getSessionFactory());
957         }
958 
959         if (result == null) {
960             Session session = null;
961 
962             try {
963                 session = openSession();
964 
965                 StringMaker query = new StringMaker();
966 
967                 query.append("SELECT COUNT(*) ");
968                 query.append(
969                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
970 
971                 query.append("userId = ?");
972 
973                 query.append(" ");
974 
975                 Query q = session.createQuery(query.toString());
976 
977                 int queryPos = 0;
978 
979                 q.setLong(queryPos++, userId);
980 
981                 Long count = null;
982 
983                 Iterator itr = q.list().iterator();
984 
985                 if (itr.hasNext()) {
986                     count = (Long)itr.next();
987                 }
988 
989                 if (count == null) {
990                     count = new Long(0);
991                 }
992 
993                 FinderCache.putResult(finderClassNameCacheEnabled,
994                     finderClassName, finderMethodName, finderParams,
995                     finderArgs, count);
996 
997                 return count.intValue();
998             }
999             catch (Exception e) {
1000                throw HibernateUtil.processException(e);
1001            }
1002            finally {
1003                closeSession(session);
1004            }
1005        }
1006        else {
1007            return ((Long)result).intValue();
1008        }
1009    }
1010
1011    public int countByMessageId(long messageId) throws SystemException {
1012        boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
1013        String finderClassName = MBMessageFlag.class.getName();
1014        String finderMethodName = "countByMessageId";
1015        String[] finderParams = new String[] { Long.class.getName() };
1016        Object[] finderArgs = new Object[] { new Long(messageId) };
1017
1018        Object result = null;
1019
1020        if (finderClassNameCacheEnabled) {
1021            result = FinderCache.getResult(finderClassName, finderMethodName,
1022                    finderParams, finderArgs, getSessionFactory());
1023        }
1024
1025        if (result == null) {
1026            Session session = null;
1027
1028            try {
1029                session = openSession();
1030
1031                StringMaker query = new StringMaker();
1032
1033                query.append("SELECT COUNT(*) ");
1034                query.append(
1035                    "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
1036
1037                query.append("messageId = ?");
1038
1039                query.append(" ");
1040
1041                Query q = session.createQuery(query.toString());
1042
1043                int queryPos = 0;
1044
1045                q.setLong(queryPos++, messageId);
1046
1047                Long count = null;
1048
1049                Iterator itr = q.list().iterator();
1050
1051                if (itr.hasNext()) {
1052                    count = (Long)itr.next();
1053                }
1054
1055                if (count == null) {
1056                    count = new Long(0);
1057                }
1058
1059                FinderCache.putResult(finderClassNameCacheEnabled,
1060                    finderClassName, finderMethodName, finderParams,
1061                    finderArgs, count);
1062
1063                return count.intValue();
1064            }
1065            catch (Exception e) {
1066                throw HibernateUtil.processException(e);
1067            }
1068            finally {
1069                closeSession(session);
1070            }
1071        }
1072        else {
1073            return ((Long)result).intValue();
1074        }
1075    }
1076
1077    public int countByU_M(long userId, long messageId)
1078        throws SystemException {
1079        boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
1080        String finderClassName = MBMessageFlag.class.getName();
1081        String finderMethodName = "countByU_M";
1082        String[] finderParams = new String[] {
1083                Long.class.getName(), Long.class.getName()
1084            };
1085        Object[] finderArgs = new Object[] { new Long(userId), new Long(messageId) };
1086
1087        Object result = null;
1088
1089        if (finderClassNameCacheEnabled) {
1090            result = FinderCache.getResult(finderClassName, finderMethodName,
1091                    finderParams, finderArgs, getSessionFactory());
1092        }
1093
1094        if (result == null) {
1095            Session session = null;
1096
1097            try {
1098                session = openSession();
1099
1100                StringMaker query = new StringMaker();
1101
1102                query.append("SELECT COUNT(*) ");
1103                query.append(
1104                    "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
1105
1106                query.append("userId = ?");
1107
1108                query.append(" AND ");
1109
1110                query.append("messageId = ?");
1111
1112                query.append(" ");
1113
1114                Query q = session.createQuery(query.toString());
1115
1116                int queryPos = 0;
1117
1118                q.setLong(queryPos++, userId);
1119
1120                q.setLong(queryPos++, messageId);
1121
1122                Long count = null;
1123
1124                Iterator itr = q.list().iterator();
1125
1126                if (itr.hasNext()) {
1127                    count = (Long)itr.next();
1128                }
1129
1130                if (count == null) {
1131                    count = new Long(0);
1132                }
1133
1134                FinderCache.putResult(finderClassNameCacheEnabled,
1135                    finderClassName, finderMethodName, finderParams,
1136                    finderArgs, count);
1137
1138                return count.intValue();
1139            }
1140            catch (Exception e) {
1141                throw HibernateUtil.processException(e);
1142            }
1143            finally {
1144                closeSession(session);
1145            }
1146        }
1147        else {
1148            return ((Long)result).intValue();
1149        }
1150    }
1151
1152    public int countAll() throws SystemException {
1153        boolean finderClassNameCacheEnabled = MBMessageFlagModelImpl.CACHE_ENABLED;
1154        String finderClassName = MBMessageFlag.class.getName();
1155        String finderMethodName = "countAll";
1156        String[] finderParams = new String[] {  };
1157        Object[] finderArgs = new Object[] {  };
1158
1159        Object result = null;
1160
1161        if (finderClassNameCacheEnabled) {
1162            result = FinderCache.getResult(finderClassName, finderMethodName,
1163                    finderParams, finderArgs, getSessionFactory());
1164        }
1165
1166        if (result == null) {
1167            Session session = null;
1168
1169            try {
1170                session = openSession();
1171
1172                Query q = session.createQuery(
1173                        "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBMessageFlag");
1174
1175                Long count = null;
1176
1177                Iterator itr = q.list().iterator();
1178
1179                if (itr.hasNext()) {
1180                    count = (Long)itr.next();
1181                }
1182
1183                if (count == null) {
1184                    count = new Long(0);
1185                }
1186
1187                FinderCache.putResult(finderClassNameCacheEnabled,
1188                    finderClassName, finderMethodName, finderParams,
1189                    finderArgs, count);
1190
1191                return count.intValue();
1192            }
1193            catch (Exception e) {
1194                throw HibernateUtil.processException(e);
1195            }
1196            finally {
1197                closeSession(session);
1198            }
1199        }
1200        else {
1201            return ((Long)result).intValue();
1202        }
1203    }
1204
1205    protected void initDao() {
1206    }
1207
1208    private static ModelListener _getListener() {
1209        if (Validator.isNotNull(_LISTENER)) {
1210            try {
1211                return (ModelListener)Class.forName(_LISTENER).newInstance();
1212            }
1213            catch (Exception e) {
1214                _log.error(e);
1215            }
1216        }
1217
1218        return null;
1219    }
1220
1221    private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
1222                "value.object.listener.com.liferay.portlet.messageboards.model.MBMessageFlag"));
1223    private static Log _log = LogFactory.getLog(MBMessageFlagPersistenceImpl.class);
1224}