001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.annotation.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.model.ModelListener;
037    import com.liferay.portal.service.persistence.BatchSessionUtil;
038    import com.liferay.portal.service.persistence.ResourcePersistence;
039    import com.liferay.portal.service.persistence.UserPersistence;
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    
053    /**
054     * @author    Brian Wing Shun Chan
055     * @see       MBDiscussionPersistence
056     * @see       MBDiscussionUtil
057     * @generated
058     */
059    public class MBDiscussionPersistenceImpl extends BasePersistenceImpl<MBDiscussion>
060            implements MBDiscussionPersistence {
061            public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
062            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
063                    ".List";
064            public static final FinderPath FINDER_PATH_FIND_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
065                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
066                            "findByClassNameId",
067                            new String[] {
068                                    Long.class.getName(),
069                                    
070                            "java.lang.Integer", "java.lang.Integer",
071                                    "com.liferay.portal.kernel.util.OrderByComparator"
072                            });
073            public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
074                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
075                            "countByClassNameId", new String[] { Long.class.getName() });
076            public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
077                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
078                            FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
079                            new String[] { Long.class.getName() });
080            public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
081                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
082                            "countByThreadId", new String[] { Long.class.getName() });
083            public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
084                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
085                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
086                            new String[] { Long.class.getName(), Long.class.getName() });
087            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
088                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
089                            "countByC_C",
090                            new String[] { Long.class.getName(), Long.class.getName() });
091            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
092                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
093                            "findAll", new String[0]);
094            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
095                            MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
096                            "countAll", new String[0]);
097    
098            public void cacheResult(MBDiscussion mbDiscussion) {
099                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
100                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
101    
102                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
103                            new Object[] { new Long(mbDiscussion.getThreadId()) }, mbDiscussion);
104    
105                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
106                            new Object[] {
107                                    new Long(mbDiscussion.getClassNameId()),
108                                    new Long(mbDiscussion.getClassPK())
109                            }, mbDiscussion);
110            }
111    
112            public void cacheResult(List<MBDiscussion> mbDiscussions) {
113                    for (MBDiscussion mbDiscussion : mbDiscussions) {
114                            if (EntityCacheUtil.getResult(
115                                                    MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
116                                                    MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(),
117                                                    this) == null) {
118                                    cacheResult(mbDiscussion);
119                            }
120                    }
121            }
122    
123            public void clearCache() {
124                    CacheRegistryUtil.clear(MBDiscussionImpl.class.getName());
125                    EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
126                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
127                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
128            }
129    
130            public void clearCache(MBDiscussion mbDiscussion) {
131                    EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
132                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
133    
134                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
135                            new Object[] { new Long(mbDiscussion.getThreadId()) });
136    
137                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
138                            new Object[] {
139                                    new Long(mbDiscussion.getClassNameId()),
140                                    new Long(mbDiscussion.getClassPK())
141                            });
142            }
143    
144            public MBDiscussion create(long discussionId) {
145                    MBDiscussion mbDiscussion = new MBDiscussionImpl();
146    
147                    mbDiscussion.setNew(true);
148                    mbDiscussion.setPrimaryKey(discussionId);
149    
150                    return mbDiscussion;
151            }
152    
153            public MBDiscussion remove(Serializable primaryKey)
154                    throws NoSuchModelException, SystemException {
155                    return remove(((Long)primaryKey).longValue());
156            }
157    
158            public MBDiscussion remove(long discussionId)
159                    throws NoSuchDiscussionException, SystemException {
160                    Session session = null;
161    
162                    try {
163                            session = openSession();
164    
165                            MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
166                                            new Long(discussionId));
167    
168                            if (mbDiscussion == null) {
169                                    if (_log.isWarnEnabled()) {
170                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
171                                    }
172    
173                                    throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
174                                            discussionId);
175                            }
176    
177                            return remove(mbDiscussion);
178                    }
179                    catch (NoSuchDiscussionException nsee) {
180                            throw nsee;
181                    }
182                    catch (Exception e) {
183                            throw processException(e);
184                    }
185                    finally {
186                            closeSession(session);
187                    }
188            }
189    
190            protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
191                    throws SystemException {
192                    mbDiscussion = toUnwrappedModel(mbDiscussion);
193    
194                    Session session = null;
195    
196                    try {
197                            session = openSession();
198    
199                            if (mbDiscussion.isCachedModel() || BatchSessionUtil.isEnabled()) {
200                                    Object staleObject = session.get(MBDiscussionImpl.class,
201                                                    mbDiscussion.getPrimaryKeyObj());
202    
203                                    if (staleObject != null) {
204                                            session.evict(staleObject);
205                                    }
206                            }
207    
208                            session.delete(mbDiscussion);
209    
210                            session.flush();
211                    }
212                    catch (Exception e) {
213                            throw processException(e);
214                    }
215                    finally {
216                            closeSession(session);
217                    }
218    
219                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
220    
221                    MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
222    
223                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
224                            new Object[] { new Long(mbDiscussionModelImpl.getOriginalThreadId()) });
225    
226                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
227                            new Object[] {
228                                    new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
229                                    new Long(mbDiscussionModelImpl.getOriginalClassPK())
230                            });
231    
232                    EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
233                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
234    
235                    return mbDiscussion;
236            }
237    
238            public MBDiscussion updateImpl(
239                    com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
240                    boolean merge) throws SystemException {
241                    mbDiscussion = toUnwrappedModel(mbDiscussion);
242    
243                    boolean isNew = mbDiscussion.isNew();
244    
245                    MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
246    
247                    Session session = null;
248    
249                    try {
250                            session = openSession();
251    
252                            BatchSessionUtil.update(session, mbDiscussion, merge);
253    
254                            mbDiscussion.setNew(false);
255                    }
256                    catch (Exception e) {
257                            throw processException(e);
258                    }
259                    finally {
260                            closeSession(session);
261                    }
262    
263                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
264    
265                    EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
266                            MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
267    
268                    if (!isNew &&
269                                    (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
270                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
271                                    new Object[] {
272                                            new Long(mbDiscussionModelImpl.getOriginalThreadId())
273                                    });
274                    }
275    
276                    if (isNew ||
277                                    (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
278                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
279                                    new Object[] { new Long(mbDiscussion.getThreadId()) },
280                                    mbDiscussion);
281                    }
282    
283                    if (!isNew &&
284                                    ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
285                                    (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
286                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
287                                    new Object[] {
288                                            new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
289                                            new Long(mbDiscussionModelImpl.getOriginalClassPK())
290                                    });
291                    }
292    
293                    if (isNew ||
294                                    ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
295                                    (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
296                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
297                                    new Object[] {
298                                            new Long(mbDiscussion.getClassNameId()),
299                                            new Long(mbDiscussion.getClassPK())
300                                    }, mbDiscussion);
301                    }
302    
303                    return mbDiscussion;
304            }
305    
306            protected MBDiscussion toUnwrappedModel(MBDiscussion mbDiscussion) {
307                    if (mbDiscussion instanceof MBDiscussionImpl) {
308                            return mbDiscussion;
309                    }
310    
311                    MBDiscussionImpl mbDiscussionImpl = new MBDiscussionImpl();
312    
313                    mbDiscussionImpl.setNew(mbDiscussion.isNew());
314                    mbDiscussionImpl.setPrimaryKey(mbDiscussion.getPrimaryKey());
315    
316                    mbDiscussionImpl.setDiscussionId(mbDiscussion.getDiscussionId());
317                    mbDiscussionImpl.setClassNameId(mbDiscussion.getClassNameId());
318                    mbDiscussionImpl.setClassPK(mbDiscussion.getClassPK());
319                    mbDiscussionImpl.setThreadId(mbDiscussion.getThreadId());
320    
321                    return mbDiscussionImpl;
322            }
323    
324            public MBDiscussion findByPrimaryKey(Serializable primaryKey)
325                    throws NoSuchModelException, SystemException {
326                    return findByPrimaryKey(((Long)primaryKey).longValue());
327            }
328    
329            public MBDiscussion findByPrimaryKey(long discussionId)
330                    throws NoSuchDiscussionException, SystemException {
331                    MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
332    
333                    if (mbDiscussion == null) {
334                            if (_log.isWarnEnabled()) {
335                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
336                            }
337    
338                            throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
339                                    discussionId);
340                    }
341    
342                    return mbDiscussion;
343            }
344    
345            public MBDiscussion fetchByPrimaryKey(Serializable primaryKey)
346                    throws SystemException {
347                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
348            }
349    
350            public MBDiscussion fetchByPrimaryKey(long discussionId)
351                    throws SystemException {
352                    MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
353                                    MBDiscussionImpl.class, discussionId, this);
354    
355                    if (mbDiscussion == null) {
356                            Session session = null;
357    
358                            try {
359                                    session = openSession();
360    
361                                    mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
362                                                    new Long(discussionId));
363                            }
364                            catch (Exception e) {
365                                    throw processException(e);
366                            }
367                            finally {
368                                    if (mbDiscussion != null) {
369                                            cacheResult(mbDiscussion);
370                                    }
371    
372                                    closeSession(session);
373                            }
374                    }
375    
376                    return mbDiscussion;
377            }
378    
379            public List<MBDiscussion> findByClassNameId(long classNameId)
380                    throws SystemException {
381                    return findByClassNameId(classNameId, QueryUtil.ALL_POS,
382                            QueryUtil.ALL_POS, null);
383            }
384    
385            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
386                    int end) throws SystemException {
387                    return findByClassNameId(classNameId, start, end, null);
388            }
389    
390            public List<MBDiscussion> findByClassNameId(long classNameId, int start,
391                    int end, OrderByComparator orderByComparator) throws SystemException {
392                    Object[] finderArgs = new Object[] {
393                                    classNameId,
394                                    
395                                    String.valueOf(start), String.valueOf(end),
396                                    String.valueOf(orderByComparator)
397                            };
398    
399                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
400                                    finderArgs, this);
401    
402                    if (list == null) {
403                            Session session = null;
404    
405                            try {
406                                    session = openSession();
407    
408                                    StringBundler query = null;
409    
410                                    if (orderByComparator != null) {
411                                            query = new StringBundler(3 +
412                                                            (orderByComparator.getOrderByFields().length * 3));
413                                    }
414                                    else {
415                                            query = new StringBundler(2);
416                                    }
417    
418                                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
419    
420                                    query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
421    
422                                    if (orderByComparator != null) {
423                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
424                                                    orderByComparator);
425                                    }
426    
427                                    String sql = query.toString();
428    
429                                    Query q = session.createQuery(sql);
430    
431                                    QueryPos qPos = QueryPos.getInstance(q);
432    
433                                    qPos.add(classNameId);
434    
435                                    list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
436                                                    start, end);
437                            }
438                            catch (Exception e) {
439                                    throw processException(e);
440                            }
441                            finally {
442                                    if (list == null) {
443                                            list = new ArrayList<MBDiscussion>();
444                                    }
445    
446                                    cacheResult(list);
447    
448                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
449                                            finderArgs, list);
450    
451                                    closeSession(session);
452                            }
453                    }
454    
455                    return list;
456            }
457    
458            public MBDiscussion findByClassNameId_First(long classNameId,
459                    OrderByComparator orderByComparator)
460                    throws NoSuchDiscussionException, SystemException {
461                    List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1,
462                                    orderByComparator);
463    
464                    if (list.isEmpty()) {
465                            StringBundler msg = new StringBundler(4);
466    
467                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
468    
469                            msg.append("classNameId=");
470                            msg.append(classNameId);
471    
472                            msg.append(StringPool.CLOSE_CURLY_BRACE);
473    
474                            throw new NoSuchDiscussionException(msg.toString());
475                    }
476                    else {
477                            return list.get(0);
478                    }
479            }
480    
481            public MBDiscussion findByClassNameId_Last(long classNameId,
482                    OrderByComparator orderByComparator)
483                    throws NoSuchDiscussionException, SystemException {
484                    int count = countByClassNameId(classNameId);
485    
486                    List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
487                                    count, orderByComparator);
488    
489                    if (list.isEmpty()) {
490                            StringBundler msg = new StringBundler(4);
491    
492                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
493    
494                            msg.append("classNameId=");
495                            msg.append(classNameId);
496    
497                            msg.append(StringPool.CLOSE_CURLY_BRACE);
498    
499                            throw new NoSuchDiscussionException(msg.toString());
500                    }
501                    else {
502                            return list.get(0);
503                    }
504            }
505    
506            public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
507                    long classNameId, OrderByComparator orderByComparator)
508                    throws NoSuchDiscussionException, SystemException {
509                    MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
510    
511                    Session session = null;
512    
513                    try {
514                            session = openSession();
515    
516                            MBDiscussion[] array = new MBDiscussionImpl[3];
517    
518                            array[0] = getByClassNameId_PrevAndNext(session, mbDiscussion,
519                                            classNameId, orderByComparator, true);
520    
521                            array[1] = mbDiscussion;
522    
523                            array[2] = getByClassNameId_PrevAndNext(session, mbDiscussion,
524                                            classNameId, orderByComparator, false);
525    
526                            return array;
527                    }
528                    catch (Exception e) {
529                            throw processException(e);
530                    }
531                    finally {
532                            closeSession(session);
533                    }
534            }
535    
536            protected MBDiscussion getByClassNameId_PrevAndNext(Session session,
537                    MBDiscussion mbDiscussion, long classNameId,
538                    OrderByComparator orderByComparator, boolean previous) {
539                    StringBundler query = null;
540    
541                    if (orderByComparator != null) {
542                            query = new StringBundler(6 +
543                                            (orderByComparator.getOrderByFields().length * 6));
544                    }
545                    else {
546                            query = new StringBundler(3);
547                    }
548    
549                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
550    
551                    query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
552    
553                    if (orderByComparator != null) {
554                            String[] orderByFields = orderByComparator.getOrderByFields();
555    
556                            if (orderByFields.length > 0) {
557                                    query.append(WHERE_AND);
558                            }
559    
560                            for (int i = 0; i < orderByFields.length; i++) {
561                                    query.append(_ORDER_BY_ENTITY_ALIAS);
562                                    query.append(orderByFields[i]);
563    
564                                    if ((i + 1) < orderByFields.length) {
565                                            if (orderByComparator.isAscending() ^ previous) {
566                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
567                                            }
568                                            else {
569                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
570                                            }
571                                    }
572                                    else {
573                                            if (orderByComparator.isAscending() ^ previous) {
574                                                    query.append(WHERE_GREATER_THAN);
575                                            }
576                                            else {
577                                                    query.append(WHERE_LESSER_THAN);
578                                            }
579                                    }
580                            }
581    
582                            query.append(ORDER_BY_CLAUSE);
583    
584                            for (int i = 0; i < orderByFields.length; i++) {
585                                    query.append(_ORDER_BY_ENTITY_ALIAS);
586                                    query.append(orderByFields[i]);
587    
588                                    if ((i + 1) < orderByFields.length) {
589                                            if (orderByComparator.isAscending() ^ previous) {
590                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
591                                            }
592                                            else {
593                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
594                                            }
595                                    }
596                                    else {
597                                            if (orderByComparator.isAscending() ^ previous) {
598                                                    query.append(ORDER_BY_ASC);
599                                            }
600                                            else {
601                                                    query.append(ORDER_BY_DESC);
602                                            }
603                                    }
604                            }
605                    }
606    
607                    String sql = query.toString();
608    
609                    Query q = session.createQuery(sql);
610    
611                    q.setFirstResult(0);
612                    q.setMaxResults(2);
613    
614                    QueryPos qPos = QueryPos.getInstance(q);
615    
616                    qPos.add(classNameId);
617    
618                    if (orderByComparator != null) {
619                            Object[] values = orderByComparator.getOrderByValues(mbDiscussion);
620    
621                            for (Object value : values) {
622                                    qPos.add(value);
623                            }
624                    }
625    
626                    List<MBDiscussion> list = q.list();
627    
628                    if (list.size() == 2) {
629                            return list.get(1);
630                    }
631                    else {
632                            return null;
633                    }
634            }
635    
636            public MBDiscussion findByThreadId(long threadId)
637                    throws NoSuchDiscussionException, SystemException {
638                    MBDiscussion mbDiscussion = fetchByThreadId(threadId);
639    
640                    if (mbDiscussion == null) {
641                            StringBundler msg = new StringBundler(4);
642    
643                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
644    
645                            msg.append("threadId=");
646                            msg.append(threadId);
647    
648                            msg.append(StringPool.CLOSE_CURLY_BRACE);
649    
650                            if (_log.isWarnEnabled()) {
651                                    _log.warn(msg.toString());
652                            }
653    
654                            throw new NoSuchDiscussionException(msg.toString());
655                    }
656    
657                    return mbDiscussion;
658            }
659    
660            public MBDiscussion fetchByThreadId(long threadId)
661                    throws SystemException {
662                    return fetchByThreadId(threadId, true);
663            }
664    
665            public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
666                    throws SystemException {
667                    Object[] finderArgs = new Object[] { threadId };
668    
669                    Object result = null;
670    
671                    if (retrieveFromCache) {
672                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
673                                            finderArgs, this);
674                    }
675    
676                    if (result == null) {
677                            Session session = null;
678    
679                            try {
680                                    session = openSession();
681    
682                                    StringBundler query = new StringBundler(2);
683    
684                                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
685    
686                                    query.append(_FINDER_COLUMN_THREADID_THREADID_2);
687    
688                                    String sql = query.toString();
689    
690                                    Query q = session.createQuery(sql);
691    
692                                    QueryPos qPos = QueryPos.getInstance(q);
693    
694                                    qPos.add(threadId);
695    
696                                    List<MBDiscussion> list = q.list();
697    
698                                    result = list;
699    
700                                    MBDiscussion mbDiscussion = null;
701    
702                                    if (list.isEmpty()) {
703                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
704                                                    finderArgs, list);
705                                    }
706                                    else {
707                                            mbDiscussion = list.get(0);
708    
709                                            cacheResult(mbDiscussion);
710    
711                                            if ((mbDiscussion.getThreadId() != threadId)) {
712                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
713                                                            finderArgs, mbDiscussion);
714                                            }
715                                    }
716    
717                                    return mbDiscussion;
718                            }
719                            catch (Exception e) {
720                                    throw processException(e);
721                            }
722                            finally {
723                                    if (result == null) {
724                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
725                                                    finderArgs, new ArrayList<MBDiscussion>());
726                                    }
727    
728                                    closeSession(session);
729                            }
730                    }
731                    else {
732                            if (result instanceof List<?>) {
733                                    return null;
734                            }
735                            else {
736                                    return (MBDiscussion)result;
737                            }
738                    }
739            }
740    
741            public MBDiscussion findByC_C(long classNameId, long classPK)
742                    throws NoSuchDiscussionException, SystemException {
743                    MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
744    
745                    if (mbDiscussion == null) {
746                            StringBundler msg = new StringBundler(6);
747    
748                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
749    
750                            msg.append("classNameId=");
751                            msg.append(classNameId);
752    
753                            msg.append(", classPK=");
754                            msg.append(classPK);
755    
756                            msg.append(StringPool.CLOSE_CURLY_BRACE);
757    
758                            if (_log.isWarnEnabled()) {
759                                    _log.warn(msg.toString());
760                            }
761    
762                            throw new NoSuchDiscussionException(msg.toString());
763                    }
764    
765                    return mbDiscussion;
766            }
767    
768            public MBDiscussion fetchByC_C(long classNameId, long classPK)
769                    throws SystemException {
770                    return fetchByC_C(classNameId, classPK, true);
771            }
772    
773            public MBDiscussion fetchByC_C(long classNameId, long classPK,
774                    boolean retrieveFromCache) throws SystemException {
775                    Object[] finderArgs = new Object[] { classNameId, classPK };
776    
777                    Object result = null;
778    
779                    if (retrieveFromCache) {
780                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
781                                            finderArgs, this);
782                    }
783    
784                    if (result == null) {
785                            Session session = null;
786    
787                            try {
788                                    session = openSession();
789    
790                                    StringBundler query = new StringBundler(3);
791    
792                                    query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
793    
794                                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
795    
796                                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
797    
798                                    String sql = query.toString();
799    
800                                    Query q = session.createQuery(sql);
801    
802                                    QueryPos qPos = QueryPos.getInstance(q);
803    
804                                    qPos.add(classNameId);
805    
806                                    qPos.add(classPK);
807    
808                                    List<MBDiscussion> list = q.list();
809    
810                                    result = list;
811    
812                                    MBDiscussion mbDiscussion = null;
813    
814                                    if (list.isEmpty()) {
815                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
816                                                    finderArgs, list);
817                                    }
818                                    else {
819                                            mbDiscussion = list.get(0);
820    
821                                            cacheResult(mbDiscussion);
822    
823                                            if ((mbDiscussion.getClassNameId() != classNameId) ||
824                                                            (mbDiscussion.getClassPK() != classPK)) {
825                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
826                                                            finderArgs, mbDiscussion);
827                                            }
828                                    }
829    
830                                    return mbDiscussion;
831                            }
832                            catch (Exception e) {
833                                    throw processException(e);
834                            }
835                            finally {
836                                    if (result == null) {
837                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
838                                                    finderArgs, new ArrayList<MBDiscussion>());
839                                    }
840    
841                                    closeSession(session);
842                            }
843                    }
844                    else {
845                            if (result instanceof List<?>) {
846                                    return null;
847                            }
848                            else {
849                                    return (MBDiscussion)result;
850                            }
851                    }
852            }
853    
854            public List<MBDiscussion> findAll() throws SystemException {
855                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
856            }
857    
858            public List<MBDiscussion> findAll(int start, int end)
859                    throws SystemException {
860                    return findAll(start, end, null);
861            }
862    
863            public List<MBDiscussion> findAll(int start, int end,
864                    OrderByComparator orderByComparator) throws SystemException {
865                    Object[] finderArgs = new Object[] {
866                                    String.valueOf(start), String.valueOf(end),
867                                    String.valueOf(orderByComparator)
868                            };
869    
870                    List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
871                                    finderArgs, this);
872    
873                    if (list == null) {
874                            Session session = null;
875    
876                            try {
877                                    session = openSession();
878    
879                                    StringBundler query = null;
880                                    String sql = null;
881    
882                                    if (orderByComparator != null) {
883                                            query = new StringBundler(2 +
884                                                            (orderByComparator.getOrderByFields().length * 3));
885    
886                                            query.append(_SQL_SELECT_MBDISCUSSION);
887    
888                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
889                                                    orderByComparator);
890    
891                                            sql = query.toString();
892                                    }
893                                    else {
894                                            sql = _SQL_SELECT_MBDISCUSSION;
895                                    }
896    
897                                    Query q = session.createQuery(sql);
898    
899                                    if (orderByComparator == null) {
900                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
901                                                            start, end, false);
902    
903                                            Collections.sort(list);
904                                    }
905                                    else {
906                                            list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
907                                                            start, end);
908                                    }
909                            }
910                            catch (Exception e) {
911                                    throw processException(e);
912                            }
913                            finally {
914                                    if (list == null) {
915                                            list = new ArrayList<MBDiscussion>();
916                                    }
917    
918                                    cacheResult(list);
919    
920                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
921    
922                                    closeSession(session);
923                            }
924                    }
925    
926                    return list;
927            }
928    
929            public void removeByClassNameId(long classNameId) throws SystemException {
930                    for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
931                            remove(mbDiscussion);
932                    }
933            }
934    
935            public void removeByThreadId(long threadId)
936                    throws NoSuchDiscussionException, SystemException {
937                    MBDiscussion mbDiscussion = findByThreadId(threadId);
938    
939                    remove(mbDiscussion);
940            }
941    
942            public void removeByC_C(long classNameId, long classPK)
943                    throws NoSuchDiscussionException, SystemException {
944                    MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
945    
946                    remove(mbDiscussion);
947            }
948    
949            public void removeAll() throws SystemException {
950                    for (MBDiscussion mbDiscussion : findAll()) {
951                            remove(mbDiscussion);
952                    }
953            }
954    
955            public int countByClassNameId(long classNameId) throws SystemException {
956                    Object[] finderArgs = new Object[] { classNameId };
957    
958                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
959                                    finderArgs, this);
960    
961                    if (count == null) {
962                            Session session = null;
963    
964                            try {
965                                    session = openSession();
966    
967                                    StringBundler query = new StringBundler(2);
968    
969                                    query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
970    
971                                    query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
972    
973                                    String sql = query.toString();
974    
975                                    Query q = session.createQuery(sql);
976    
977                                    QueryPos qPos = QueryPos.getInstance(q);
978    
979                                    qPos.add(classNameId);
980    
981                                    count = (Long)q.uniqueResult();
982                            }
983                            catch (Exception e) {
984                                    throw processException(e);
985                            }
986                            finally {
987                                    if (count == null) {
988                                            count = Long.valueOf(0);
989                                    }
990    
991                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
992                                            finderArgs, count);
993    
994                                    closeSession(session);
995                            }
996                    }
997    
998                    return count.intValue();
999            }
1000    
1001            public int countByThreadId(long threadId) throws SystemException {
1002                    Object[] finderArgs = new Object[] { threadId };
1003    
1004                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1005                                    finderArgs, this);
1006    
1007                    if (count == null) {
1008                            Session session = null;
1009    
1010                            try {
1011                                    session = openSession();
1012    
1013                                    StringBundler query = new StringBundler(2);
1014    
1015                                    query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1016    
1017                                    query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1018    
1019                                    String sql = query.toString();
1020    
1021                                    Query q = session.createQuery(sql);
1022    
1023                                    QueryPos qPos = QueryPos.getInstance(q);
1024    
1025                                    qPos.add(threadId);
1026    
1027                                    count = (Long)q.uniqueResult();
1028                            }
1029                            catch (Exception e) {
1030                                    throw processException(e);
1031                            }
1032                            finally {
1033                                    if (count == null) {
1034                                            count = Long.valueOf(0);
1035                                    }
1036    
1037                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1038                                            finderArgs, count);
1039    
1040                                    closeSession(session);
1041                            }
1042                    }
1043    
1044                    return count.intValue();
1045            }
1046    
1047            public int countByC_C(long classNameId, long classPK)
1048                    throws SystemException {
1049                    Object[] finderArgs = new Object[] { classNameId, classPK };
1050    
1051                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1052                                    finderArgs, this);
1053    
1054                    if (count == null) {
1055                            Session session = null;
1056    
1057                            try {
1058                                    session = openSession();
1059    
1060                                    StringBundler query = new StringBundler(3);
1061    
1062                                    query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1063    
1064                                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1065    
1066                                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1067    
1068                                    String sql = query.toString();
1069    
1070                                    Query q = session.createQuery(sql);
1071    
1072                                    QueryPos qPos = QueryPos.getInstance(q);
1073    
1074                                    qPos.add(classNameId);
1075    
1076                                    qPos.add(classPK);
1077    
1078                                    count = (Long)q.uniqueResult();
1079                            }
1080                            catch (Exception e) {
1081                                    throw processException(e);
1082                            }
1083                            finally {
1084                                    if (count == null) {
1085                                            count = Long.valueOf(0);
1086                                    }
1087    
1088                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1089                                            count);
1090    
1091                                    closeSession(session);
1092                            }
1093                    }
1094    
1095                    return count.intValue();
1096            }
1097    
1098            public int countAll() throws SystemException {
1099                    Object[] finderArgs = new Object[0];
1100    
1101                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1102                                    finderArgs, this);
1103    
1104                    if (count == null) {
1105                            Session session = null;
1106    
1107                            try {
1108                                    session = openSession();
1109    
1110                                    Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
1111    
1112                                    count = (Long)q.uniqueResult();
1113                            }
1114                            catch (Exception e) {
1115                                    throw processException(e);
1116                            }
1117                            finally {
1118                                    if (count == null) {
1119                                            count = Long.valueOf(0);
1120                                    }
1121    
1122                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1123                                            count);
1124    
1125                                    closeSession(session);
1126                            }
1127                    }
1128    
1129                    return count.intValue();
1130            }
1131    
1132            public void afterPropertiesSet() {
1133                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1134                                            com.liferay.portal.util.PropsUtil.get(
1135                                                    "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1136    
1137                    if (listenerClassNames.length > 0) {
1138                            try {
1139                                    List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1140    
1141                                    for (String listenerClassName : listenerClassNames) {
1142                                            listenersList.add((ModelListener<MBDiscussion>)InstanceFactory.newInstance(
1143                                                            listenerClassName));
1144                                    }
1145    
1146                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1147                            }
1148                            catch (Exception e) {
1149                                    _log.error(e);
1150                            }
1151                    }
1152            }
1153    
1154            @BeanReference(type = MBBanPersistence.class)
1155            protected MBBanPersistence mbBanPersistence;
1156            @BeanReference(type = MBCategoryPersistence.class)
1157            protected MBCategoryPersistence mbCategoryPersistence;
1158            @BeanReference(type = MBDiscussionPersistence.class)
1159            protected MBDiscussionPersistence mbDiscussionPersistence;
1160            @BeanReference(type = MBMailingListPersistence.class)
1161            protected MBMailingListPersistence mbMailingListPersistence;
1162            @BeanReference(type = MBMessagePersistence.class)
1163            protected MBMessagePersistence mbMessagePersistence;
1164            @BeanReference(type = MBMessageFlagPersistence.class)
1165            protected MBMessageFlagPersistence mbMessageFlagPersistence;
1166            @BeanReference(type = MBStatsUserPersistence.class)
1167            protected MBStatsUserPersistence mbStatsUserPersistence;
1168            @BeanReference(type = MBThreadPersistence.class)
1169            protected MBThreadPersistence mbThreadPersistence;
1170            @BeanReference(type = ResourcePersistence.class)
1171            protected ResourcePersistence resourcePersistence;
1172            @BeanReference(type = UserPersistence.class)
1173            protected UserPersistence userPersistence;
1174            private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
1175            private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
1176            private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
1177            private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
1178            private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1179            private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
1180            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
1181            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
1182            private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
1183            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
1184            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
1185            private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1186    }