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