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.setClassNameId(ratingsStats.getClassNameId());
608                    ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
609                    ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
610                    ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
611                    ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
612    
613                    return ratingsStatsImpl;
614            }
615    
616            /**
617             * Returns the ratings stats with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
618             *
619             * @param primaryKey the primary key of the ratings stats
620             * @return the ratings stats
621             * @throws NoSuchStatsException if a ratings stats with the primary key could not be found
622             */
623            @Override
624            public RatingsStats findByPrimaryKey(Serializable primaryKey)
625                    throws NoSuchStatsException {
626                    RatingsStats ratingsStats = fetchByPrimaryKey(primaryKey);
627    
628                    if (ratingsStats == null) {
629                            if (_log.isWarnEnabled()) {
630                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
631                            }
632    
633                            throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
634                                    primaryKey);
635                    }
636    
637                    return ratingsStats;
638            }
639    
640            /**
641             * Returns the ratings stats with the primary key or throws a {@link NoSuchStatsException} if it could not be found.
642             *
643             * @param statsId the primary key of the ratings stats
644             * @return the ratings stats
645             * @throws NoSuchStatsException if a ratings stats with the primary key could not be found
646             */
647            @Override
648            public RatingsStats findByPrimaryKey(long statsId)
649                    throws NoSuchStatsException {
650                    return findByPrimaryKey((Serializable)statsId);
651            }
652    
653            /**
654             * Returns the ratings stats with the primary key or returns <code>null</code> if it could not be found.
655             *
656             * @param primaryKey the primary key of the ratings stats
657             * @return the ratings stats, or <code>null</code> if a ratings stats with the primary key could not be found
658             */
659            @Override
660            public RatingsStats fetchByPrimaryKey(Serializable primaryKey) {
661                    RatingsStats ratingsStats = (RatingsStats)entityCache.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
662                                    RatingsStatsImpl.class, primaryKey);
663    
664                    if (ratingsStats == _nullRatingsStats) {
665                            return null;
666                    }
667    
668                    if (ratingsStats == null) {
669                            Session session = null;
670    
671                            try {
672                                    session = openSession();
673    
674                                    ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
675                                                    primaryKey);
676    
677                                    if (ratingsStats != null) {
678                                            cacheResult(ratingsStats);
679                                    }
680                                    else {
681                                            entityCache.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
682                                                    RatingsStatsImpl.class, primaryKey, _nullRatingsStats);
683                                    }
684                            }
685                            catch (Exception e) {
686                                    entityCache.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
687                                            RatingsStatsImpl.class, primaryKey);
688    
689                                    throw processException(e);
690                            }
691                            finally {
692                                    closeSession(session);
693                            }
694                    }
695    
696                    return ratingsStats;
697            }
698    
699            /**
700             * Returns the ratings stats with the primary key or returns <code>null</code> if it could not be found.
701             *
702             * @param statsId the primary key of the ratings stats
703             * @return the ratings stats, or <code>null</code> if a ratings stats with the primary key could not be found
704             */
705            @Override
706            public RatingsStats fetchByPrimaryKey(long statsId) {
707                    return fetchByPrimaryKey((Serializable)statsId);
708            }
709    
710            @Override
711            public Map<Serializable, RatingsStats> fetchByPrimaryKeys(
712                    Set<Serializable> primaryKeys) {
713                    if (primaryKeys.isEmpty()) {
714                            return Collections.emptyMap();
715                    }
716    
717                    Map<Serializable, RatingsStats> map = new HashMap<Serializable, RatingsStats>();
718    
719                    if (primaryKeys.size() == 1) {
720                            Iterator<Serializable> iterator = primaryKeys.iterator();
721    
722                            Serializable primaryKey = iterator.next();
723    
724                            RatingsStats ratingsStats = fetchByPrimaryKey(primaryKey);
725    
726                            if (ratingsStats != null) {
727                                    map.put(primaryKey, ratingsStats);
728                            }
729    
730                            return map;
731                    }
732    
733                    Set<Serializable> uncachedPrimaryKeys = null;
734    
735                    for (Serializable primaryKey : primaryKeys) {
736                            RatingsStats ratingsStats = (RatingsStats)entityCache.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
737                                            RatingsStatsImpl.class, primaryKey);
738    
739                            if (ratingsStats == null) {
740                                    if (uncachedPrimaryKeys == null) {
741                                            uncachedPrimaryKeys = new HashSet<Serializable>();
742                                    }
743    
744                                    uncachedPrimaryKeys.add(primaryKey);
745                            }
746                            else {
747                                    map.put(primaryKey, ratingsStats);
748                            }
749                    }
750    
751                    if (uncachedPrimaryKeys == null) {
752                            return map;
753                    }
754    
755                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
756                                    1);
757    
758                    query.append(_SQL_SELECT_RATINGSSTATS_WHERE_PKS_IN);
759    
760                    for (Serializable primaryKey : uncachedPrimaryKeys) {
761                            query.append(String.valueOf(primaryKey));
762    
763                            query.append(StringPool.COMMA);
764                    }
765    
766                    query.setIndex(query.index() - 1);
767    
768                    query.append(StringPool.CLOSE_PARENTHESIS);
769    
770                    String sql = query.toString();
771    
772                    Session session = null;
773    
774                    try {
775                            session = openSession();
776    
777                            Query q = session.createQuery(sql);
778    
779                            for (RatingsStats ratingsStats : (List<RatingsStats>)q.list()) {
780                                    map.put(ratingsStats.getPrimaryKeyObj(), ratingsStats);
781    
782                                    cacheResult(ratingsStats);
783    
784                                    uncachedPrimaryKeys.remove(ratingsStats.getPrimaryKeyObj());
785                            }
786    
787                            for (Serializable primaryKey : uncachedPrimaryKeys) {
788                                    entityCache.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
789                                            RatingsStatsImpl.class, primaryKey, _nullRatingsStats);
790                            }
791                    }
792                    catch (Exception e) {
793                            throw processException(e);
794                    }
795                    finally {
796                            closeSession(session);
797                    }
798    
799                    return map;
800            }
801    
802            /**
803             * Returns all the ratings statses.
804             *
805             * @return the ratings statses
806             */
807            @Override
808            public List<RatingsStats> findAll() {
809                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
810            }
811    
812            /**
813             * Returns a range of all the ratings statses.
814             *
815             * <p>
816             * 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.
817             * </p>
818             *
819             * @param start the lower bound of the range of ratings statses
820             * @param end the upper bound of the range of ratings statses (not inclusive)
821             * @return the range of ratings statses
822             */
823            @Override
824            public List<RatingsStats> findAll(int start, int end) {
825                    return findAll(start, end, null);
826            }
827    
828            /**
829             * Returns an ordered range of all the ratings statses.
830             *
831             * <p>
832             * 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.
833             * </p>
834             *
835             * @param start the lower bound of the range of ratings statses
836             * @param end the upper bound of the range of ratings statses (not inclusive)
837             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
838             * @return the ordered range of ratings statses
839             */
840            @Override
841            public List<RatingsStats> findAll(int start, int end,
842                    OrderByComparator<RatingsStats> orderByComparator) {
843                    return findAll(start, end, orderByComparator, true);
844            }
845    
846            /**
847             * Returns an ordered range of all the ratings statses.
848             *
849             * <p>
850             * 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.
851             * </p>
852             *
853             * @param start the lower bound of the range of ratings statses
854             * @param end the upper bound of the range of ratings statses (not inclusive)
855             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
856             * @param retrieveFromCache whether to retrieve from the finder cache
857             * @return the ordered range of ratings statses
858             */
859            @Override
860            public List<RatingsStats> findAll(int start, int end,
861                    OrderByComparator<RatingsStats> orderByComparator,
862                    boolean retrieveFromCache) {
863                    boolean pagination = true;
864                    FinderPath finderPath = null;
865                    Object[] finderArgs = null;
866    
867                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
868                                    (orderByComparator == null)) {
869                            pagination = false;
870                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
871                            finderArgs = FINDER_ARGS_EMPTY;
872                    }
873                    else {
874                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
875                            finderArgs = new Object[] { start, end, orderByComparator };
876                    }
877    
878                    List<RatingsStats> list = null;
879    
880                    if (retrieveFromCache) {
881                            list = (List<RatingsStats>)finderCache.getResult(finderPath,
882                                            finderArgs, this);
883                    }
884    
885                    if (list == null) {
886                            StringBundler query = null;
887                            String sql = null;
888    
889                            if (orderByComparator != null) {
890                                    query = new StringBundler(2 +
891                                                    (orderByComparator.getOrderByFields().length * 3));
892    
893                                    query.append(_SQL_SELECT_RATINGSSTATS);
894    
895                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
896                                            orderByComparator);
897    
898                                    sql = query.toString();
899                            }
900                            else {
901                                    sql = _SQL_SELECT_RATINGSSTATS;
902    
903                                    if (pagination) {
904                                            sql = sql.concat(RatingsStatsModelImpl.ORDER_BY_JPQL);
905                                    }
906                            }
907    
908                            Session session = null;
909    
910                            try {
911                                    session = openSession();
912    
913                                    Query q = session.createQuery(sql);
914    
915                                    if (!pagination) {
916                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
917                                                            start, end, false);
918    
919                                            Collections.sort(list);
920    
921                                            list = Collections.unmodifiableList(list);
922                                    }
923                                    else {
924                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
925                                                            start, end);
926                                    }
927    
928                                    cacheResult(list);
929    
930                                    finderCache.putResult(finderPath, finderArgs, list);
931                            }
932                            catch (Exception e) {
933                                    finderCache.removeResult(finderPath, finderArgs);
934    
935                                    throw processException(e);
936                            }
937                            finally {
938                                    closeSession(session);
939                            }
940                    }
941    
942                    return list;
943            }
944    
945            /**
946             * Removes all the ratings statses from the database.
947             *
948             */
949            @Override
950            public void removeAll() {
951                    for (RatingsStats ratingsStats : findAll()) {
952                            remove(ratingsStats);
953                    }
954            }
955    
956            /**
957             * Returns the number of ratings statses.
958             *
959             * @return the number of ratings statses
960             */
961            @Override
962            public int countAll() {
963                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
964                                    FINDER_ARGS_EMPTY, this);
965    
966                    if (count == null) {
967                            Session session = null;
968    
969                            try {
970                                    session = openSession();
971    
972                                    Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
973    
974                                    count = (Long)q.uniqueResult();
975    
976                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
977                                            count);
978                            }
979                            catch (Exception e) {
980                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
981                                            FINDER_ARGS_EMPTY);
982    
983                                    throw processException(e);
984                            }
985                            finally {
986                                    closeSession(session);
987                            }
988                    }
989    
990                    return count.intValue();
991            }
992    
993            @Override
994            protected Map<String, Integer> getTableColumnsMap() {
995                    return RatingsStatsModelImpl.TABLE_COLUMNS_MAP;
996            }
997    
998            /**
999             * Initializes the ratings stats persistence.
1000             */
1001            public void afterPropertiesSet() {
1002            }
1003    
1004            public void destroy() {
1005                    entityCache.removeCache(RatingsStatsImpl.class.getName());
1006                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
1007                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1008                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1009            }
1010    
1011            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
1012            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
1013            private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
1014            private static final String _SQL_SELECT_RATINGSSTATS_WHERE_PKS_IN = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE statsId IN (";
1015            private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
1016            private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
1017            private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
1018            private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
1019            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
1020            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
1021            private static final Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
1022            private static final RatingsStats _nullRatingsStats = new RatingsStatsImpl() {
1023                            @Override
1024                            public Object clone() {
1025                                    return this;
1026                            }
1027    
1028                            @Override
1029                            public CacheModel<RatingsStats> toCacheModel() {
1030                                    return _nullRatingsStatsCacheModel;
1031                            }
1032                    };
1033    
1034            private static final CacheModel<RatingsStats> _nullRatingsStatsCacheModel = new CacheModel<RatingsStats>() {
1035                            @Override
1036                            public RatingsStats toEntityModel() {
1037                                    return _nullRatingsStats;
1038                            }
1039                    };
1040    }