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