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