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