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