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