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