1   /**
2    * Copyright (c) 2000-2007 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.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
36  import com.liferay.portlet.messageboards.model.MBMessageFlag;
37  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="MBMessageFlagPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class MBMessageFlagPersistenceImpl extends BasePersistence
58      implements MBMessageFlagPersistence {
59      public MBMessageFlag create(long messageFlagId) {
60          MBMessageFlag mbMessageFlag = new MBMessageFlagImpl();
61          mbMessageFlag.setNew(true);
62          mbMessageFlag.setPrimaryKey(messageFlagId);
63  
64          return mbMessageFlag;
65      }
66  
67      public MBMessageFlag remove(long messageFlagId)
68          throws NoSuchMessageFlagException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              MBMessageFlag mbMessageFlag = (MBMessageFlag)session.get(MBMessageFlagImpl.class,
75                      new Long(messageFlagId));
76  
77              if (mbMessageFlag == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No MBMessageFlag exists with the primary key " +
80                          messageFlagId);
81                  }
82  
83                  throw new NoSuchMessageFlagException(
84                      "No MBMessageFlag exists with the primary key " +
85                      messageFlagId);
86              }
87  
88              return remove(mbMessageFlag);
89          }
90          catch (NoSuchMessageFlagException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw HibernateUtil.processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public MBMessageFlag remove(MBMessageFlag mbMessageFlag)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(mbMessageFlag);
108             session.flush();
109 
110             return mbMessageFlag;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(MBMessageFlag.class.getName());
118         }
119     }
120 
121     public MBMessageFlag update(
122         com.liferay.portlet.messageboards.model.MBMessageFlag mbMessageFlag)
123         throws SystemException {
124         return update(mbMessageFlag, false);
125     }
126 
127     public MBMessageFlag update(
128         com.liferay.portlet.messageboards.model.MBMessageFlag mbMessageFlag,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(mbMessageFlag);
137             }
138             else {
139                 if (mbMessageFlag.isNew()) {
140                     session.save(mbMessageFlag);
141                 }
142             }
143 
144             session.flush();
145             mbMessageFlag.setNew(false);
146 
147             return mbMessageFlag;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(MBMessageFlag.class.getName());
155         }
156     }
157 
158     public MBMessageFlag findByPrimaryKey(long messageFlagId)
159         throws NoSuchMessageFlagException, SystemException {
160         MBMessageFlag mbMessageFlag = fetchByPrimaryKey(messageFlagId);
161 
162         if (mbMessageFlag == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No MBMessageFlag exists with the primary key " +
165                     messageFlagId);
166             }
167 
168             throw new NoSuchMessageFlagException(
169                 "No MBMessageFlag exists with the primary key " +
170                 messageFlagId);
171         }
172 
173         return mbMessageFlag;
174     }
175 
176     public MBMessageFlag fetchByPrimaryKey(long messageFlagId)
177         throws SystemException {
178         Session session = null;
179 
180         try {
181             session = openSession();
182 
183             return (MBMessageFlag)session.get(MBMessageFlagImpl.class,
184                 new Long(messageFlagId));
185         }
186         catch (Exception e) {
187             throw HibernateUtil.processException(e);
188         }
189         finally {
190             closeSession(session);
191         }
192     }
193 
194     public List findByUserId(long userId) throws SystemException {
195         String finderClassName = MBMessageFlag.class.getName();
196         String finderMethodName = "findByUserId";
197         String[] finderParams = new String[] { Long.class.getName() };
198         Object[] finderArgs = new Object[] { new Long(userId) };
199         Object result = FinderCache.getResult(finderClassName,
200                 finderMethodName, finderParams, finderArgs, getSessionFactory());
201 
202         if (result == null) {
203             Session session = null;
204 
205             try {
206                 session = openSession();
207 
208                 StringMaker query = new StringMaker();
209                 query.append(
210                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
211                 query.append("userId = ?");
212                 query.append(" ");
213 
214                 Query q = session.createQuery(query.toString());
215                 int queryPos = 0;
216                 q.setLong(queryPos++, userId);
217 
218                 List list = q.list();
219                 FinderCache.putResult(finderClassName, finderMethodName,
220                     finderParams, finderArgs, list);
221 
222                 return list;
223             }
224             catch (Exception e) {
225                 throw HibernateUtil.processException(e);
226             }
227             finally {
228                 closeSession(session);
229             }
230         }
231         else {
232             return (List)result;
233         }
234     }
235 
236     public List findByUserId(long userId, int begin, int end)
237         throws SystemException {
238         return findByUserId(userId, begin, end, null);
239     }
240 
241     public List findByUserId(long userId, int begin, int end,
242         OrderByComparator obc) throws SystemException {
243         String finderClassName = MBMessageFlag.class.getName();
244         String finderMethodName = "findByUserId";
245         String[] finderParams = new String[] {
246                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
247                 "com.liferay.portal.kernel.util.OrderByComparator"
248             };
249         Object[] finderArgs = new Object[] {
250                 new Long(userId), String.valueOf(begin), String.valueOf(end),
251                 String.valueOf(obc)
252             };
253         Object result = FinderCache.getResult(finderClassName,
254                 finderMethodName, finderParams, finderArgs, getSessionFactory());
255 
256         if (result == null) {
257             Session session = null;
258 
259             try {
260                 session = openSession();
261 
262                 StringMaker query = new StringMaker();
263                 query.append(
264                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
265                 query.append("userId = ?");
266                 query.append(" ");
267 
268                 if (obc != null) {
269                     query.append("ORDER BY ");
270                     query.append(obc.getOrderBy());
271                 }
272 
273                 Query q = session.createQuery(query.toString());
274                 int queryPos = 0;
275                 q.setLong(queryPos++, userId);
276 
277                 List list = QueryUtil.list(q, getDialect(), begin, end);
278                 FinderCache.putResult(finderClassName, finderMethodName,
279                     finderParams, finderArgs, list);
280 
281                 return list;
282             }
283             catch (Exception e) {
284                 throw HibernateUtil.processException(e);
285             }
286             finally {
287                 closeSession(session);
288             }
289         }
290         else {
291             return (List)result;
292         }
293     }
294 
295     public MBMessageFlag findByUserId_First(long userId, OrderByComparator obc)
296         throws NoSuchMessageFlagException, SystemException {
297         List list = findByUserId(userId, 0, 1, obc);
298 
299         if (list.size() == 0) {
300             StringMaker msg = new StringMaker();
301             msg.append("No MBMessageFlag exists with the key ");
302             msg.append(StringPool.OPEN_CURLY_BRACE);
303             msg.append("userId=");
304             msg.append(userId);
305             msg.append(StringPool.CLOSE_CURLY_BRACE);
306             throw new NoSuchMessageFlagException(msg.toString());
307         }
308         else {
309             return (MBMessageFlag)list.get(0);
310         }
311     }
312 
313     public MBMessageFlag findByUserId_Last(long userId, OrderByComparator obc)
314         throws NoSuchMessageFlagException, SystemException {
315         int count = countByUserId(userId);
316         List list = findByUserId(userId, count - 1, count, obc);
317 
318         if (list.size() == 0) {
319             StringMaker msg = new StringMaker();
320             msg.append("No MBMessageFlag exists with the key ");
321             msg.append(StringPool.OPEN_CURLY_BRACE);
322             msg.append("userId=");
323             msg.append(userId);
324             msg.append(StringPool.CLOSE_CURLY_BRACE);
325             throw new NoSuchMessageFlagException(msg.toString());
326         }
327         else {
328             return (MBMessageFlag)list.get(0);
329         }
330     }
331 
332     public MBMessageFlag[] findByUserId_PrevAndNext(long messageFlagId,
333         long userId, OrderByComparator obc)
334         throws NoSuchMessageFlagException, SystemException {
335         MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
336         int count = countByUserId(userId);
337         Session session = null;
338 
339         try {
340             session = openSession();
341 
342             StringMaker query = new StringMaker();
343             query.append(
344                 "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
345             query.append("userId = ?");
346             query.append(" ");
347 
348             if (obc != null) {
349                 query.append("ORDER BY ");
350                 query.append(obc.getOrderBy());
351             }
352 
353             Query q = session.createQuery(query.toString());
354             int queryPos = 0;
355             q.setLong(queryPos++, userId);
356 
357             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
358                     mbMessageFlag);
359             MBMessageFlag[] array = new MBMessageFlagImpl[3];
360             array[0] = (MBMessageFlag)objArray[0];
361             array[1] = (MBMessageFlag)objArray[1];
362             array[2] = (MBMessageFlag)objArray[2];
363 
364             return array;
365         }
366         catch (Exception e) {
367             throw HibernateUtil.processException(e);
368         }
369         finally {
370             closeSession(session);
371         }
372     }
373 
374     public List findByMessageId(long messageId) throws SystemException {
375         String finderClassName = MBMessageFlag.class.getName();
376         String finderMethodName = "findByMessageId";
377         String[] finderParams = new String[] { Long.class.getName() };
378         Object[] finderArgs = new Object[] { new Long(messageId) };
379         Object result = FinderCache.getResult(finderClassName,
380                 finderMethodName, finderParams, finderArgs, getSessionFactory());
381 
382         if (result == null) {
383             Session session = null;
384 
385             try {
386                 session = openSession();
387 
388                 StringMaker query = new StringMaker();
389                 query.append(
390                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
391                 query.append("messageId = ?");
392                 query.append(" ");
393 
394                 Query q = session.createQuery(query.toString());
395                 int queryPos = 0;
396                 q.setLong(queryPos++, messageId);
397 
398                 List list = q.list();
399                 FinderCache.putResult(finderClassName, finderMethodName,
400                     finderParams, finderArgs, list);
401 
402                 return list;
403             }
404             catch (Exception e) {
405                 throw HibernateUtil.processException(e);
406             }
407             finally {
408                 closeSession(session);
409             }
410         }
411         else {
412             return (List)result;
413         }
414     }
415 
416     public List findByMessageId(long messageId, int begin, int end)
417         throws SystemException {
418         return findByMessageId(messageId, begin, end, null);
419     }
420 
421     public List findByMessageId(long messageId, int begin, int end,
422         OrderByComparator obc) throws SystemException {
423         String finderClassName = MBMessageFlag.class.getName();
424         String finderMethodName = "findByMessageId";
425         String[] finderParams = new String[] {
426                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
427                 "com.liferay.portal.kernel.util.OrderByComparator"
428             };
429         Object[] finderArgs = new Object[] {
430                 new Long(messageId), String.valueOf(begin), String.valueOf(end),
431                 String.valueOf(obc)
432             };
433         Object result = FinderCache.getResult(finderClassName,
434                 finderMethodName, finderParams, finderArgs, getSessionFactory());
435 
436         if (result == null) {
437             Session session = null;
438 
439             try {
440                 session = openSession();
441 
442                 StringMaker query = new StringMaker();
443                 query.append(
444                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
445                 query.append("messageId = ?");
446                 query.append(" ");
447 
448                 if (obc != null) {
449                     query.append("ORDER BY ");
450                     query.append(obc.getOrderBy());
451                 }
452 
453                 Query q = session.createQuery(query.toString());
454                 int queryPos = 0;
455                 q.setLong(queryPos++, messageId);
456 
457                 List list = QueryUtil.list(q, getDialect(), begin, end);
458                 FinderCache.putResult(finderClassName, finderMethodName,
459                     finderParams, finderArgs, list);
460 
461                 return list;
462             }
463             catch (Exception e) {
464                 throw HibernateUtil.processException(e);
465             }
466             finally {
467                 closeSession(session);
468             }
469         }
470         else {
471             return (List)result;
472         }
473     }
474 
475     public MBMessageFlag findByMessageId_First(long messageId,
476         OrderByComparator obc)
477         throws NoSuchMessageFlagException, SystemException {
478         List list = findByMessageId(messageId, 0, 1, obc);
479 
480         if (list.size() == 0) {
481             StringMaker msg = new StringMaker();
482             msg.append("No MBMessageFlag exists with the key ");
483             msg.append(StringPool.OPEN_CURLY_BRACE);
484             msg.append("messageId=");
485             msg.append(messageId);
486             msg.append(StringPool.CLOSE_CURLY_BRACE);
487             throw new NoSuchMessageFlagException(msg.toString());
488         }
489         else {
490             return (MBMessageFlag)list.get(0);
491         }
492     }
493 
494     public MBMessageFlag findByMessageId_Last(long messageId,
495         OrderByComparator obc)
496         throws NoSuchMessageFlagException, SystemException {
497         int count = countByMessageId(messageId);
498         List list = findByMessageId(messageId, count - 1, count, obc);
499 
500         if (list.size() == 0) {
501             StringMaker msg = new StringMaker();
502             msg.append("No MBMessageFlag exists with the key ");
503             msg.append(StringPool.OPEN_CURLY_BRACE);
504             msg.append("messageId=");
505             msg.append(messageId);
506             msg.append(StringPool.CLOSE_CURLY_BRACE);
507             throw new NoSuchMessageFlagException(msg.toString());
508         }
509         else {
510             return (MBMessageFlag)list.get(0);
511         }
512     }
513 
514     public MBMessageFlag[] findByMessageId_PrevAndNext(long messageFlagId,
515         long messageId, OrderByComparator obc)
516         throws NoSuchMessageFlagException, SystemException {
517         MBMessageFlag mbMessageFlag = findByPrimaryKey(messageFlagId);
518         int count = countByMessageId(messageId);
519         Session session = null;
520 
521         try {
522             session = openSession();
523 
524             StringMaker query = new StringMaker();
525             query.append(
526                 "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
527             query.append("messageId = ?");
528             query.append(" ");
529 
530             if (obc != null) {
531                 query.append("ORDER BY ");
532                 query.append(obc.getOrderBy());
533             }
534 
535             Query q = session.createQuery(query.toString());
536             int queryPos = 0;
537             q.setLong(queryPos++, messageId);
538 
539             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
540                     mbMessageFlag);
541             MBMessageFlag[] array = new MBMessageFlagImpl[3];
542             array[0] = (MBMessageFlag)objArray[0];
543             array[1] = (MBMessageFlag)objArray[1];
544             array[2] = (MBMessageFlag)objArray[2];
545 
546             return array;
547         }
548         catch (Exception e) {
549             throw HibernateUtil.processException(e);
550         }
551         finally {
552             closeSession(session);
553         }
554     }
555 
556     public MBMessageFlag findByU_M(long userId, long messageId)
557         throws NoSuchMessageFlagException, SystemException {
558         MBMessageFlag mbMessageFlag = fetchByU_M(userId, messageId);
559 
560         if (mbMessageFlag == null) {
561             StringMaker msg = new StringMaker();
562             msg.append("No MBMessageFlag exists with the key ");
563             msg.append(StringPool.OPEN_CURLY_BRACE);
564             msg.append("userId=");
565             msg.append(userId);
566             msg.append(", ");
567             msg.append("messageId=");
568             msg.append(messageId);
569             msg.append(StringPool.CLOSE_CURLY_BRACE);
570 
571             if (_log.isWarnEnabled()) {
572                 _log.warn(msg.toString());
573             }
574 
575             throw new NoSuchMessageFlagException(msg.toString());
576         }
577 
578         return mbMessageFlag;
579     }
580 
581     public MBMessageFlag fetchByU_M(long userId, long messageId)
582         throws SystemException {
583         String finderClassName = MBMessageFlag.class.getName();
584         String finderMethodName = "fetchByU_M";
585         String[] finderParams = new String[] {
586                 Long.class.getName(), Long.class.getName()
587             };
588         Object[] finderArgs = new Object[] { new Long(userId), new Long(messageId) };
589         Object result = FinderCache.getResult(finderClassName,
590                 finderMethodName, finderParams, finderArgs, getSessionFactory());
591 
592         if (result == null) {
593             Session session = null;
594 
595             try {
596                 session = openSession();
597 
598                 StringMaker query = new StringMaker();
599                 query.append(
600                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
601                 query.append("userId = ?");
602                 query.append(" AND ");
603                 query.append("messageId = ?");
604                 query.append(" ");
605 
606                 Query q = session.createQuery(query.toString());
607                 int queryPos = 0;
608                 q.setLong(queryPos++, userId);
609                 q.setLong(queryPos++, messageId);
610 
611                 List list = q.list();
612                 FinderCache.putResult(finderClassName, finderMethodName,
613                     finderParams, finderArgs, list);
614 
615                 if (list.size() == 0) {
616                     return null;
617                 }
618                 else {
619                     return (MBMessageFlag)list.get(0);
620                 }
621             }
622             catch (Exception e) {
623                 throw HibernateUtil.processException(e);
624             }
625             finally {
626                 closeSession(session);
627             }
628         }
629         else {
630             List list = (List)result;
631 
632             if (list.size() == 0) {
633                 return null;
634             }
635             else {
636                 return (MBMessageFlag)list.get(0);
637             }
638         }
639     }
640 
641     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
642         throws SystemException {
643         Session session = null;
644 
645         try {
646             session = openSession();
647 
648             DynamicQuery query = queryInitializer.initialize(session);
649 
650             return query.list();
651         }
652         catch (Exception e) {
653             throw HibernateUtil.processException(e);
654         }
655         finally {
656             closeSession(session);
657         }
658     }
659 
660     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
661         int begin, int end) throws SystemException {
662         Session session = null;
663 
664         try {
665             session = openSession();
666 
667             DynamicQuery query = queryInitializer.initialize(session);
668             query.setLimit(begin, end);
669 
670             return query.list();
671         }
672         catch (Exception e) {
673             throw HibernateUtil.processException(e);
674         }
675         finally {
676             closeSession(session);
677         }
678     }
679 
680     public List findAll() throws SystemException {
681         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
682     }
683 
684     public List findAll(int begin, int end) throws SystemException {
685         return findAll(begin, end, null);
686     }
687 
688     public List findAll(int begin, int end, OrderByComparator obc)
689         throws SystemException {
690         String finderClassName = MBMessageFlag.class.getName();
691         String finderMethodName = "findAll";
692         String[] finderParams = new String[] {
693                 "java.lang.Integer", "java.lang.Integer",
694                 "com.liferay.portal.kernel.util.OrderByComparator"
695             };
696         Object[] finderArgs = new Object[] {
697                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
698             };
699         Object result = FinderCache.getResult(finderClassName,
700                 finderMethodName, finderParams, finderArgs, getSessionFactory());
701 
702         if (result == null) {
703             Session session = null;
704 
705             try {
706                 session = openSession();
707 
708                 StringMaker query = new StringMaker();
709                 query.append(
710                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag ");
711 
712                 if (obc != null) {
713                     query.append("ORDER BY ");
714                     query.append(obc.getOrderBy());
715                 }
716 
717                 Query q = session.createQuery(query.toString());
718                 List list = QueryUtil.list(q, getDialect(), begin, end);
719 
720                 if (obc == null) {
721                     Collections.sort(list);
722                 }
723 
724                 FinderCache.putResult(finderClassName, finderMethodName,
725                     finderParams, finderArgs, list);
726 
727                 return list;
728             }
729             catch (Exception e) {
730                 throw HibernateUtil.processException(e);
731             }
732             finally {
733                 closeSession(session);
734             }
735         }
736         else {
737             return (List)result;
738         }
739     }
740 
741     public void removeByUserId(long userId) throws SystemException {
742         Iterator itr = findByUserId(userId).iterator();
743 
744         while (itr.hasNext()) {
745             MBMessageFlag mbMessageFlag = (MBMessageFlag)itr.next();
746             remove(mbMessageFlag);
747         }
748     }
749 
750     public void removeByMessageId(long messageId) throws SystemException {
751         Iterator itr = findByMessageId(messageId).iterator();
752 
753         while (itr.hasNext()) {
754             MBMessageFlag mbMessageFlag = (MBMessageFlag)itr.next();
755             remove(mbMessageFlag);
756         }
757     }
758 
759     public void removeByU_M(long userId, long messageId)
760         throws NoSuchMessageFlagException, SystemException {
761         MBMessageFlag mbMessageFlag = findByU_M(userId, messageId);
762         remove(mbMessageFlag);
763     }
764 
765     public void removeAll() throws SystemException {
766         Iterator itr = findAll().iterator();
767 
768         while (itr.hasNext()) {
769             remove((MBMessageFlag)itr.next());
770         }
771     }
772 
773     public int countByUserId(long userId) throws SystemException {
774         String finderClassName = MBMessageFlag.class.getName();
775         String finderMethodName = "countByUserId";
776         String[] finderParams = new String[] { Long.class.getName() };
777         Object[] finderArgs = new Object[] { new Long(userId) };
778         Object result = FinderCache.getResult(finderClassName,
779                 finderMethodName, finderParams, finderArgs, getSessionFactory());
780 
781         if (result == null) {
782             Session session = null;
783 
784             try {
785                 session = openSession();
786 
787                 StringMaker query = new StringMaker();
788                 query.append("SELECT COUNT(*) ");
789                 query.append(
790                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
791                 query.append("userId = ?");
792                 query.append(" ");
793 
794                 Query q = session.createQuery(query.toString());
795                 int queryPos = 0;
796                 q.setLong(queryPos++, userId);
797 
798                 Long count = null;
799                 Iterator itr = q.list().iterator();
800 
801                 if (itr.hasNext()) {
802                     count = (Long)itr.next();
803                 }
804 
805                 if (count == null) {
806                     count = new Long(0);
807                 }
808 
809                 FinderCache.putResult(finderClassName, finderMethodName,
810                     finderParams, finderArgs, count);
811 
812                 return count.intValue();
813             }
814             catch (Exception e) {
815                 throw HibernateUtil.processException(e);
816             }
817             finally {
818                 closeSession(session);
819             }
820         }
821         else {
822             return ((Long)result).intValue();
823         }
824     }
825 
826     public int countByMessageId(long messageId) throws SystemException {
827         String finderClassName = MBMessageFlag.class.getName();
828         String finderMethodName = "countByMessageId";
829         String[] finderParams = new String[] { Long.class.getName() };
830         Object[] finderArgs = new Object[] { new Long(messageId) };
831         Object result = FinderCache.getResult(finderClassName,
832                 finderMethodName, finderParams, finderArgs, getSessionFactory());
833 
834         if (result == null) {
835             Session session = null;
836 
837             try {
838                 session = openSession();
839 
840                 StringMaker query = new StringMaker();
841                 query.append("SELECT COUNT(*) ");
842                 query.append(
843                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
844                 query.append("messageId = ?");
845                 query.append(" ");
846 
847                 Query q = session.createQuery(query.toString());
848                 int queryPos = 0;
849                 q.setLong(queryPos++, messageId);
850 
851                 Long count = null;
852                 Iterator itr = q.list().iterator();
853 
854                 if (itr.hasNext()) {
855                     count = (Long)itr.next();
856                 }
857 
858                 if (count == null) {
859                     count = new Long(0);
860                 }
861 
862                 FinderCache.putResult(finderClassName, finderMethodName,
863                     finderParams, finderArgs, count);
864 
865                 return count.intValue();
866             }
867             catch (Exception e) {
868                 throw HibernateUtil.processException(e);
869             }
870             finally {
871                 closeSession(session);
872             }
873         }
874         else {
875             return ((Long)result).intValue();
876         }
877     }
878 
879     public int countByU_M(long userId, long messageId)
880         throws SystemException {
881         String finderClassName = MBMessageFlag.class.getName();
882         String finderMethodName = "countByU_M";
883         String[] finderParams = new String[] {
884                 Long.class.getName(), Long.class.getName()
885             };
886         Object[] finderArgs = new Object[] { new Long(userId), new Long(messageId) };
887         Object result = FinderCache.getResult(finderClassName,
888                 finderMethodName, finderParams, finderArgs, getSessionFactory());
889 
890         if (result == null) {
891             Session session = null;
892 
893             try {
894                 session = openSession();
895 
896                 StringMaker query = new StringMaker();
897                 query.append("SELECT COUNT(*) ");
898                 query.append(
899                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag WHERE ");
900                 query.append("userId = ?");
901                 query.append(" AND ");
902                 query.append("messageId = ?");
903                 query.append(" ");
904 
905                 Query q = session.createQuery(query.toString());
906                 int queryPos = 0;
907                 q.setLong(queryPos++, userId);
908                 q.setLong(queryPos++, messageId);
909 
910                 Long count = null;
911                 Iterator itr = q.list().iterator();
912 
913                 if (itr.hasNext()) {
914                     count = (Long)itr.next();
915                 }
916 
917                 if (count == null) {
918                     count = new Long(0);
919                 }
920 
921                 FinderCache.putResult(finderClassName, finderMethodName,
922                     finderParams, finderArgs, count);
923 
924                 return count.intValue();
925             }
926             catch (Exception e) {
927                 throw HibernateUtil.processException(e);
928             }
929             finally {
930                 closeSession(session);
931             }
932         }
933         else {
934             return ((Long)result).intValue();
935         }
936     }
937 
938     public int countAll() throws SystemException {
939         String finderClassName = MBMessageFlag.class.getName();
940         String finderMethodName = "countAll";
941         String[] finderParams = new String[] {  };
942         Object[] finderArgs = new Object[] {  };
943         Object result = FinderCache.getResult(finderClassName,
944                 finderMethodName, finderParams, finderArgs, getSessionFactory());
945 
946         if (result == null) {
947             Session session = null;
948 
949             try {
950                 session = openSession();
951 
952                 StringMaker query = new StringMaker();
953                 query.append("SELECT COUNT(*) ");
954                 query.append(
955                     "FROM com.liferay.portlet.messageboards.model.MBMessageFlag");
956 
957                 Query q = session.createQuery(query.toString());
958                 Long count = null;
959                 Iterator itr = q.list().iterator();
960 
961                 if (itr.hasNext()) {
962                     count = (Long)itr.next();
963                 }
964 
965                 if (count == null) {
966                     count = new Long(0);
967                 }
968 
969                 FinderCache.putResult(finderClassName, finderMethodName,
970                     finderParams, finderArgs, count);
971 
972                 return count.intValue();
973             }
974             catch (Exception e) {
975                 throw HibernateUtil.processException(e);
976             }
977             finally {
978                 closeSession(session);
979             }
980         }
981         else {
982             return ((Long)result).intValue();
983         }
984     }
985 
986     protected void initDao() {
987     }
988 
989     private static Log _log = LogFactory.getLog(MBMessageFlagPersistenceImpl.class);
990 }