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