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