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