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