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