001    /**
002     * Copyright (c) 2000-2013 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.SetUtil;
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.kernel.util.Validator;
037    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
038    import com.liferay.portal.model.CacheModel;
039    import com.liferay.portal.model.ModelListener;
040    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041    
042    import com.liferay.portlet.messageboards.NoSuchDiscussionException;
043    import com.liferay.portlet.messageboards.model.MBDiscussion;
044    import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
045    import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
046    
047    import java.io.Serializable;
048    
049    import java.util.ArrayList;
050    import java.util.Collections;
051    import java.util.List;
052    import java.util.Set;
053    
054    /**
055     * The persistence implementation for the message boards discussion service.
056     *
057     * <p>
058     * Caching information and settings can be found in <code>portal.properties</code>
059     * </p>
060     *
061     * @author Brian Wing Shun Chan
062     * @see MBDiscussionPersistence
063     * @see MBDiscussionUtil
064     * @generated
065     */
066    public class MBDiscussionPersistenceImpl extends BasePersistenceImpl<MBDiscussion>
067            implements MBDiscussionPersistence {
068            /*
069             * NOTE FOR DEVELOPERS:
070             *
071             * Never modify or reference this class directly. Always use {@link MBDiscussionUtil} to access the message boards discussion persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
072             */
073            public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
074            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List1";
076            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077                    ".List2";
078            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
079                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
080                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
082                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
084            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
085                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
086                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
087            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
088                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
090                            new String[] {
091                                    String.class.getName(),
092                                    
093                            Integer.class.getName(), Integer.class.getName(),
094                                    OrderByComparator.class.getName()
095                            });
096            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
097                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
098                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
099                            new String[] { String.class.getName() },
100                            MBDiscussionModelImpl.UUID_COLUMN_BITMASK);
101            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
102                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
104                            new String[] { String.class.getName() });
105    
106            /**
107             * Returns all the message boards discussions where uuid = &#63;.
108             *
109             * @param uuid the uuid
110             * @return the matching message boards discussions
111             * @throws SystemException if a system exception occurred
112             */
113            @Override
114            public List<MBDiscussion> findByUuid(String uuid) throws SystemException {
115                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
116            }
117    
118            /**
119             * Returns a range of all the message boards discussions where uuid = &#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 uuid the uuid
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            @Override
132            public List<MBDiscussion> findByUuid(String uuid, int start, int end)
133                    throws SystemException {
134                    return findByUuid(uuid, start, end, null);
135            }
136    
137            /**
138             * Returns an ordered range of all the message boards discussions where uuid = &#63;.
139             *
140             * <p>
141             * 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.
142             * </p>
143             *
144             * @param uuid the uuid
145             * @param start the lower bound of the range of message boards discussions
146             * @param end the upper bound of the range of message boards discussions (not inclusive)
147             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
148             * @return the ordered range of matching message boards discussions
149             * @throws SystemException if a system exception occurred
150             */
151            @Override
152            public List<MBDiscussion> findByUuid(String uuid, int start, int end,
153                    OrderByComparator orderByComparator) throws SystemException {
154                    boolean pagination = true;
155                    FinderPath finderPath = null;
156                    Object[] finderArgs = null;
157    
158                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
159                                    (orderByComparator == null)) {
160                            pagination = false;
161                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID;
162                            finderArgs = new Object[] { uuid };
163                    }
164                    else {
165                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
166                            finderArgs = new Object[] { uuid, start, end, orderByComparator };
167                    }
168    
169                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
170                                    finderArgs, this);
171    
172                    if ((list != null) && !list.isEmpty()) {
173                            for (MBDiscussion mbDiscussion : list) {
174                                    if (!Validator.equals(uuid, mbDiscussion.getUuid())) {
175                                            list = null;
176    
177                                            break;
178                                    }
179                            }
180                    }
181    
182                    if (list == null) {
183                            StringBundler query = null;
184    
185                            if (orderByComparator != null) {
186                                    query = new StringBundler(3 +
187                                                    (orderByComparator.getOrderByFields().length * 3));
188                            }
189                            else {
190                                    query = new StringBundler(3);
191                            }
192    
193                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
194    
195                            boolean bindUuid = false;
196    
197                            if (uuid == null) {
198                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
199                            }
200                            else if (uuid.equals(StringPool.BLANK)) {
201                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
202                            }
203                            else {
204                                    bindUuid = true;
205    
206                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
207                            }
208    
209                            if (orderByComparator != null) {
210                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
211                                            orderByComparator);
212                            }
213                            else
214                             if (pagination) {
215                                    query.append(MBDiscussionModelImpl.ORDER_BY_JPQL);
216                            }
217    
218                            String sql = query.toString();
219    
220                            Session session = null;
221    
222                            try {
223                                    session = openSession();
224    
225                                    Query q = session.createQuery(sql);
226    
227                                    QueryPos qPos = QueryPos.getInstance(q);
228    
229                                    if (bindUuid) {
230                                            qPos.add(uuid);
231                                    }
232    
233                                    if (!pagination) {
234                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
235                                                            start, end, false);
236    
237                                            Collections.sort(list);
238    
239                                            list = new UnmodifiableList<MBDiscussion>(list);
240                                    }
241                                    else {
242                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
243                                                            start, end);
244                                    }
245    
246                                    cacheResult(list);
247    
248                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
249                            }
250                            catch (Exception e) {
251                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
252    
253                                    throw processException(e);
254                            }
255                            finally {
256                                    closeSession(session);
257                            }
258                    }
259    
260                    return list;
261            }
262    
263            /**
264             * Returns the first message boards discussion in the ordered set where uuid = &#63;.
265             *
266             * @param uuid the uuid
267             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
268             * @return the first matching message boards discussion
269             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
270             * @throws SystemException if a system exception occurred
271             */
272            @Override
273            public MBDiscussion findByUuid_First(String uuid,
274                    OrderByComparator orderByComparator)
275                    throws NoSuchDiscussionException, SystemException {
276                    MBDiscussion mbDiscussion = fetchByUuid_First(uuid, orderByComparator);
277    
278                    if (mbDiscussion != null) {
279                            return mbDiscussion;
280                    }
281    
282                    StringBundler msg = new StringBundler(4);
283    
284                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
285    
286                    msg.append("uuid=");
287                    msg.append(uuid);
288    
289                    msg.append(StringPool.CLOSE_CURLY_BRACE);
290    
291                    throw new NoSuchDiscussionException(msg.toString());
292            }
293    
294            /**
295             * Returns the first message boards discussion in the ordered set where uuid = &#63;.
296             *
297             * @param uuid the uuid
298             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
299             * @return the first matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
300             * @throws SystemException if a system exception occurred
301             */
302            @Override
303            public MBDiscussion fetchByUuid_First(String uuid,
304                    OrderByComparator orderByComparator) throws SystemException {
305                    List<MBDiscussion> list = findByUuid(uuid, 0, 1, orderByComparator);
306    
307                    if (!list.isEmpty()) {
308                            return list.get(0);
309                    }
310    
311                    return null;
312            }
313    
314            /**
315             * Returns the last message boards discussion in the ordered set where uuid = &#63;.
316             *
317             * @param uuid the uuid
318             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
319             * @return the last matching message boards discussion
320             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
321             * @throws SystemException if a system exception occurred
322             */
323            @Override
324            public MBDiscussion findByUuid_Last(String uuid,
325                    OrderByComparator orderByComparator)
326                    throws NoSuchDiscussionException, SystemException {
327                    MBDiscussion mbDiscussion = fetchByUuid_Last(uuid, orderByComparator);
328    
329                    if (mbDiscussion != null) {
330                            return mbDiscussion;
331                    }
332    
333                    StringBundler msg = new StringBundler(4);
334    
335                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
336    
337                    msg.append("uuid=");
338                    msg.append(uuid);
339    
340                    msg.append(StringPool.CLOSE_CURLY_BRACE);
341    
342                    throw new NoSuchDiscussionException(msg.toString());
343            }
344    
345            /**
346             * Returns the last message boards discussion in the ordered set where uuid = &#63;.
347             *
348             * @param uuid the uuid
349             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
350             * @return the last matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
351             * @throws SystemException if a system exception occurred
352             */
353            @Override
354            public MBDiscussion fetchByUuid_Last(String uuid,
355                    OrderByComparator orderByComparator) throws SystemException {
356                    int count = countByUuid(uuid);
357    
358                    if (count == 0) {
359                            return null;
360                    }
361    
362                    List<MBDiscussion> list = findByUuid(uuid, count - 1, count,
363                                    orderByComparator);
364    
365                    if (!list.isEmpty()) {
366                            return list.get(0);
367                    }
368    
369                    return null;
370            }
371    
372            /**
373             * Returns the message boards discussions before and after the current message boards discussion in the ordered set where uuid = &#63;.
374             *
375             * @param discussionId the primary key of the current message boards discussion
376             * @param uuid the uuid
377             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
378             * @return the previous, current, and next message boards discussion
379             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
380             * @throws SystemException if a system exception occurred
381             */
382            @Override
383            public MBDiscussion[] findByUuid_PrevAndNext(long discussionId,
384                    String uuid, OrderByComparator orderByComparator)
385                    throws NoSuchDiscussionException, SystemException {
386                    MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
387    
388                    Session session = null;
389    
390                    try {
391                            session = openSession();
392    
393                            MBDiscussion[] array = new MBDiscussionImpl[3];
394    
395                            array[0] = getByUuid_PrevAndNext(session, mbDiscussion, uuid,
396                                            orderByComparator, true);
397    
398                            array[1] = mbDiscussion;
399    
400                            array[2] = getByUuid_PrevAndNext(session, mbDiscussion, uuid,
401                                            orderByComparator, false);
402    
403                            return array;
404                    }
405                    catch (Exception e) {
406                            throw processException(e);
407                    }
408                    finally {
409                            closeSession(session);
410                    }
411            }
412    
413            protected MBDiscussion getByUuid_PrevAndNext(Session session,
414                    MBDiscussion mbDiscussion, String uuid,
415                    OrderByComparator orderByComparator, boolean previous) {
416                    StringBundler query = null;
417    
418                    if (orderByComparator != null) {
419                            query = new StringBundler(6 +
420                                            (orderByComparator.getOrderByFields().length * 6));
421                    }
422                    else {
423                            query = new StringBundler(3);
424                    }
425    
426                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
427    
428                    boolean bindUuid = false;
429    
430                    if (uuid == null) {
431                            query.append(_FINDER_COLUMN_UUID_UUID_1);
432                    }
433                    else if (uuid.equals(StringPool.BLANK)) {
434                            query.append(_FINDER_COLUMN_UUID_UUID_3);
435                    }
436                    else {
437                            bindUuid = true;
438    
439                            query.append(_FINDER_COLUMN_UUID_UUID_2);
440                    }
441    
442                    if (orderByComparator != null) {
443                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
444    
445                            if (orderByConditionFields.length > 0) {
446                                    query.append(WHERE_AND);
447                            }
448    
449                            for (int i = 0; i < orderByConditionFields.length; i++) {
450                                    query.append(_ORDER_BY_ENTITY_ALIAS);
451                                    query.append(orderByConditionFields[i]);
452    
453                                    if ((i + 1) < orderByConditionFields.length) {
454                                            if (orderByComparator.isAscending() ^ previous) {
455                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
456                                            }
457                                            else {
458                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
459                                            }
460                                    }
461                                    else {
462                                            if (orderByComparator.isAscending() ^ previous) {
463                                                    query.append(WHERE_GREATER_THAN);
464                                            }
465                                            else {
466                                                    query.append(WHERE_LESSER_THAN);
467                                            }
468                                    }
469                            }
470    
471                            query.append(ORDER_BY_CLAUSE);
472    
473                            String[] orderByFields = orderByComparator.getOrderByFields();
474    
475                            for (int i = 0; i < orderByFields.length; i++) {
476                                    query.append(_ORDER_BY_ENTITY_ALIAS);
477                                    query.append(orderByFields[i]);
478    
479                                    if ((i + 1) < orderByFields.length) {
480                                            if (orderByComparator.isAscending() ^ previous) {
481                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
482                                            }
483                                            else {
484                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
485                                            }
486                                    }
487                                    else {
488                                            if (orderByComparator.isAscending() ^ previous) {
489                                                    query.append(ORDER_BY_ASC);
490                                            }
491                                            else {
492                                                    query.append(ORDER_BY_DESC);
493                                            }
494                                    }
495                            }
496                    }
497                    else {
498                            query.append(MBDiscussionModelImpl.ORDER_BY_JPQL);
499                    }
500    
501                    String sql = query.toString();
502    
503                    Query q = session.createQuery(sql);
504    
505                    q.setFirstResult(0);
506                    q.setMaxResults(2);
507    
508                    QueryPos qPos = QueryPos.getInstance(q);
509    
510                    if (bindUuid) {
511                            qPos.add(uuid);
512                    }
513    
514                    if (orderByComparator != null) {
515                            Object[] values = orderByComparator.getOrderByConditionValues(mbDiscussion);
516    
517                            for (Object value : values) {
518                                    qPos.add(value);
519                            }
520                    }
521    
522                    List<MBDiscussion> list = q.list();
523    
524                    if (list.size() == 2) {
525                            return list.get(1);
526                    }
527                    else {
528                            return null;
529                    }
530            }
531    
532            /**
533             * Removes all the message boards discussions where uuid = &#63; from the database.
534             *
535             * @param uuid the uuid
536             * @throws SystemException if a system exception occurred
537             */
538            @Override
539            public void removeByUuid(String uuid) throws SystemException {
540                    for (MBDiscussion mbDiscussion : findByUuid(uuid, QueryUtil.ALL_POS,
541                                    QueryUtil.ALL_POS, null)) {
542                            remove(mbDiscussion);
543                    }
544            }
545    
546            /**
547             * Returns the number of message boards discussions where uuid = &#63;.
548             *
549             * @param uuid the uuid
550             * @return the number of matching message boards discussions
551             * @throws SystemException if a system exception occurred
552             */
553            @Override
554            public int countByUuid(String uuid) throws SystemException {
555                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;
556    
557                    Object[] finderArgs = new Object[] { uuid };
558    
559                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
560                                    this);
561    
562                    if (count == null) {
563                            StringBundler query = new StringBundler(2);
564    
565                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
566    
567                            boolean bindUuid = false;
568    
569                            if (uuid == null) {
570                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
571                            }
572                            else if (uuid.equals(StringPool.BLANK)) {
573                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
574                            }
575                            else {
576                                    bindUuid = true;
577    
578                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
579                            }
580    
581                            String sql = query.toString();
582    
583                            Session session = null;
584    
585                            try {
586                                    session = openSession();
587    
588                                    Query q = session.createQuery(sql);
589    
590                                    QueryPos qPos = QueryPos.getInstance(q);
591    
592                                    if (bindUuid) {
593                                            qPos.add(uuid);
594                                    }
595    
596                                    count = (Long)q.uniqueResult();
597    
598                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
599                            }
600                            catch (Exception e) {
601                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
602    
603                                    throw processException(e);
604                            }
605                            finally {
606                                    closeSession(session);
607                            }
608                    }
609    
610                    return count.intValue();
611            }
612    
613            private static final String _FINDER_COLUMN_UUID_UUID_1 = "mbDiscussion.uuid IS NULL";
614            private static final String _FINDER_COLUMN_UUID_UUID_2 = "mbDiscussion.uuid = ?";
615            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(mbDiscussion.uuid IS NULL OR mbDiscussion.uuid = '')";
616            public static final FinderPath FINDER_PATH_FETCH_BY_UUID_G = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
617                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
618                            FINDER_CLASS_NAME_ENTITY, "fetchByUUID_G",
619                            new String[] { String.class.getName(), Long.class.getName() },
620                            MBDiscussionModelImpl.UUID_COLUMN_BITMASK |
621                            MBDiscussionModelImpl.GROUPID_COLUMN_BITMASK);
622            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_G = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
623                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
624                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUUID_G",
625                            new String[] { String.class.getName(), Long.class.getName() });
626    
627            /**
628             * Returns the message boards discussion where uuid = &#63; and groupId = &#63; or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
629             *
630             * @param uuid the uuid
631             * @param groupId the group ID
632             * @return the matching message boards discussion
633             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
634             * @throws SystemException if a system exception occurred
635             */
636            @Override
637            public MBDiscussion findByUUID_G(String uuid, long groupId)
638                    throws NoSuchDiscussionException, SystemException {
639                    MBDiscussion mbDiscussion = fetchByUUID_G(uuid, groupId);
640    
641                    if (mbDiscussion == null) {
642                            StringBundler msg = new StringBundler(6);
643    
644                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
645    
646                            msg.append("uuid=");
647                            msg.append(uuid);
648    
649                            msg.append(", groupId=");
650                            msg.append(groupId);
651    
652                            msg.append(StringPool.CLOSE_CURLY_BRACE);
653    
654                            if (_log.isWarnEnabled()) {
655                                    _log.warn(msg.toString());
656                            }
657    
658                            throw new NoSuchDiscussionException(msg.toString());
659                    }
660    
661                    return mbDiscussion;
662            }
663    
664            /**
665             * Returns the message boards discussion where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
666             *
667             * @param uuid the uuid
668             * @param groupId the group ID
669             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
670             * @throws SystemException if a system exception occurred
671             */
672            @Override
673            public MBDiscussion fetchByUUID_G(String uuid, long groupId)
674                    throws SystemException {
675                    return fetchByUUID_G(uuid, groupId, true);
676            }
677    
678            /**
679             * Returns the message boards discussion where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
680             *
681             * @param uuid the uuid
682             * @param groupId the group ID
683             * @param retrieveFromCache whether to use the finder cache
684             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
685             * @throws SystemException if a system exception occurred
686             */
687            @Override
688            public MBDiscussion fetchByUUID_G(String uuid, long groupId,
689                    boolean retrieveFromCache) throws SystemException {
690                    Object[] finderArgs = new Object[] { uuid, groupId };
691    
692                    Object result = null;
693    
694                    if (retrieveFromCache) {
695                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_UUID_G,
696                                            finderArgs, this);
697                    }
698    
699                    if (result instanceof MBDiscussion) {
700                            MBDiscussion mbDiscussion = (MBDiscussion)result;
701    
702                            if (!Validator.equals(uuid, mbDiscussion.getUuid()) ||
703                                            (groupId != mbDiscussion.getGroupId())) {
704                                    result = null;
705                            }
706                    }
707    
708                    if (result == null) {
709                            StringBundler query = new StringBundler(4);
710    
711                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
712    
713                            boolean bindUuid = false;
714    
715                            if (uuid == null) {
716                                    query.append(_FINDER_COLUMN_UUID_G_UUID_1);
717                            }
718                            else if (uuid.equals(StringPool.BLANK)) {
719                                    query.append(_FINDER_COLUMN_UUID_G_UUID_3);
720                            }
721                            else {
722                                    bindUuid = true;
723    
724                                    query.append(_FINDER_COLUMN_UUID_G_UUID_2);
725                            }
726    
727                            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
728    
729                            String sql = query.toString();
730    
731                            Session session = null;
732    
733                            try {
734                                    session = openSession();
735    
736                                    Query q = session.createQuery(sql);
737    
738                                    QueryPos qPos = QueryPos.getInstance(q);
739    
740                                    if (bindUuid) {
741                                            qPos.add(uuid);
742                                    }
743    
744                                    qPos.add(groupId);
745    
746                                    List<MBDiscussion> list = q.list();
747    
748                                    if (list.isEmpty()) {
749                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
750                                                    finderArgs, list);
751                                    }
752                                    else {
753                                            MBDiscussion mbDiscussion = list.get(0);
754    
755                                            result = mbDiscussion;
756    
757                                            cacheResult(mbDiscussion);
758    
759                                            if ((mbDiscussion.getUuid() == null) ||
760                                                            !mbDiscussion.getUuid().equals(uuid) ||
761                                                            (mbDiscussion.getGroupId() != groupId)) {
762                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
763                                                            finderArgs, mbDiscussion);
764                                            }
765                                    }
766                            }
767                            catch (Exception e) {
768                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
769                                            finderArgs);
770    
771                                    throw processException(e);
772                            }
773                            finally {
774                                    closeSession(session);
775                            }
776                    }
777    
778                    if (result instanceof List<?>) {
779                            return null;
780                    }
781                    else {
782                            return (MBDiscussion)result;
783                    }
784            }
785    
786            /**
787             * Removes the message boards discussion where uuid = &#63; and groupId = &#63; from the database.
788             *
789             * @param uuid the uuid
790             * @param groupId the group ID
791             * @return the message boards discussion that was removed
792             * @throws SystemException if a system exception occurred
793             */
794            @Override
795            public MBDiscussion removeByUUID_G(String uuid, long groupId)
796                    throws NoSuchDiscussionException, SystemException {
797                    MBDiscussion mbDiscussion = findByUUID_G(uuid, groupId);
798    
799                    return remove(mbDiscussion);
800            }
801    
802            /**
803             * Returns the number of message boards discussions where uuid = &#63; and groupId = &#63;.
804             *
805             * @param uuid the uuid
806             * @param groupId the group ID
807             * @return the number of matching message boards discussions
808             * @throws SystemException if a system exception occurred
809             */
810            @Override
811            public int countByUUID_G(String uuid, long groupId)
812                    throws SystemException {
813                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_G;
814    
815                    Object[] finderArgs = new Object[] { uuid, groupId };
816    
817                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
818                                    this);
819    
820                    if (count == null) {
821                            StringBundler query = new StringBundler(3);
822    
823                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
824    
825                            boolean bindUuid = false;
826    
827                            if (uuid == null) {
828                                    query.append(_FINDER_COLUMN_UUID_G_UUID_1);
829                            }
830                            else if (uuid.equals(StringPool.BLANK)) {
831                                    query.append(_FINDER_COLUMN_UUID_G_UUID_3);
832                            }
833                            else {
834                                    bindUuid = true;
835    
836                                    query.append(_FINDER_COLUMN_UUID_G_UUID_2);
837                            }
838    
839                            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
840    
841                            String sql = query.toString();
842    
843                            Session session = null;
844    
845                            try {
846                                    session = openSession();
847    
848                                    Query q = session.createQuery(sql);
849    
850                                    QueryPos qPos = QueryPos.getInstance(q);
851    
852                                    if (bindUuid) {
853                                            qPos.add(uuid);
854                                    }
855    
856                                    qPos.add(groupId);
857    
858                                    count = (Long)q.uniqueResult();
859    
860                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
861                            }
862                            catch (Exception e) {
863                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
864    
865                                    throw processException(e);
866                            }
867                            finally {
868                                    closeSession(session);
869                            }
870                    }
871    
872                    return count.intValue();
873            }
874    
875            private static final String _FINDER_COLUMN_UUID_G_UUID_1 = "mbDiscussion.uuid IS NULL AND ";
876            private static final String _FINDER_COLUMN_UUID_G_UUID_2 = "mbDiscussion.uuid = ? AND ";
877            private static final String _FINDER_COLUMN_UUID_G_UUID_3 = "(mbDiscussion.uuid IS NULL OR mbDiscussion.uuid = '') AND ";
878            private static final String _FINDER_COLUMN_UUID_G_GROUPID_2 = "mbDiscussion.groupId = ?";
879            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
880                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
881                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
882                            new String[] {
883                                    String.class.getName(), Long.class.getName(),
884                                    
885                            Integer.class.getName(), Integer.class.getName(),
886                                    OrderByComparator.class.getName()
887                            });
888            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
889                    new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
890                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
891                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
892                            new String[] { String.class.getName(), Long.class.getName() },
893                            MBDiscussionModelImpl.UUID_COLUMN_BITMASK |
894                            MBDiscussionModelImpl.COMPANYID_COLUMN_BITMASK);
895            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
896                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
897                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
898                            new String[] { String.class.getName(), Long.class.getName() });
899    
900            /**
901             * Returns all the message boards discussions where uuid = &#63; and companyId = &#63;.
902             *
903             * @param uuid the uuid
904             * @param companyId the company ID
905             * @return the matching message boards discussions
906             * @throws SystemException if a system exception occurred
907             */
908            @Override
909            public List<MBDiscussion> findByUuid_C(String uuid, long companyId)
910                    throws SystemException {
911                    return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
912                            QueryUtil.ALL_POS, null);
913            }
914    
915            /**
916             * Returns a range of all the message boards discussions where uuid = &#63; and companyId = &#63;.
917             *
918             * <p>
919             * 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.
920             * </p>
921             *
922             * @param uuid the uuid
923             * @param companyId the company ID
924             * @param start the lower bound of the range of message boards discussions
925             * @param end the upper bound of the range of message boards discussions (not inclusive)
926             * @return the range of matching message boards discussions
927             * @throws SystemException if a system exception occurred
928             */
929            @Override
930            public List<MBDiscussion> findByUuid_C(String uuid, long companyId,
931                    int start, int end) throws SystemException {
932                    return findByUuid_C(uuid, companyId, start, end, null);
933            }
934    
935            /**
936             * Returns an ordered range of all the message boards discussions where uuid = &#63; and companyId = &#63;.
937             *
938             * <p>
939             * 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.
940             * </p>
941             *
942             * @param uuid the uuid
943             * @param companyId the company ID
944             * @param start the lower bound of the range of message boards discussions
945             * @param end the upper bound of the range of message boards discussions (not inclusive)
946             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
947             * @return the ordered range of matching message boards discussions
948             * @throws SystemException if a system exception occurred
949             */
950            @Override
951            public List<MBDiscussion> findByUuid_C(String uuid, long companyId,
952                    int start, int end, OrderByComparator orderByComparator)
953                    throws SystemException {
954                    boolean pagination = true;
955                    FinderPath finderPath = null;
956                    Object[] finderArgs = null;
957    
958                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
959                                    (orderByComparator == null)) {
960                            pagination = false;
961                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C;
962                            finderArgs = new Object[] { uuid, companyId };
963                    }
964                    else {
965                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
966                            finderArgs = new Object[] {
967                                            uuid, companyId,
968                                            
969                                            start, end, orderByComparator
970                                    };
971                    }
972    
973                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
974                                    finderArgs, this);
975    
976                    if ((list != null) && !list.isEmpty()) {
977                            for (MBDiscussion mbDiscussion : list) {
978                                    if (!Validator.equals(uuid, mbDiscussion.getUuid()) ||
979                                                    (companyId != mbDiscussion.getCompanyId())) {
980                                            list = null;
981    
982                                            break;
983                                    }
984                            }
985                    }
986    
987                    if (list == null) {
988                            StringBundler query = null;
989    
990                            if (orderByComparator != null) {
991                                    query = new StringBundler(4 +
992                                                    (orderByComparator.getOrderByFields().length * 3));
993                            }
994                            else {
995                                    query = new StringBundler(4);
996                            }
997    
998                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
999    
1000                            boolean bindUuid = false;
1001    
1002                            if (uuid == null) {
1003                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1004                            }
1005                            else if (uuid.equals(StringPool.BLANK)) {
1006                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1007                            }
1008                            else {
1009                                    bindUuid = true;
1010    
1011                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1012                            }
1013    
1014                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1015    
1016                            if (orderByComparator != null) {
1017                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1018                                            orderByComparator);
1019                            }
1020                            else
1021                             if (pagination) {
1022                                    query.append(MBDiscussionModelImpl.ORDER_BY_JPQL);
1023                            }
1024    
1025                            String sql = query.toString();
1026    
1027                            Session session = null;
1028    
1029                            try {
1030                                    session = openSession();
1031    
1032                                    Query q = session.createQuery(sql);
1033    
1034                                    QueryPos qPos = QueryPos.getInstance(q);
1035    
1036                                    if (bindUuid) {
1037                                            qPos.add(uuid);
1038                                    }
1039    
1040                                    qPos.add(companyId);
1041    
1042                                    if (!pagination) {
1043                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1044                                                            start, end, false);
1045    
1046                                            Collections.sort(list);
1047    
1048                                            list = new UnmodifiableList<MBDiscussion>(list);
1049                                    }
1050                                    else {
1051                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1052                                                            start, end);
1053                                    }
1054    
1055                                    cacheResult(list);
1056    
1057                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1058                            }
1059                            catch (Exception e) {
1060                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1061    
1062                                    throw processException(e);
1063                            }
1064                            finally {
1065                                    closeSession(session);
1066                            }
1067                    }
1068    
1069                    return list;
1070            }
1071    
1072            /**
1073             * Returns the first message boards discussion in the ordered set where uuid = &#63; and companyId = &#63;.
1074             *
1075             * @param uuid the uuid
1076             * @param companyId the company ID
1077             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1078             * @return the first matching message boards discussion
1079             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1080             * @throws SystemException if a system exception occurred
1081             */
1082            @Override
1083            public MBDiscussion findByUuid_C_First(String uuid, long companyId,
1084                    OrderByComparator orderByComparator)
1085                    throws NoSuchDiscussionException, SystemException {
1086                    MBDiscussion mbDiscussion = fetchByUuid_C_First(uuid, companyId,
1087                                    orderByComparator);
1088    
1089                    if (mbDiscussion != null) {
1090                            return mbDiscussion;
1091                    }
1092    
1093                    StringBundler msg = new StringBundler(6);
1094    
1095                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1096    
1097                    msg.append("uuid=");
1098                    msg.append(uuid);
1099    
1100                    msg.append(", companyId=");
1101                    msg.append(companyId);
1102    
1103                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1104    
1105                    throw new NoSuchDiscussionException(msg.toString());
1106            }
1107    
1108            /**
1109             * Returns the first message boards discussion in the ordered set where uuid = &#63; and companyId = &#63;.
1110             *
1111             * @param uuid the uuid
1112             * @param companyId the company ID
1113             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1114             * @return the first matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1115             * @throws SystemException if a system exception occurred
1116             */
1117            @Override
1118            public MBDiscussion fetchByUuid_C_First(String uuid, long companyId,
1119                    OrderByComparator orderByComparator) throws SystemException {
1120                    List<MBDiscussion> list = findByUuid_C(uuid, companyId, 0, 1,
1121                                    orderByComparator);
1122    
1123                    if (!list.isEmpty()) {
1124                            return list.get(0);
1125                    }
1126    
1127                    return null;
1128            }
1129    
1130            /**
1131             * Returns the last message boards discussion in the ordered set where uuid = &#63; and companyId = &#63;.
1132             *
1133             * @param uuid the uuid
1134             * @param companyId the company ID
1135             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1136             * @return the last matching message boards discussion
1137             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1138             * @throws SystemException if a system exception occurred
1139             */
1140            @Override
1141            public MBDiscussion findByUuid_C_Last(String uuid, long companyId,
1142                    OrderByComparator orderByComparator)
1143                    throws NoSuchDiscussionException, SystemException {
1144                    MBDiscussion mbDiscussion = fetchByUuid_C_Last(uuid, companyId,
1145                                    orderByComparator);
1146    
1147                    if (mbDiscussion != null) {
1148                            return mbDiscussion;
1149                    }
1150    
1151                    StringBundler msg = new StringBundler(6);
1152    
1153                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1154    
1155                    msg.append("uuid=");
1156                    msg.append(uuid);
1157    
1158                    msg.append(", companyId=");
1159                    msg.append(companyId);
1160    
1161                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1162    
1163                    throw new NoSuchDiscussionException(msg.toString());
1164            }
1165    
1166            /**
1167             * Returns the last message boards discussion in the ordered set where uuid = &#63; and companyId = &#63;.
1168             *
1169             * @param uuid the uuid
1170             * @param companyId the company ID
1171             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1172             * @return the last matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1173             * @throws SystemException if a system exception occurred
1174             */
1175            @Override
1176            public MBDiscussion fetchByUuid_C_Last(String uuid, long companyId,
1177                    OrderByComparator orderByComparator) throws SystemException {
1178                    int count = countByUuid_C(uuid, companyId);
1179    
1180                    if (count == 0) {
1181                            return null;
1182                    }
1183    
1184                    List<MBDiscussion> list = findByUuid_C(uuid, companyId, count - 1,
1185                                    count, orderByComparator);
1186    
1187                    if (!list.isEmpty()) {
1188                            return list.get(0);
1189                    }
1190    
1191                    return null;
1192            }
1193    
1194            /**
1195             * Returns the message boards discussions before and after the current message boards discussion in the ordered set where uuid = &#63; and companyId = &#63;.
1196             *
1197             * @param discussionId the primary key of the current message boards discussion
1198             * @param uuid the uuid
1199             * @param companyId the company ID
1200             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1201             * @return the previous, current, and next message boards discussion
1202             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
1203             * @throws SystemException if a system exception occurred
1204             */
1205            @Override
1206            public MBDiscussion[] findByUuid_C_PrevAndNext(long discussionId,
1207                    String uuid, long companyId, OrderByComparator orderByComparator)
1208                    throws NoSuchDiscussionException, SystemException {
1209                    MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
1210    
1211                    Session session = null;
1212    
1213                    try {
1214                            session = openSession();
1215    
1216                            MBDiscussion[] array = new MBDiscussionImpl[3];
1217    
1218                            array[0] = getByUuid_C_PrevAndNext(session, mbDiscussion, uuid,
1219                                            companyId, orderByComparator, true);
1220    
1221                            array[1] = mbDiscussion;
1222    
1223                            array[2] = getByUuid_C_PrevAndNext(session, mbDiscussion, uuid,
1224                                            companyId, orderByComparator, false);
1225    
1226                            return array;
1227                    }
1228                    catch (Exception e) {
1229                            throw processException(e);
1230                    }
1231                    finally {
1232                            closeSession(session);
1233                    }
1234            }
1235    
1236            protected MBDiscussion getByUuid_C_PrevAndNext(Session session,
1237                    MBDiscussion mbDiscussion, String uuid, long companyId,
1238                    OrderByComparator orderByComparator, boolean previous) {
1239                    StringBundler query = null;
1240    
1241                    if (orderByComparator != null) {
1242                            query = new StringBundler(6 +
1243                                            (orderByComparator.getOrderByFields().length * 6));
1244                    }
1245                    else {
1246                            query = new StringBundler(3);
1247                    }
1248    
1249                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
1250    
1251                    boolean bindUuid = false;
1252    
1253                    if (uuid == null) {
1254                            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1255                    }
1256                    else if (uuid.equals(StringPool.BLANK)) {
1257                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1258                    }
1259                    else {
1260                            bindUuid = true;
1261    
1262                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1263                    }
1264    
1265                    query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1266    
1267                    if (orderByComparator != null) {
1268                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1269    
1270                            if (orderByConditionFields.length > 0) {
1271                                    query.append(WHERE_AND);
1272                            }
1273    
1274                            for (int i = 0; i < orderByConditionFields.length; i++) {
1275                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1276                                    query.append(orderByConditionFields[i]);
1277    
1278                                    if ((i + 1) < orderByConditionFields.length) {
1279                                            if (orderByComparator.isAscending() ^ previous) {
1280                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1281                                            }
1282                                            else {
1283                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1284                                            }
1285                                    }
1286                                    else {
1287                                            if (orderByComparator.isAscending() ^ previous) {
1288                                                    query.append(WHERE_GREATER_THAN);
1289                                            }
1290                                            else {
1291                                                    query.append(WHERE_LESSER_THAN);
1292                                            }
1293                                    }
1294                            }
1295    
1296                            query.append(ORDER_BY_CLAUSE);
1297    
1298                            String[] orderByFields = orderByComparator.getOrderByFields();
1299    
1300                            for (int i = 0; i < orderByFields.length; i++) {
1301                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1302                                    query.append(orderByFields[i]);
1303    
1304                                    if ((i + 1) < orderByFields.length) {
1305                                            if (orderByComparator.isAscending() ^ previous) {
1306                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1307                                            }
1308                                            else {
1309                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1310                                            }
1311                                    }
1312                                    else {
1313                                            if (orderByComparator.isAscending() ^ previous) {
1314                                                    query.append(ORDER_BY_ASC);
1315                                            }
1316                                            else {
1317                                                    query.append(ORDER_BY_DESC);
1318                                            }
1319                                    }
1320                            }
1321                    }
1322                    else {
1323                            query.append(MBDiscussionModelImpl.ORDER_BY_JPQL);
1324                    }
1325    
1326                    String sql = query.toString();
1327    
1328                    Query q = session.createQuery(sql);
1329    
1330                    q.setFirstResult(0);
1331                    q.setMaxResults(2);
1332    
1333                    QueryPos qPos = QueryPos.getInstance(q);
1334    
1335                    if (bindUuid) {
1336                            qPos.add(uuid);
1337                    }
1338    
1339                    qPos.add(companyId);
1340    
1341                    if (orderByComparator != null) {
1342                            Object[] values = orderByComparator.getOrderByConditionValues(mbDiscussion);
1343    
1344                            for (Object value : values) {
1345                                    qPos.add(value);
1346                            }
1347                    }
1348    
1349                    List<MBDiscussion> list = q.list();
1350    
1351                    if (list.size() == 2) {
1352                            return list.get(1);
1353                    }
1354                    else {
1355                            return null;
1356                    }
1357            }
1358    
1359            /**
1360             * Removes all the message boards discussions where uuid = &#63; and companyId = &#63; from the database.
1361             *
1362             * @param uuid the uuid
1363             * @param companyId the company ID
1364             * @throws SystemException if a system exception occurred
1365             */
1366            @Override
1367            public void removeByUuid_C(String uuid, long companyId)
1368                    throws SystemException {
1369                    for (MBDiscussion mbDiscussion : findByUuid_C(uuid, companyId,
1370                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1371                            remove(mbDiscussion);
1372                    }
1373            }
1374    
1375            /**
1376             * Returns the number of message boards discussions where uuid = &#63; and companyId = &#63;.
1377             *
1378             * @param uuid the uuid
1379             * @param companyId the company ID
1380             * @return the number of matching message boards discussions
1381             * @throws SystemException if a system exception occurred
1382             */
1383            @Override
1384            public int countByUuid_C(String uuid, long companyId)
1385                    throws SystemException {
1386                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;
1387    
1388                    Object[] finderArgs = new Object[] { uuid, companyId };
1389    
1390                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1391                                    this);
1392    
1393                    if (count == null) {
1394                            StringBundler query = new StringBundler(3);
1395    
1396                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1397    
1398                            boolean bindUuid = false;
1399    
1400                            if (uuid == null) {
1401                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1402                            }
1403                            else if (uuid.equals(StringPool.BLANK)) {
1404                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1405                            }
1406                            else {
1407                                    bindUuid = true;
1408    
1409                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1410                            }
1411    
1412                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1413    
1414                            String sql = query.toString();
1415    
1416                            Session session = null;
1417    
1418                            try {
1419                                    session = openSession();
1420    
1421                                    Query q = session.createQuery(sql);
1422    
1423                                    QueryPos qPos = QueryPos.getInstance(q);
1424    
1425                                    if (bindUuid) {
1426                                            qPos.add(uuid);
1427                                    }
1428    
1429                                    qPos.add(companyId);
1430    
1431                                    count = (Long)q.uniqueResult();
1432    
1433                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1434                            }
1435                            catch (Exception e) {
1436                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1437    
1438                                    throw processException(e);
1439                            }
1440                            finally {
1441                                    closeSession(session);
1442                            }
1443                    }
1444    
1445                    return count.intValue();
1446            }
1447    
1448            private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "mbDiscussion.uuid IS NULL AND ";
1449            private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "mbDiscussion.uuid = ? AND ";
1450            private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(mbDiscussion.uuid IS NULL OR mbDiscussion.uuid = '') AND ";
1451            private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "mbDiscussion.companyId = ?";
1452            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSNAMEID =
1453                    new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
1454                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
1455                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByClassNameId",
1456                            new String[] {
1457                                    Long.class.getName(),
1458                                    
1459                            Integer.class.getName(), Integer.class.getName(),
1460                                    OrderByComparator.class.getName()
1461                            });
1462            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID =
1463                    new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
1464                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
1465                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByClassNameId",
1466                            new String[] { Long.class.getName() },
1467                            MBDiscussionModelImpl.CLASSNAMEID_COLUMN_BITMASK);
1468            public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
1469                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1470                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByClassNameId",
1471                            new String[] { Long.class.getName() });
1472    
1473            /**
1474             * Returns all the message boards discussions where classNameId = &#63;.
1475             *
1476             * @param classNameId the class name ID
1477             * @return the matching message boards discussions
1478             * @throws SystemException if a system exception occurred
1479             */
1480            @Override
1481            public List<MBDiscussion> findByClassNameId(long classNameId)
1482                    throws SystemException {
1483                    return findByClassNameId(classNameId, QueryUtil.ALL_POS,
1484                            QueryUtil.ALL_POS, null);
1485            }
1486    
1487            /**
1488             * Returns a range of all the message boards discussions where classNameId = &#63;.
1489             *
1490             * <p>
1491             * 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.
1492             * </p>
1493             *
1494             * @param classNameId the class name ID
1495             * @param start the lower bound of the range of message boards discussions
1496             * @param end the upper bound of the range of message boards discussions (not inclusive)
1497             * @return the range of matching message boards discussions
1498             * @throws SystemException if a system exception occurred
1499             */
1500            @Override
1501            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
1502                    int end) throws SystemException {
1503                    return findByClassNameId(classNameId, start, end, null);
1504            }
1505    
1506            /**
1507             * Returns an ordered range of all the message boards discussions where classNameId = &#63;.
1508             *
1509             * <p>
1510             * 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.
1511             * </p>
1512             *
1513             * @param classNameId the class name ID
1514             * @param start the lower bound of the range of message boards discussions
1515             * @param end the upper bound of the range of message boards discussions (not inclusive)
1516             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1517             * @return the ordered range of matching message boards discussions
1518             * @throws SystemException if a system exception occurred
1519             */
1520            @Override
1521            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
1522                    int end, OrderByComparator orderByComparator) throws SystemException {
1523                    boolean pagination = true;
1524                    FinderPath finderPath = null;
1525                    Object[] finderArgs = null;
1526    
1527                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1528                                    (orderByComparator == null)) {
1529                            pagination = false;
1530                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID;
1531                            finderArgs = new Object[] { classNameId };
1532                    }
1533                    else {
1534                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSNAMEID;
1535                            finderArgs = new Object[] { classNameId, start, end, orderByComparator };
1536                    }
1537    
1538                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
1539                                    finderArgs, this);
1540    
1541                    if ((list != null) && !list.isEmpty()) {
1542                            for (MBDiscussion mbDiscussion : list) {
1543                                    if ((classNameId != mbDiscussion.getClassNameId())) {
1544                                            list = null;
1545    
1546                                            break;
1547                                    }
1548                            }
1549                    }
1550    
1551                    if (list == null) {
1552                            StringBundler query = null;
1553    
1554                            if (orderByComparator != null) {
1555                                    query = new StringBundler(3 +
1556                                                    (orderByComparator.getOrderByFields().length * 3));
1557                            }
1558                            else {
1559                                    query = new StringBundler(3);
1560                            }
1561    
1562                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
1563    
1564                            query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
1565    
1566                            if (orderByComparator != null) {
1567                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1568                                            orderByComparator);
1569                            }
1570                            else
1571                             if (pagination) {
1572                                    query.append(MBDiscussionModelImpl.ORDER_BY_JPQL);
1573                            }
1574    
1575                            String sql = query.toString();
1576    
1577                            Session session = null;
1578    
1579                            try {
1580                                    session = openSession();
1581    
1582                                    Query q = session.createQuery(sql);
1583    
1584                                    QueryPos qPos = QueryPos.getInstance(q);
1585    
1586                                    qPos.add(classNameId);
1587    
1588                                    if (!pagination) {
1589                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1590                                                            start, end, false);
1591    
1592                                            Collections.sort(list);
1593    
1594                                            list = new UnmodifiableList<MBDiscussion>(list);
1595                                    }
1596                                    else {
1597                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
1598                                                            start, end);
1599                                    }
1600    
1601                                    cacheResult(list);
1602    
1603                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1604                            }
1605                            catch (Exception e) {
1606                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1607    
1608                                    throw processException(e);
1609                            }
1610                            finally {
1611                                    closeSession(session);
1612                            }
1613                    }
1614    
1615                    return list;
1616            }
1617    
1618            /**
1619             * Returns the first message boards discussion in the ordered set where classNameId = &#63;.
1620             *
1621             * @param classNameId the class name ID
1622             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1623             * @return the first matching message boards discussion
1624             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1625             * @throws SystemException if a system exception occurred
1626             */
1627            @Override
1628            public MBDiscussion findByClassNameId_First(long classNameId,
1629                    OrderByComparator orderByComparator)
1630                    throws NoSuchDiscussionException, SystemException {
1631                    MBDiscussion mbDiscussion = fetchByClassNameId_First(classNameId,
1632                                    orderByComparator);
1633    
1634                    if (mbDiscussion != null) {
1635                            return mbDiscussion;
1636                    }
1637    
1638                    StringBundler msg = new StringBundler(4);
1639    
1640                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1641    
1642                    msg.append("classNameId=");
1643                    msg.append(classNameId);
1644    
1645                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1646    
1647                    throw new NoSuchDiscussionException(msg.toString());
1648            }
1649    
1650            /**
1651             * Returns the first message boards discussion in the ordered set where classNameId = &#63;.
1652             *
1653             * @param classNameId the class name ID
1654             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1655             * @return the first matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1656             * @throws SystemException if a system exception occurred
1657             */
1658            @Override
1659            public MBDiscussion fetchByClassNameId_First(long classNameId,
1660                    OrderByComparator orderByComparator) throws SystemException {
1661                    List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1,
1662                                    orderByComparator);
1663    
1664                    if (!list.isEmpty()) {
1665                            return list.get(0);
1666                    }
1667    
1668                    return null;
1669            }
1670    
1671            /**
1672             * Returns the last message boards discussion in the ordered set where classNameId = &#63;.
1673             *
1674             * @param classNameId the class name ID
1675             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1676             * @return the last matching message boards discussion
1677             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1678             * @throws SystemException if a system exception occurred
1679             */
1680            @Override
1681            public MBDiscussion findByClassNameId_Last(long classNameId,
1682                    OrderByComparator orderByComparator)
1683                    throws NoSuchDiscussionException, SystemException {
1684                    MBDiscussion mbDiscussion = fetchByClassNameId_Last(classNameId,
1685                                    orderByComparator);
1686    
1687                    if (mbDiscussion != null) {
1688                            return mbDiscussion;
1689                    }
1690    
1691                    StringBundler msg = new StringBundler(4);
1692    
1693                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1694    
1695                    msg.append("classNameId=");
1696                    msg.append(classNameId);
1697    
1698                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1699    
1700                    throw new NoSuchDiscussionException(msg.toString());
1701            }
1702    
1703            /**
1704             * Returns the last message boards discussion in the ordered set where classNameId = &#63;.
1705             *
1706             * @param classNameId the class name ID
1707             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1708             * @return the last matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1709             * @throws SystemException if a system exception occurred
1710             */
1711            @Override
1712            public MBDiscussion fetchByClassNameId_Last(long classNameId,
1713                    OrderByComparator orderByComparator) throws SystemException {
1714                    int count = countByClassNameId(classNameId);
1715    
1716                    if (count == 0) {
1717                            return null;
1718                    }
1719    
1720                    List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
1721                                    count, orderByComparator);
1722    
1723                    if (!list.isEmpty()) {
1724                            return list.get(0);
1725                    }
1726    
1727                    return null;
1728            }
1729    
1730            /**
1731             * Returns the message boards discussions before and after the current message boards discussion in the ordered set where classNameId = &#63;.
1732             *
1733             * @param discussionId the primary key of the current message boards discussion
1734             * @param classNameId the class name ID
1735             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1736             * @return the previous, current, and next message boards discussion
1737             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
1738             * @throws SystemException if a system exception occurred
1739             */
1740            @Override
1741            public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
1742                    long classNameId, OrderByComparator orderByComparator)
1743                    throws NoSuchDiscussionException, SystemException {
1744                    MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
1745    
1746                    Session session = null;
1747    
1748                    try {
1749                            session = openSession();
1750    
1751                            MBDiscussion[] array = new MBDiscussionImpl[3];
1752    
1753                            array[0] = getByClassNameId_PrevAndNext(session, mbDiscussion,
1754                                            classNameId, orderByComparator, true);
1755    
1756                            array[1] = mbDiscussion;
1757    
1758                            array[2] = getByClassNameId_PrevAndNext(session, mbDiscussion,
1759                                            classNameId, orderByComparator, false);
1760    
1761                            return array;
1762                    }
1763                    catch (Exception e) {
1764                            throw processException(e);
1765                    }
1766                    finally {
1767                            closeSession(session);
1768                    }
1769            }
1770    
1771            protected MBDiscussion getByClassNameId_PrevAndNext(Session session,
1772                    MBDiscussion mbDiscussion, long classNameId,
1773                    OrderByComparator orderByComparator, boolean previous) {
1774                    StringBundler query = null;
1775    
1776                    if (orderByComparator != null) {
1777                            query = new StringBundler(6 +
1778                                            (orderByComparator.getOrderByFields().length * 6));
1779                    }
1780                    else {
1781                            query = new StringBundler(3);
1782                    }
1783    
1784                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
1785    
1786                    query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
1787    
1788                    if (orderByComparator != null) {
1789                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1790    
1791                            if (orderByConditionFields.length > 0) {
1792                                    query.append(WHERE_AND);
1793                            }
1794    
1795                            for (int i = 0; i < orderByConditionFields.length; i++) {
1796                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1797                                    query.append(orderByConditionFields[i]);
1798    
1799                                    if ((i + 1) < orderByConditionFields.length) {
1800                                            if (orderByComparator.isAscending() ^ previous) {
1801                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1802                                            }
1803                                            else {
1804                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1805                                            }
1806                                    }
1807                                    else {
1808                                            if (orderByComparator.isAscending() ^ previous) {
1809                                                    query.append(WHERE_GREATER_THAN);
1810                                            }
1811                                            else {
1812                                                    query.append(WHERE_LESSER_THAN);
1813                                            }
1814                                    }
1815                            }
1816    
1817                            query.append(ORDER_BY_CLAUSE);
1818    
1819                            String[] orderByFields = orderByComparator.getOrderByFields();
1820    
1821                            for (int i = 0; i < orderByFields.length; i++) {
1822                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1823                                    query.append(orderByFields[i]);
1824    
1825                                    if ((i + 1) < orderByFields.length) {
1826                                            if (orderByComparator.isAscending() ^ previous) {
1827                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1828                                            }
1829                                            else {
1830                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1831                                            }
1832                                    }
1833                                    else {
1834                                            if (orderByComparator.isAscending() ^ previous) {
1835                                                    query.append(ORDER_BY_ASC);
1836                                            }
1837                                            else {
1838                                                    query.append(ORDER_BY_DESC);
1839                                            }
1840                                    }
1841                            }
1842                    }
1843                    else {
1844                            query.append(MBDiscussionModelImpl.ORDER_BY_JPQL);
1845                    }
1846    
1847                    String sql = query.toString();
1848    
1849                    Query q = session.createQuery(sql);
1850    
1851                    q.setFirstResult(0);
1852                    q.setMaxResults(2);
1853    
1854                    QueryPos qPos = QueryPos.getInstance(q);
1855    
1856                    qPos.add(classNameId);
1857    
1858                    if (orderByComparator != null) {
1859                            Object[] values = orderByComparator.getOrderByConditionValues(mbDiscussion);
1860    
1861                            for (Object value : values) {
1862                                    qPos.add(value);
1863                            }
1864                    }
1865    
1866                    List<MBDiscussion> list = q.list();
1867    
1868                    if (list.size() == 2) {
1869                            return list.get(1);
1870                    }
1871                    else {
1872                            return null;
1873                    }
1874            }
1875    
1876            /**
1877             * Removes all the message boards discussions where classNameId = &#63; from the database.
1878             *
1879             * @param classNameId the class name ID
1880             * @throws SystemException if a system exception occurred
1881             */
1882            @Override
1883            public void removeByClassNameId(long classNameId) throws SystemException {
1884                    for (MBDiscussion mbDiscussion : findByClassNameId(classNameId,
1885                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1886                            remove(mbDiscussion);
1887                    }
1888            }
1889    
1890            /**
1891             * Returns the number of message boards discussions where classNameId = &#63;.
1892             *
1893             * @param classNameId the class name ID
1894             * @return the number of matching message boards discussions
1895             * @throws SystemException if a system exception occurred
1896             */
1897            @Override
1898            public int countByClassNameId(long classNameId) throws SystemException {
1899                    FinderPath finderPath = FINDER_PATH_COUNT_BY_CLASSNAMEID;
1900    
1901                    Object[] finderArgs = new Object[] { classNameId };
1902    
1903                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1904                                    this);
1905    
1906                    if (count == null) {
1907                            StringBundler query = new StringBundler(2);
1908    
1909                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1910    
1911                            query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
1912    
1913                            String sql = query.toString();
1914    
1915                            Session session = null;
1916    
1917                            try {
1918                                    session = openSession();
1919    
1920                                    Query q = session.createQuery(sql);
1921    
1922                                    QueryPos qPos = QueryPos.getInstance(q);
1923    
1924                                    qPos.add(classNameId);
1925    
1926                                    count = (Long)q.uniqueResult();
1927    
1928                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1929                            }
1930                            catch (Exception e) {
1931                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1932    
1933                                    throw processException(e);
1934                            }
1935                            finally {
1936                                    closeSession(session);
1937                            }
1938                    }
1939    
1940                    return count.intValue();
1941            }
1942    
1943            private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1944            public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
1945                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
1946                            FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
1947                            new String[] { Long.class.getName() },
1948                            MBDiscussionModelImpl.THREADID_COLUMN_BITMASK);
1949            public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
1950                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1951                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByThreadId",
1952                            new String[] { Long.class.getName() });
1953    
1954            /**
1955             * Returns the message boards discussion where threadId = &#63; or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
1956             *
1957             * @param threadId the thread ID
1958             * @return the matching message boards discussion
1959             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
1960             * @throws SystemException if a system exception occurred
1961             */
1962            @Override
1963            public MBDiscussion findByThreadId(long threadId)
1964                    throws NoSuchDiscussionException, SystemException {
1965                    MBDiscussion mbDiscussion = fetchByThreadId(threadId);
1966    
1967                    if (mbDiscussion == null) {
1968                            StringBundler msg = new StringBundler(4);
1969    
1970                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1971    
1972                            msg.append("threadId=");
1973                            msg.append(threadId);
1974    
1975                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1976    
1977                            if (_log.isWarnEnabled()) {
1978                                    _log.warn(msg.toString());
1979                            }
1980    
1981                            throw new NoSuchDiscussionException(msg.toString());
1982                    }
1983    
1984                    return mbDiscussion;
1985            }
1986    
1987            /**
1988             * Returns the message boards discussion where threadId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1989             *
1990             * @param threadId the thread ID
1991             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
1992             * @throws SystemException if a system exception occurred
1993             */
1994            @Override
1995            public MBDiscussion fetchByThreadId(long threadId)
1996                    throws SystemException {
1997                    return fetchByThreadId(threadId, true);
1998            }
1999    
2000            /**
2001             * Returns the message boards discussion where threadId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
2002             *
2003             * @param threadId the thread ID
2004             * @param retrieveFromCache whether to use the finder cache
2005             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
2006             * @throws SystemException if a system exception occurred
2007             */
2008            @Override
2009            public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
2010                    throws SystemException {
2011                    Object[] finderArgs = new Object[] { threadId };
2012    
2013                    Object result = null;
2014    
2015                    if (retrieveFromCache) {
2016                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
2017                                            finderArgs, this);
2018                    }
2019    
2020                    if (result instanceof MBDiscussion) {
2021                            MBDiscussion mbDiscussion = (MBDiscussion)result;
2022    
2023                            if ((threadId != mbDiscussion.getThreadId())) {
2024                                    result = null;
2025                            }
2026                    }
2027    
2028                    if (result == null) {
2029                            StringBundler query = new StringBundler(3);
2030    
2031                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
2032    
2033                            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
2034    
2035                            String sql = query.toString();
2036    
2037                            Session session = null;
2038    
2039                            try {
2040                                    session = openSession();
2041    
2042                                    Query q = session.createQuery(sql);
2043    
2044                                    QueryPos qPos = QueryPos.getInstance(q);
2045    
2046                                    qPos.add(threadId);
2047    
2048                                    List<MBDiscussion> list = q.list();
2049    
2050                                    if (list.isEmpty()) {
2051                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
2052                                                    finderArgs, list);
2053                                    }
2054                                    else {
2055                                            MBDiscussion mbDiscussion = list.get(0);
2056    
2057                                            result = mbDiscussion;
2058    
2059                                            cacheResult(mbDiscussion);
2060    
2061                                            if ((mbDiscussion.getThreadId() != threadId)) {
2062                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
2063                                                            finderArgs, mbDiscussion);
2064                                            }
2065                                    }
2066                            }
2067                            catch (Exception e) {
2068                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
2069                                            finderArgs);
2070    
2071                                    throw processException(e);
2072                            }
2073                            finally {
2074                                    closeSession(session);
2075                            }
2076                    }
2077    
2078                    if (result instanceof List<?>) {
2079                            return null;
2080                    }
2081                    else {
2082                            return (MBDiscussion)result;
2083                    }
2084            }
2085    
2086            /**
2087             * Removes the message boards discussion where threadId = &#63; from the database.
2088             *
2089             * @param threadId the thread ID
2090             * @return the message boards discussion that was removed
2091             * @throws SystemException if a system exception occurred
2092             */
2093            @Override
2094            public MBDiscussion removeByThreadId(long threadId)
2095                    throws NoSuchDiscussionException, SystemException {
2096                    MBDiscussion mbDiscussion = findByThreadId(threadId);
2097    
2098                    return remove(mbDiscussion);
2099            }
2100    
2101            /**
2102             * Returns the number of message boards discussions where threadId = &#63;.
2103             *
2104             * @param threadId the thread ID
2105             * @return the number of matching message boards discussions
2106             * @throws SystemException if a system exception occurred
2107             */
2108            @Override
2109            public int countByThreadId(long threadId) throws SystemException {
2110                    FinderPath finderPath = FINDER_PATH_COUNT_BY_THREADID;
2111    
2112                    Object[] finderArgs = new Object[] { threadId };
2113    
2114                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2115                                    this);
2116    
2117                    if (count == null) {
2118                            StringBundler query = new StringBundler(2);
2119    
2120                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
2121    
2122                            query.append(_FINDER_COLUMN_THREADID_THREADID_2);
2123    
2124                            String sql = query.toString();
2125    
2126                            Session session = null;
2127    
2128                            try {
2129                                    session = openSession();
2130    
2131                                    Query q = session.createQuery(sql);
2132    
2133                                    QueryPos qPos = QueryPos.getInstance(q);
2134    
2135                                    qPos.add(threadId);
2136    
2137                                    count = (Long)q.uniqueResult();
2138    
2139                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2140                            }
2141                            catch (Exception e) {
2142                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2143    
2144                                    throw processException(e);
2145                            }
2146                            finally {
2147                                    closeSession(session);
2148                            }
2149                    }
2150    
2151                    return count.intValue();
2152            }
2153    
2154            private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
2155            public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2156                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, MBDiscussionImpl.class,
2157                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
2158                            new String[] { Long.class.getName(), Long.class.getName() },
2159                            MBDiscussionModelImpl.CLASSNAMEID_COLUMN_BITMASK |
2160                            MBDiscussionModelImpl.CLASSPK_COLUMN_BITMASK);
2161            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2162                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, Long.class,
2163                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
2164                            new String[] { Long.class.getName(), Long.class.getName() });
2165    
2166            /**
2167             * 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.
2168             *
2169             * @param classNameId the class name ID
2170             * @param classPK the class p k
2171             * @return the matching message boards discussion
2172             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a matching message boards discussion could not be found
2173             * @throws SystemException if a system exception occurred
2174             */
2175            @Override
2176            public MBDiscussion findByC_C(long classNameId, long classPK)
2177                    throws NoSuchDiscussionException, SystemException {
2178                    MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
2179    
2180                    if (mbDiscussion == null) {
2181                            StringBundler msg = new StringBundler(6);
2182    
2183                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2184    
2185                            msg.append("classNameId=");
2186                            msg.append(classNameId);
2187    
2188                            msg.append(", classPK=");
2189                            msg.append(classPK);
2190    
2191                            msg.append(StringPool.CLOSE_CURLY_BRACE);
2192    
2193                            if (_log.isWarnEnabled()) {
2194                                    _log.warn(msg.toString());
2195                            }
2196    
2197                            throw new NoSuchDiscussionException(msg.toString());
2198                    }
2199    
2200                    return mbDiscussion;
2201            }
2202    
2203            /**
2204             * 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.
2205             *
2206             * @param classNameId the class name ID
2207             * @param classPK the class p k
2208             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
2209             * @throws SystemException if a system exception occurred
2210             */
2211            @Override
2212            public MBDiscussion fetchByC_C(long classNameId, long classPK)
2213                    throws SystemException {
2214                    return fetchByC_C(classNameId, classPK, true);
2215            }
2216    
2217            /**
2218             * 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.
2219             *
2220             * @param classNameId the class name ID
2221             * @param classPK the class p k
2222             * @param retrieveFromCache whether to use the finder cache
2223             * @return the matching message boards discussion, or <code>null</code> if a matching message boards discussion could not be found
2224             * @throws SystemException if a system exception occurred
2225             */
2226            @Override
2227            public MBDiscussion fetchByC_C(long classNameId, long classPK,
2228                    boolean retrieveFromCache) throws SystemException {
2229                    Object[] finderArgs = new Object[] { classNameId, classPK };
2230    
2231                    Object result = null;
2232    
2233                    if (retrieveFromCache) {
2234                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
2235                                            finderArgs, this);
2236                    }
2237    
2238                    if (result instanceof MBDiscussion) {
2239                            MBDiscussion mbDiscussion = (MBDiscussion)result;
2240    
2241                            if ((classNameId != mbDiscussion.getClassNameId()) ||
2242                                            (classPK != mbDiscussion.getClassPK())) {
2243                                    result = null;
2244                            }
2245                    }
2246    
2247                    if (result == null) {
2248                            StringBundler query = new StringBundler(4);
2249    
2250                            query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
2251    
2252                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2253    
2254                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
2255    
2256                            String sql = query.toString();
2257    
2258                            Session session = null;
2259    
2260                            try {
2261                                    session = openSession();
2262    
2263                                    Query q = session.createQuery(sql);
2264    
2265                                    QueryPos qPos = QueryPos.getInstance(q);
2266    
2267                                    qPos.add(classNameId);
2268    
2269                                    qPos.add(classPK);
2270    
2271                                    List<MBDiscussion> list = q.list();
2272    
2273                                    if (list.isEmpty()) {
2274                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
2275                                                    finderArgs, list);
2276                                    }
2277                                    else {
2278                                            MBDiscussion mbDiscussion = list.get(0);
2279    
2280                                            result = mbDiscussion;
2281    
2282                                            cacheResult(mbDiscussion);
2283    
2284                                            if ((mbDiscussion.getClassNameId() != classNameId) ||
2285                                                            (mbDiscussion.getClassPK() != classPK)) {
2286                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
2287                                                            finderArgs, mbDiscussion);
2288                                            }
2289                                    }
2290                            }
2291                            catch (Exception e) {
2292                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
2293                                            finderArgs);
2294    
2295                                    throw processException(e);
2296                            }
2297                            finally {
2298                                    closeSession(session);
2299                            }
2300                    }
2301    
2302                    if (result instanceof List<?>) {
2303                            return null;
2304                    }
2305                    else {
2306                            return (MBDiscussion)result;
2307                    }
2308            }
2309    
2310            /**
2311             * Removes the message boards discussion where classNameId = &#63; and classPK = &#63; from the database.
2312             *
2313             * @param classNameId the class name ID
2314             * @param classPK the class p k
2315             * @return the message boards discussion that was removed
2316             * @throws SystemException if a system exception occurred
2317             */
2318            @Override
2319            public MBDiscussion removeByC_C(long classNameId, long classPK)
2320                    throws NoSuchDiscussionException, SystemException {
2321                    MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
2322    
2323                    return remove(mbDiscussion);
2324            }
2325    
2326            /**
2327             * Returns the number of message boards discussions where classNameId = &#63; and classPK = &#63;.
2328             *
2329             * @param classNameId the class name ID
2330             * @param classPK the class p k
2331             * @return the number of matching message boards discussions
2332             * @throws SystemException if a system exception occurred
2333             */
2334            @Override
2335            public int countByC_C(long classNameId, long classPK)
2336                    throws SystemException {
2337                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
2338    
2339                    Object[] finderArgs = new Object[] { classNameId, classPK };
2340    
2341                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2342                                    this);
2343    
2344                    if (count == null) {
2345                            StringBundler query = new StringBundler(3);
2346    
2347                            query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
2348    
2349                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2350    
2351                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
2352    
2353                            String sql = query.toString();
2354    
2355                            Session session = null;
2356    
2357                            try {
2358                                    session = openSession();
2359    
2360                                    Query q = session.createQuery(sql);
2361    
2362                                    QueryPos qPos = QueryPos.getInstance(q);
2363    
2364                                    qPos.add(classNameId);
2365    
2366                                    qPos.add(classPK);
2367    
2368                                    count = (Long)q.uniqueResult();
2369    
2370                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2371                            }
2372                            catch (Exception e) {
2373                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2374    
2375                                    throw processException(e);
2376                            }
2377                            finally {
2378                                    closeSession(session);
2379                            }
2380                    }
2381    
2382                    return count.intValue();
2383            }
2384    
2385            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
2386            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
2387    
2388            public MBDiscussionPersistenceImpl() {
2389                    setModelClass(MBDiscussion.class);
2390            }
2391    
2392            /**
2393             * Caches the message boards discussion in the entity cache if it is enabled.
2394             *
2395             * @param mbDiscussion the message boards discussion
2396             */
2397            @Override
2398            public void cacheResult(MBDiscussion mbDiscussion) {
2399                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2400                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
2401    
2402                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
2403                            new Object[] { mbDiscussion.getUuid(), mbDiscussion.getGroupId() },
2404                            mbDiscussion);
2405    
2406                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
2407                            new Object[] { mbDiscussion.getThreadId() }, mbDiscussion);
2408    
2409                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
2410                            new Object[] {
2411                                    mbDiscussion.getClassNameId(), mbDiscussion.getClassPK()
2412                            }, mbDiscussion);
2413    
2414                    mbDiscussion.resetOriginalValues();
2415            }
2416    
2417            /**
2418             * Caches the message boards discussions in the entity cache if it is enabled.
2419             *
2420             * @param mbDiscussions the message boards discussions
2421             */
2422            @Override
2423            public void cacheResult(List<MBDiscussion> mbDiscussions) {
2424                    for (MBDiscussion mbDiscussion : mbDiscussions) {
2425                            if (EntityCacheUtil.getResult(
2426                                                    MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2427                                                    MBDiscussionImpl.class, mbDiscussion.getPrimaryKey()) == null) {
2428                                    cacheResult(mbDiscussion);
2429                            }
2430                            else {
2431                                    mbDiscussion.resetOriginalValues();
2432                            }
2433                    }
2434            }
2435    
2436            /**
2437             * Clears the cache for all message boards discussions.
2438             *
2439             * <p>
2440             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
2441             * </p>
2442             */
2443            @Override
2444            public void clearCache() {
2445                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
2446                            CacheRegistryUtil.clear(MBDiscussionImpl.class.getName());
2447                    }
2448    
2449                    EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
2450    
2451                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
2452                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2453                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2454            }
2455    
2456            /**
2457             * Clears the cache for the message boards discussion.
2458             *
2459             * <p>
2460             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
2461             * </p>
2462             */
2463            @Override
2464            public void clearCache(MBDiscussion mbDiscussion) {
2465                    EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2466                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
2467    
2468                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2469                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2470    
2471                    clearUniqueFindersCache(mbDiscussion);
2472            }
2473    
2474            @Override
2475            public void clearCache(List<MBDiscussion> mbDiscussions) {
2476                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2477                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2478    
2479                    for (MBDiscussion mbDiscussion : mbDiscussions) {
2480                            EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2481                                    MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
2482    
2483                            clearUniqueFindersCache(mbDiscussion);
2484                    }
2485            }
2486    
2487            protected void cacheUniqueFindersCache(MBDiscussion mbDiscussion) {
2488                    if (mbDiscussion.isNew()) {
2489                            Object[] args = new Object[] {
2490                                            mbDiscussion.getUuid(), mbDiscussion.getGroupId()
2491                                    };
2492    
2493                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
2494                                    Long.valueOf(1));
2495                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
2496                                    mbDiscussion);
2497    
2498                            args = new Object[] { mbDiscussion.getThreadId() };
2499    
2500                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID, args,
2501                                    Long.valueOf(1));
2502                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID, args,
2503                                    mbDiscussion);
2504    
2505                            args = new Object[] {
2506                                            mbDiscussion.getClassNameId(), mbDiscussion.getClassPK()
2507                                    };
2508    
2509                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
2510                                    Long.valueOf(1));
2511                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args,
2512                                    mbDiscussion);
2513                    }
2514                    else {
2515                            MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
2516    
2517                            if ((mbDiscussionModelImpl.getColumnBitmask() &
2518                                            FINDER_PATH_FETCH_BY_UUID_G.getColumnBitmask()) != 0) {
2519                                    Object[] args = new Object[] {
2520                                                    mbDiscussion.getUuid(), mbDiscussion.getGroupId()
2521                                            };
2522    
2523                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G, args,
2524                                            Long.valueOf(1));
2525                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, args,
2526                                            mbDiscussion);
2527                            }
2528    
2529                            if ((mbDiscussionModelImpl.getColumnBitmask() &
2530                                            FINDER_PATH_FETCH_BY_THREADID.getColumnBitmask()) != 0) {
2531                                    Object[] args = new Object[] { mbDiscussion.getThreadId() };
2532    
2533                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID, args,
2534                                            Long.valueOf(1));
2535                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID, args,
2536                                            mbDiscussion);
2537                            }
2538    
2539                            if ((mbDiscussionModelImpl.getColumnBitmask() &
2540                                            FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
2541                                    Object[] args = new Object[] {
2542                                                    mbDiscussion.getClassNameId(), mbDiscussion.getClassPK()
2543                                            };
2544    
2545                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
2546                                            Long.valueOf(1));
2547                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args,
2548                                            mbDiscussion);
2549                            }
2550                    }
2551            }
2552    
2553            protected void clearUniqueFindersCache(MBDiscussion mbDiscussion) {
2554                    MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
2555    
2556                    Object[] args = new Object[] {
2557                                    mbDiscussion.getUuid(), mbDiscussion.getGroupId()
2558                            };
2559    
2560                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_G, args);
2561                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, args);
2562    
2563                    if ((mbDiscussionModelImpl.getColumnBitmask() &
2564                                    FINDER_PATH_FETCH_BY_UUID_G.getColumnBitmask()) != 0) {
2565                            args = new Object[] {
2566                                            mbDiscussionModelImpl.getOriginalUuid(),
2567                                            mbDiscussionModelImpl.getOriginalGroupId()
2568                                    };
2569    
2570                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_G, args);
2571                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, args);
2572                    }
2573    
2574                    args = new Object[] { mbDiscussion.getThreadId() };
2575    
2576                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_THREADID, args);
2577                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID, args);
2578    
2579                    if ((mbDiscussionModelImpl.getColumnBitmask() &
2580                                    FINDER_PATH_FETCH_BY_THREADID.getColumnBitmask()) != 0) {
2581                            args = new Object[] { mbDiscussionModelImpl.getOriginalThreadId() };
2582    
2583                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_THREADID, args);
2584                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID, args);
2585                    }
2586    
2587                    args = new Object[] {
2588                                    mbDiscussion.getClassNameId(), mbDiscussion.getClassPK()
2589                            };
2590    
2591                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
2592                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
2593    
2594                    if ((mbDiscussionModelImpl.getColumnBitmask() &
2595                                    FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
2596                            args = new Object[] {
2597                                            mbDiscussionModelImpl.getOriginalClassNameId(),
2598                                            mbDiscussionModelImpl.getOriginalClassPK()
2599                                    };
2600    
2601                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
2602                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
2603                    }
2604            }
2605    
2606            /**
2607             * Creates a new message boards discussion with the primary key. Does not add the message boards discussion to the database.
2608             *
2609             * @param discussionId the primary key for the new message boards discussion
2610             * @return the new message boards discussion
2611             */
2612            @Override
2613            public MBDiscussion create(long discussionId) {
2614                    MBDiscussion mbDiscussion = new MBDiscussionImpl();
2615    
2616                    mbDiscussion.setNew(true);
2617                    mbDiscussion.setPrimaryKey(discussionId);
2618    
2619                    String uuid = PortalUUIDUtil.generate();
2620    
2621                    mbDiscussion.setUuid(uuid);
2622    
2623                    return mbDiscussion;
2624            }
2625    
2626            /**
2627             * Removes the message boards discussion with the primary key from the database. Also notifies the appropriate model listeners.
2628             *
2629             * @param discussionId the primary key of the message boards discussion
2630             * @return the message boards discussion that was removed
2631             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
2632             * @throws SystemException if a system exception occurred
2633             */
2634            @Override
2635            public MBDiscussion remove(long discussionId)
2636                    throws NoSuchDiscussionException, SystemException {
2637                    return remove((Serializable)discussionId);
2638            }
2639    
2640            /**
2641             * Removes the message boards discussion with the primary key from the database. Also notifies the appropriate model listeners.
2642             *
2643             * @param primaryKey the primary key of the message boards discussion
2644             * @return the message boards discussion that was removed
2645             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
2646             * @throws SystemException if a system exception occurred
2647             */
2648            @Override
2649            public MBDiscussion remove(Serializable primaryKey)
2650                    throws NoSuchDiscussionException, SystemException {
2651                    Session session = null;
2652    
2653                    try {
2654                            session = openSession();
2655    
2656                            MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
2657                                            primaryKey);
2658    
2659                            if (mbDiscussion == null) {
2660                                    if (_log.isWarnEnabled()) {
2661                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2662                                    }
2663    
2664                                    throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2665                                            primaryKey);
2666                            }
2667    
2668                            return remove(mbDiscussion);
2669                    }
2670                    catch (NoSuchDiscussionException nsee) {
2671                            throw nsee;
2672                    }
2673                    catch (Exception e) {
2674                            throw processException(e);
2675                    }
2676                    finally {
2677                            closeSession(session);
2678                    }
2679            }
2680    
2681            @Override
2682            protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
2683                    throws SystemException {
2684                    mbDiscussion = toUnwrappedModel(mbDiscussion);
2685    
2686                    Session session = null;
2687    
2688                    try {
2689                            session = openSession();
2690    
2691                            if (!session.contains(mbDiscussion)) {
2692                                    mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
2693                                                    mbDiscussion.getPrimaryKeyObj());
2694                            }
2695    
2696                            if (mbDiscussion != null) {
2697                                    session.delete(mbDiscussion);
2698                            }
2699                    }
2700                    catch (Exception e) {
2701                            throw processException(e);
2702                    }
2703                    finally {
2704                            closeSession(session);
2705                    }
2706    
2707                    if (mbDiscussion != null) {
2708                            clearCache(mbDiscussion);
2709                    }
2710    
2711                    return mbDiscussion;
2712            }
2713    
2714            @Override
2715            public MBDiscussion updateImpl(
2716                    com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion)
2717                    throws SystemException {
2718                    mbDiscussion = toUnwrappedModel(mbDiscussion);
2719    
2720                    boolean isNew = mbDiscussion.isNew();
2721    
2722                    MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
2723    
2724                    if (Validator.isNull(mbDiscussion.getUuid())) {
2725                            String uuid = PortalUUIDUtil.generate();
2726    
2727                            mbDiscussion.setUuid(uuid);
2728                    }
2729    
2730                    Session session = null;
2731    
2732                    try {
2733                            session = openSession();
2734    
2735                            if (mbDiscussion.isNew()) {
2736                                    session.save(mbDiscussion);
2737    
2738                                    mbDiscussion.setNew(false);
2739                            }
2740                            else {
2741                                    session.merge(mbDiscussion);
2742                            }
2743                    }
2744                    catch (Exception e) {
2745                            throw processException(e);
2746                    }
2747                    finally {
2748                            closeSession(session);
2749                    }
2750    
2751                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2752    
2753                    if (isNew || !MBDiscussionModelImpl.COLUMN_BITMASK_ENABLED) {
2754                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2755                    }
2756    
2757                    else {
2758                            if ((mbDiscussionModelImpl.getColumnBitmask() &
2759                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
2760                                    Object[] args = new Object[] {
2761                                                    mbDiscussionModelImpl.getOriginalUuid()
2762                                            };
2763    
2764                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
2765                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
2766                                            args);
2767    
2768                                    args = new Object[] { mbDiscussionModelImpl.getUuid() };
2769    
2770                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
2771                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
2772                                            args);
2773                            }
2774    
2775                            if ((mbDiscussionModelImpl.getColumnBitmask() &
2776                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
2777                                    Object[] args = new Object[] {
2778                                                    mbDiscussionModelImpl.getOriginalUuid(),
2779                                                    mbDiscussionModelImpl.getOriginalCompanyId()
2780                                            };
2781    
2782                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
2783                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
2784                                            args);
2785    
2786                                    args = new Object[] {
2787                                                    mbDiscussionModelImpl.getUuid(),
2788                                                    mbDiscussionModelImpl.getCompanyId()
2789                                            };
2790    
2791                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
2792                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
2793                                            args);
2794                            }
2795    
2796                            if ((mbDiscussionModelImpl.getColumnBitmask() &
2797                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID.getColumnBitmask()) != 0) {
2798                                    Object[] args = new Object[] {
2799                                                    mbDiscussionModelImpl.getOriginalClassNameId()
2800                                            };
2801    
2802                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
2803                                            args);
2804                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID,
2805                                            args);
2806    
2807                                    args = new Object[] { mbDiscussionModelImpl.getClassNameId() };
2808    
2809                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
2810                                            args);
2811                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSNAMEID,
2812                                            args);
2813                            }
2814                    }
2815    
2816                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2817                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
2818    
2819                    clearUniqueFindersCache(mbDiscussion);
2820                    cacheUniqueFindersCache(mbDiscussion);
2821    
2822                    return mbDiscussion;
2823            }
2824    
2825            protected MBDiscussion toUnwrappedModel(MBDiscussion mbDiscussion) {
2826                    if (mbDiscussion instanceof MBDiscussionImpl) {
2827                            return mbDiscussion;
2828                    }
2829    
2830                    MBDiscussionImpl mbDiscussionImpl = new MBDiscussionImpl();
2831    
2832                    mbDiscussionImpl.setNew(mbDiscussion.isNew());
2833                    mbDiscussionImpl.setPrimaryKey(mbDiscussion.getPrimaryKey());
2834    
2835                    mbDiscussionImpl.setUuid(mbDiscussion.getUuid());
2836                    mbDiscussionImpl.setDiscussionId(mbDiscussion.getDiscussionId());
2837                    mbDiscussionImpl.setGroupId(mbDiscussion.getGroupId());
2838                    mbDiscussionImpl.setCompanyId(mbDiscussion.getCompanyId());
2839                    mbDiscussionImpl.setUserId(mbDiscussion.getUserId());
2840                    mbDiscussionImpl.setUserName(mbDiscussion.getUserName());
2841                    mbDiscussionImpl.setCreateDate(mbDiscussion.getCreateDate());
2842                    mbDiscussionImpl.setModifiedDate(mbDiscussion.getModifiedDate());
2843                    mbDiscussionImpl.setClassNameId(mbDiscussion.getClassNameId());
2844                    mbDiscussionImpl.setClassPK(mbDiscussion.getClassPK());
2845                    mbDiscussionImpl.setThreadId(mbDiscussion.getThreadId());
2846    
2847                    return mbDiscussionImpl;
2848            }
2849    
2850            /**
2851             * Returns the message boards discussion with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
2852             *
2853             * @param primaryKey the primary key of the message boards discussion
2854             * @return the message boards discussion
2855             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
2856             * @throws SystemException if a system exception occurred
2857             */
2858            @Override
2859            public MBDiscussion findByPrimaryKey(Serializable primaryKey)
2860                    throws NoSuchDiscussionException, SystemException {
2861                    MBDiscussion mbDiscussion = fetchByPrimaryKey(primaryKey);
2862    
2863                    if (mbDiscussion == null) {
2864                            if (_log.isWarnEnabled()) {
2865                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2866                            }
2867    
2868                            throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2869                                    primaryKey);
2870                    }
2871    
2872                    return mbDiscussion;
2873            }
2874    
2875            /**
2876             * Returns the message boards discussion with the primary key or throws a {@link com.liferay.portlet.messageboards.NoSuchDiscussionException} if it could not be found.
2877             *
2878             * @param discussionId the primary key of the message boards discussion
2879             * @return the message boards discussion
2880             * @throws com.liferay.portlet.messageboards.NoSuchDiscussionException if a message boards discussion with the primary key could not be found
2881             * @throws SystemException if a system exception occurred
2882             */
2883            @Override
2884            public MBDiscussion findByPrimaryKey(long discussionId)
2885                    throws NoSuchDiscussionException, SystemException {
2886                    return findByPrimaryKey((Serializable)discussionId);
2887            }
2888    
2889            /**
2890             * Returns the message boards discussion with the primary key or returns <code>null</code> if it could not be found.
2891             *
2892             * @param primaryKey the primary key of the message boards discussion
2893             * @return the message boards discussion, or <code>null</code> if a message boards discussion with the primary key could not be found
2894             * @throws SystemException if a system exception occurred
2895             */
2896            @Override
2897            public MBDiscussion fetchByPrimaryKey(Serializable primaryKey)
2898                    throws SystemException {
2899                    MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2900                                    MBDiscussionImpl.class, primaryKey);
2901    
2902                    if (mbDiscussion == _nullMBDiscussion) {
2903                            return null;
2904                    }
2905    
2906                    if (mbDiscussion == null) {
2907                            Session session = null;
2908    
2909                            try {
2910                                    session = openSession();
2911    
2912                                    mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
2913                                                    primaryKey);
2914    
2915                                    if (mbDiscussion != null) {
2916                                            cacheResult(mbDiscussion);
2917                                    }
2918                                    else {
2919                                            EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2920                                                    MBDiscussionImpl.class, primaryKey, _nullMBDiscussion);
2921                                    }
2922                            }
2923                            catch (Exception e) {
2924                                    EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
2925                                            MBDiscussionImpl.class, primaryKey);
2926    
2927                                    throw processException(e);
2928                            }
2929                            finally {
2930                                    closeSession(session);
2931                            }
2932                    }
2933    
2934                    return mbDiscussion;
2935            }
2936    
2937            /**
2938             * Returns the message boards discussion with the primary key or returns <code>null</code> if it could not be found.
2939             *
2940             * @param discussionId the primary key of the message boards discussion
2941             * @return the message boards discussion, or <code>null</code> if a message boards discussion with the primary key could not be found
2942             * @throws SystemException if a system exception occurred
2943             */
2944            @Override
2945            public MBDiscussion fetchByPrimaryKey(long discussionId)
2946                    throws SystemException {
2947                    return fetchByPrimaryKey((Serializable)discussionId);
2948            }
2949    
2950            /**
2951             * Returns all the message boards discussions.
2952             *
2953             * @return the message boards discussions
2954             * @throws SystemException if a system exception occurred
2955             */
2956            @Override
2957            public List<MBDiscussion> findAll() throws SystemException {
2958                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2959            }
2960    
2961            /**
2962             * Returns a range of all the message boards discussions.
2963             *
2964             * <p>
2965             * 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.
2966             * </p>
2967             *
2968             * @param start the lower bound of the range of message boards discussions
2969             * @param end the upper bound of the range of message boards discussions (not inclusive)
2970             * @return the range of message boards discussions
2971             * @throws SystemException if a system exception occurred
2972             */
2973            @Override
2974            public List<MBDiscussion> findAll(int start, int end)
2975                    throws SystemException {
2976                    return findAll(start, end, null);
2977            }
2978    
2979            /**
2980             * Returns an ordered range of all the message boards discussions.
2981             *
2982             * <p>
2983             * 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.
2984             * </p>
2985             *
2986             * @param start the lower bound of the range of message boards discussions
2987             * @param end the upper bound of the range of message boards discussions (not inclusive)
2988             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2989             * @return the ordered range of message boards discussions
2990             * @throws SystemException if a system exception occurred
2991             */
2992            @Override
2993            public List<MBDiscussion> findAll(int start, int end,
2994                    OrderByComparator orderByComparator) throws SystemException {
2995                    boolean pagination = true;
2996                    FinderPath finderPath = null;
2997                    Object[] finderArgs = null;
2998    
2999                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3000                                    (orderByComparator == null)) {
3001                            pagination = false;
3002                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
3003                            finderArgs = FINDER_ARGS_EMPTY;
3004                    }
3005                    else {
3006                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
3007                            finderArgs = new Object[] { start, end, orderByComparator };
3008                    }
3009    
3010                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(finderPath,
3011                                    finderArgs, this);
3012    
3013                    if (list == null) {
3014                            StringBundler query = null;
3015                            String sql = null;
3016    
3017                            if (orderByComparator != null) {
3018                                    query = new StringBundler(2 +
3019                                                    (orderByComparator.getOrderByFields().length * 3));
3020    
3021                                    query.append(_SQL_SELECT_MBDISCUSSION);
3022    
3023                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3024                                            orderByComparator);
3025    
3026                                    sql = query.toString();
3027                            }
3028                            else {
3029                                    sql = _SQL_SELECT_MBDISCUSSION;
3030    
3031                                    if (pagination) {
3032                                            sql = sql.concat(MBDiscussionModelImpl.ORDER_BY_JPQL);
3033                                    }
3034                            }
3035    
3036                            Session session = null;
3037    
3038                            try {
3039                                    session = openSession();
3040    
3041                                    Query q = session.createQuery(sql);
3042    
3043                                    if (!pagination) {
3044                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
3045                                                            start, end, false);
3046    
3047                                            Collections.sort(list);
3048    
3049                                            list = new UnmodifiableList<MBDiscussion>(list);
3050                                    }
3051                                    else {
3052                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
3053                                                            start, end);
3054                                    }
3055    
3056                                    cacheResult(list);
3057    
3058                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3059                            }
3060                            catch (Exception e) {
3061                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3062    
3063                                    throw processException(e);
3064                            }
3065                            finally {
3066                                    closeSession(session);
3067                            }
3068                    }
3069    
3070                    return list;
3071            }
3072    
3073            /**
3074             * Removes all the message boards discussions from the database.
3075             *
3076             * @throws SystemException if a system exception occurred
3077             */
3078            @Override
3079            public void removeAll() throws SystemException {
3080                    for (MBDiscussion mbDiscussion : findAll()) {
3081                            remove(mbDiscussion);
3082                    }
3083            }
3084    
3085            /**
3086             * Returns the number of message boards discussions.
3087             *
3088             * @return the number of message boards discussions
3089             * @throws SystemException if a system exception occurred
3090             */
3091            @Override
3092            public int countAll() throws SystemException {
3093                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
3094                                    FINDER_ARGS_EMPTY, this);
3095    
3096                    if (count == null) {
3097                            Session session = null;
3098    
3099                            try {
3100                                    session = openSession();
3101    
3102                                    Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
3103    
3104                                    count = (Long)q.uniqueResult();
3105    
3106                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
3107                                            FINDER_ARGS_EMPTY, count);
3108                            }
3109                            catch (Exception e) {
3110                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
3111                                            FINDER_ARGS_EMPTY);
3112    
3113                                    throw processException(e);
3114                            }
3115                            finally {
3116                                    closeSession(session);
3117                            }
3118                    }
3119    
3120                    return count.intValue();
3121            }
3122    
3123            @Override
3124            protected Set<String> getBadColumnNames() {
3125                    return _badColumnNames;
3126            }
3127    
3128            /**
3129             * Initializes the message boards discussion persistence.
3130             */
3131            public void afterPropertiesSet() {
3132                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
3133                                            com.liferay.portal.util.PropsUtil.get(
3134                                                    "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
3135    
3136                    if (listenerClassNames.length > 0) {
3137                            try {
3138                                    List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
3139    
3140                                    for (String listenerClassName : listenerClassNames) {
3141                                            listenersList.add((ModelListener<MBDiscussion>)InstanceFactory.newInstance(
3142                                                            getClassLoader(), listenerClassName));
3143                                    }
3144    
3145                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
3146                            }
3147                            catch (Exception e) {
3148                                    _log.error(e);
3149                            }
3150                    }
3151            }
3152    
3153            public void destroy() {
3154                    EntityCacheUtil.removeCache(MBDiscussionImpl.class.getName());
3155                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
3156                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3157                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3158            }
3159    
3160            private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
3161            private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
3162            private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
3163            private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
3164            private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
3165            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
3166            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
3167            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
3168            private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
3169            private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
3170                                    "uuid"
3171                            });
3172            private static MBDiscussion _nullMBDiscussion = new MBDiscussionImpl() {
3173                            @Override
3174                            public Object clone() {
3175                                    return this;
3176                            }
3177    
3178                            @Override
3179                            public CacheModel<MBDiscussion> toCacheModel() {
3180                                    return _nullMBDiscussionCacheModel;
3181                            }
3182                    };
3183    
3184            private static CacheModel<MBDiscussion> _nullMBDiscussionCacheModel = new CacheModel<MBDiscussion>() {
3185                            @Override
3186                            public MBDiscussion toEntityModel() {
3187                                    return _nullMBDiscussion;
3188                            }
3189                    };
3190    }