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