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.NoSuchThreadException;
40  import com.liferay.portlet.messageboards.model.MBThread;
41  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
42  import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
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="MBThreadPersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class MBThreadPersistenceImpl extends BasePersistence
63      implements MBThreadPersistence {
64      public MBThread create(long threadId) {
65          MBThread mbThread = new MBThreadImpl();
66  
67          mbThread.setNew(true);
68          mbThread.setPrimaryKey(threadId);
69  
70          return mbThread;
71      }
72  
73      public MBThread remove(long threadId)
74          throws NoSuchThreadException, SystemException {
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              MBThread mbThread = (MBThread)session.get(MBThreadImpl.class,
81                      new Long(threadId));
82  
83              if (mbThread == null) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn("No MBThread exists with the primary key " +
86                          threadId);
87                  }
88  
89                  throw new NoSuchThreadException(
90                      "No MBThread exists with the primary key " + threadId);
91              }
92  
93              return remove(mbThread);
94          }
95          catch (NoSuchThreadException nsee) {
96              throw nsee;
97          }
98          catch (Exception e) {
99              throw HibernateUtil.processException(e);
100         }
101         finally {
102             closeSession(session);
103         }
104     }
105 
106     public MBThread remove(MBThread mbThread) throws SystemException {
107         ModelListener listener = _getListener();
108 
109         if (listener != null) {
110             listener.onBeforeRemove(mbThread);
111         }
112 
113         mbThread = removeImpl(mbThread);
114 
115         if (listener != null) {
116             listener.onAfterRemove(mbThread);
117         }
118 
119         return mbThread;
120     }
121 
122     protected MBThread removeImpl(MBThread mbThread) throws SystemException {
123         Session session = null;
124 
125         try {
126             session = openSession();
127 
128             session.delete(mbThread);
129 
130             session.flush();
131 
132             return mbThread;
133         }
134         catch (Exception e) {
135             throw HibernateUtil.processException(e);
136         }
137         finally {
138             closeSession(session);
139 
140             FinderCache.clearCache(MBThread.class.getName());
141         }
142     }
143 
144     public MBThread update(MBThread mbThread) throws SystemException {
145         return update(mbThread, false);
146     }
147 
148     public MBThread update(MBThread mbThread, boolean merge)
149         throws SystemException {
150         ModelListener listener = _getListener();
151 
152         boolean isNew = mbThread.isNew();
153 
154         if (listener != null) {
155             if (isNew) {
156                 listener.onBeforeCreate(mbThread);
157             }
158             else {
159                 listener.onBeforeUpdate(mbThread);
160             }
161         }
162 
163         mbThread = updateImpl(mbThread, merge);
164 
165         if (listener != null) {
166             if (isNew) {
167                 listener.onAfterCreate(mbThread);
168             }
169             else {
170                 listener.onAfterUpdate(mbThread);
171             }
172         }
173 
174         return mbThread;
175     }
176 
177     public MBThread updateImpl(
178         com.liferay.portlet.messageboards.model.MBThread mbThread, boolean merge)
179         throws SystemException {
180         Session session = null;
181 
182         try {
183             session = openSession();
184 
185             if (merge) {
186                 session.merge(mbThread);
187             }
188             else {
189                 if (mbThread.isNew()) {
190                     session.save(mbThread);
191                 }
192             }
193 
194             session.flush();
195 
196             mbThread.setNew(false);
197 
198             return mbThread;
199         }
200         catch (Exception e) {
201             throw HibernateUtil.processException(e);
202         }
203         finally {
204             closeSession(session);
205 
206             FinderCache.clearCache(MBThread.class.getName());
207         }
208     }
209 
210     public MBThread findByPrimaryKey(long threadId)
211         throws NoSuchThreadException, SystemException {
212         MBThread mbThread = fetchByPrimaryKey(threadId);
213 
214         if (mbThread == null) {
215             if (_log.isWarnEnabled()) {
216                 _log.warn("No MBThread exists with the primary key " +
217                     threadId);
218             }
219 
220             throw new NoSuchThreadException(
221                 "No MBThread exists with the primary key " + threadId);
222         }
223 
224         return mbThread;
225     }
226 
227     public MBThread fetchByPrimaryKey(long threadId) throws SystemException {
228         Session session = null;
229 
230         try {
231             session = openSession();
232 
233             return (MBThread)session.get(MBThreadImpl.class, new Long(threadId));
234         }
235         catch (Exception e) {
236             throw HibernateUtil.processException(e);
237         }
238         finally {
239             closeSession(session);
240         }
241     }
242 
243     public List findByCategoryId(long categoryId) throws SystemException {
244         boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
245         String finderClassName = MBThread.class.getName();
246         String finderMethodName = "findByCategoryId";
247         String[] finderParams = new String[] { Long.class.getName() };
248         Object[] finderArgs = new Object[] { new Long(categoryId) };
249 
250         Object result = null;
251 
252         if (finderClassNameCacheEnabled) {
253             result = FinderCache.getResult(finderClassName, finderMethodName,
254                     finderParams, finderArgs, getSessionFactory());
255         }
256 
257         if (result == null) {
258             Session session = null;
259 
260             try {
261                 session = openSession();
262 
263                 StringMaker query = new StringMaker();
264 
265                 query.append(
266                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
267 
268                 query.append("categoryId = ?");
269 
270                 query.append(" ");
271 
272                 query.append("ORDER BY ");
273 
274                 query.append("priority DESC, ");
275                 query.append("lastPostDate DESC");
276 
277                 Query q = session.createQuery(query.toString());
278 
279                 int queryPos = 0;
280 
281                 q.setLong(queryPos++, categoryId);
282 
283                 List list = q.list();
284 
285                 FinderCache.putResult(finderClassNameCacheEnabled,
286                     finderClassName, finderMethodName, finderParams,
287                     finderArgs, list);
288 
289                 return list;
290             }
291             catch (Exception e) {
292                 throw HibernateUtil.processException(e);
293             }
294             finally {
295                 closeSession(session);
296             }
297         }
298         else {
299             return (List)result;
300         }
301     }
302 
303     public List findByCategoryId(long categoryId, int begin, int end)
304         throws SystemException {
305         return findByCategoryId(categoryId, begin, end, null);
306     }
307 
308     public List findByCategoryId(long categoryId, int begin, int end,
309         OrderByComparator obc) throws SystemException {
310         boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
311         String finderClassName = MBThread.class.getName();
312         String finderMethodName = "findByCategoryId";
313         String[] finderParams = new String[] {
314                 Long.class.getName(),
315                 
316                 "java.lang.Integer", "java.lang.Integer",
317                 "com.liferay.portal.kernel.util.OrderByComparator"
318             };
319         Object[] finderArgs = new Object[] {
320                 new Long(categoryId),
321                 
322                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
323             };
324 
325         Object result = null;
326 
327         if (finderClassNameCacheEnabled) {
328             result = FinderCache.getResult(finderClassName, finderMethodName,
329                     finderParams, finderArgs, getSessionFactory());
330         }
331 
332         if (result == null) {
333             Session session = null;
334 
335             try {
336                 session = openSession();
337 
338                 StringMaker query = new StringMaker();
339 
340                 query.append(
341                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
342 
343                 query.append("categoryId = ?");
344 
345                 query.append(" ");
346 
347                 if (obc != null) {
348                     query.append("ORDER BY ");
349                     query.append(obc.getOrderBy());
350                 }
351 
352                 else {
353                     query.append("ORDER BY ");
354 
355                     query.append("priority DESC, ");
356                     query.append("lastPostDate DESC");
357                 }
358 
359                 Query q = session.createQuery(query.toString());
360 
361                 int queryPos = 0;
362 
363                 q.setLong(queryPos++, categoryId);
364 
365                 List list = QueryUtil.list(q, getDialect(), begin, end);
366 
367                 FinderCache.putResult(finderClassNameCacheEnabled,
368                     finderClassName, finderMethodName, finderParams,
369                     finderArgs, list);
370 
371                 return list;
372             }
373             catch (Exception e) {
374                 throw HibernateUtil.processException(e);
375             }
376             finally {
377                 closeSession(session);
378             }
379         }
380         else {
381             return (List)result;
382         }
383     }
384 
385     public MBThread findByCategoryId_First(long categoryId,
386         OrderByComparator obc) throws NoSuchThreadException, SystemException {
387         List list = findByCategoryId(categoryId, 0, 1, obc);
388 
389         if (list.size() == 0) {
390             StringMaker msg = new StringMaker();
391 
392             msg.append("No MBThread exists with the key {");
393 
394             msg.append("categoryId=" + categoryId);
395 
396             msg.append(StringPool.CLOSE_CURLY_BRACE);
397 
398             throw new NoSuchThreadException(msg.toString());
399         }
400         else {
401             return (MBThread)list.get(0);
402         }
403     }
404 
405     public MBThread findByCategoryId_Last(long categoryId, OrderByComparator obc)
406         throws NoSuchThreadException, SystemException {
407         int count = countByCategoryId(categoryId);
408 
409         List list = findByCategoryId(categoryId, count - 1, count, obc);
410 
411         if (list.size() == 0) {
412             StringMaker msg = new StringMaker();
413 
414             msg.append("No MBThread exists with the key {");
415 
416             msg.append("categoryId=" + categoryId);
417 
418             msg.append(StringPool.CLOSE_CURLY_BRACE);
419 
420             throw new NoSuchThreadException(msg.toString());
421         }
422         else {
423             return (MBThread)list.get(0);
424         }
425     }
426 
427     public MBThread[] findByCategoryId_PrevAndNext(long threadId,
428         long categoryId, OrderByComparator obc)
429         throws NoSuchThreadException, SystemException {
430         MBThread mbThread = findByPrimaryKey(threadId);
431 
432         int count = countByCategoryId(categoryId);
433 
434         Session session = null;
435 
436         try {
437             session = openSession();
438 
439             StringMaker query = new StringMaker();
440 
441             query.append(
442                 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
443 
444             query.append("categoryId = ?");
445 
446             query.append(" ");
447 
448             if (obc != null) {
449                 query.append("ORDER BY ");
450                 query.append(obc.getOrderBy());
451             }
452 
453             else {
454                 query.append("ORDER BY ");
455 
456                 query.append("priority DESC, ");
457                 query.append("lastPostDate DESC");
458             }
459 
460             Query q = session.createQuery(query.toString());
461 
462             int queryPos = 0;
463 
464             q.setLong(queryPos++, categoryId);
465 
466             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, mbThread);
467 
468             MBThread[] array = new MBThreadImpl[3];
469 
470             array[0] = (MBThread)objArray[0];
471             array[1] = (MBThread)objArray[1];
472             array[2] = (MBThread)objArray[2];
473 
474             return array;
475         }
476         catch (Exception e) {
477             throw HibernateUtil.processException(e);
478         }
479         finally {
480             closeSession(session);
481         }
482     }
483 
484     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
485         throws SystemException {
486         Session session = null;
487 
488         try {
489             session = openSession();
490 
491             DynamicQuery query = queryInitializer.initialize(session);
492 
493             return query.list();
494         }
495         catch (Exception e) {
496             throw HibernateUtil.processException(e);
497         }
498         finally {
499             closeSession(session);
500         }
501     }
502 
503     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
504         int begin, int end) throws SystemException {
505         Session session = null;
506 
507         try {
508             session = openSession();
509 
510             DynamicQuery query = queryInitializer.initialize(session);
511 
512             query.setLimit(begin, end);
513 
514             return query.list();
515         }
516         catch (Exception e) {
517             throw HibernateUtil.processException(e);
518         }
519         finally {
520             closeSession(session);
521         }
522     }
523 
524     public List findAll() throws SystemException {
525         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
526     }
527 
528     public List findAll(int begin, int end) throws SystemException {
529         return findAll(begin, end, null);
530     }
531 
532     public List findAll(int begin, int end, OrderByComparator obc)
533         throws SystemException {
534         boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
535         String finderClassName = MBThread.class.getName();
536         String finderMethodName = "findAll";
537         String[] finderParams = new String[] {
538                 "java.lang.Integer", "java.lang.Integer",
539                 "com.liferay.portal.kernel.util.OrderByComparator"
540             };
541         Object[] finderArgs = new Object[] {
542                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
543             };
544 
545         Object result = null;
546 
547         if (finderClassNameCacheEnabled) {
548             result = FinderCache.getResult(finderClassName, finderMethodName,
549                     finderParams, finderArgs, getSessionFactory());
550         }
551 
552         if (result == null) {
553             Session session = null;
554 
555             try {
556                 session = openSession();
557 
558                 StringMaker query = new StringMaker();
559 
560                 query.append(
561                     "FROM com.liferay.portlet.messageboards.model.MBThread ");
562 
563                 if (obc != null) {
564                     query.append("ORDER BY ");
565                     query.append(obc.getOrderBy());
566                 }
567 
568                 else {
569                     query.append("ORDER BY ");
570 
571                     query.append("priority DESC, ");
572                     query.append("lastPostDate DESC");
573                 }
574 
575                 Query q = session.createQuery(query.toString());
576 
577                 List list = QueryUtil.list(q, getDialect(), begin, end);
578 
579                 if (obc == null) {
580                     Collections.sort(list);
581                 }
582 
583                 FinderCache.putResult(finderClassNameCacheEnabled,
584                     finderClassName, finderMethodName, finderParams,
585                     finderArgs, list);
586 
587                 return list;
588             }
589             catch (Exception e) {
590                 throw HibernateUtil.processException(e);
591             }
592             finally {
593                 closeSession(session);
594             }
595         }
596         else {
597             return (List)result;
598         }
599     }
600 
601     public void removeByCategoryId(long categoryId) throws SystemException {
602         Iterator itr = findByCategoryId(categoryId).iterator();
603 
604         while (itr.hasNext()) {
605             MBThread mbThread = (MBThread)itr.next();
606 
607             remove(mbThread);
608         }
609     }
610 
611     public void removeAll() throws SystemException {
612         Iterator itr = findAll().iterator();
613 
614         while (itr.hasNext()) {
615             remove((MBThread)itr.next());
616         }
617     }
618 
619     public int countByCategoryId(long categoryId) throws SystemException {
620         boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
621         String finderClassName = MBThread.class.getName();
622         String finderMethodName = "countByCategoryId";
623         String[] finderParams = new String[] { Long.class.getName() };
624         Object[] finderArgs = new Object[] { new Long(categoryId) };
625 
626         Object result = null;
627 
628         if (finderClassNameCacheEnabled) {
629             result = FinderCache.getResult(finderClassName, finderMethodName,
630                     finderParams, finderArgs, getSessionFactory());
631         }
632 
633         if (result == null) {
634             Session session = null;
635 
636             try {
637                 session = openSession();
638 
639                 StringMaker query = new StringMaker();
640 
641                 query.append("SELECT COUNT(*) ");
642                 query.append(
643                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
644 
645                 query.append("categoryId = ?");
646 
647                 query.append(" ");
648 
649                 Query q = session.createQuery(query.toString());
650 
651                 int queryPos = 0;
652 
653                 q.setLong(queryPos++, categoryId);
654 
655                 Long count = null;
656 
657                 Iterator itr = q.list().iterator();
658 
659                 if (itr.hasNext()) {
660                     count = (Long)itr.next();
661                 }
662 
663                 if (count == null) {
664                     count = new Long(0);
665                 }
666 
667                 FinderCache.putResult(finderClassNameCacheEnabled,
668                     finderClassName, finderMethodName, finderParams,
669                     finderArgs, count);
670 
671                 return count.intValue();
672             }
673             catch (Exception e) {
674                 throw HibernateUtil.processException(e);
675             }
676             finally {
677                 closeSession(session);
678             }
679         }
680         else {
681             return ((Long)result).intValue();
682         }
683     }
684 
685     public int countAll() throws SystemException {
686         boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
687         String finderClassName = MBThread.class.getName();
688         String finderMethodName = "countAll";
689         String[] finderParams = new String[] {  };
690         Object[] finderArgs = new Object[] {  };
691 
692         Object result = null;
693 
694         if (finderClassNameCacheEnabled) {
695             result = FinderCache.getResult(finderClassName, finderMethodName,
696                     finderParams, finderArgs, getSessionFactory());
697         }
698 
699         if (result == null) {
700             Session session = null;
701 
702             try {
703                 session = openSession();
704 
705                 Query q = session.createQuery(
706                         "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBThread");
707 
708                 Long count = null;
709 
710                 Iterator itr = q.list().iterator();
711 
712                 if (itr.hasNext()) {
713                     count = (Long)itr.next();
714                 }
715 
716                 if (count == null) {
717                     count = new Long(0);
718                 }
719 
720                 FinderCache.putResult(finderClassNameCacheEnabled,
721                     finderClassName, finderMethodName, finderParams,
722                     finderArgs, count);
723 
724                 return count.intValue();
725             }
726             catch (Exception e) {
727                 throw HibernateUtil.processException(e);
728             }
729             finally {
730                 closeSession(session);
731             }
732         }
733         else {
734             return ((Long)result).intValue();
735         }
736     }
737 
738     protected void initDao() {
739     }
740 
741     private static ModelListener _getListener() {
742         if (Validator.isNotNull(_LISTENER)) {
743             try {
744                 return (ModelListener)Class.forName(_LISTENER).newInstance();
745             }
746             catch (Exception e) {
747                 _log.error(e);
748             }
749         }
750 
751         return null;
752     }
753 
754     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
755                 "value.object.listener.com.liferay.portlet.messageboards.model.MBThread"));
756     private static Log _log = LogFactory.getLog(MBThreadPersistenceImpl.class);
757 }