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