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