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.ratings.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.ratings.NoSuchStatsException;
043    import com.liferay.portlet.ratings.model.RatingsStats;
044    import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
045    import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
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 ratings stats service.
055     *
056     * <p>
057     * Never modify or reference this class directly. Always use {@link RatingsStatsUtil} to access the ratings stats 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 RatingsStatsPersistence
066     * @see RatingsStatsUtil
067     * @generated
068     */
069    public class RatingsStatsPersistenceImpl extends BasePersistenceImpl<RatingsStats>
070            implements RatingsStatsPersistence {
071            public static final String FINDER_CLASS_NAME_ENTITY = RatingsStatsImpl.class.getName();
072            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
073                    ".List";
074            public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
075                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED,
076                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
077                            new String[] { Long.class.getName(), Long.class.getName() });
078            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
079                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
080                            "countByC_C",
081                            new String[] { Long.class.getName(), Long.class.getName() });
082            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
083                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084                            "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
086                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087                            "countAll", new String[0]);
088    
089            /**
090             * Caches the ratings stats in the entity cache if it is enabled.
091             *
092             * @param ratingsStats the ratings stats to cache
093             */
094            public void cacheResult(RatingsStats ratingsStats) {
095                    EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
096                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
097    
098                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
099                            new Object[] {
100                                    new Long(ratingsStats.getClassNameId()),
101                                    new Long(ratingsStats.getClassPK())
102                            }, ratingsStats);
103            }
104    
105            /**
106             * Caches the ratings statses in the entity cache if it is enabled.
107             *
108             * @param ratingsStatses the ratings statses to cache
109             */
110            public void cacheResult(List<RatingsStats> ratingsStatses) {
111                    for (RatingsStats ratingsStats : ratingsStatses) {
112                            if (EntityCacheUtil.getResult(
113                                                    RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
114                                                    RatingsStatsImpl.class, ratingsStats.getPrimaryKey(),
115                                                    this) == null) {
116                                    cacheResult(ratingsStats);
117                            }
118                    }
119            }
120    
121            /**
122             * Clears the cache for all ratings statses.
123             *
124             * <p>
125             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
126             * </p>
127             */
128            public void clearCache() {
129                    CacheRegistryUtil.clear(RatingsStatsImpl.class.getName());
130                    EntityCacheUtil.clearCache(RatingsStatsImpl.class.getName());
131                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
132                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
133            }
134    
135            /**
136             * Clears the cache for the ratings stats.
137             *
138             * <p>
139             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
140             * </p>
141             */
142            public void clearCache(RatingsStats ratingsStats) {
143                    EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
144                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
145    
146                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
147                            new Object[] {
148                                    new Long(ratingsStats.getClassNameId()),
149                                    new Long(ratingsStats.getClassPK())
150                            });
151            }
152    
153            /**
154             * Creates a new ratings stats with the primary key. Does not add the ratings stats to the database.
155             *
156             * @param statsId the primary key for the new ratings stats
157             * @return the new ratings stats
158             */
159            public RatingsStats create(long statsId) {
160                    RatingsStats ratingsStats = new RatingsStatsImpl();
161    
162                    ratingsStats.setNew(true);
163                    ratingsStats.setPrimaryKey(statsId);
164    
165                    return ratingsStats;
166            }
167    
168            /**
169             * Removes the ratings stats with the primary key from the database. Also notifies the appropriate model listeners.
170             *
171             * @param primaryKey the primary key of the ratings stats to remove
172             * @return the ratings stats that was removed
173             * @throws com.liferay.portal.NoSuchModelException if a ratings stats with the primary key could not be found
174             * @throws SystemException if a system exception occurred
175             */
176            public RatingsStats remove(Serializable primaryKey)
177                    throws NoSuchModelException, SystemException {
178                    return remove(((Long)primaryKey).longValue());
179            }
180    
181            /**
182             * Removes the ratings stats with the primary key from the database. Also notifies the appropriate model listeners.
183             *
184             * @param statsId the primary key of the ratings stats to remove
185             * @return the ratings stats that was removed
186             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
187             * @throws SystemException if a system exception occurred
188             */
189            public RatingsStats remove(long statsId)
190                    throws NoSuchStatsException, SystemException {
191                    Session session = null;
192    
193                    try {
194                            session = openSession();
195    
196                            RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
197                                            new Long(statsId));
198    
199                            if (ratingsStats == null) {
200                                    if (_log.isWarnEnabled()) {
201                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
202                                    }
203    
204                                    throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
205                                            statsId);
206                            }
207    
208                            return remove(ratingsStats);
209                    }
210                    catch (NoSuchStatsException nsee) {
211                            throw nsee;
212                    }
213                    catch (Exception e) {
214                            throw processException(e);
215                    }
216                    finally {
217                            closeSession(session);
218                    }
219            }
220    
221            protected RatingsStats removeImpl(RatingsStats ratingsStats)
222                    throws SystemException {
223                    ratingsStats = toUnwrappedModel(ratingsStats);
224    
225                    Session session = null;
226    
227                    try {
228                            session = openSession();
229    
230                            BatchSessionUtil.delete(session, ratingsStats);
231                    }
232                    catch (Exception e) {
233                            throw processException(e);
234                    }
235                    finally {
236                            closeSession(session);
237                    }
238    
239                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
240    
241                    RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
242    
243                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
244                            new Object[] {
245                                    new Long(ratingsStatsModelImpl.getClassNameId()),
246                                    new Long(ratingsStatsModelImpl.getClassPK())
247                            });
248    
249                    EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
250                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
251    
252                    return ratingsStats;
253            }
254    
255            public RatingsStats updateImpl(
256                    com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
257                    boolean merge) throws SystemException {
258                    ratingsStats = toUnwrappedModel(ratingsStats);
259    
260                    boolean isNew = ratingsStats.isNew();
261    
262                    RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
263    
264                    Session session = null;
265    
266                    try {
267                            session = openSession();
268    
269                            BatchSessionUtil.update(session, ratingsStats, merge);
270    
271                            ratingsStats.setNew(false);
272                    }
273                    catch (Exception e) {
274                            throw processException(e);
275                    }
276                    finally {
277                            closeSession(session);
278                    }
279    
280                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
281    
282                    EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
283                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
284    
285                    if (!isNew &&
286                                    ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
287                                    (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
288                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
289                                    new Object[] {
290                                            new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
291                                            new Long(ratingsStatsModelImpl.getOriginalClassPK())
292                                    });
293                    }
294    
295                    if (isNew ||
296                                    ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
297                                    (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
298                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
299                                    new Object[] {
300                                            new Long(ratingsStats.getClassNameId()),
301                                            new Long(ratingsStats.getClassPK())
302                                    }, ratingsStats);
303                    }
304    
305                    return ratingsStats;
306            }
307    
308            protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
309                    if (ratingsStats instanceof RatingsStatsImpl) {
310                            return ratingsStats;
311                    }
312    
313                    RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
314    
315                    ratingsStatsImpl.setNew(ratingsStats.isNew());
316                    ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
317    
318                    ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
319                    ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
320                    ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
321                    ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
322                    ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
323                    ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
324    
325                    return ratingsStatsImpl;
326            }
327    
328            /**
329             * Finds the ratings stats with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
330             *
331             * @param primaryKey the primary key of the ratings stats to find
332             * @return the ratings stats
333             * @throws com.liferay.portal.NoSuchModelException if a ratings stats with the primary key could not be found
334             * @throws SystemException if a system exception occurred
335             */
336            public RatingsStats findByPrimaryKey(Serializable primaryKey)
337                    throws NoSuchModelException, SystemException {
338                    return findByPrimaryKey(((Long)primaryKey).longValue());
339            }
340    
341            /**
342             * Finds the ratings stats with the primary key or throws a {@link com.liferay.portlet.ratings.NoSuchStatsException} if it could not be found.
343             *
344             * @param statsId the primary key of the ratings stats to find
345             * @return the ratings stats
346             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
347             * @throws SystemException if a system exception occurred
348             */
349            public RatingsStats findByPrimaryKey(long statsId)
350                    throws NoSuchStatsException, SystemException {
351                    RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
352    
353                    if (ratingsStats == null) {
354                            if (_log.isWarnEnabled()) {
355                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + statsId);
356                            }
357    
358                            throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
359                                    statsId);
360                    }
361    
362                    return ratingsStats;
363            }
364    
365            /**
366             * Finds the ratings stats with the primary key or returns <code>null</code> if it could not be found.
367             *
368             * @param primaryKey the primary key of the ratings stats to find
369             * @return the ratings stats, or <code>null</code> if a ratings stats with the primary key could not be found
370             * @throws SystemException if a system exception occurred
371             */
372            public RatingsStats fetchByPrimaryKey(Serializable primaryKey)
373                    throws SystemException {
374                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
375            }
376    
377            /**
378             * Finds the ratings stats with the primary key or returns <code>null</code> if it could not be found.
379             *
380             * @param statsId the primary key of the ratings stats to find
381             * @return the ratings stats, or <code>null</code> if a ratings stats with the primary key could not be found
382             * @throws SystemException if a system exception occurred
383             */
384            public RatingsStats fetchByPrimaryKey(long statsId)
385                    throws SystemException {
386                    RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
387                                    RatingsStatsImpl.class, statsId, this);
388    
389                    if (ratingsStats == null) {
390                            Session session = null;
391    
392                            try {
393                                    session = openSession();
394    
395                                    ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
396                                                    new Long(statsId));
397                            }
398                            catch (Exception e) {
399                                    throw processException(e);
400                            }
401                            finally {
402                                    if (ratingsStats != null) {
403                                            cacheResult(ratingsStats);
404                                    }
405    
406                                    closeSession(session);
407                            }
408                    }
409    
410                    return ratingsStats;
411            }
412    
413            /**
414             * Finds the ratings stats where classNameId = &#63; and classPK = &#63; or throws a {@link com.liferay.portlet.ratings.NoSuchStatsException} if it could not be found.
415             *
416             * @param classNameId the class name id to search with
417             * @param classPK the class p k to search with
418             * @return the matching ratings stats
419             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a matching ratings stats could not be found
420             * @throws SystemException if a system exception occurred
421             */
422            public RatingsStats findByC_C(long classNameId, long classPK)
423                    throws NoSuchStatsException, SystemException {
424                    RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
425    
426                    if (ratingsStats == null) {
427                            StringBundler msg = new StringBundler(6);
428    
429                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
430    
431                            msg.append("classNameId=");
432                            msg.append(classNameId);
433    
434                            msg.append(", classPK=");
435                            msg.append(classPK);
436    
437                            msg.append(StringPool.CLOSE_CURLY_BRACE);
438    
439                            if (_log.isWarnEnabled()) {
440                                    _log.warn(msg.toString());
441                            }
442    
443                            throw new NoSuchStatsException(msg.toString());
444                    }
445    
446                    return ratingsStats;
447            }
448    
449            /**
450             * Finds the ratings stats where classNameId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
451             *
452             * @param classNameId the class name id to search with
453             * @param classPK the class p k to search with
454             * @return the matching ratings stats, or <code>null</code> if a matching ratings stats could not be found
455             * @throws SystemException if a system exception occurred
456             */
457            public RatingsStats fetchByC_C(long classNameId, long classPK)
458                    throws SystemException {
459                    return fetchByC_C(classNameId, classPK, true);
460            }
461    
462            /**
463             * Finds the ratings stats where classNameId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
464             *
465             * @param classNameId the class name id to search with
466             * @param classPK the class p k to search with
467             * @return the matching ratings stats, or <code>null</code> if a matching ratings stats could not be found
468             * @throws SystemException if a system exception occurred
469             */
470            public RatingsStats fetchByC_C(long classNameId, long classPK,
471                    boolean retrieveFromCache) throws SystemException {
472                    Object[] finderArgs = new Object[] { classNameId, classPK };
473    
474                    Object result = null;
475    
476                    if (retrieveFromCache) {
477                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
478                                            finderArgs, this);
479                    }
480    
481                    if (result == null) {
482                            StringBundler query = new StringBundler(3);
483    
484                            query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
485    
486                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
487    
488                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
489    
490                            String sql = query.toString();
491    
492                            Session session = null;
493    
494                            try {
495                                    session = openSession();
496    
497                                    Query q = session.createQuery(sql);
498    
499                                    QueryPos qPos = QueryPos.getInstance(q);
500    
501                                    qPos.add(classNameId);
502    
503                                    qPos.add(classPK);
504    
505                                    List<RatingsStats> list = q.list();
506    
507                                    result = list;
508    
509                                    RatingsStats ratingsStats = null;
510    
511                                    if (list.isEmpty()) {
512                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
513                                                    finderArgs, list);
514                                    }
515                                    else {
516                                            ratingsStats = list.get(0);
517    
518                                            cacheResult(ratingsStats);
519    
520                                            if ((ratingsStats.getClassNameId() != classNameId) ||
521                                                            (ratingsStats.getClassPK() != classPK)) {
522                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
523                                                            finderArgs, ratingsStats);
524                                            }
525                                    }
526    
527                                    return ratingsStats;
528                            }
529                            catch (Exception e) {
530                                    throw processException(e);
531                            }
532                            finally {
533                                    if (result == null) {
534                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
535                                                    finderArgs);
536                                    }
537    
538                                    closeSession(session);
539                            }
540                    }
541                    else {
542                            if (result instanceof List<?>) {
543                                    return null;
544                            }
545                            else {
546                                    return (RatingsStats)result;
547                            }
548                    }
549            }
550    
551            /**
552             * Finds all the ratings statses.
553             *
554             * @return the ratings statses
555             * @throws SystemException if a system exception occurred
556             */
557            public List<RatingsStats> findAll() throws SystemException {
558                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
559            }
560    
561            /**
562             * Finds a range of all the ratings statses.
563             *
564             * <p>
565             * 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.
566             * </p>
567             *
568             * @param start the lower bound of the range of ratings statses to return
569             * @param end the upper bound of the range of ratings statses to return (not inclusive)
570             * @return the range of ratings statses
571             * @throws SystemException if a system exception occurred
572             */
573            public List<RatingsStats> findAll(int start, int end)
574                    throws SystemException {
575                    return findAll(start, end, null);
576            }
577    
578            /**
579             * Finds an ordered range of all the ratings statses.
580             *
581             * <p>
582             * 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.
583             * </p>
584             *
585             * @param start the lower bound of the range of ratings statses to return
586             * @param end the upper bound of the range of ratings statses to return (not inclusive)
587             * @param orderByComparator the comparator to order the results by
588             * @return the ordered range of ratings statses
589             * @throws SystemException if a system exception occurred
590             */
591            public List<RatingsStats> findAll(int start, int end,
592                    OrderByComparator orderByComparator) throws SystemException {
593                    Object[] finderArgs = new Object[] {
594                                    String.valueOf(start), String.valueOf(end),
595                                    String.valueOf(orderByComparator)
596                            };
597    
598                    List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
599                                    finderArgs, this);
600    
601                    if (list == null) {
602                            StringBundler query = null;
603                            String sql = null;
604    
605                            if (orderByComparator != null) {
606                                    query = new StringBundler(2 +
607                                                    (orderByComparator.getOrderByFields().length * 3));
608    
609                                    query.append(_SQL_SELECT_RATINGSSTATS);
610    
611                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
612                                            orderByComparator);
613    
614                                    sql = query.toString();
615                            }
616                            else {
617                                    sql = _SQL_SELECT_RATINGSSTATS;
618                            }
619    
620                            Session session = null;
621    
622                            try {
623                                    session = openSession();
624    
625                                    Query q = session.createQuery(sql);
626    
627                                    if (orderByComparator == null) {
628                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
629                                                            start, end, false);
630    
631                                            Collections.sort(list);
632                                    }
633                                    else {
634                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
635                                                            start, end);
636                                    }
637                            }
638                            catch (Exception e) {
639                                    throw processException(e);
640                            }
641                            finally {
642                                    if (list == null) {
643                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
644                                                    finderArgs);
645                                    }
646                                    else {
647                                            cacheResult(list);
648    
649                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
650                                                    list);
651                                    }
652    
653                                    closeSession(session);
654                            }
655                    }
656    
657                    return list;
658            }
659    
660            /**
661             * Removes the ratings stats where classNameId = &#63; and classPK = &#63; from the database.
662             *
663             * @param classNameId the class name id to search with
664             * @param classPK the class p k to search with
665             * @throws SystemException if a system exception occurred
666             */
667            public void removeByC_C(long classNameId, long classPK)
668                    throws NoSuchStatsException, SystemException {
669                    RatingsStats ratingsStats = findByC_C(classNameId, classPK);
670    
671                    remove(ratingsStats);
672            }
673    
674            /**
675             * Removes all the ratings statses from the database.
676             *
677             * @throws SystemException if a system exception occurred
678             */
679            public void removeAll() throws SystemException {
680                    for (RatingsStats ratingsStats : findAll()) {
681                            remove(ratingsStats);
682                    }
683            }
684    
685            /**
686             * Counts all the ratings statses where classNameId = &#63; and classPK = &#63;.
687             *
688             * @param classNameId the class name id to search with
689             * @param classPK the class p k to search with
690             * @return the number of matching ratings statses
691             * @throws SystemException if a system exception occurred
692             */
693            public int countByC_C(long classNameId, long classPK)
694                    throws SystemException {
695                    Object[] finderArgs = new Object[] { classNameId, classPK };
696    
697                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
698                                    finderArgs, this);
699    
700                    if (count == null) {
701                            StringBundler query = new StringBundler(3);
702    
703                            query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
704    
705                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
706    
707                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
708    
709                            String sql = query.toString();
710    
711                            Session session = null;
712    
713                            try {
714                                    session = openSession();
715    
716                                    Query q = session.createQuery(sql);
717    
718                                    QueryPos qPos = QueryPos.getInstance(q);
719    
720                                    qPos.add(classNameId);
721    
722                                    qPos.add(classPK);
723    
724                                    count = (Long)q.uniqueResult();
725                            }
726                            catch (Exception e) {
727                                    throw processException(e);
728                            }
729                            finally {
730                                    if (count == null) {
731                                            count = Long.valueOf(0);
732                                    }
733    
734                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
735                                            count);
736    
737                                    closeSession(session);
738                            }
739                    }
740    
741                    return count.intValue();
742            }
743    
744            /**
745             * Counts all the ratings statses.
746             *
747             * @return the number of ratings statses
748             * @throws SystemException if a system exception occurred
749             */
750            public int countAll() throws SystemException {
751                    Object[] finderArgs = new Object[0];
752    
753                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
754                                    finderArgs, this);
755    
756                    if (count == null) {
757                            Session session = null;
758    
759                            try {
760                                    session = openSession();
761    
762                                    Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
763    
764                                    count = (Long)q.uniqueResult();
765                            }
766                            catch (Exception e) {
767                                    throw processException(e);
768                            }
769                            finally {
770                                    if (count == null) {
771                                            count = Long.valueOf(0);
772                                    }
773    
774                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
775                                            count);
776    
777                                    closeSession(session);
778                            }
779                    }
780    
781                    return count.intValue();
782            }
783    
784            /**
785             * Initializes the ratings stats persistence.
786             */
787            public void afterPropertiesSet() {
788                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
789                                            com.liferay.portal.util.PropsUtil.get(
790                                                    "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
791    
792                    if (listenerClassNames.length > 0) {
793                            try {
794                                    List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
795    
796                                    for (String listenerClassName : listenerClassNames) {
797                                            listenersList.add((ModelListener<RatingsStats>)InstanceFactory.newInstance(
798                                                            listenerClassName));
799                                    }
800    
801                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
802                            }
803                            catch (Exception e) {
804                                    _log.error(e);
805                            }
806                    }
807            }
808    
809            public void destroy() {
810                    EntityCacheUtil.removeCache(RatingsStatsImpl.class.getName());
811                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
812                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
813            }
814    
815            @BeanReference(type = RatingsEntryPersistence.class)
816            protected RatingsEntryPersistence ratingsEntryPersistence;
817            @BeanReference(type = RatingsStatsPersistence.class)
818            protected RatingsStatsPersistence ratingsStatsPersistence;
819            @BeanReference(type = ResourcePersistence.class)
820            protected ResourcePersistence resourcePersistence;
821            @BeanReference(type = UserPersistence.class)
822            protected UserPersistence userPersistence;
823            private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
824            private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
825            private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
826            private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
827            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
828            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
829            private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
830            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
831            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
832            private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
833    }