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            /**
317             * Caches the ratings stats in the entity cache if it is enabled.
318             *
319             * @param ratingsStats the ratings stats
320             */
321            @Override
322            public void cacheResult(RatingsStats ratingsStats) {
323                    EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
324                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
325    
326                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
327                            new Object[] {
328                                    ratingsStats.getClassNameId(), ratingsStats.getClassPK()
329                            }, ratingsStats);
330    
331                    ratingsStats.resetOriginalValues();
332            }
333    
334            /**
335             * Caches the ratings statses in the entity cache if it is enabled.
336             *
337             * @param ratingsStatses the ratings statses
338             */
339            @Override
340            public void cacheResult(List<RatingsStats> ratingsStatses) {
341                    for (RatingsStats ratingsStats : ratingsStatses) {
342                            if (EntityCacheUtil.getResult(
343                                                    RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
344                                                    RatingsStatsImpl.class, ratingsStats.getPrimaryKey()) == null) {
345                                    cacheResult(ratingsStats);
346                            }
347                            else {
348                                    ratingsStats.resetOriginalValues();
349                            }
350                    }
351            }
352    
353            /**
354             * Clears the cache for all ratings statses.
355             *
356             * <p>
357             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
358             * </p>
359             */
360            @Override
361            public void clearCache() {
362                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
363                            CacheRegistryUtil.clear(RatingsStatsImpl.class.getName());
364                    }
365    
366                    EntityCacheUtil.clearCache(RatingsStatsImpl.class.getName());
367    
368                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
369                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
370                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
371            }
372    
373            /**
374             * Clears the cache for the ratings stats.
375             *
376             * <p>
377             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
378             * </p>
379             */
380            @Override
381            public void clearCache(RatingsStats ratingsStats) {
382                    EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
383                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
384    
385                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
386                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
387    
388                    clearUniqueFindersCache(ratingsStats);
389            }
390    
391            @Override
392            public void clearCache(List<RatingsStats> ratingsStatses) {
393                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
394                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
395    
396                    for (RatingsStats ratingsStats : ratingsStatses) {
397                            EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
398                                    RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
399    
400                            clearUniqueFindersCache(ratingsStats);
401                    }
402            }
403    
404            protected void cacheUniqueFindersCache(RatingsStats ratingsStats) {
405                    if (ratingsStats.isNew()) {
406                            Object[] args = new Object[] {
407                                            ratingsStats.getClassNameId(), ratingsStats.getClassPK()
408                                    };
409    
410                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
411                                    Long.valueOf(1));
412                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args,
413                                    ratingsStats);
414                    }
415                    else {
416                            RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
417    
418                            if ((ratingsStatsModelImpl.getColumnBitmask() &
419                                            FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
420                                    Object[] args = new Object[] {
421                                                    ratingsStats.getClassNameId(), ratingsStats.getClassPK()
422                                            };
423    
424                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
425                                            Long.valueOf(1));
426                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args,
427                                            ratingsStats);
428                            }
429                    }
430            }
431    
432            protected void clearUniqueFindersCache(RatingsStats ratingsStats) {
433                    RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
434    
435                    Object[] args = new Object[] {
436                                    ratingsStats.getClassNameId(), ratingsStats.getClassPK()
437                            };
438    
439                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
440                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
441    
442                    if ((ratingsStatsModelImpl.getColumnBitmask() &
443                                    FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
444                            args = new Object[] {
445                                            ratingsStatsModelImpl.getOriginalClassNameId(),
446                                            ratingsStatsModelImpl.getOriginalClassPK()
447                                    };
448    
449                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
450                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
451                    }
452            }
453    
454            /**
455             * Creates a new ratings stats with the primary key. Does not add the ratings stats to the database.
456             *
457             * @param statsId the primary key for the new ratings stats
458             * @return the new ratings stats
459             */
460            @Override
461            public RatingsStats create(long statsId) {
462                    RatingsStats ratingsStats = new RatingsStatsImpl();
463    
464                    ratingsStats.setNew(true);
465                    ratingsStats.setPrimaryKey(statsId);
466    
467                    return ratingsStats;
468            }
469    
470            /**
471             * Removes the ratings stats with the primary key from the database. Also notifies the appropriate model listeners.
472             *
473             * @param statsId the primary key of the ratings stats
474             * @return the ratings stats that was removed
475             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
476             * @throws SystemException if a system exception occurred
477             */
478            @Override
479            public RatingsStats remove(long statsId)
480                    throws NoSuchStatsException, SystemException {
481                    return remove((Serializable)statsId);
482            }
483    
484            /**
485             * Removes the ratings stats with the primary key from the database. Also notifies the appropriate model listeners.
486             *
487             * @param primaryKey the primary key of the ratings stats
488             * @return the ratings stats that was removed
489             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
490             * @throws SystemException if a system exception occurred
491             */
492            @Override
493            public RatingsStats remove(Serializable primaryKey)
494                    throws NoSuchStatsException, SystemException {
495                    Session session = null;
496    
497                    try {
498                            session = openSession();
499    
500                            RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
501                                            primaryKey);
502    
503                            if (ratingsStats == null) {
504                                    if (_log.isWarnEnabled()) {
505                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
506                                    }
507    
508                                    throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
509                                            primaryKey);
510                            }
511    
512                            return remove(ratingsStats);
513                    }
514                    catch (NoSuchStatsException nsee) {
515                            throw nsee;
516                    }
517                    catch (Exception e) {
518                            throw processException(e);
519                    }
520                    finally {
521                            closeSession(session);
522                    }
523            }
524    
525            @Override
526            protected RatingsStats removeImpl(RatingsStats ratingsStats)
527                    throws SystemException {
528                    ratingsStats = toUnwrappedModel(ratingsStats);
529    
530                    Session session = null;
531    
532                    try {
533                            session = openSession();
534    
535                            if (!session.contains(ratingsStats)) {
536                                    ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
537                                                    ratingsStats.getPrimaryKeyObj());
538                            }
539    
540                            if (ratingsStats != null) {
541                                    session.delete(ratingsStats);
542                            }
543                    }
544                    catch (Exception e) {
545                            throw processException(e);
546                    }
547                    finally {
548                            closeSession(session);
549                    }
550    
551                    if (ratingsStats != null) {
552                            clearCache(ratingsStats);
553                    }
554    
555                    return ratingsStats;
556            }
557    
558            @Override
559            public RatingsStats updateImpl(
560                    com.liferay.portlet.ratings.model.RatingsStats ratingsStats)
561                    throws SystemException {
562                    ratingsStats = toUnwrappedModel(ratingsStats);
563    
564                    boolean isNew = ratingsStats.isNew();
565    
566                    Session session = null;
567    
568                    try {
569                            session = openSession();
570    
571                            if (ratingsStats.isNew()) {
572                                    session.save(ratingsStats);
573    
574                                    ratingsStats.setNew(false);
575                            }
576                            else {
577                                    session.merge(ratingsStats);
578                            }
579                    }
580                    catch (Exception e) {
581                            throw processException(e);
582                    }
583                    finally {
584                            closeSession(session);
585                    }
586    
587                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
588    
589                    if (isNew || !RatingsStatsModelImpl.COLUMN_BITMASK_ENABLED) {
590                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
591                    }
592    
593                    EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
594                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
595    
596                    clearUniqueFindersCache(ratingsStats);
597                    cacheUniqueFindersCache(ratingsStats);
598    
599                    return ratingsStats;
600            }
601    
602            protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
603                    if (ratingsStats instanceof RatingsStatsImpl) {
604                            return ratingsStats;
605                    }
606    
607                    RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
608    
609                    ratingsStatsImpl.setNew(ratingsStats.isNew());
610                    ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
611    
612                    ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
613                    ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
614                    ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
615                    ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
616                    ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
617                    ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
618    
619                    return ratingsStatsImpl;
620            }
621    
622            /**
623             * Returns the ratings stats with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
624             *
625             * @param primaryKey the primary key of the ratings stats
626             * @return the ratings stats
627             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
628             * @throws SystemException if a system exception occurred
629             */
630            @Override
631            public RatingsStats findByPrimaryKey(Serializable primaryKey)
632                    throws NoSuchStatsException, SystemException {
633                    RatingsStats ratingsStats = fetchByPrimaryKey(primaryKey);
634    
635                    if (ratingsStats == null) {
636                            if (_log.isWarnEnabled()) {
637                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
638                            }
639    
640                            throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
641                                    primaryKey);
642                    }
643    
644                    return ratingsStats;
645            }
646    
647            /**
648             * Returns the ratings stats with the primary key or throws a {@link com.liferay.portlet.ratings.NoSuchStatsException} if it could not be found.
649             *
650             * @param statsId the primary key of the ratings stats
651             * @return the ratings stats
652             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
653             * @throws SystemException if a system exception occurred
654             */
655            @Override
656            public RatingsStats findByPrimaryKey(long statsId)
657                    throws NoSuchStatsException, SystemException {
658                    return findByPrimaryKey((Serializable)statsId);
659            }
660    
661            /**
662             * Returns the ratings stats with the primary key or returns <code>null</code> if it could not be found.
663             *
664             * @param primaryKey the primary key of the ratings stats
665             * @return the ratings stats, or <code>null</code> if a ratings stats with the primary key could not be found
666             * @throws SystemException if a system exception occurred
667             */
668            @Override
669            public RatingsStats fetchByPrimaryKey(Serializable primaryKey)
670                    throws SystemException {
671                    RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
672                                    RatingsStatsImpl.class, primaryKey);
673    
674                    if (ratingsStats == _nullRatingsStats) {
675                            return null;
676                    }
677    
678                    if (ratingsStats == null) {
679                            Session session = null;
680    
681                            try {
682                                    session = openSession();
683    
684                                    ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
685                                                    primaryKey);
686    
687                                    if (ratingsStats != null) {
688                                            cacheResult(ratingsStats);
689                                    }
690                                    else {
691                                            EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
692                                                    RatingsStatsImpl.class, primaryKey, _nullRatingsStats);
693                                    }
694                            }
695                            catch (Exception e) {
696                                    EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
697                                            RatingsStatsImpl.class, primaryKey);
698    
699                                    throw processException(e);
700                            }
701                            finally {
702                                    closeSession(session);
703                            }
704                    }
705    
706                    return ratingsStats;
707            }
708    
709            /**
710             * Returns the ratings stats with the primary key or returns <code>null</code> if it could not be found.
711             *
712             * @param statsId the primary key of the ratings stats
713             * @return the ratings stats, or <code>null</code> if a ratings stats with the primary key could not be found
714             * @throws SystemException if a system exception occurred
715             */
716            @Override
717            public RatingsStats fetchByPrimaryKey(long statsId)
718                    throws SystemException {
719                    return fetchByPrimaryKey((Serializable)statsId);
720            }
721    
722            /**
723             * Returns all the ratings statses.
724             *
725             * @return the ratings statses
726             * @throws SystemException if a system exception occurred
727             */
728            @Override
729            public List<RatingsStats> findAll() throws SystemException {
730                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
731            }
732    
733            /**
734             * Returns a range of all the ratings statses.
735             *
736             * <p>
737             * 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.
738             * </p>
739             *
740             * @param start the lower bound of the range of ratings statses
741             * @param end the upper bound of the range of ratings statses (not inclusive)
742             * @return the range of ratings statses
743             * @throws SystemException if a system exception occurred
744             */
745            @Override
746            public List<RatingsStats> findAll(int start, int end)
747                    throws SystemException {
748                    return findAll(start, end, null);
749            }
750    
751            /**
752             * Returns an ordered range of all the ratings statses.
753             *
754             * <p>
755             * 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.
756             * </p>
757             *
758             * @param start the lower bound of the range of ratings statses
759             * @param end the upper bound of the range of ratings statses (not inclusive)
760             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
761             * @return the ordered range of ratings statses
762             * @throws SystemException if a system exception occurred
763             */
764            @Override
765            public List<RatingsStats> findAll(int start, int end,
766                    OrderByComparator orderByComparator) throws SystemException {
767                    boolean pagination = true;
768                    FinderPath finderPath = null;
769                    Object[] finderArgs = null;
770    
771                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
772                                    (orderByComparator == null)) {
773                            pagination = false;
774                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
775                            finderArgs = FINDER_ARGS_EMPTY;
776                    }
777                    else {
778                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
779                            finderArgs = new Object[] { start, end, orderByComparator };
780                    }
781    
782                    List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(finderPath,
783                                    finderArgs, this);
784    
785                    if (list == null) {
786                            StringBundler query = null;
787                            String sql = null;
788    
789                            if (orderByComparator != null) {
790                                    query = new StringBundler(2 +
791                                                    (orderByComparator.getOrderByFields().length * 3));
792    
793                                    query.append(_SQL_SELECT_RATINGSSTATS);
794    
795                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
796                                            orderByComparator);
797    
798                                    sql = query.toString();
799                            }
800                            else {
801                                    sql = _SQL_SELECT_RATINGSSTATS;
802    
803                                    if (pagination) {
804                                            sql = sql.concat(RatingsStatsModelImpl.ORDER_BY_JPQL);
805                                    }
806                            }
807    
808                            Session session = null;
809    
810                            try {
811                                    session = openSession();
812    
813                                    Query q = session.createQuery(sql);
814    
815                                    if (!pagination) {
816                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
817                                                            start, end, false);
818    
819                                            Collections.sort(list);
820    
821                                            list = new UnmodifiableList<RatingsStats>(list);
822                                    }
823                                    else {
824                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
825                                                            start, end);
826                                    }
827    
828                                    cacheResult(list);
829    
830                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
831                            }
832                            catch (Exception e) {
833                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
834    
835                                    throw processException(e);
836                            }
837                            finally {
838                                    closeSession(session);
839                            }
840                    }
841    
842                    return list;
843            }
844    
845            /**
846             * Removes all the ratings statses from the database.
847             *
848             * @throws SystemException if a system exception occurred
849             */
850            @Override
851            public void removeAll() throws SystemException {
852                    for (RatingsStats ratingsStats : findAll()) {
853                            remove(ratingsStats);
854                    }
855            }
856    
857            /**
858             * Returns the number of ratings statses.
859             *
860             * @return the number of ratings statses
861             * @throws SystemException if a system exception occurred
862             */
863            @Override
864            public int countAll() throws SystemException {
865                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
866                                    FINDER_ARGS_EMPTY, this);
867    
868                    if (count == null) {
869                            Session session = null;
870    
871                            try {
872                                    session = openSession();
873    
874                                    Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
875    
876                                    count = (Long)q.uniqueResult();
877    
878                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
879                                            FINDER_ARGS_EMPTY, count);
880                            }
881                            catch (Exception e) {
882                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
883                                            FINDER_ARGS_EMPTY);
884    
885                                    throw processException(e);
886                            }
887                            finally {
888                                    closeSession(session);
889                            }
890                    }
891    
892                    return count.intValue();
893            }
894    
895            /**
896             * Initializes the ratings stats persistence.
897             */
898            public void afterPropertiesSet() {
899                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
900                                            com.liferay.portal.util.PropsUtil.get(
901                                                    "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
902    
903                    if (listenerClassNames.length > 0) {
904                            try {
905                                    List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
906    
907                                    for (String listenerClassName : listenerClassNames) {
908                                            listenersList.add((ModelListener<RatingsStats>)InstanceFactory.newInstance(
909                                                            getClassLoader(), listenerClassName));
910                                    }
911    
912                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
913                            }
914                            catch (Exception e) {
915                                    _log.error(e);
916                            }
917                    }
918            }
919    
920            public void destroy() {
921                    EntityCacheUtil.removeCache(RatingsStatsImpl.class.getName());
922                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
923                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
924                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
925            }
926    
927            private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
928            private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
929            private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
930            private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
931            private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
932            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
933            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
934            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
935            private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
936            private static RatingsStats _nullRatingsStats = new RatingsStatsImpl() {
937                            @Override
938                            public Object clone() {
939                                    return this;
940                            }
941    
942                            @Override
943                            public CacheModel<RatingsStats> toCacheModel() {
944                                    return _nullRatingsStatsCacheModel;
945                            }
946                    };
947    
948            private static CacheModel<RatingsStats> _nullRatingsStatsCacheModel = new CacheModel<RatingsStats>() {
949                            @Override
950                            public RatingsStats toEntityModel() {
951                                    return _nullRatingsStats;
952                            }
953                    };
954    }