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