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_WITH_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_WITHOUT_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                    else {
354                            if ((mbDiscussionModelImpl.getColumnBitmask() &
355                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID.getColumnBitmask()) != 0) {
356                                    Object[] args = new Object[] {
357                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalClassNameId())
358                                            };
359    
360                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
361                                            args);
362                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID,
363                                            args);
364    
365                                    args = new Object[] {
366                                                    Long.valueOf(mbDiscussionModelImpl.getClassNameId())
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                    }
375    
376                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
377                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
378    
379                    if (isNew) {
380                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
381                                    new Object[] { Long.valueOf(mbDiscussion.getThreadId()) },
382                                    mbDiscussion);
383    
384                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
385                                    new Object[] {
386                                            Long.valueOf(mbDiscussion.getClassNameId()),
387                                            Long.valueOf(mbDiscussion.getClassPK())
388                                    }, mbDiscussion);
389                    }
390                    else {
391                            if ((mbDiscussionModelImpl.getColumnBitmask() &
392                                            FINDER_PATH_FETCH_BY_THREADID.getColumnBitmask()) != 0) {
393                                    Object[] args = new Object[] {
394                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalThreadId())
395                                            };
396    
397                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_THREADID, args);
398                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID, args);
399    
400                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
401                                            new Object[] { Long.valueOf(mbDiscussion.getThreadId()) },
402                                            mbDiscussion);
403                            }
404    
405                            if ((mbDiscussionModelImpl.getColumnBitmask() &
406                                            FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
407                                    Object[] args = new Object[] {
408                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalClassNameId()),
409                                                    Long.valueOf(mbDiscussionModelImpl.getOriginalClassPK())
410                                            };
411    
412                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
413                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
414    
415                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
416                                            new Object[] {
417                                                    Long.valueOf(mbDiscussion.getClassNameId()),
418                                                    Long.valueOf(mbDiscussion.getClassPK())
419                                            }, mbDiscussion);
420                            }
421                    }
422    
423                    return mbDiscussion;
424            }
425    
426            protected MBDiscussion toUnwrappedModel(MBDiscussion mbDiscussion) {
427                    if (mbDiscussion instanceof MBDiscussionImpl) {
428                            return mbDiscussion;
429                    }
430    
431                    MBDiscussionImpl mbDiscussionImpl = new MBDiscussionImpl();
432    
433                    mbDiscussionImpl.setNew(mbDiscussion.isNew());
434                    mbDiscussionImpl.setPrimaryKey(mbDiscussion.getPrimaryKey());
435    
436                    mbDiscussionImpl.setDiscussionId(mbDiscussion.getDiscussionId());
437                    mbDiscussionImpl.setClassNameId(mbDiscussion.getClassNameId());
438                    mbDiscussionImpl.setClassPK(mbDiscussion.getClassPK());
439                    mbDiscussionImpl.setThreadId(mbDiscussion.getThreadId());
440    
441                    return mbDiscussionImpl;
442            }
443    
444            /**
445             * Returns the message boards discussion with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
446             *
447             * @param primaryKey the primary key of the message boards discussion
448             * @return the message boards discussion
449             * @throws com.liferay.portal.NoSuchModelException if a message boards discussion with the primary key could not be found
450             * @throws SystemException if a system exception occurred
451             */
452            @Override
453            public MBDiscussion findByPrimaryKey(Serializable primaryKey)
454                    throws NoSuchModelException, SystemException {
455                    return findByPrimaryKey(((Long)primaryKey).longValue());
456            }
457    
458            /**
459             * Returns the message boards discussion with the primary key or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
460             *
461             * @param discussionId the primary key of the message boards discussion
462             * @return the message boards discussion
463             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
464             * @throws SystemException if a system exception occurred
465             */
466            public MBDiscussion findByPrimaryKey(long discussionId)
467                    throws NoSuchDiscussionException, SystemException {
468                    MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
469    
470                    if (mbDiscussion == null) {
471                            if (_log.isWarnEnabled()) {
472                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
473                            }
474    
475                            throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
476                                    discussionId);
477                    }
478    
479                    return mbDiscussion;
480            }
481    
482            /**
483             * Returns the message boards discussion with the primary key or returns <code>null</code> if it could not be found.
484             *
485             * @param primaryKey the primary key of the message boards discussion
486             * @return the message boards discussion, or <code>null</code> if a message boards discussion with the primary key could not be found
487             * @throws SystemException if a system exception occurred
488             */
489            @Override
490            public MBDiscussion fetchByPrimaryKey(Serializable primaryKey)
491                    throws SystemException {
492                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
493            }
494    
495            /**
496             * Returns the message boards discussion with the primary key or returns <code>null</code> if it could not be found.
497             *
498             * @param discussionId the primary key of the message boards discussion
499             * @return the message boards discussion, or <code>null</code> if a message boards discussion with the primary key could not be found
500             * @throws SystemException if a system exception occurred
501             */
502            public MBDiscussion fetchByPrimaryKey(long discussionId)
503                    throws SystemException {
504                    MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
505                                    MBDiscussionImpl.class, discussionId);
506    
507                    if (mbDiscussion == _nullMBDiscussion) {
508                            return null;
509                    }
510    
511                    if (mbDiscussion == null) {
512                            Session session = null;
513    
514                            boolean hasException = false;
515    
516                            try {
517                                    session = openSession();
518    
519                                    mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
520                                                    Long.valueOf(discussionId));
521                            }
522                            catch (Exception e) {
523                                    hasException = true;
524    
525                                    throw processException(e);
526                            }
527                            finally {
528                                    if (mbDiscussion != null) {
529                                            cacheResult(mbDiscussion);
530                                    }
531                                    else if (!hasException) {
532                                            EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
533                                                    MBDiscussionImpl.class, discussionId, _nullMBDiscussion);
534                                    }
535    
536                                    closeSession(session);
537                            }
538                    }
539    
540                    return mbDiscussion;
541            }
542    
543            /**
544             * Returns all the message boards discussions where classNameId = &#63;.
545             *
546             * @param classNameId the class name ID
547             * @return the matching message boards discussions
548             * @throws SystemException if a system exception occurred
549             */
550            public List<MBDiscussion> findByClassNameId(long classNameId)
551                    throws SystemException {
552                    return findByClassNameId(classNameId, QueryUtil.ALL_POS,
553                            QueryUtil.ALL_POS, null);
554            }
555    
556            /**
557             * Returns a range of all the message boards discussions where classNameId = &#63;.
558             *
559             * <p>
560             * 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.
561             * </p>
562             *
563             * @param classNameId the class name ID
564             * @param start the lower bound of the range of message boards discussions
565             * @param end the upper bound of the range of message boards discussions (not inclusive)
566             * @return the range of matching message boards discussions
567             * @throws SystemException if a system exception occurred
568             */
569            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
570                    int end) throws SystemException {
571                    return findByClassNameId(classNameId, start, end, null);
572            }
573    
574            /**
575             * Returns an ordered range of all the message boards discussions where classNameId = &#63;.
576             *
577             * <p>
578             * 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.
579             * </p>
580             *
581             * @param classNameId the class name ID
582             * @param start the lower bound of the range of message boards discussions
583             * @param end the upper bound of the range of message boards discussions (not inclusive)
584             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
585             * @return the ordered range of matching message boards discussions
586             * @throws SystemException if a system exception occurred
587             */
588            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
589                    int end, OrderByComparator orderByComparator) throws SystemException {
590                    FinderPath finderPath = null;
591                    Object[] finderArgs = null;
592    
593                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
594                                    (orderByComparator == null)) {
595                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID;
596                            finderArgs = new Object[] { classNameId };
597                    }
598                    else {
599                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSNAMEID;
600                            finderArgs = new Object[] { classNameId, start, end, orderByComparator };
601                    }
602    
603                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
604                                    finderArgs, this);
605    
606                    if ((list != null) && !list.isEmpty()) {
607                            for (MBDiscussion mbDiscussion : list) {
608                                    if ((classNameId != mbDiscussion.getClassNameId())) {
609                                            list = null;
610    
611                                            break;
612                                    }
613                            }
614                    }
615    
616                    if (list == null) {
617                            StringBundler query = null;
618    
619                            if (orderByComparator != null) {
620                                    query = new StringBundler(3 +
621                                                    (orderByComparator.getOrderByFields().length * 3));
622                            }
623                            else {
624                                    query = new StringBundler(2);
625                            }
626    
627                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
628    
629                            query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
630    
631                            if (orderByComparator != null) {
632                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
633                                            orderByComparator);
634                            }
635    
636                            String sql = query.toString();
637    
638                            Session session = null;
639    
640                            try {
641                                    session = openSession();
642    
643                                    Query q = session.createQuery(sql);
644    
645                                    QueryPos qPos = QueryPos.getInstance(q);
646    
647                                    qPos.add(classNameId);
648    
649                                    list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
650                                                    start, end);
651                            }
652                            catch (Exception e) {
653                                    throw processException(e);
654                            }
655                            finally {
656                                    if (list == null) {
657                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
658                                    }
659                                    else {
660                                            cacheResult(list);
661    
662                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
663                                    }
664    
665                                    closeSession(session);
666                            }
667                    }
668    
669                    return list;
670            }
671    
672            /**
673             * Returns the first message boards discussion in the ordered set where classNameId = &#63;.
674             *
675             * <p>
676             * 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.
677             * </p>
678             *
679             * @param classNameId the class name ID
680             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
681             * @return the first matching message boards discussion
682             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
683             * @throws SystemException if a system exception occurred
684             */
685            public MBDiscussion findByClassNameId_First(long classNameId,
686                    OrderByComparator orderByComparator)
687                    throws NoSuchDiscussionException, SystemException {
688                    List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1,
689                                    orderByComparator);
690    
691                    if (list.isEmpty()) {
692                            StringBundler msg = new StringBundler(4);
693    
694                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
695    
696                            msg.append("classNameId=");
697                            msg.append(classNameId);
698    
699                            msg.append(StringPool.CLOSE_CURLY_BRACE);
700    
701                            throw new NoSuchDiscussionException(msg.toString());
702                    }
703                    else {
704                            return list.get(0);
705                    }
706            }
707    
708            /**
709             * Returns the last message boards discussion in the ordered set where classNameId = &#63;.
710             *
711             * <p>
712             * 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.
713             * </p>
714             *
715             * @param classNameId the class name ID
716             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
717             * @return the last matching message boards discussion
718             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
719             * @throws SystemException if a system exception occurred
720             */
721            public MBDiscussion findByClassNameId_Last(long classNameId,
722                    OrderByComparator orderByComparator)
723                    throws NoSuchDiscussionException, SystemException {
724                    int count = countByClassNameId(classNameId);
725    
726                    List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
727                                    count, orderByComparator);
728    
729                    if (list.isEmpty()) {
730                            StringBundler msg = new StringBundler(4);
731    
732                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
733    
734                            msg.append("classNameId=");
735                            msg.append(classNameId);
736    
737                            msg.append(StringPool.CLOSE_CURLY_BRACE);
738    
739                            throw new NoSuchDiscussionException(msg.toString());
740                    }
741                    else {
742                            return list.get(0);
743                    }
744            }
745    
746            /**
747             * Returns the message boards discussions before and after the current message boards discussion in the ordered set where classNameId = &#63;.
748             *
749             * <p>
750             * 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.
751             * </p>
752             *
753             * @param discussionId the primary key of the current message boards discussion
754             * @param classNameId the class name ID
755             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
756             * @return the previous, current, and next message boards discussion
757             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
758             * @throws SystemException if a system exception occurred
759             */
760            public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
761                    long classNameId, OrderByComparator orderByComparator)
762                    throws NoSuchDiscussionException, SystemException {
763                    MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
764    
765                    Session session = null;
766    
767                    try {
768                            session = openSession();
769    
770                            MBDiscussion[] array = new MBDiscussionImpl[3];
771    
772                            array[0] = getByClassNameId_PrevAndNext(session, mbDiscussion,
773                                            classNameId, orderByComparator, true);
774    
775                            array[1] = mbDiscussion;
776    
777                            array[2] = getByClassNameId_PrevAndNext(session, mbDiscussion,
778                                            classNameId, orderByComparator, false);
779    
780                            return array;
781                    }
782                    catch (Exception e) {
783                            throw processException(e);
784                    }
785                    finally {
786                            closeSession(session);
787                    }
788            }
789    
790            protected MBDiscussion getByClassNameId_PrevAndNext(Session session,
791                    MBDiscussion mbDiscussion, long classNameId,
792                    OrderByComparator orderByComparator, boolean previous) {
793                    StringBundler query = null;
794    
795                    if (orderByComparator != null) {
796                            query = new StringBundler(6 +
797                                            (orderByComparator.getOrderByFields().length * 6));
798                    }
799                    else {
800                            query = new StringBundler(3);
801                    }
802    
803                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
804    
805                    query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
806    
807                    if (orderByComparator != null) {
808                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
809    
810                            if (orderByConditionFields.length > 0) {
811                                    query.append(WHERE_AND);
812                            }
813    
814                            for (int i = 0; i < orderByConditionFields.length; i++) {
815                                    query.append(_ORDER_BY_ENTITY_ALIAS);
816                                    query.append(orderByConditionFields[i]);
817    
818                                    if ((i + 1) < orderByConditionFields.length) {
819                                            if (orderByComparator.isAscending() ^ previous) {
820                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
821                                            }
822                                            else {
823                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
824                                            }
825                                    }
826                                    else {
827                                            if (orderByComparator.isAscending() ^ previous) {
828                                                    query.append(WHERE_GREATER_THAN);
829                                            }
830                                            else {
831                                                    query.append(WHERE_LESSER_THAN);
832                                            }
833                                    }
834                            }
835    
836                            query.append(ORDER_BY_CLAUSE);
837    
838                            String[] orderByFields = orderByComparator.getOrderByFields();
839    
840                            for (int i = 0; i < orderByFields.length; i++) {
841                                    query.append(_ORDER_BY_ENTITY_ALIAS);
842                                    query.append(orderByFields[i]);
843    
844                                    if ((i + 1) < orderByFields.length) {
845                                            if (orderByComparator.isAscending() ^ previous) {
846                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
847                                            }
848                                            else {
849                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
850                                            }
851                                    }
852                                    else {
853                                            if (orderByComparator.isAscending() ^ previous) {
854                                                    query.append(ORDER_BY_ASC);
855                                            }
856                                            else {
857                                                    query.append(ORDER_BY_DESC);
858                                            }
859                                    }
860                            }
861                    }
862    
863                    String sql = query.toString();
864    
865                    Query q = session.createQuery(sql);
866    
867                    q.setFirstResult(0);
868                    q.setMaxResults(2);
869    
870                    QueryPos qPos = QueryPos.getInstance(q);
871    
872                    qPos.add(classNameId);
873    
874                    if (orderByComparator != null) {
875                            Object[] values = orderByComparator.getOrderByConditionValues(mbDiscussion);
876    
877                            for (Object value : values) {
878                                    qPos.add(value);
879                            }
880                    }
881    
882                    List<MBDiscussion> list = q.list();
883    
884                    if (list.size() == 2) {
885                            return list.get(1);
886                    }
887                    else {
888                            return null;
889                    }
890            }
891    
892            /**
893             * Returns the message boards discussion where threadId = &#63; or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
894             *
895             * @param threadId the thread ID
896             * @return the matching message boards discussion
897             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
898             * @throws SystemException if a system exception occurred
899             */
900            public MBDiscussion findByThreadId(long threadId)
901                    throws NoSuchDiscussionException, SystemException {
902                    MBDiscussion mbDiscussion = fetchByThreadId(threadId);
903    
904                    if (mbDiscussion == null) {
905                            StringBundler msg = new StringBundler(4);
906    
907                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
908    
909                            msg.append("threadId=");
910                            msg.append(threadId);
911    
912                            msg.append(StringPool.CLOSE_CURLY_BRACE);
913    
914                            if (_log.isWarnEnabled()) {
915                                    _log.warn(msg.toString());
916                            }
917    
918                            throw new NoSuchDiscussionException(msg.toString());
919                    }
920    
921                    return mbDiscussion;
922            }
923    
924            /**
925             * Returns the message boards discussion where threadId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
926             *
927             * @param threadId the thread ID
928             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
929             * @throws SystemException if a system exception occurred
930             */
931            public MBDiscussion fetchByThreadId(long threadId)
932                    throws SystemException {
933                    return fetchByThreadId(threadId, true);
934            }
935    
936            /**
937             * Returns the message boards discussion where threadId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
938             *
939             * @param threadId the thread ID
940             * @param retrieveFromCache whether to use the finder cache
941             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
942             * @throws SystemException if a system exception occurred
943             */
944            public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
945                    throws SystemException {
946                    Object[] finderArgs = new Object[] { threadId };
947    
948                    Object result = null;
949    
950                    if (retrieveFromCache) {
951                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
952                                            finderArgs, this);
953                    }
954    
955                    if (result instanceof MBDiscussion) {
956                            MBDiscussion mbDiscussion = (MBDiscussion)result;
957    
958                            if ((threadId != mbDiscussion.getThreadId())) {
959                                    result = null;
960                            }
961                    }
962    
963                    if (result == null) {
964                            StringBundler query = new StringBundler(2);
965    
966                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
967    
968                            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
969    
970                            String sql = query.toString();
971    
972                            Session session = null;
973    
974                            try {
975                                    session = openSession();
976    
977                                    Query q = session.createQuery(sql);
978    
979                                    QueryPos qPos = QueryPos.getInstance(q);
980    
981                                    qPos.add(threadId);
982    
983                                    List<MBDiscussion> list = q.list();
984    
985                                    result = list;
986    
987                                    MBDiscussion mbDiscussion = null;
988    
989                                    if (list.isEmpty()) {
990                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
991                                                    finderArgs, list);
992                                    }
993                                    else {
994                                            mbDiscussion = list.get(0);
995    
996                                            cacheResult(mbDiscussion);
997    
998                                            if ((mbDiscussion.getThreadId() != threadId)) {
999                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
1000                                                            finderArgs, mbDiscussion);
1001                                            }
1002                                    }
1003    
1004                                    return mbDiscussion;
1005                            }
1006                            catch (Exception e) {
1007                                    throw processException(e);
1008                            }
1009                            finally {
1010                                    if (result == null) {
1011                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
1012                                                    finderArgs);
1013                                    }
1014    
1015                                    closeSession(session);
1016                            }
1017                    }
1018                    else {
1019                            if (result instanceof List<?>) {
1020                                    return null;
1021                            }
1022                            else {
1023                                    return (MBDiscussion)result;
1024                            }
1025                    }
1026            }
1027    
1028            /**
1029             * 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.
1030             *
1031             * @param classNameId the class name ID
1032             * @param classPK the class p k
1033             * @return the matching message boards discussion
1034             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1035             * @throws SystemException if a system exception occurred
1036             */
1037            public MBDiscussion findByC_C(long classNameId, long classPK)
1038                    throws NoSuchDiscussionException, SystemException {
1039                    MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
1040    
1041                    if (mbDiscussion == null) {
1042                            StringBundler msg = new StringBundler(6);
1043    
1044                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1045    
1046                            msg.append("classNameId=");
1047                            msg.append(classNameId);
1048    
1049                            msg.append(", classPK=");
1050                            msg.append(classPK);
1051    
1052                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1053    
1054                            if (_log.isWarnEnabled()) {
1055                                    _log.warn(msg.toString());
1056                            }
1057    
1058                            throw new NoSuchDiscussionException(msg.toString());
1059                    }
1060    
1061                    return mbDiscussion;
1062            }
1063    
1064            /**
1065             * 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.
1066             *
1067             * @param classNameId the class name ID
1068             * @param classPK the class p k
1069             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1070             * @throws SystemException if a system exception occurred
1071             */
1072            public MBDiscussion fetchByC_C(long classNameId, long classPK)
1073                    throws SystemException {
1074                    return fetchByC_C(classNameId, classPK, true);
1075            }
1076    
1077            /**
1078             * 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.
1079             *
1080             * @param classNameId the class name ID
1081             * @param classPK the class p k
1082             * @param retrieveFromCache whether to use the finder cache
1083             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1084             * @throws SystemException if a system exception occurred
1085             */
1086            public MBDiscussion fetchByC_C(long classNameId, long classPK,
1087                    boolean retrieveFromCache) throws SystemException {
1088                    Object[] finderArgs = new Object[] { classNameId, classPK };
1089    
1090                    Object result = null;
1091    
1092                    if (retrieveFromCache) {
1093                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
1094                                            finderArgs, this);
1095                    }
1096    
1097                    if (result instanceof MBDiscussion) {
1098                            MBDiscussion mbDiscussion = (MBDiscussion)result;
1099    
1100                            if ((classNameId != mbDiscussion.getClassNameId()) ||
1101                                            (classPK != mbDiscussion.getClassPK())) {
1102                                    result = null;
1103                            }
1104                    }
1105    
1106                    if (result == null) {
1107                            StringBundler query = new StringBundler(3);
1108    
1109                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
1110    
1111                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1112    
1113                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1114    
1115                            String sql = query.toString();
1116    
1117                            Session session = null;
1118    
1119                            try {
1120                                    session = openSession();
1121    
1122                                    Query q = session.createQuery(sql);
1123    
1124                                    QueryPos qPos = QueryPos.getInstance(q);
1125    
1126                                    qPos.add(classNameId);
1127    
1128                                    qPos.add(classPK);
1129    
1130                                    List<MBDiscussion> list = q.list();
1131    
1132                                    result = list;
1133    
1134                                    MBDiscussion mbDiscussion = null;
1135    
1136                                    if (list.isEmpty()) {
1137                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
1138                                                    finderArgs, list);
1139                                    }
1140                                    else {
1141                                            mbDiscussion = list.get(0);
1142    
1143                                            cacheResult(mbDiscussion);
1144    
1145                                            if ((mbDiscussion.getClassNameId() != classNameId) ||
1146                                                            (mbDiscussion.getClassPK() != classPK)) {
1147                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
1148                                                            finderArgs, mbDiscussion);
1149                                            }
1150                                    }
1151    
1152                                    return mbDiscussion;
1153                            }
1154                            catch (Exception e) {
1155                                    throw processException(e);
1156                            }
1157                            finally {
1158                                    if (result == null) {
1159                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
1160                                                    finderArgs);
1161                                    }
1162    
1163                                    closeSession(session);
1164                            }
1165                    }
1166                    else {
1167                            if (result instanceof List<?>) {
1168                                    return null;
1169                            }
1170                            else {
1171                                    return (MBDiscussion)result;
1172                            }
1173                    }
1174            }
1175    
1176            /**
1177             * Returns all the message boards discussions.
1178             *
1179             * @return the message boards discussions
1180             * @throws SystemException if a system exception occurred
1181             */
1182            public List<MBDiscussion> findAll() throws SystemException {
1183                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1184            }
1185    
1186            /**
1187             * Returns a range of all the message boards discussions.
1188             *
1189             * <p>
1190             * 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.
1191             * </p>
1192             *
1193             * @param start the lower bound of the range of message boards discussions
1194             * @param end the upper bound of the range of message boards discussions (not inclusive)
1195             * @return the range of message boards discussions
1196             * @throws SystemException if a system exception occurred
1197             */
1198            public List<MBDiscussion> findAll(int start, int end)
1199                    throws SystemException {
1200                    return findAll(start, end, null);
1201            }
1202    
1203            /**
1204             * Returns an ordered range of all the message boards discussions.
1205             *
1206             * <p>
1207             * 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.
1208             * </p>
1209             *
1210             * @param start the lower bound of the range of message boards discussions
1211             * @param end the upper bound of the range of message boards discussions (not inclusive)
1212             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1213             * @return the ordered range of message boards discussions
1214             * @throws SystemException if a system exception occurred
1215             */
1216            public List<MBDiscussion> findAll(int start, int end,
1217                    OrderByComparator orderByComparator) throws SystemException {
1218                    FinderPath finderPath = null;
1219                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
1220    
1221                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1222                                    (orderByComparator == null)) {
1223                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1224                            finderArgs = FINDER_ARGS_EMPTY;
1225                    }
1226                    else {
1227                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1228                            finderArgs = new Object[] { start, end, orderByComparator };
1229                    }
1230    
1231                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
1232                                    finderArgs, this);
1233    
1234                    if (list == null) {
1235                            StringBundler query = null;
1236                            String sql = null;
1237    
1238                            if (orderByComparator != null) {
1239                                    query = new StringBundler(2 +
1240                                                    (orderByComparator.getOrderByFields().length * 3));
1241    
1242                                    query.append(_SQL_SELECT_MBDISCUSSION);
1243    
1244                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1245                                            orderByComparator);
1246    
1247                                    sql = query.toString();
1248                            }
1249                            else {
1250                                    sql = _SQL_SELECT_MBDISCUSSION;
1251                            }
1252    
1253                            Session session = null;
1254    
1255                            try {
1256                                    session = openSession();
1257    
1258                                    Query q = session.createQuery(sql);
1259    
1260                                    if (orderByComparator == null) {
1261                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1262                                                            start, end, false);
1263    
1264                                            Collections.sort(list);
1265                                    }
1266                                    else {
1267                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1268                                                            start, end);
1269                                    }
1270                            }
1271                            catch (Exception e) {
1272                                    throw processException(e);
1273                            }
1274                            finally {
1275                                    if (list == null) {
1276                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
1277                                    }
1278                                    else {
1279                                            cacheResult(list);
1280    
1281                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
1282                                    }
1283    
1284                                    closeSession(session);
1285                            }
1286                    }
1287    
1288                    return list;
1289            }
1290    
1291            /**
1292             * Removes all the message boards discussions where classNameId = &#63; from the database.
1293             *
1294             * @param classNameId the class name ID
1295             * @throws SystemException if a system exception occurred
1296             */
1297            public void removeByClassNameId(long classNameId) throws SystemException {
1298                    for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
1299                            remove(mbDiscussion);
1300                    }
1301            }
1302    
1303            /**
1304             * Removes the message boards discussion where threadId = &#63; from the database.
1305             *
1306             * @param threadId the thread ID
1307             * @throws SystemException if a system exception occurred
1308             */
1309            public void removeByThreadId(long threadId)
1310                    throws NoSuchDiscussionException, SystemException {
1311                    MBDiscussion mbDiscussion = findByThreadId(threadId);
1312    
1313                    remove(mbDiscussion);
1314            }
1315    
1316            /**
1317             * Removes the message boards discussion where classNameId = &#63; and classPK = &#63; from the database.
1318             *
1319             * @param classNameId the class name ID
1320             * @param classPK the class p k
1321             * @throws SystemException if a system exception occurred
1322             */
1323            public void removeByC_C(long classNameId, long classPK)
1324                    throws NoSuchDiscussionException, SystemException {
1325                    MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
1326    
1327                    remove(mbDiscussion);
1328            }
1329    
1330            /**
1331             * Removes all the message boards discussions from the database.
1332             *
1333             * @throws SystemException if a system exception occurred
1334             */
1335            public void removeAll() throws SystemException {
1336                    for (MBDiscussion mbDiscussion : findAll()) {
1337                            remove(mbDiscussion);
1338                    }
1339            }
1340    
1341            /**
1342             * Returns the number of message boards discussions where classNameId = &#63;.
1343             *
1344             * @param classNameId the class name ID
1345             * @return the number of matching message boards discussions
1346             * @throws SystemException if a system exception occurred
1347             */
1348            public int countByClassNameId(long classNameId) throws SystemException {
1349                    Object[] finderArgs = new Object[] { classNameId };
1350    
1351                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1352                                    finderArgs, this);
1353    
1354                    if (count == null) {
1355                            StringBundler query = new StringBundler(2);
1356    
1357                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1358    
1359                            query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
1360    
1361                            String sql = query.toString();
1362    
1363                            Session session = null;
1364    
1365                            try {
1366                                    session = openSession();
1367    
1368                                    Query q = session.createQuery(sql);
1369    
1370                                    QueryPos qPos = QueryPos.getInstance(q);
1371    
1372                                    qPos.add(classNameId);
1373    
1374                                    count = (Long)q.uniqueResult();
1375                            }
1376                            catch (Exception e) {
1377                                    throw processException(e);
1378                            }
1379                            finally {
1380                                    if (count == null) {
1381                                            count = Long.valueOf(0);
1382                                    }
1383    
1384                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1385                                            finderArgs, count);
1386    
1387                                    closeSession(session);
1388                            }
1389                    }
1390    
1391                    return count.intValue();
1392            }
1393    
1394            /**
1395             * Returns the number of message boards discussions where threadId = &#63;.
1396             *
1397             * @param threadId the thread ID
1398             * @return the number of matching message boards discussions
1399             * @throws SystemException if a system exception occurred
1400             */
1401            public int countByThreadId(long threadId) throws SystemException {
1402                    Object[] finderArgs = new Object[] { threadId };
1403    
1404                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1405                                    finderArgs, this);
1406    
1407                    if (count == null) {
1408                            StringBundler query = new StringBundler(2);
1409    
1410                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1411    
1412                            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1413    
1414                            String sql = query.toString();
1415    
1416                            Session session = null;
1417    
1418                            try {
1419                                    session = openSession();
1420    
1421                                    Query q = session.createQuery(sql);
1422    
1423                                    QueryPos qPos = QueryPos.getInstance(q);
1424    
1425                                    qPos.add(threadId);
1426    
1427                                    count = (Long)q.uniqueResult();
1428                            }
1429                            catch (Exception e) {
1430                                    throw processException(e);
1431                            }
1432                            finally {
1433                                    if (count == null) {
1434                                            count = Long.valueOf(0);
1435                                    }
1436    
1437                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1438                                            finderArgs, count);
1439    
1440                                    closeSession(session);
1441                            }
1442                    }
1443    
1444                    return count.intValue();
1445            }
1446    
1447            /**
1448             * Returns the number of message boards discussions where classNameId = &#63; and classPK = &#63;.
1449             *
1450             * @param classNameId the class name ID
1451             * @param classPK the class p k
1452             * @return the number of matching message boards discussions
1453             * @throws SystemException if a system exception occurred
1454             */
1455            public int countByC_C(long classNameId, long classPK)
1456                    throws SystemException {
1457                    Object[] finderArgs = new Object[] { classNameId, classPK };
1458    
1459                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1460                                    finderArgs, this);
1461    
1462                    if (count == null) {
1463                            StringBundler query = new StringBundler(3);
1464    
1465                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1466    
1467                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1468    
1469                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1470    
1471                            String sql = query.toString();
1472    
1473                            Session session = null;
1474    
1475                            try {
1476                                    session = openSession();
1477    
1478                                    Query q = session.createQuery(sql);
1479    
1480                                    QueryPos qPos = QueryPos.getInstance(q);
1481    
1482                                    qPos.add(classNameId);
1483    
1484                                    qPos.add(classPK);
1485    
1486                                    count = (Long)q.uniqueResult();
1487                            }
1488                            catch (Exception e) {
1489                                    throw processException(e);
1490                            }
1491                            finally {
1492                                    if (count == null) {
1493                                            count = Long.valueOf(0);
1494                                    }
1495    
1496                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1497                                            count);
1498    
1499                                    closeSession(session);
1500                            }
1501                    }
1502    
1503                    return count.intValue();
1504            }
1505    
1506            /**
1507             * Returns the number of message boards discussions.
1508             *
1509             * @return the number of message boards discussions
1510             * @throws SystemException if a system exception occurred
1511             */
1512            public int countAll() throws SystemException {
1513                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1514                                    FINDER_ARGS_EMPTY, this);
1515    
1516                    if (count == null) {
1517                            Session session = null;
1518    
1519                            try {
1520                                    session = openSession();
1521    
1522                                    Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
1523    
1524                                    count = (Long)q.uniqueResult();
1525                            }
1526                            catch (Exception e) {
1527                                    throw processException(e);
1528                            }
1529                            finally {
1530                                    if (count == null) {
1531                                            count = Long.valueOf(0);
1532                                    }
1533    
1534                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1535                                            FINDER_ARGS_EMPTY, count);
1536    
1537                                    closeSession(session);
1538                            }
1539                    }
1540    
1541                    return count.intValue();
1542            }
1543    
1544            /**
1545             * Initializes the message boards discussion persistence.
1546             */
1547            public void afterPropertiesSet() {
1548                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1549                                            com.liferay.portal.util.PropsUtil.get(
1550                                                    "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1551    
1552                    if (listenerClassNames.length > 0) {
1553                            try {
1554                                    List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1555    
1556                                    for (String listenerClassName : listenerClassNames) {
1557                                            listenersList.add((ModelListener<MBDiscussion>)InstanceFactory.newInstance(
1558                                                            listenerClassName));
1559                                    }
1560    
1561                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1562                            }
1563                            catch (Exception e) {
1564                                    _log.error(e);
1565                            }
1566                    }
1567            }
1568    
1569            public void destroy() {
1570                    EntityCacheUtil.removeCache(MBDiscussionImpl.class.getName());
1571                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1572                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1573            }
1574    
1575            @BeanReference(type = MBBanPersistence.class)
1576            protected MBBanPersistence mbBanPersistence;
1577            @BeanReference(type = MBCategoryPersistence.class)
1578            protected MBCategoryPersistence mbCategoryPersistence;
1579            @BeanReference(type = MBDiscussionPersistence.class)
1580            protected MBDiscussionPersistence mbDiscussionPersistence;
1581            @BeanReference(type = MBMailingListPersistence.class)
1582            protected MBMailingListPersistence mbMailingListPersistence;
1583            @BeanReference(type = MBMessagePersistence.class)
1584            protected MBMessagePersistence mbMessagePersistence;
1585            @BeanReference(type = MBStatsUserPersistence.class)
1586            protected MBStatsUserPersistence mbStatsUserPersistence;
1587            @BeanReference(type = MBThreadPersistence.class)
1588            protected MBThreadPersistence mbThreadPersistence;
1589            @BeanReference(type = MBThreadFlagPersistence.class)
1590            protected MBThreadFlagPersistence mbThreadFlagPersistence;
1591            @BeanReference(type = ResourcePersistence.class)
1592            protected ResourcePersistence resourcePersistence;
1593            @BeanReference(type = UserPersistence.class)
1594            protected UserPersistence userPersistence;
1595            private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
1596            private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
1597            private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
1598            private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
1599            private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1600            private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
1601            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
1602            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
1603            private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
1604            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
1605            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
1606            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1607            private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1608            private static MBDiscussion _nullMBDiscussion = new MBDiscussionImpl() {
1609                            @Override
1610                            public Object clone() {
1611                                    return this;
1612                            }
1613    
1614                            @Override
1615                            public CacheModel<MBDiscussion> toCacheModel() {
1616                                    return _nullMBDiscussionCacheModel;
1617                            }
1618                    };
1619    
1620            private static CacheModel<MBDiscussion> _nullMBDiscussionCacheModel = new CacheModel<MBDiscussion>() {
1621                            public MBDiscussion toEntityModel() {
1622                                    return _nullMBDiscussion;
1623                            }
1624                    };
1625    }