001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.bean.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.model.CacheModel;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.service.persistence.UserPersistence;
039    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
040    
041    import com.liferay.portlet.messageboards.NoSuchDiscussionException;
042    import com.liferay.portlet.messageboards.model.MBDiscussion;
043    import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
044    import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
045    
046    import java.io.Serializable;
047    
048    import java.util.ArrayList;
049    import java.util.Collections;
050    import java.util.List;
051    
052    /**
053     * The persistence implementation for the message boards discussion service.
054     *
055     * <p>
056     * Caching information and settings can be found in <code>portal.properties</code>
057     * </p>
058     *
059     * @author Brian Wing Shun Chan
060     * @see MBDiscussionPersistence
061     * @see MBDiscussionUtil
062     * @generated
063     */
064    public class MBDiscussionPersistenceImpl extends BasePersistenceImpl<MBDiscussion>
065            implements MBDiscussionPersistence {
066            /*
067             * NOTE FOR DEVELOPERS:
068             *
069             * Never modify or reference this class directly. Always use {@link MBDiscussionUtil} to access the message boards discussion persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
070             */
071            public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
072            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List1";
074            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List2";
076            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSNAMEID =
077                    new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
078                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByClassNameId",
080                            new String[] {
081                                    Long.class.getName(),
082                                    
083                            "java.lang.Integer", "java.lang.Integer",
084                                    "com.liferay.portal.kernel.util.OrderByComparator"
085                            });
086            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID =
087                    new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
088                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByClassNameId",
090                            new String[] { Long.class.getName() },
091                            MBDiscussionModelImpl.CLASSNAMEID_COLUMN_BITMASK);
092            public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
093                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
094                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByClassNameId",
095                            new String[] { Long.class.getName() });
096            public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
097                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
098                            FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
099                            new String[] { Long.class.getName() },
100                            MBDiscussionModelImpl.THREADID_COLUMN_BITMASK);
101            public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
102                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByThreadId",
104                            new String[] { Long.class.getName() });
105            public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
106                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
107                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
108                            new String[] { Long.class.getName(), Long.class.getName() },
109                            MBDiscussionModelImpl.CLASSNAMEID_COLUMN_BITMASK |
110                            MBDiscussionModelImpl.CLASSPK_COLUMN_BITMASK);
111            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
112                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
113                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
114                            new String[] { Long.class.getName(), Long.class.getName() });
115            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
116                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
117                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
118            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
119                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
120                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
121            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
122                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
123                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
124    
125            /**
126             * Caches the message boards discussion in the entity cache if it is enabled.
127             *
128             * @param mbDiscussion the message boards discussion
129             */
130            public void cacheResult(MBDiscussion mbDiscussion) {
131                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
132                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
133    
134                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
135                            new Object[] { Long.valueOf(mbDiscussion.getThreadId()) },
136                            mbDiscussion);
137    
138                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
139                            new Object[] {
140                                    Long.valueOf(mbDiscussion.getClassNameId()),
141                                    Long.valueOf(mbDiscussion.getClassPK())
142                            }, mbDiscussion);
143    
144                    mbDiscussion.resetOriginalValues();
145            }
146    
147            /**
148             * Caches the message boards discussions in the entity cache if it is enabled.
149             *
150             * @param mbDiscussions the message boards discussions
151             */
152            public void cacheResult(List<MBDiscussion> mbDiscussions) {
153                    for (MBDiscussion mbDiscussion : mbDiscussions) {
154                            if (EntityCacheUtil.getResult(
155                                                    MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
156                                                    MBDiscussionImpl.class, mbDiscussion.getPrimaryKey()) == null) {
157                                    cacheResult(mbDiscussion);
158                            }
159                            else {
160                                    mbDiscussion.resetOriginalValues();
161                            }
162                    }
163            }
164    
165            /**
166             * Clears the cache for all message boards discussions.
167             *
168             * <p>
169             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
170             * </p>
171             */
172            @Override
173            public void clearCache() {
174                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
175                            CacheRegistryUtil.clear(MBDiscussionImpl.class.getName());
176                    }
177    
178                    EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
179    
180                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
181                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
182                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
183            }
184    
185            /**
186             * Clears the cache for the message boards discussion.
187             *
188             * <p>
189             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
190             * </p>
191             */
192            @Override
193            public void clearCache(MBDiscussion mbDiscussion) {
194                    EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
195                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
196    
197                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
198                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
199    
200                    clearUniqueFindersCache(mbDiscussion);
201            }
202    
203            @Override
204            public void clearCache(List<MBDiscussion> mbDiscussions) {
205                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
206                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
207    
208                    for (MBDiscussion mbDiscussion : mbDiscussions) {
209                            EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
210                                    MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
211    
212                            clearUniqueFindersCache(mbDiscussion);
213                    }
214            }
215    
216            protected void clearUniqueFindersCache(MBDiscussion mbDiscussion) {
217                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
218                            new Object[] { Long.valueOf(mbDiscussion.getThreadId()) });
219    
220                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
221                            new Object[] {
222                                    Long.valueOf(mbDiscussion.getClassNameId()),
223                                    Long.valueOf(mbDiscussion.getClassPK())
224                            });
225            }
226    
227            /**
228             * Creates a new message boards discussion with the primary key. Does not add the message boards discussion to the database.
229             *
230             * @param discussionId the primary key for the new message boards discussion
231             * @return the new message boards discussion
232             */
233            public MBDiscussion create(long discussionId) {
234                    MBDiscussion mbDiscussion = new MBDiscussionImpl();
235    
236                    mbDiscussion.setNew(true);
237                    mbDiscussion.setPrimaryKey(discussionId);
238    
239                    return mbDiscussion;
240            }
241    
242            /**
243             * Removes the message boards discussion with the primary key from the database. Also notifies the appropriate model listeners.
244             *
245             * @param discussionId the primary key of the message boards discussion
246             * @return the message boards discussion that was removed
247             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
248             * @throws SystemException if a system exception occurred
249             */
250            public MBDiscussion remove(long discussionId)
251                    throws NoSuchDiscussionException, SystemException {
252                    return remove(Long.valueOf(discussionId));
253            }
254    
255            /**
256             * Removes the message boards discussion with the primary key from the database. Also notifies the appropriate model listeners.
257             *
258             * @param primaryKey the primary key of the message boards discussion
259             * @return the message boards discussion that was removed
260             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
261             * @throws SystemException if a system exception occurred
262             */
263            @Override
264            public MBDiscussion remove(Serializable primaryKey)
265                    throws NoSuchDiscussionException, SystemException {
266                    Session session = null;
267    
268                    try {
269                            session = openSession();
270    
271                            MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
272                                            primaryKey);
273    
274                            if (mbDiscussion == null) {
275                                    if (_log.isWarnEnabled()) {
276                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
277                                    }
278    
279                                    throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
280                                            primaryKey);
281                            }
282    
283                            return remove(mbDiscussion);
284                    }
285                    catch (NoSuchDiscussionException nsee) {
286                            throw nsee;
287                    }
288                    catch (Exception e) {
289                            throw processException(e);
290                    }
291                    finally {
292                            closeSession(session);
293                    }
294            }
295    
296            @Override
297            protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
298                    throws SystemException {
299                    mbDiscussion = toUnwrappedModel(mbDiscussion);
300    
301                    Session session = null;
302    
303                    try {
304                            session = openSession();
305    
306                            if (mbDiscussion.isCachedModel()) {
307                                    mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
308                                                    mbDiscussion.getPrimaryKeyObj());
309                            }
310    
311                            session.delete(mbDiscussion);
312                    }
313                    catch (Exception e) {
314                            throw processException(e);
315                    }
316                    finally {
317                            closeSession(session);
318                    }
319    
320                    clearCache(mbDiscussion);
321    
322                    return mbDiscussion;
323            }
324    
325            @Override
326            public MBDiscussion updateImpl(
327                    com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion)
328                    throws SystemException {
329                    mbDiscussion = toUnwrappedModel(mbDiscussion);
330    
331                    boolean isNew = mbDiscussion.isNew();
332    
333                    MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
334    
335                    Session session = null;
336    
337                    try {
338                            session = openSession();
339    
340                            if (mbDiscussion.isNew()) {
341                                    session.save(mbDiscussion);
342    
343                                    mbDiscussion.setNew(false);
344                            }
345                            else {
346                                    session.merge(mbDiscussion);
347                            }
348                    }
349                    catch (Exception e) {
350                            throw processException(e);
351                    }
352                    finally {
353                            closeSession(session);
354                    }
355    
356                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
357    
358                    if (isNew || !MBDiscussionModelImpl.COLUMN_BITMASK_ENABLED) {
359                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
360                    }
361    
362                    else {
363                            if ((mbDiscussionModelImpl.getColumnBitmask() &
364                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID.getColumnBitmask()) != 0) {
365                                    Object[] args = new Object[] {
366                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalClassNameId())
367                                            };
368    
369                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
370                                            args);
371                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID,
372                                            args);
373    
374                                    args = new Object[] {
375                                                    Long.valueOf(mbDiscussionModelImpl.getClassNameId())
376                                            };
377    
378                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
379                                            args);
380                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID,
381                                            args);
382                            }
383                    }
384    
385                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
386                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
387    
388                    if (isNew) {
389                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
390                                    new Object[] { Long.valueOf(mbDiscussion.getThreadId()) },
391                                    mbDiscussion);
392    
393                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
394                                    new Object[] {
395                                            Long.valueOf(mbDiscussion.getClassNameId()),
396                                            Long.valueOf(mbDiscussion.getClassPK())
397                                    }, mbDiscussion);
398                    }
399                    else {
400                            if ((mbDiscussionModelImpl.getColumnBitmask() &
401                                            FINDER_PATH_FETCH_BY_THREADID.getColumnBitmask()) != 0) {
402                                    Object[] args = new Object[] {
403                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalThreadId())
404                                            };
405    
406                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_THREADID, args);
407    
408                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID, args);
409    
410                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
411                                            new Object[] { Long.valueOf(mbDiscussion.getThreadId()) },
412                                            mbDiscussion);
413                            }
414    
415                            if ((mbDiscussionModelImpl.getColumnBitmask() &
416                                            FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
417                                    Object[] args = new Object[] {
418                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalClassNameId()),
419                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalClassPK())
420                                            };
421    
422                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
423    
424                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
425    
426                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
427                                            new Object[] {
428                                                    Long.valueOf(mbDiscussion.getClassNameId()),
429                                                    Long.valueOf(mbDiscussion.getClassPK())
430                                            }, mbDiscussion);
431                            }
432                    }
433    
434                    return mbDiscussion;
435            }
436    
437            protected MBDiscussion toUnwrappedModel(MBDiscussion mbDiscussion) {
438                    if (mbDiscussion instanceof MBDiscussionImpl) {
439                            return mbDiscussion;
440                    }
441    
442                    MBDiscussionImpl mbDiscussionImpl = new MBDiscussionImpl();
443    
444                    mbDiscussionImpl.setNew(mbDiscussion.isNew());
445                    mbDiscussionImpl.setPrimaryKey(mbDiscussion.getPrimaryKey());
446    
447                    mbDiscussionImpl.setDiscussionId(mbDiscussion.getDiscussionId());
448                    mbDiscussionImpl.setClassNameId(mbDiscussion.getClassNameId());
449                    mbDiscussionImpl.setClassPK(mbDiscussion.getClassPK());
450                    mbDiscussionImpl.setThreadId(mbDiscussion.getThreadId());
451    
452                    return mbDiscussionImpl;
453            }
454    
455            /**
456             * Returns the message boards discussion with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
457             *
458             * @param primaryKey the primary key of the message boards discussion
459             * @return the message boards discussion
460             * @throws com.liferay.portal.NoSuchModelException if a message boards discussion with the primary key could not be found
461             * @throws SystemException if a system exception occurred
462             */
463            @Override
464            public MBDiscussion findByPrimaryKey(Serializable primaryKey)
465                    throws NoSuchModelException, SystemException {
466                    return findByPrimaryKey(((Long)primaryKey).longValue());
467            }
468    
469            /**
470             * Returns the message boards discussion with the primary key or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
471             *
472             * @param discussionId the primary key of the message boards discussion
473             * @return the message boards discussion
474             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
475             * @throws SystemException if a system exception occurred
476             */
477            public MBDiscussion findByPrimaryKey(long discussionId)
478                    throws NoSuchDiscussionException, SystemException {
479                    MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
480    
481                    if (mbDiscussion == null) {
482                            if (_log.isWarnEnabled()) {
483                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
484                            }
485    
486                            throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
487                                    discussionId);
488                    }
489    
490                    return mbDiscussion;
491            }
492    
493            /**
494             * Returns the message boards discussion with the primary key or returns <code>null</code> if it could not be found.
495             *
496             * @param primaryKey the primary key of the message boards discussion
497             * @return the message boards discussion, or <code>null</code> if a message boards discussion with the primary key could not be found
498             * @throws SystemException if a system exception occurred
499             */
500            @Override
501            public MBDiscussion fetchByPrimaryKey(Serializable primaryKey)
502                    throws SystemException {
503                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
504            }
505    
506            /**
507             * Returns the message boards discussion with the primary key or returns <code>null</code> if it could not be found.
508             *
509             * @param discussionId the primary key of the message boards discussion
510             * @return the message boards discussion, or <code>null</code> if a message boards discussion with the primary key could not be found
511             * @throws SystemException if a system exception occurred
512             */
513            public MBDiscussion fetchByPrimaryKey(long discussionId)
514                    throws SystemException {
515                    MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
516                                    MBDiscussionImpl.class, discussionId);
517    
518                    if (mbDiscussion == _nullMBDiscussion) {
519                            return null;
520                    }
521    
522                    if (mbDiscussion == null) {
523                            Session session = null;
524    
525                            boolean hasException = false;
526    
527                            try {
528                                    session = openSession();
529    
530                                    mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
531                                                    Long.valueOf(discussionId));
532                            }
533                            catch (Exception e) {
534                                    hasException = true;
535    
536                                    throw processException(e);
537                            }
538                            finally {
539                                    if (mbDiscussion != null) {
540                                            cacheResult(mbDiscussion);
541                                    }
542                                    else if (!hasException) {
543                                            EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
544                                                    MBDiscussionImpl.class, discussionId, _nullMBDiscussion);
545                                    }
546    
547                                    closeSession(session);
548                            }
549                    }
550    
551                    return mbDiscussion;
552            }
553    
554            /**
555             * Returns all the message boards discussions where classNameId = &#63;.
556             *
557             * @param classNameId the class name ID
558             * @return the matching message boards discussions
559             * @throws SystemException if a system exception occurred
560             */
561            public List<MBDiscussion> findByClassNameId(long classNameId)
562                    throws SystemException {
563                    return findByClassNameId(classNameId, QueryUtil.ALL_POS,
564                            QueryUtil.ALL_POS, null);
565            }
566    
567            /**
568             * Returns a range of all the message boards discussions where classNameId = &#63;.
569             *
570             * <p>
571             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
572             * </p>
573             *
574             * @param classNameId the class name ID
575             * @param start the lower bound of the range of message boards discussions
576             * @param end the upper bound of the range of message boards discussions (not inclusive)
577             * @return the range of matching message boards discussions
578             * @throws SystemException if a system exception occurred
579             */
580            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
581                    int end) throws SystemException {
582                    return findByClassNameId(classNameId, start, end, null);
583            }
584    
585            /**
586             * Returns an ordered range of all the message boards discussions where classNameId = &#63;.
587             *
588             * <p>
589             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
590             * </p>
591             *
592             * @param classNameId the class name ID
593             * @param start the lower bound of the range of message boards discussions
594             * @param end the upper bound of the range of message boards discussions (not inclusive)
595             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
596             * @return the ordered range of matching message boards discussions
597             * @throws SystemException if a system exception occurred
598             */
599            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
600                    int end, OrderByComparator orderByComparator) throws SystemException {
601                    FinderPath finderPath = null;
602                    Object[] finderArgs = null;
603    
604                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
605                                    (orderByComparator == null)) {
606                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID;
607                            finderArgs = new Object[] { classNameId };
608                    }
609                    else {
610                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSNAMEID;
611                            finderArgs = new Object[] { classNameId, start, end, orderByComparator };
612                    }
613    
614                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
615                                    finderArgs, this);
616    
617                    if ((list != null) && !list.isEmpty()) {
618                            for (MBDiscussion mbDiscussion : list) {
619                                    if ((classNameId != mbDiscussion.getClassNameId())) {
620                                            list = null;
621    
622                                            break;
623                                    }
624                            }
625                    }
626    
627                    if (list == null) {
628                            StringBundler query = null;
629    
630                            if (orderByComparator != null) {
631                                    query = new StringBundler(3 +
632                                                    (orderByComparator.getOrderByFields().length * 3));
633                            }
634                            else {
635                                    query = new StringBundler(2);
636                            }
637    
638                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
639    
640                            query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
641    
642                            if (orderByComparator != null) {
643                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
644                                            orderByComparator);
645                            }
646    
647                            String sql = query.toString();
648    
649                            Session session = null;
650    
651                            try {
652                                    session = openSession();
653    
654                                    Query q = session.createQuery(sql);
655    
656                                    QueryPos qPos = QueryPos.getInstance(q);
657    
658                                    qPos.add(classNameId);
659    
660                                    list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
661                                                    start, end);
662                            }
663                            catch (Exception e) {
664                                    throw processException(e);
665                            }
666                            finally {
667                                    if (list == null) {
668                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
669                                    }
670                                    else {
671                                            cacheResult(list);
672    
673                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
674                                    }
675    
676                                    closeSession(session);
677                            }
678                    }
679    
680                    return list;
681            }
682    
683            /**
684             * Returns the first message boards discussion in the ordered set where classNameId = &#63;.
685             *
686             * @param classNameId the class name ID
687             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
688             * @return the first matching message boards discussion
689             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
690             * @throws SystemException if a system exception occurred
691             */
692            public MBDiscussion findByClassNameId_First(long classNameId,
693                    OrderByComparator orderByComparator)
694                    throws NoSuchDiscussionException, SystemException {
695                    MBDiscussion mbDiscussion = fetchByClassNameId_First(classNameId,
696                                    orderByComparator);
697    
698                    if (mbDiscussion != null) {
699                            return mbDiscussion;
700                    }
701    
702                    StringBundler msg = new StringBundler(4);
703    
704                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
705    
706                    msg.append("classNameId=");
707                    msg.append(classNameId);
708    
709                    msg.append(StringPool.CLOSE_CURLY_BRACE);
710    
711                    throw new NoSuchDiscussionException(msg.toString());
712            }
713    
714            /**
715             * Returns the first message boards discussion in the ordered set where classNameId = &#63;.
716             *
717             * @param classNameId the class name ID
718             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
719             * @return the first matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
720             * @throws SystemException if a system exception occurred
721             */
722            public MBDiscussion fetchByClassNameId_First(long classNameId,
723                    OrderByComparator orderByComparator) throws SystemException {
724                    List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1,
725                                    orderByComparator);
726    
727                    if (!list.isEmpty()) {
728                            return list.get(0);
729                    }
730    
731                    return null;
732            }
733    
734            /**
735             * Returns the last message boards discussion in the ordered set where classNameId = &#63;.
736             *
737             * @param classNameId the class name ID
738             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
739             * @return the last matching message boards discussion
740             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
741             * @throws SystemException if a system exception occurred
742             */
743            public MBDiscussion findByClassNameId_Last(long classNameId,
744                    OrderByComparator orderByComparator)
745                    throws NoSuchDiscussionException, SystemException {
746                    MBDiscussion mbDiscussion = fetchByClassNameId_Last(classNameId,
747                                    orderByComparator);
748    
749                    if (mbDiscussion != null) {
750                            return mbDiscussion;
751                    }
752    
753                    StringBundler msg = new StringBundler(4);
754    
755                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
756    
757                    msg.append("classNameId=");
758                    msg.append(classNameId);
759    
760                    msg.append(StringPool.CLOSE_CURLY_BRACE);
761    
762                    throw new NoSuchDiscussionException(msg.toString());
763            }
764    
765            /**
766             * Returns the last message boards discussion in the ordered set where classNameId = &#63;.
767             *
768             * @param classNameId the class name ID
769             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
770             * @return the last matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
771             * @throws SystemException if a system exception occurred
772             */
773            public MBDiscussion fetchByClassNameId_Last(long classNameId,
774                    OrderByComparator orderByComparator) throws SystemException {
775                    int count = countByClassNameId(classNameId);
776    
777                    List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
778                                    count, orderByComparator);
779    
780                    if (!list.isEmpty()) {
781                            return list.get(0);
782                    }
783    
784                    return null;
785            }
786    
787            /**
788             * Returns the message boards discussions before and after the current message boards discussion in the ordered set where classNameId = &#63;.
789             *
790             * @param discussionId the primary key of the current message boards discussion
791             * @param classNameId the class name ID
792             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
793             * @return the previous, current, and next message boards discussion
794             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
795             * @throws SystemException if a system exception occurred
796             */
797            public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
798                    long classNameId, OrderByComparator orderByComparator)
799                    throws NoSuchDiscussionException, SystemException {
800                    MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
801    
802                    Session session = null;
803    
804                    try {
805                            session = openSession();
806    
807                            MBDiscussion[] array = new MBDiscussionImpl[3];
808    
809                            array[0] = getByClassNameId_PrevAndNext(session, mbDiscussion,
810                                            classNameId, orderByComparator, true);
811    
812                            array[1] = mbDiscussion;
813    
814                            array[2] = getByClassNameId_PrevAndNext(session, mbDiscussion,
815                                            classNameId, orderByComparator, false);
816    
817                            return array;
818                    }
819                    catch (Exception e) {
820                            throw processException(e);
821                    }
822                    finally {
823                            closeSession(session);
824                    }
825            }
826    
827            protected MBDiscussion getByClassNameId_PrevAndNext(Session session,
828                    MBDiscussion mbDiscussion, long classNameId,
829                    OrderByComparator orderByComparator, boolean previous) {
830                    StringBundler query = null;
831    
832                    if (orderByComparator != null) {
833                            query = new StringBundler(6 +
834                                            (orderByComparator.getOrderByFields().length * 6));
835                    }
836                    else {
837                            query = new StringBundler(3);
838                    }
839    
840                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
841    
842                    query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
843    
844                    if (orderByComparator != null) {
845                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
846    
847                            if (orderByConditionFields.length > 0) {
848                                    query.append(WHERE_AND);
849                            }
850    
851                            for (int i = 0; i < orderByConditionFields.length; i++) {
852                                    query.append(_ORDER_BY_ENTITY_ALIAS);
853                                    query.append(orderByConditionFields[i]);
854    
855                                    if ((i + 1) < orderByConditionFields.length) {
856                                            if (orderByComparator.isAscending() ^ previous) {
857                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
858                                            }
859                                            else {
860                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
861                                            }
862                                    }
863                                    else {
864                                            if (orderByComparator.isAscending() ^ previous) {
865                                                    query.append(WHERE_GREATER_THAN);
866                                            }
867                                            else {
868                                                    query.append(WHERE_LESSER_THAN);
869                                            }
870                                    }
871                            }
872    
873                            query.append(ORDER_BY_CLAUSE);
874    
875                            String[] orderByFields = orderByComparator.getOrderByFields();
876    
877                            for (int i = 0; i < orderByFields.length; i++) {
878                                    query.append(_ORDER_BY_ENTITY_ALIAS);
879                                    query.append(orderByFields[i]);
880    
881                                    if ((i + 1) < orderByFields.length) {
882                                            if (orderByComparator.isAscending() ^ previous) {
883                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
884                                            }
885                                            else {
886                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
887                                            }
888                                    }
889                                    else {
890                                            if (orderByComparator.isAscending() ^ previous) {
891                                                    query.append(ORDER_BY_ASC);
892                                            }
893                                            else {
894                                                    query.append(ORDER_BY_DESC);
895                                            }
896                                    }
897                            }
898                    }
899    
900                    String sql = query.toString();
901    
902                    Query q = session.createQuery(sql);
903    
904                    q.setFirstResult(0);
905                    q.setMaxResults(2);
906    
907                    QueryPos qPos = QueryPos.getInstance(q);
908    
909                    qPos.add(classNameId);
910    
911                    if (orderByComparator != null) {
912                            Object[] values = orderByComparator.getOrderByConditionValues(mbDiscussion);
913    
914                            for (Object value : values) {
915                                    qPos.add(value);
916                            }
917                    }
918    
919                    List<MBDiscussion> list = q.list();
920    
921                    if (list.size() == 2) {
922                            return list.get(1);
923                    }
924                    else {
925                            return null;
926                    }
927            }
928    
929            /**
930             * Returns the message boards discussion where threadId = &#63; or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
931             *
932             * @param threadId the thread ID
933             * @return the matching message boards discussion
934             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
935             * @throws SystemException if a system exception occurred
936             */
937            public MBDiscussion findByThreadId(long threadId)
938                    throws NoSuchDiscussionException, SystemException {
939                    MBDiscussion mbDiscussion = fetchByThreadId(threadId);
940    
941                    if (mbDiscussion == null) {
942                            StringBundler msg = new StringBundler(4);
943    
944                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
945    
946                            msg.append("threadId=");
947                            msg.append(threadId);
948    
949                            msg.append(StringPool.CLOSE_CURLY_BRACE);
950    
951                            if (_log.isWarnEnabled()) {
952                                    _log.warn(msg.toString());
953                            }
954    
955                            throw new NoSuchDiscussionException(msg.toString());
956                    }
957    
958                    return mbDiscussion;
959            }
960    
961            /**
962             * Returns the message boards discussion where threadId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
963             *
964             * @param threadId the thread ID
965             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
966             * @throws SystemException if a system exception occurred
967             */
968            public MBDiscussion fetchByThreadId(long threadId)
969                    throws SystemException {
970                    return fetchByThreadId(threadId, true);
971            }
972    
973            /**
974             * Returns the message boards discussion where threadId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
975             *
976             * @param threadId the thread ID
977             * @param retrieveFromCache whether to use the finder cache
978             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
979             * @throws SystemException if a system exception occurred
980             */
981            public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
982                    throws SystemException {
983                    Object[] finderArgs = new Object[] { threadId };
984    
985                    Object result = null;
986    
987                    if (retrieveFromCache) {
988                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
989                                            finderArgs, this);
990                    }
991    
992                    if (result instanceof MBDiscussion) {
993                            MBDiscussion mbDiscussion = (MBDiscussion)result;
994    
995                            if ((threadId != mbDiscussion.getThreadId())) {
996                                    result = null;
997                            }
998                    }
999    
1000                    if (result == null) {
1001                            StringBundler query = new StringBundler(2);
1002    
1003                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
1004    
1005                            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1006    
1007                            String sql = query.toString();
1008    
1009                            Session session = null;
1010    
1011                            try {
1012                                    session = openSession();
1013    
1014                                    Query q = session.createQuery(sql);
1015    
1016                                    QueryPos qPos = QueryPos.getInstance(q);
1017    
1018                                    qPos.add(threadId);
1019    
1020                                    List<MBDiscussion> list = q.list();
1021    
1022                                    result = list;
1023    
1024                                    MBDiscussion mbDiscussion = null;
1025    
1026                                    if (list.isEmpty()) {
1027                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
1028                                                    finderArgs, list);
1029                                    }
1030                                    else {
1031                                            mbDiscussion = list.get(0);
1032    
1033                                            cacheResult(mbDiscussion);
1034    
1035                                            if ((mbDiscussion.getThreadId() != threadId)) {
1036                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
1037                                                            finderArgs, mbDiscussion);
1038                                            }
1039                                    }
1040    
1041                                    return mbDiscussion;
1042                            }
1043                            catch (Exception e) {
1044                                    throw processException(e);
1045                            }
1046                            finally {
1047                                    if (result == null) {
1048                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
1049                                                    finderArgs);
1050                                    }
1051    
1052                                    closeSession(session);
1053                            }
1054                    }
1055                    else {
1056                            if (result instanceof List<?>) {
1057                                    return null;
1058                            }
1059                            else {
1060                                    return (MBDiscussion)result;
1061                            }
1062                    }
1063            }
1064    
1065            /**
1066             * Returns the message boards discussion where classNameId = &#63; and classPK = &#63; or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
1067             *
1068             * @param classNameId the class name ID
1069             * @param classPK the class p k
1070             * @return the matching message boards discussion
1071             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1072             * @throws SystemException if a system exception occurred
1073             */
1074            public MBDiscussion findByC_C(long classNameId, long classPK)
1075                    throws NoSuchDiscussionException, SystemException {
1076                    MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
1077    
1078                    if (mbDiscussion == null) {
1079                            StringBundler msg = new StringBundler(6);
1080    
1081                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1082    
1083                            msg.append("classNameId=");
1084                            msg.append(classNameId);
1085    
1086                            msg.append(", classPK=");
1087                            msg.append(classPK);
1088    
1089                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1090    
1091                            if (_log.isWarnEnabled()) {
1092                                    _log.warn(msg.toString());
1093                            }
1094    
1095                            throw new NoSuchDiscussionException(msg.toString());
1096                    }
1097    
1098                    return mbDiscussion;
1099            }
1100    
1101            /**
1102             * Returns the message boards discussion where classNameId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1103             *
1104             * @param classNameId the class name ID
1105             * @param classPK the class p k
1106             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1107             * @throws SystemException if a system exception occurred
1108             */
1109            public MBDiscussion fetchByC_C(long classNameId, long classPK)
1110                    throws SystemException {
1111                    return fetchByC_C(classNameId, classPK, true);
1112            }
1113    
1114            /**
1115             * Returns the message boards discussion where classNameId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1116             *
1117             * @param classNameId the class name ID
1118             * @param classPK the class p k
1119             * @param retrieveFromCache whether to use the finder cache
1120             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1121             * @throws SystemException if a system exception occurred
1122             */
1123            public MBDiscussion fetchByC_C(long classNameId, long classPK,
1124                    boolean retrieveFromCache) throws SystemException {
1125                    Object[] finderArgs = new Object[] { classNameId, classPK };
1126    
1127                    Object result = null;
1128    
1129                    if (retrieveFromCache) {
1130                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
1131                                            finderArgs, this);
1132                    }
1133    
1134                    if (result instanceof MBDiscussion) {
1135                            MBDiscussion mbDiscussion = (MBDiscussion)result;
1136    
1137                            if ((classNameId != mbDiscussion.getClassNameId()) ||
1138                                            (classPK != mbDiscussion.getClassPK())) {
1139                                    result = null;
1140                            }
1141                    }
1142    
1143                    if (result == null) {
1144                            StringBundler query = new StringBundler(3);
1145    
1146                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
1147    
1148                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1149    
1150                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1151    
1152                            String sql = query.toString();
1153    
1154                            Session session = null;
1155    
1156                            try {
1157                                    session = openSession();
1158    
1159                                    Query q = session.createQuery(sql);
1160    
1161                                    QueryPos qPos = QueryPos.getInstance(q);
1162    
1163                                    qPos.add(classNameId);
1164    
1165                                    qPos.add(classPK);
1166    
1167                                    List<MBDiscussion> list = q.list();
1168    
1169                                    result = list;
1170    
1171                                    MBDiscussion mbDiscussion = null;
1172    
1173                                    if (list.isEmpty()) {
1174                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
1175                                                    finderArgs, list);
1176                                    }
1177                                    else {
1178                                            mbDiscussion = list.get(0);
1179    
1180                                            cacheResult(mbDiscussion);
1181    
1182                                            if ((mbDiscussion.getClassNameId() != classNameId) ||
1183                                                            (mbDiscussion.getClassPK() != classPK)) {
1184                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
1185                                                            finderArgs, mbDiscussion);
1186                                            }
1187                                    }
1188    
1189                                    return mbDiscussion;
1190                            }
1191                            catch (Exception e) {
1192                                    throw processException(e);
1193                            }
1194                            finally {
1195                                    if (result == null) {
1196                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
1197                                                    finderArgs);
1198                                    }
1199    
1200                                    closeSession(session);
1201                            }
1202                    }
1203                    else {
1204                            if (result instanceof List<?>) {
1205                                    return null;
1206                            }
1207                            else {
1208                                    return (MBDiscussion)result;
1209                            }
1210                    }
1211            }
1212    
1213            /**
1214             * Returns all the message boards discussions.
1215             *
1216             * @return the message boards discussions
1217             * @throws SystemException if a system exception occurred
1218             */
1219            public List<MBDiscussion> findAll() throws SystemException {
1220                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1221            }
1222    
1223            /**
1224             * Returns a range of all the message boards discussions.
1225             *
1226             * <p>
1227             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1228             * </p>
1229             *
1230             * @param start the lower bound of the range of message boards discussions
1231             * @param end the upper bound of the range of message boards discussions (not inclusive)
1232             * @return the range of message boards discussions
1233             * @throws SystemException if a system exception occurred
1234             */
1235            public List<MBDiscussion> findAll(int start, int end)
1236                    throws SystemException {
1237                    return findAll(start, end, null);
1238            }
1239    
1240            /**
1241             * Returns an ordered range of all the message boards discussions.
1242             *
1243             * <p>
1244             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1245             * </p>
1246             *
1247             * @param start the lower bound of the range of message boards discussions
1248             * @param end the upper bound of the range of message boards discussions (not inclusive)
1249             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1250             * @return the ordered range of message boards discussions
1251             * @throws SystemException if a system exception occurred
1252             */
1253            public List<MBDiscussion> findAll(int start, int end,
1254                    OrderByComparator orderByComparator) throws SystemException {
1255                    FinderPath finderPath = null;
1256                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
1257    
1258                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1259                                    (orderByComparator == null)) {
1260                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1261                            finderArgs = FINDER_ARGS_EMPTY;
1262                    }
1263                    else {
1264                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1265                            finderArgs = new Object[] { start, end, orderByComparator };
1266                    }
1267    
1268                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
1269                                    finderArgs, this);
1270    
1271                    if (list == null) {
1272                            StringBundler query = null;
1273                            String sql = null;
1274    
1275                            if (orderByComparator != null) {
1276                                    query = new StringBundler(2 +
1277                                                    (orderByComparator.getOrderByFields().length * 3));
1278    
1279                                    query.append(_SQL_SELECT_MBDISCUSSION);
1280    
1281                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1282                                            orderByComparator);
1283    
1284                                    sql = query.toString();
1285                            }
1286                            else {
1287                                    sql = _SQL_SELECT_MBDISCUSSION;
1288                            }
1289    
1290                            Session session = null;
1291    
1292                            try {
1293                                    session = openSession();
1294    
1295                                    Query q = session.createQuery(sql);
1296    
1297                                    if (orderByComparator == null) {
1298                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1299                                                            start, end, false);
1300    
1301                                            Collections.sort(list);
1302                                    }
1303                                    else {
1304                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1305                                                            start, end);
1306                                    }
1307                            }
1308                            catch (Exception e) {
1309                                    throw processException(e);
1310                            }
1311                            finally {
1312                                    if (list == null) {
1313                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
1314                                    }
1315                                    else {
1316                                            cacheResult(list);
1317    
1318                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
1319                                    }
1320    
1321                                    closeSession(session);
1322                            }
1323                    }
1324    
1325                    return list;
1326            }
1327    
1328            /**
1329             * Removes all the message boards discussions where classNameId = &#63; from the database.
1330             *
1331             * @param classNameId the class name ID
1332             * @throws SystemException if a system exception occurred
1333             */
1334            public void removeByClassNameId(long classNameId) throws SystemException {
1335                    for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
1336                            remove(mbDiscussion);
1337                    }
1338            }
1339    
1340            /**
1341             * Removes the message boards discussion where threadId = &#63; from the database.
1342             *
1343             * @param threadId the thread ID
1344             * @return the message boards discussion that was removed
1345             * @throws SystemException if a system exception occurred
1346             */
1347            public MBDiscussion removeByThreadId(long threadId)
1348                    throws NoSuchDiscussionException, SystemException {
1349                    MBDiscussion mbDiscussion = findByThreadId(threadId);
1350    
1351                    return remove(mbDiscussion);
1352            }
1353    
1354            /**
1355             * Removes the message boards discussion where classNameId = &#63; and classPK = &#63; from the database.
1356             *
1357             * @param classNameId the class name ID
1358             * @param classPK the class p k
1359             * @return the message boards discussion that was removed
1360             * @throws SystemException if a system exception occurred
1361             */
1362            public MBDiscussion removeByC_C(long classNameId, long classPK)
1363                    throws NoSuchDiscussionException, SystemException {
1364                    MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
1365    
1366                    return remove(mbDiscussion);
1367            }
1368    
1369            /**
1370             * Removes all the message boards discussions from the database.
1371             *
1372             * @throws SystemException if a system exception occurred
1373             */
1374            public void removeAll() throws SystemException {
1375                    for (MBDiscussion mbDiscussion : findAll()) {
1376                            remove(mbDiscussion);
1377                    }
1378            }
1379    
1380            /**
1381             * Returns the number of message boards discussions where classNameId = &#63;.
1382             *
1383             * @param classNameId the class name ID
1384             * @return the number of matching message boards discussions
1385             * @throws SystemException if a system exception occurred
1386             */
1387            public int countByClassNameId(long classNameId) throws SystemException {
1388                    Object[] finderArgs = new Object[] { classNameId };
1389    
1390                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1391                                    finderArgs, this);
1392    
1393                    if (count == null) {
1394                            StringBundler query = new StringBundler(2);
1395    
1396                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1397    
1398                            query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
1399    
1400                            String sql = query.toString();
1401    
1402                            Session session = null;
1403    
1404                            try {
1405                                    session = openSession();
1406    
1407                                    Query q = session.createQuery(sql);
1408    
1409                                    QueryPos qPos = QueryPos.getInstance(q);
1410    
1411                                    qPos.add(classNameId);
1412    
1413                                    count = (Long)q.uniqueResult();
1414                            }
1415                            catch (Exception e) {
1416                                    throw processException(e);
1417                            }
1418                            finally {
1419                                    if (count == null) {
1420                                            count = Long.valueOf(0);
1421                                    }
1422    
1423                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1424                                            finderArgs, count);
1425    
1426                                    closeSession(session);
1427                            }
1428                    }
1429    
1430                    return count.intValue();
1431            }
1432    
1433            /**
1434             * Returns the number of message boards discussions where threadId = &#63;.
1435             *
1436             * @param threadId the thread ID
1437             * @return the number of matching message boards discussions
1438             * @throws SystemException if a system exception occurred
1439             */
1440            public int countByThreadId(long threadId) throws SystemException {
1441                    Object[] finderArgs = new Object[] { threadId };
1442    
1443                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1444                                    finderArgs, this);
1445    
1446                    if (count == null) {
1447                            StringBundler query = new StringBundler(2);
1448    
1449                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1450    
1451                            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1452    
1453                            String sql = query.toString();
1454    
1455                            Session session = null;
1456    
1457                            try {
1458                                    session = openSession();
1459    
1460                                    Query q = session.createQuery(sql);
1461    
1462                                    QueryPos qPos = QueryPos.getInstance(q);
1463    
1464                                    qPos.add(threadId);
1465    
1466                                    count = (Long)q.uniqueResult();
1467                            }
1468                            catch (Exception e) {
1469                                    throw processException(e);
1470                            }
1471                            finally {
1472                                    if (count == null) {
1473                                            count = Long.valueOf(0);
1474                                    }
1475    
1476                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1477                                            finderArgs, count);
1478    
1479                                    closeSession(session);
1480                            }
1481                    }
1482    
1483                    return count.intValue();
1484            }
1485    
1486            /**
1487             * Returns the number of message boards discussions where classNameId = &#63; and classPK = &#63;.
1488             *
1489             * @param classNameId the class name ID
1490             * @param classPK the class p k
1491             * @return the number of matching message boards discussions
1492             * @throws SystemException if a system exception occurred
1493             */
1494            public int countByC_C(long classNameId, long classPK)
1495                    throws SystemException {
1496                    Object[] finderArgs = new Object[] { classNameId, classPK };
1497    
1498                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1499                                    finderArgs, this);
1500    
1501                    if (count == null) {
1502                            StringBundler query = new StringBundler(3);
1503    
1504                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1505    
1506                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1507    
1508                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1509    
1510                            String sql = query.toString();
1511    
1512                            Session session = null;
1513    
1514                            try {
1515                                    session = openSession();
1516    
1517                                    Query q = session.createQuery(sql);
1518    
1519                                    QueryPos qPos = QueryPos.getInstance(q);
1520    
1521                                    qPos.add(classNameId);
1522    
1523                                    qPos.add(classPK);
1524    
1525                                    count = (Long)q.uniqueResult();
1526                            }
1527                            catch (Exception e) {
1528                                    throw processException(e);
1529                            }
1530                            finally {
1531                                    if (count == null) {
1532                                            count = Long.valueOf(0);
1533                                    }
1534    
1535                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1536                                            count);
1537    
1538                                    closeSession(session);
1539                            }
1540                    }
1541    
1542                    return count.intValue();
1543            }
1544    
1545            /**
1546             * Returns the number of message boards discussions.
1547             *
1548             * @return the number of message boards discussions
1549             * @throws SystemException if a system exception occurred
1550             */
1551            public int countAll() throws SystemException {
1552                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1553                                    FINDER_ARGS_EMPTY, this);
1554    
1555                    if (count == null) {
1556                            Session session = null;
1557    
1558                            try {
1559                                    session = openSession();
1560    
1561                                    Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
1562    
1563                                    count = (Long)q.uniqueResult();
1564                            }
1565                            catch (Exception e) {
1566                                    throw processException(e);
1567                            }
1568                            finally {
1569                                    if (count == null) {
1570                                            count = Long.valueOf(0);
1571                                    }
1572    
1573                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1574                                            FINDER_ARGS_EMPTY, count);
1575    
1576                                    closeSession(session);
1577                            }
1578                    }
1579    
1580                    return count.intValue();
1581            }
1582    
1583            /**
1584             * Initializes the message boards discussion persistence.
1585             */
1586            public void afterPropertiesSet() {
1587                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1588                                            com.liferay.portal.util.PropsUtil.get(
1589                                                    "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1590    
1591                    if (listenerClassNames.length > 0) {
1592                            try {
1593                                    List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1594    
1595                                    for (String listenerClassName : listenerClassNames) {
1596                                            listenersList.add((ModelListener<MBDiscussion>)InstanceFactory.newInstance(
1597                                                            listenerClassName));
1598                                    }
1599    
1600                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1601                            }
1602                            catch (Exception e) {
1603                                    _log.error(e);
1604                            }
1605                    }
1606            }
1607    
1608            public void destroy() {
1609                    EntityCacheUtil.removeCache(MBDiscussionImpl.class.getName());
1610                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1611                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1612            }
1613    
1614            @BeanReference(type = MBBanPersistence.class)
1615            protected MBBanPersistence mbBanPersistence;
1616            @BeanReference(type = MBCategoryPersistence.class)
1617            protected MBCategoryPersistence mbCategoryPersistence;
1618            @BeanReference(type = MBDiscussionPersistence.class)
1619            protected MBDiscussionPersistence mbDiscussionPersistence;
1620            @BeanReference(type = MBMailingListPersistence.class)
1621            protected MBMailingListPersistence mbMailingListPersistence;
1622            @BeanReference(type = MBMessagePersistence.class)
1623            protected MBMessagePersistence mbMessagePersistence;
1624            @BeanReference(type = MBStatsUserPersistence.class)
1625            protected MBStatsUserPersistence mbStatsUserPersistence;
1626            @BeanReference(type = MBThreadPersistence.class)
1627            protected MBThreadPersistence mbThreadPersistence;
1628            @BeanReference(type = MBThreadFlagPersistence.class)
1629            protected MBThreadFlagPersistence mbThreadFlagPersistence;
1630            @BeanReference(type = UserPersistence.class)
1631            protected UserPersistence userPersistence;
1632            private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
1633            private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
1634            private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
1635            private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
1636            private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1637            private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
1638            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
1639            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
1640            private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
1641            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
1642            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
1643            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1644            private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1645            private static MBDiscussion _nullMBDiscussion = new MBDiscussionImpl() {
1646                            @Override
1647                            public Object clone() {
1648                                    return this;
1649                            }
1650    
1651                            @Override
1652                            public CacheModel<MBDiscussion> toCacheModel() {
1653                                    return _nullMBDiscussionCacheModel;
1654                            }
1655                    };
1656    
1657            private static CacheModel<MBDiscussion> _nullMBDiscussionCacheModel = new CacheModel<MBDiscussion>() {
1658                            public MBDiscussion toEntityModel() {
1659                                    return _nullMBDiscussion;
1660                            }
1661                    };
1662    }