1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ModelListener;
37  import com.liferay.portal.service.persistence.BatchSessionUtil;
38  import com.liferay.portal.service.persistence.ResourcePersistence;
39  import com.liferay.portal.service.persistence.UserPersistence;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
43  import com.liferay.portlet.messageboards.model.MBDiscussion;
44  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
45  import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
46  
47  import java.io.Serializable;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  
53  /**
54   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * <p>
57   * ServiceBuilder generated this class. Modifications in this class will be
58   * overwritten the next time is generated.
59   * </p>
60   *
61   * @author    Brian Wing Shun Chan
62   * @see       MBDiscussionPersistence
63   * @see       MBDiscussionUtil
64   * @generated
65   */
66  public class MBDiscussionPersistenceImpl extends BasePersistenceImpl<MBDiscussion>
67      implements MBDiscussionPersistence {
68      public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
69      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
70          ".List";
71      public static final FinderPath FINDER_PATH_FIND_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
72              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
73              "findByClassNameId",
74              new String[] {
75                  Long.class.getName(),
76                  
77              "java.lang.Integer", "java.lang.Integer",
78                  "com.liferay.portal.kernel.util.OrderByComparator"
79              });
80      public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
81              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
82              "countByClassNameId", new String[] { Long.class.getName() });
83      public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
84              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
85              FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
86              new String[] { Long.class.getName() });
87      public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
88              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
89              "countByThreadId", new String[] { Long.class.getName() });
90      public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
91              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
93              new String[] { Long.class.getName(), Long.class.getName() });
94      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
95              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
96              "countByC_C",
97              new String[] { Long.class.getName(), Long.class.getName() });
98      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
99              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
100             "findAll", new String[0]);
101     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
102             MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
103             "countAll", new String[0]);
104 
105     public void cacheResult(MBDiscussion mbDiscussion) {
106         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
107             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
108 
109         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
110             new Object[] { new Long(mbDiscussion.getThreadId()) }, mbDiscussion);
111 
112         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
113             new Object[] {
114                 new Long(mbDiscussion.getClassNameId()),
115                 new Long(mbDiscussion.getClassPK())
116             }, mbDiscussion);
117     }
118 
119     public void cacheResult(List<MBDiscussion> mbDiscussions) {
120         for (MBDiscussion mbDiscussion : mbDiscussions) {
121             if (EntityCacheUtil.getResult(
122                         MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
123                         MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(),
124                         this) == null) {
125                 cacheResult(mbDiscussion);
126             }
127         }
128     }
129 
130     public void clearCache() {
131         CacheRegistry.clear(MBDiscussionImpl.class.getName());
132         EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
133         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
134         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
135     }
136 
137     public void clearCache(MBDiscussion mbDiscussion) {
138         EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
139             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
140 
141         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
142             new Object[] { new Long(mbDiscussion.getThreadId()) });
143 
144         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
145             new Object[] {
146                 new Long(mbDiscussion.getClassNameId()),
147                 new Long(mbDiscussion.getClassPK())
148             });
149     }
150 
151     public MBDiscussion create(long discussionId) {
152         MBDiscussion mbDiscussion = new MBDiscussionImpl();
153 
154         mbDiscussion.setNew(true);
155         mbDiscussion.setPrimaryKey(discussionId);
156 
157         return mbDiscussion;
158     }
159 
160     public MBDiscussion remove(Serializable primaryKey)
161         throws NoSuchModelException, SystemException {
162         return remove(((Long)primaryKey).longValue());
163     }
164 
165     public MBDiscussion remove(long discussionId)
166         throws NoSuchDiscussionException, SystemException {
167         Session session = null;
168 
169         try {
170             session = openSession();
171 
172             MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
173                     new Long(discussionId));
174 
175             if (mbDiscussion == null) {
176                 if (_log.isWarnEnabled()) {
177                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
178                 }
179 
180                 throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
181                     discussionId);
182             }
183 
184             return remove(mbDiscussion);
185         }
186         catch (NoSuchDiscussionException nsee) {
187             throw nsee;
188         }
189         catch (Exception e) {
190             throw processException(e);
191         }
192         finally {
193             closeSession(session);
194         }
195     }
196 
197     protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
198         throws SystemException {
199         mbDiscussion = toUnwrappedModel(mbDiscussion);
200 
201         Session session = null;
202 
203         try {
204             session = openSession();
205 
206             BatchSessionUtil.delete(session, mbDiscussion);
207         }
208         catch (Exception e) {
209             throw processException(e);
210         }
211         finally {
212             closeSession(session);
213         }
214 
215         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
216 
217         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
218 
219         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
220             new Object[] { new Long(mbDiscussionModelImpl.getOriginalThreadId()) });
221 
222         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
223             new Object[] {
224                 new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
225                 new Long(mbDiscussionModelImpl.getOriginalClassPK())
226             });
227 
228         EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
229             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
230 
231         return mbDiscussion;
232     }
233 
234     /**
235      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
236      */
237     public MBDiscussion update(MBDiscussion mbDiscussion)
238         throws SystemException {
239         if (_log.isWarnEnabled()) {
240             _log.warn(
241                 "Using the deprecated update(MBDiscussion mbDiscussion) method. Use update(MBDiscussion mbDiscussion, boolean merge) instead.");
242         }
243 
244         return update(mbDiscussion, false);
245     }
246 
247     public MBDiscussion updateImpl(
248         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
249         boolean merge) throws SystemException {
250         mbDiscussion = toUnwrappedModel(mbDiscussion);
251 
252         boolean isNew = mbDiscussion.isNew();
253 
254         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
255 
256         Session session = null;
257 
258         try {
259             session = openSession();
260 
261             BatchSessionUtil.update(session, mbDiscussion, merge);
262 
263             mbDiscussion.setNew(false);
264         }
265         catch (Exception e) {
266             throw processException(e);
267         }
268         finally {
269             closeSession(session);
270         }
271 
272         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
273 
274         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
275             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
276 
277         if (!isNew &&
278                 (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
279             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
280                 new Object[] {
281                     new Long(mbDiscussionModelImpl.getOriginalThreadId())
282                 });
283         }
284 
285         if (isNew ||
286                 (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
287             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
288                 new Object[] { new Long(mbDiscussion.getThreadId()) },
289                 mbDiscussion);
290         }
291 
292         if (!isNew &&
293                 ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
294                 (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
295             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
296                 new Object[] {
297                     new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
298                     new Long(mbDiscussionModelImpl.getOriginalClassPK())
299                 });
300         }
301 
302         if (isNew ||
303                 ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
304                 (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
305             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
306                 new Object[] {
307                     new Long(mbDiscussion.getClassNameId()),
308                     new Long(mbDiscussion.getClassPK())
309                 }, mbDiscussion);
310         }
311 
312         return mbDiscussion;
313     }
314 
315     protected MBDiscussion toUnwrappedModel(MBDiscussion mbDiscussion) {
316         if (mbDiscussion instanceof MBDiscussionImpl) {
317             return mbDiscussion;
318         }
319 
320         MBDiscussionImpl mbDiscussionImpl = new MBDiscussionImpl();
321 
322         mbDiscussionImpl.setNew(mbDiscussion.isNew());
323         mbDiscussionImpl.setPrimaryKey(mbDiscussion.getPrimaryKey());
324 
325         mbDiscussionImpl.setDiscussionId(mbDiscussion.getDiscussionId());
326         mbDiscussionImpl.setClassNameId(mbDiscussion.getClassNameId());
327         mbDiscussionImpl.setClassPK(mbDiscussion.getClassPK());
328         mbDiscussionImpl.setThreadId(mbDiscussion.getThreadId());
329 
330         return mbDiscussionImpl;
331     }
332 
333     public MBDiscussion findByPrimaryKey(Serializable primaryKey)
334         throws NoSuchModelException, SystemException {
335         return findByPrimaryKey(((Long)primaryKey).longValue());
336     }
337 
338     public MBDiscussion findByPrimaryKey(long discussionId)
339         throws NoSuchDiscussionException, SystemException {
340         MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
341 
342         if (mbDiscussion == null) {
343             if (_log.isWarnEnabled()) {
344                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
345             }
346 
347             throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
348                 discussionId);
349         }
350 
351         return mbDiscussion;
352     }
353 
354     public MBDiscussion fetchByPrimaryKey(Serializable primaryKey)
355         throws SystemException {
356         return fetchByPrimaryKey(((Long)primaryKey).longValue());
357     }
358 
359     public MBDiscussion fetchByPrimaryKey(long discussionId)
360         throws SystemException {
361         MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
362                 MBDiscussionImpl.class, discussionId, this);
363 
364         if (mbDiscussion == null) {
365             Session session = null;
366 
367             try {
368                 session = openSession();
369 
370                 mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
371                         new Long(discussionId));
372             }
373             catch (Exception e) {
374                 throw processException(e);
375             }
376             finally {
377                 if (mbDiscussion != null) {
378                     cacheResult(mbDiscussion);
379                 }
380 
381                 closeSession(session);
382             }
383         }
384 
385         return mbDiscussion;
386     }
387 
388     public List<MBDiscussion> findByClassNameId(long classNameId)
389         throws SystemException {
390         return findByClassNameId(classNameId, QueryUtil.ALL_POS,
391             QueryUtil.ALL_POS, null);
392     }
393 
394     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
395         int end) throws SystemException {
396         return findByClassNameId(classNameId, start, end, null);
397     }
398 
399     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
400         int end, OrderByComparator orderByComparator) throws SystemException {
401         Object[] finderArgs = new Object[] {
402                 classNameId,
403                 
404                 String.valueOf(start), String.valueOf(end),
405                 String.valueOf(orderByComparator)
406             };
407 
408         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
409                 finderArgs, this);
410 
411         if (list == null) {
412             StringBundler query = null;
413 
414             if (orderByComparator != null) {
415                 query = new StringBundler(3 +
416                         (orderByComparator.getOrderByFields().length * 3));
417             }
418             else {
419                 query = new StringBundler(2);
420             }
421 
422             query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
423 
424             query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
425 
426             if (orderByComparator != null) {
427                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
428                     orderByComparator);
429             }
430 
431             String sql = query.toString();
432 
433             Session session = null;
434 
435             try {
436                 session = openSession();
437 
438                 Query q = session.createQuery(sql);
439 
440                 QueryPos qPos = QueryPos.getInstance(q);
441 
442                 qPos.add(classNameId);
443 
444                 list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
445                         start, end);
446             }
447             catch (Exception e) {
448                 throw processException(e);
449             }
450             finally {
451                 if (list == null) {
452                     list = new ArrayList<MBDiscussion>();
453                 }
454 
455                 cacheResult(list);
456 
457                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
458                     finderArgs, list);
459 
460                 closeSession(session);
461             }
462         }
463 
464         return list;
465     }
466 
467     public MBDiscussion findByClassNameId_First(long classNameId,
468         OrderByComparator orderByComparator)
469         throws NoSuchDiscussionException, SystemException {
470         List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1,
471                 orderByComparator);
472 
473         if (list.isEmpty()) {
474             StringBundler msg = new StringBundler(4);
475 
476             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
477 
478             msg.append("classNameId=");
479             msg.append(classNameId);
480 
481             msg.append(StringPool.CLOSE_CURLY_BRACE);
482 
483             throw new NoSuchDiscussionException(msg.toString());
484         }
485         else {
486             return list.get(0);
487         }
488     }
489 
490     public MBDiscussion findByClassNameId_Last(long classNameId,
491         OrderByComparator orderByComparator)
492         throws NoSuchDiscussionException, SystemException {
493         int count = countByClassNameId(classNameId);
494 
495         List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
496                 count, orderByComparator);
497 
498         if (list.isEmpty()) {
499             StringBundler msg = new StringBundler(4);
500 
501             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
502 
503             msg.append("classNameId=");
504             msg.append(classNameId);
505 
506             msg.append(StringPool.CLOSE_CURLY_BRACE);
507 
508             throw new NoSuchDiscussionException(msg.toString());
509         }
510         else {
511             return list.get(0);
512         }
513     }
514 
515     public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
516         long classNameId, OrderByComparator orderByComparator)
517         throws NoSuchDiscussionException, SystemException {
518         MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
519 
520         Session session = null;
521 
522         try {
523             session = openSession();
524 
525             MBDiscussion[] array = new MBDiscussionImpl[3];
526 
527             array[0] = getByClassNameId_PrevAndNext(session, mbDiscussion,
528                     classNameId, orderByComparator, true);
529 
530             array[1] = mbDiscussion;
531 
532             array[2] = getByClassNameId_PrevAndNext(session, mbDiscussion,
533                     classNameId, orderByComparator, false);
534 
535             return array;
536         }
537         catch (Exception e) {
538             throw processException(e);
539         }
540         finally {
541             closeSession(session);
542         }
543     }
544 
545     protected MBDiscussion getByClassNameId_PrevAndNext(Session session,
546         MBDiscussion mbDiscussion, long classNameId,
547         OrderByComparator orderByComparator, boolean previous) {
548         StringBundler query = null;
549 
550         if (orderByComparator != null) {
551             query = new StringBundler(6 +
552                     (orderByComparator.getOrderByFields().length * 6));
553         }
554         else {
555             query = new StringBundler(3);
556         }
557 
558         query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
559 
560         query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
561 
562         if (orderByComparator != null) {
563             String[] orderByFields = orderByComparator.getOrderByFields();
564 
565             if (orderByFields.length > 0) {
566                 query.append(WHERE_AND);
567             }
568 
569             for (int i = 0; i < orderByFields.length; i++) {
570                 query.append(_ORDER_BY_ENTITY_ALIAS);
571                 query.append(orderByFields[i]);
572 
573                 if ((i + 1) < orderByFields.length) {
574                     if (orderByComparator.isAscending() ^ previous) {
575                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
576                     }
577                     else {
578                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
579                     }
580                 }
581                 else {
582                     if (orderByComparator.isAscending() ^ previous) {
583                         query.append(WHERE_GREATER_THAN);
584                     }
585                     else {
586                         query.append(WHERE_LESSER_THAN);
587                     }
588                 }
589             }
590 
591             query.append(ORDER_BY_CLAUSE);
592 
593             for (int i = 0; i < orderByFields.length; i++) {
594                 query.append(_ORDER_BY_ENTITY_ALIAS);
595                 query.append(orderByFields[i]);
596 
597                 if ((i + 1) < orderByFields.length) {
598                     if (orderByComparator.isAscending() ^ previous) {
599                         query.append(ORDER_BY_ASC_HAS_NEXT);
600                     }
601                     else {
602                         query.append(ORDER_BY_DESC_HAS_NEXT);
603                     }
604                 }
605                 else {
606                     if (orderByComparator.isAscending() ^ previous) {
607                         query.append(ORDER_BY_ASC);
608                     }
609                     else {
610                         query.append(ORDER_BY_DESC);
611                     }
612                 }
613             }
614         }
615 
616         String sql = query.toString();
617 
618         Query q = session.createQuery(sql);
619 
620         q.setFirstResult(0);
621         q.setMaxResults(2);
622 
623         QueryPos qPos = QueryPos.getInstance(q);
624 
625         qPos.add(classNameId);
626 
627         if (orderByComparator != null) {
628             Object[] values = orderByComparator.getOrderByValues(mbDiscussion);
629 
630             for (Object value : values) {
631                 qPos.add(value);
632             }
633         }
634 
635         List<MBDiscussion> list = q.list();
636 
637         if (list.size() == 2) {
638             return list.get(1);
639         }
640         else {
641             return null;
642         }
643     }
644 
645     public MBDiscussion findByThreadId(long threadId)
646         throws NoSuchDiscussionException, SystemException {
647         MBDiscussion mbDiscussion = fetchByThreadId(threadId);
648 
649         if (mbDiscussion == null) {
650             StringBundler msg = new StringBundler(4);
651 
652             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
653 
654             msg.append("threadId=");
655             msg.append(threadId);
656 
657             msg.append(StringPool.CLOSE_CURLY_BRACE);
658 
659             if (_log.isWarnEnabled()) {
660                 _log.warn(msg.toString());
661             }
662 
663             throw new NoSuchDiscussionException(msg.toString());
664         }
665 
666         return mbDiscussion;
667     }
668 
669     public MBDiscussion fetchByThreadId(long threadId)
670         throws SystemException {
671         return fetchByThreadId(threadId, true);
672     }
673 
674     public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
675         throws SystemException {
676         Object[] finderArgs = new Object[] { threadId };
677 
678         Object result = null;
679 
680         if (retrieveFromCache) {
681             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
682                     finderArgs, this);
683         }
684 
685         if (result == null) {
686             StringBundler query = new StringBundler(2);
687 
688             query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
689 
690             query.append(_FINDER_COLUMN_THREADID_THREADID_2);
691 
692             String sql = query.toString();
693 
694             Session session = null;
695 
696             try {
697                 session = openSession();
698 
699                 Query q = session.createQuery(sql);
700 
701                 QueryPos qPos = QueryPos.getInstance(q);
702 
703                 qPos.add(threadId);
704 
705                 List<MBDiscussion> list = q.list();
706 
707                 result = list;
708 
709                 MBDiscussion mbDiscussion = null;
710 
711                 if (list.isEmpty()) {
712                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
713                         finderArgs, list);
714                 }
715                 else {
716                     mbDiscussion = list.get(0);
717 
718                     cacheResult(mbDiscussion);
719 
720                     if ((mbDiscussion.getThreadId() != threadId)) {
721                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
722                             finderArgs, mbDiscussion);
723                     }
724                 }
725 
726                 return mbDiscussion;
727             }
728             catch (Exception e) {
729                 throw processException(e);
730             }
731             finally {
732                 if (result == null) {
733                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
734                         finderArgs, new ArrayList<MBDiscussion>());
735                 }
736 
737                 closeSession(session);
738             }
739         }
740         else {
741             if (result instanceof List<?>) {
742                 return null;
743             }
744             else {
745                 return (MBDiscussion)result;
746             }
747         }
748     }
749 
750     public MBDiscussion findByC_C(long classNameId, long classPK)
751         throws NoSuchDiscussionException, SystemException {
752         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
753 
754         if (mbDiscussion == null) {
755             StringBundler msg = new StringBundler(6);
756 
757             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
758 
759             msg.append("classNameId=");
760             msg.append(classNameId);
761 
762             msg.append(", classPK=");
763             msg.append(classPK);
764 
765             msg.append(StringPool.CLOSE_CURLY_BRACE);
766 
767             if (_log.isWarnEnabled()) {
768                 _log.warn(msg.toString());
769             }
770 
771             throw new NoSuchDiscussionException(msg.toString());
772         }
773 
774         return mbDiscussion;
775     }
776 
777     public MBDiscussion fetchByC_C(long classNameId, long classPK)
778         throws SystemException {
779         return fetchByC_C(classNameId, classPK, true);
780     }
781 
782     public MBDiscussion fetchByC_C(long classNameId, long classPK,
783         boolean retrieveFromCache) throws SystemException {
784         Object[] finderArgs = new Object[] { classNameId, classPK };
785 
786         Object result = null;
787 
788         if (retrieveFromCache) {
789             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
790                     finderArgs, this);
791         }
792 
793         if (result == null) {
794             StringBundler query = new StringBundler(3);
795 
796             query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
797 
798             query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
799 
800             query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
801 
802             String sql = query.toString();
803 
804             Session session = null;
805 
806             try {
807                 session = openSession();
808 
809                 Query q = session.createQuery(sql);
810 
811                 QueryPos qPos = QueryPos.getInstance(q);
812 
813                 qPos.add(classNameId);
814 
815                 qPos.add(classPK);
816 
817                 List<MBDiscussion> list = q.list();
818 
819                 result = list;
820 
821                 MBDiscussion mbDiscussion = null;
822 
823                 if (list.isEmpty()) {
824                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
825                         finderArgs, list);
826                 }
827                 else {
828                     mbDiscussion = list.get(0);
829 
830                     cacheResult(mbDiscussion);
831 
832                     if ((mbDiscussion.getClassNameId() != classNameId) ||
833                             (mbDiscussion.getClassPK() != classPK)) {
834                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
835                             finderArgs, mbDiscussion);
836                     }
837                 }
838 
839                 return mbDiscussion;
840             }
841             catch (Exception e) {
842                 throw processException(e);
843             }
844             finally {
845                 if (result == null) {
846                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
847                         finderArgs, new ArrayList<MBDiscussion>());
848                 }
849 
850                 closeSession(session);
851             }
852         }
853         else {
854             if (result instanceof List<?>) {
855                 return null;
856             }
857             else {
858                 return (MBDiscussion)result;
859             }
860         }
861     }
862 
863     public List<MBDiscussion> findAll() throws SystemException {
864         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
865     }
866 
867     public List<MBDiscussion> findAll(int start, int end)
868         throws SystemException {
869         return findAll(start, end, null);
870     }
871 
872     public List<MBDiscussion> findAll(int start, int end,
873         OrderByComparator orderByComparator) throws SystemException {
874         Object[] finderArgs = new Object[] {
875                 String.valueOf(start), String.valueOf(end),
876                 String.valueOf(orderByComparator)
877             };
878 
879         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
880                 finderArgs, this);
881 
882         if (list == null) {
883             StringBundler query = null;
884             String sql = null;
885 
886             if (orderByComparator != null) {
887                 query = new StringBundler(2 +
888                         (orderByComparator.getOrderByFields().length * 3));
889 
890                 query.append(_SQL_SELECT_MBDISCUSSION);
891 
892                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
893                     orderByComparator);
894 
895                 sql = query.toString();
896             }
897             else {
898                 sql = _SQL_SELECT_MBDISCUSSION;
899             }
900 
901             Session session = null;
902 
903             try {
904                 session = openSession();
905 
906                 Query q = session.createQuery(sql);
907 
908                 if (orderByComparator == null) {
909                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
910                             start, end, false);
911 
912                     Collections.sort(list);
913                 }
914                 else {
915                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
916                             start, end);
917                 }
918             }
919             catch (Exception e) {
920                 throw processException(e);
921             }
922             finally {
923                 if (list == null) {
924                     list = new ArrayList<MBDiscussion>();
925                 }
926 
927                 cacheResult(list);
928 
929                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
930 
931                 closeSession(session);
932             }
933         }
934 
935         return list;
936     }
937 
938     public void removeByClassNameId(long classNameId) throws SystemException {
939         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
940             remove(mbDiscussion);
941         }
942     }
943 
944     public void removeByThreadId(long threadId)
945         throws NoSuchDiscussionException, SystemException {
946         MBDiscussion mbDiscussion = findByThreadId(threadId);
947 
948         remove(mbDiscussion);
949     }
950 
951     public void removeByC_C(long classNameId, long classPK)
952         throws NoSuchDiscussionException, SystemException {
953         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
954 
955         remove(mbDiscussion);
956     }
957 
958     public void removeAll() throws SystemException {
959         for (MBDiscussion mbDiscussion : findAll()) {
960             remove(mbDiscussion);
961         }
962     }
963 
964     public int countByClassNameId(long classNameId) throws SystemException {
965         Object[] finderArgs = new Object[] { classNameId };
966 
967         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
968                 finderArgs, this);
969 
970         if (count == null) {
971             StringBundler query = new StringBundler(2);
972 
973             query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
974 
975             query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
976 
977             String sql = query.toString();
978 
979             Session session = null;
980 
981             try {
982                 session = openSession();
983 
984                 Query q = session.createQuery(sql);
985 
986                 QueryPos qPos = QueryPos.getInstance(q);
987 
988                 qPos.add(classNameId);
989 
990                 count = (Long)q.uniqueResult();
991             }
992             catch (Exception e) {
993                 throw processException(e);
994             }
995             finally {
996                 if (count == null) {
997                     count = Long.valueOf(0);
998                 }
999 
1000                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1001                    finderArgs, count);
1002
1003                closeSession(session);
1004            }
1005        }
1006
1007        return count.intValue();
1008    }
1009
1010    public int countByThreadId(long threadId) throws SystemException {
1011        Object[] finderArgs = new Object[] { threadId };
1012
1013        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1014                finderArgs, this);
1015
1016        if (count == null) {
1017            StringBundler query = new StringBundler(2);
1018
1019            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1020
1021            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1022
1023            String sql = query.toString();
1024
1025            Session session = null;
1026
1027            try {
1028                session = openSession();
1029
1030                Query q = session.createQuery(sql);
1031
1032                QueryPos qPos = QueryPos.getInstance(q);
1033
1034                qPos.add(threadId);
1035
1036                count = (Long)q.uniqueResult();
1037            }
1038            catch (Exception e) {
1039                throw processException(e);
1040            }
1041            finally {
1042                if (count == null) {
1043                    count = Long.valueOf(0);
1044                }
1045
1046                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1047                    finderArgs, count);
1048
1049                closeSession(session);
1050            }
1051        }
1052
1053        return count.intValue();
1054    }
1055
1056    public int countByC_C(long classNameId, long classPK)
1057        throws SystemException {
1058        Object[] finderArgs = new Object[] { classNameId, classPK };
1059
1060        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1061                finderArgs, this);
1062
1063        if (count == null) {
1064            StringBundler query = new StringBundler(3);
1065
1066            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1067
1068            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1069
1070            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1071
1072            String sql = query.toString();
1073
1074            Session session = null;
1075
1076            try {
1077                session = openSession();
1078
1079                Query q = session.createQuery(sql);
1080
1081                QueryPos qPos = QueryPos.getInstance(q);
1082
1083                qPos.add(classNameId);
1084
1085                qPos.add(classPK);
1086
1087                count = (Long)q.uniqueResult();
1088            }
1089            catch (Exception e) {
1090                throw processException(e);
1091            }
1092            finally {
1093                if (count == null) {
1094                    count = Long.valueOf(0);
1095                }
1096
1097                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1098                    count);
1099
1100                closeSession(session);
1101            }
1102        }
1103
1104        return count.intValue();
1105    }
1106
1107    public int countAll() throws SystemException {
1108        Object[] finderArgs = new Object[0];
1109
1110        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1111                finderArgs, this);
1112
1113        if (count == null) {
1114            Session session = null;
1115
1116            try {
1117                session = openSession();
1118
1119                Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
1120
1121                count = (Long)q.uniqueResult();
1122            }
1123            catch (Exception e) {
1124                throw processException(e);
1125            }
1126            finally {
1127                if (count == null) {
1128                    count = Long.valueOf(0);
1129                }
1130
1131                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1132                    count);
1133
1134                closeSession(session);
1135            }
1136        }
1137
1138        return count.intValue();
1139    }
1140
1141    public void afterPropertiesSet() {
1142        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1143                    com.liferay.portal.util.PropsUtil.get(
1144                        "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1145
1146        if (listenerClassNames.length > 0) {
1147            try {
1148                List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1149
1150                for (String listenerClassName : listenerClassNames) {
1151                    listenersList.add((ModelListener<MBDiscussion>)InstanceFactory.newInstance(
1152                            listenerClassName));
1153                }
1154
1155                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1156            }
1157            catch (Exception e) {
1158                _log.error(e);
1159            }
1160        }
1161    }
1162
1163    public void destroy() {
1164        EntityCacheUtil.removeCache(MBDiscussionImpl.class.getName());
1165        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1166        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1167    }
1168
1169    @BeanReference(type = MBBanPersistence.class)
1170    protected MBBanPersistence mbBanPersistence;
1171    @BeanReference(type = MBCategoryPersistence.class)
1172    protected MBCategoryPersistence mbCategoryPersistence;
1173    @BeanReference(type = MBDiscussionPersistence.class)
1174    protected MBDiscussionPersistence mbDiscussionPersistence;
1175    @BeanReference(type = MBMailingListPersistence.class)
1176    protected MBMailingListPersistence mbMailingListPersistence;
1177    @BeanReference(type = MBMessagePersistence.class)
1178    protected MBMessagePersistence mbMessagePersistence;
1179    @BeanReference(type = MBMessageFlagPersistence.class)
1180    protected MBMessageFlagPersistence mbMessageFlagPersistence;
1181    @BeanReference(type = MBStatsUserPersistence.class)
1182    protected MBStatsUserPersistence mbStatsUserPersistence;
1183    @BeanReference(type = MBThreadPersistence.class)
1184    protected MBThreadPersistence mbThreadPersistence;
1185    @BeanReference(type = ResourcePersistence.class)
1186    protected ResourcePersistence resourcePersistence;
1187    @BeanReference(type = UserPersistence.class)
1188    protected UserPersistence userPersistence;
1189    private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
1190    private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
1191    private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
1192    private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
1193    private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1194    private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
1195    private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
1196    private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
1197    private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
1198    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
1199    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
1200    private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1201}