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.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.model.CacheModel;
033    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
034    
035    import com.liferay.portlet.ratings.NoSuchStatsException;
036    import com.liferay.portlet.ratings.model.RatingsStats;
037    import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
038    import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
039    import com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence;
040    
041    import java.io.Serializable;
042    
043    import java.util.Collections;
044    import java.util.HashMap;
045    import java.util.HashSet;
046    import java.util.Iterator;
047    import java.util.List;
048    import java.util.Map;
049    import java.util.Set;
050    
051    /**
052     * The persistence implementation for the ratings stats service.
053     *
054     * <p>
055     * Caching information and settings can be found in <code>portal.properties</code>
056     * </p>
057     *
058     * @author Brian Wing Shun Chan
059     * @see RatingsStatsPersistence
060     * @see RatingsStatsUtil
061     * @generated
062     */
063    @ProviderType
064    public class RatingsStatsPersistenceImpl extends BasePersistenceImpl<RatingsStats>
065            implements RatingsStatsPersistence {
066            /*
067             * NOTE FOR DEVELOPERS:
068             *
069             * 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.
070             */
071            public static final String FINDER_CLASS_NAME_ENTITY = RatingsStatsImpl.class.getName();
072            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List1";
074            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List2";
076            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
077                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, RatingsStatsImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
079            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
080                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, RatingsStatsImpl.class,
081                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
083                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, Long.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
085            public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
086                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, RatingsStatsImpl.class,
087                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
088                            new String[] { Long.class.getName(), Long.class.getName() },
089                            RatingsStatsModelImpl.CLASSNAMEID_COLUMN_BITMASK |
090                            RatingsStatsModelImpl.CLASSPK_COLUMN_BITMASK);
091            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
092                            RatingsStatsModelImpl.FINDER_CACHE_ENABLED, Long.class,
093                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
094                            new String[] { Long.class.getName(), Long.class.getName() });
095    
096            /**
097             * Returns the ratings stats where classNameId = &#63; and classPK = &#63; or throws a {@link com.liferay.portlet.ratings.NoSuchStatsException} if it could not be found.
098             *
099             * @param classNameId the class name ID
100             * @param classPK the class p k
101             * @return the matching ratings stats
102             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a matching ratings stats could not be found
103             */
104            @Override
105            public RatingsStats findByC_C(long classNameId, long classPK)
106                    throws NoSuchStatsException {
107                    RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
108    
109                    if (ratingsStats == null) {
110                            StringBundler msg = new StringBundler(6);
111    
112                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
113    
114                            msg.append("classNameId=");
115                            msg.append(classNameId);
116    
117                            msg.append(", classPK=");
118                            msg.append(classPK);
119    
120                            msg.append(StringPool.CLOSE_CURLY_BRACE);
121    
122                            if (_log.isWarnEnabled()) {
123                                    _log.warn(msg.toString());
124                            }
125    
126                            throw new NoSuchStatsException(msg.toString());
127                    }
128    
129                    return ratingsStats;
130            }
131    
132            /**
133             * 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.
134             *
135             * @param classNameId the class name ID
136             * @param classPK the class p k
137             * @return the matching ratings stats, or <code>null</code> if a matching ratings stats could not be found
138             */
139            @Override
140            public RatingsStats fetchByC_C(long classNameId, long classPK) {
141                    return fetchByC_C(classNameId, classPK, true);
142            }
143    
144            /**
145             * 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.
146             *
147             * @param classNameId the class name ID
148             * @param classPK the class p k
149             * @param retrieveFromCache whether to use the finder cache
150             * @return the matching ratings stats, or <code>null</code> if a matching ratings stats could not be found
151             */
152            @Override
153            public RatingsStats fetchByC_C(long classNameId, long classPK,
154                    boolean retrieveFromCache) {
155                    Object[] finderArgs = new Object[] { classNameId, classPK };
156    
157                    Object result = null;
158    
159                    if (retrieveFromCache) {
160                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
161                                            finderArgs, this);
162                    }
163    
164                    if (result instanceof RatingsStats) {
165                            RatingsStats ratingsStats = (RatingsStats)result;
166    
167                            if ((classNameId != ratingsStats.getClassNameId()) ||
168                                            (classPK != ratingsStats.getClassPK())) {
169                                    result = null;
170                            }
171                    }
172    
173                    if (result == null) {
174                            StringBundler query = new StringBundler(4);
175    
176                            query.append(_SQL_SELECT_RATINGSSTATS_WHERE);
177    
178                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
179    
180                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
181    
182                            String sql = query.toString();
183    
184                            Session session = null;
185    
186                            try {
187                                    session = openSession();
188    
189                                    Query q = session.createQuery(sql);
190    
191                                    QueryPos qPos = QueryPos.getInstance(q);
192    
193                                    qPos.add(classNameId);
194    
195                                    qPos.add(classPK);
196    
197                                    List<RatingsStats> list = q.list();
198    
199                                    if (list.isEmpty()) {
200                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
201                                                    finderArgs, list);
202                                    }
203                                    else {
204                                            RatingsStats ratingsStats = list.get(0);
205    
206                                            result = ratingsStats;
207    
208                                            cacheResult(ratingsStats);
209    
210                                            if ((ratingsStats.getClassNameId() != classNameId) ||
211                                                            (ratingsStats.getClassPK() != classPK)) {
212                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
213                                                            finderArgs, ratingsStats);
214                                            }
215                                    }
216                            }
217                            catch (Exception e) {
218                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
219                                            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)FinderCacheUtil.getResult(finderPath, finderArgs,
265                                    this);
266    
267                    if (count == null) {
268                            StringBundler query = new StringBundler(3);
269    
270                            query.append(_SQL_COUNT_RATINGSSTATS_WHERE);
271    
272                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
273    
274                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
275    
276                            String sql = query.toString();
277    
278                            Session session = null;
279    
280                            try {
281                                    session = openSession();
282    
283                                    Query q = session.createQuery(sql);
284    
285                                    QueryPos qPos = QueryPos.getInstance(q);
286    
287                                    qPos.add(classNameId);
288    
289                                    qPos.add(classPK);
290    
291                                    count = (Long)q.uniqueResult();
292    
293                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
294                            }
295                            catch (Exception e) {
296                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
297    
298                                    throw processException(e);
299                            }
300                            finally {
301                                    closeSession(session);
302                            }
303                    }
304    
305                    return count.intValue();
306            }
307    
308            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsStats.classNameId = ? AND ";
309            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsStats.classPK = ?";
310    
311            public RatingsStatsPersistenceImpl() {
312                    setModelClass(RatingsStats.class);
313            }
314    
315            /**
316             * Caches the ratings stats in the entity cache if it is enabled.
317             *
318             * @param ratingsStats the ratings stats
319             */
320            @Override
321            public void cacheResult(RatingsStats ratingsStats) {
322                    EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
323                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
324    
325                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
326                            new Object[] {
327                                    ratingsStats.getClassNameId(), ratingsStats.getClassPK()
328                            }, ratingsStats);
329    
330                    ratingsStats.resetOriginalValues();
331            }
332    
333            /**
334             * Caches the ratings statses in the entity cache if it is enabled.
335             *
336             * @param ratingsStatses the ratings statses
337             */
338            @Override
339            public void cacheResult(List<RatingsStats> ratingsStatses) {
340                    for (RatingsStats ratingsStats : ratingsStatses) {
341                            if (EntityCacheUtil.getResult(
342                                                    RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
343                                                    RatingsStatsImpl.class, ratingsStats.getPrimaryKey()) == null) {
344                                    cacheResult(ratingsStats);
345                            }
346                            else {
347                                    ratingsStats.resetOriginalValues();
348                            }
349                    }
350            }
351    
352            /**
353             * Clears the cache for all ratings statses.
354             *
355             * <p>
356             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
357             * </p>
358             */
359            @Override
360            public void clearCache() {
361                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
362                            CacheRegistryUtil.clear(RatingsStatsImpl.class.getName());
363                    }
364    
365                    EntityCacheUtil.clearCache(RatingsStatsImpl.class);
366    
367                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
368                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
369                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
370            }
371    
372            /**
373             * Clears the cache for the ratings stats.
374             *
375             * <p>
376             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
377             * </p>
378             */
379            @Override
380            public void clearCache(RatingsStats ratingsStats) {
381                    EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
382                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
383    
384                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
385                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
386    
387                    clearUniqueFindersCache(ratingsStats);
388            }
389    
390            @Override
391            public void clearCache(List<RatingsStats> ratingsStatses) {
392                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
393                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
394    
395                    for (RatingsStats ratingsStats : ratingsStatses) {
396                            EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
397                                    RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
398    
399                            clearUniqueFindersCache(ratingsStats);
400                    }
401            }
402    
403            protected void cacheUniqueFindersCache(RatingsStats ratingsStats) {
404                    if (ratingsStats.isNew()) {
405                            Object[] args = new Object[] {
406                                            ratingsStats.getClassNameId(), ratingsStats.getClassPK()
407                                    };
408    
409                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
410                                    Long.valueOf(1));
411                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args,
412                                    ratingsStats);
413                    }
414                    else {
415                            RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
416    
417                            if ((ratingsStatsModelImpl.getColumnBitmask() &
418                                            FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
419                                    Object[] args = new Object[] {
420                                                    ratingsStats.getClassNameId(), ratingsStats.getClassPK()
421                                            };
422    
423                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, args,
424                                            Long.valueOf(1));
425                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C, args,
426                                            ratingsStats);
427                            }
428                    }
429            }
430    
431            protected void clearUniqueFindersCache(RatingsStats ratingsStats) {
432                    RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
433    
434                    Object[] args = new Object[] {
435                                    ratingsStats.getClassNameId(), ratingsStats.getClassPK()
436                            };
437    
438                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
439                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
440    
441                    if ((ratingsStatsModelImpl.getColumnBitmask() &
442                                    FINDER_PATH_FETCH_BY_C_C.getColumnBitmask()) != 0) {
443                            args = new Object[] {
444                                            ratingsStatsModelImpl.getOriginalClassNameId(),
445                                            ratingsStatsModelImpl.getOriginalClassPK()
446                                    };
447    
448                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
449                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C, args);
450                    }
451            }
452    
453            /**
454             * Creates a new ratings stats with the primary key. Does not add the ratings stats to the database.
455             *
456             * @param statsId the primary key for the new ratings stats
457             * @return the new ratings stats
458             */
459            @Override
460            public RatingsStats create(long statsId) {
461                    RatingsStats ratingsStats = new RatingsStatsImpl();
462    
463                    ratingsStats.setNew(true);
464                    ratingsStats.setPrimaryKey(statsId);
465    
466                    return ratingsStats;
467            }
468    
469            /**
470             * Removes the ratings stats with the primary key from the database. Also notifies the appropriate model listeners.
471             *
472             * @param statsId the primary key of the ratings stats
473             * @return the ratings stats that was removed
474             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
475             */
476            @Override
477            public RatingsStats remove(long statsId) throws NoSuchStatsException {
478                    return remove((Serializable)statsId);
479            }
480    
481            /**
482             * Removes the ratings stats with the primary key from the database. Also notifies the appropriate model listeners.
483             *
484             * @param primaryKey the primary key of the ratings stats
485             * @return the ratings stats that was removed
486             * @throws com.liferay.portlet.ratings.NoSuchStatsException if a ratings stats with the primary key could not be found
487             */
488            @Override
489            public RatingsStats remove(Serializable primaryKey)
490                    throws NoSuchStatsException {
491                    Session session = null;
492    
493                    try {
494                            session = openSession();
495    
496                            RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
497                                            primaryKey);
498    
499                            if (ratingsStats == null) {
500                                    if (_log.isWarnEnabled()) {
501                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
502                                    }
503    
504                                    throw new NoSuchStatsException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
505                                            primaryKey);
506                            }
507    
508                            return remove(ratingsStats);
509                    }
510                    catch (NoSuchStatsException nsee) {
511                            throw nsee;
512                    }
513                    catch (Exception e) {
514                            throw processException(e);
515                    }
516                    finally {
517                            closeSession(session);
518                    }
519            }
520    
521            @Override
522            protected RatingsStats removeImpl(RatingsStats ratingsStats) {
523                    ratingsStats = toUnwrappedModel(ratingsStats);
524    
525                    Session session = null;
526    
527                    try {
528                            session = openSession();
529    
530                            if (!session.contains(ratingsStats)) {
531                                    ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
532                                                    ratingsStats.getPrimaryKeyObj());
533                            }
534    
535                            if (ratingsStats != null) {
536                                    session.delete(ratingsStats);
537                            }
538                    }
539                    catch (Exception e) {
540                            throw processException(e);
541                    }
542                    finally {
543                            closeSession(session);
544                    }
545    
546                    if (ratingsStats != null) {
547                            clearCache(ratingsStats);
548                    }
549    
550                    return ratingsStats;
551            }
552    
553            @Override
554            public RatingsStats updateImpl(
555                    com.liferay.portlet.ratings.model.RatingsStats ratingsStats) {
556                    ratingsStats = toUnwrappedModel(ratingsStats);
557    
558                    boolean isNew = ratingsStats.isNew();
559    
560                    Session session = null;
561    
562                    try {
563                            session = openSession();
564    
565                            if (ratingsStats.isNew()) {
566                                    session.save(ratingsStats);
567    
568                                    ratingsStats.setNew(false);
569                            }
570                            else {
571                                    session.merge(ratingsStats);
572                            }
573                    }
574                    catch (Exception e) {
575                            throw processException(e);
576                    }
577                    finally {
578                            closeSession(session);
579                    }
580    
581                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
582    
583                    if (isNew || !RatingsStatsModelImpl.COLUMN_BITMASK_ENABLED) {
584                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
585                    }
586    
587                    EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
588                            RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats,
589                            false);
590    
591                    clearUniqueFindersCache(ratingsStats);
592                    cacheUniqueFindersCache(ratingsStats);
593    
594                    ratingsStats.resetOriginalValues();
595    
596                    return ratingsStats;
597            }
598    
599            protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
600                    if (ratingsStats instanceof RatingsStatsImpl) {
601                            return ratingsStats;
602                    }
603    
604                    RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
605    
606                    ratingsStatsImpl.setNew(ratingsStats.isNew());
607                    ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
608    
609                    ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
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 com.liferay.portlet.ratings.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 com.liferay.portlet.ratings.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 com.liferay.portlet.ratings.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)EntityCacheUtil.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                                            EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
685                                                    RatingsStatsImpl.class, primaryKey, _nullRatingsStats);
686                                    }
687                            }
688                            catch (Exception e) {
689                                    EntityCacheUtil.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)EntityCacheUtil.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                                    EntityCacheUtil.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
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                    boolean pagination = true;
847                    FinderPath finderPath = null;
848                    Object[] finderArgs = null;
849    
850                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
851                                    (orderByComparator == null)) {
852                            pagination = false;
853                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
854                            finderArgs = FINDER_ARGS_EMPTY;
855                    }
856                    else {
857                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
858                            finderArgs = new Object[] { start, end, orderByComparator };
859                    }
860    
861                    List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(finderPath,
862                                    finderArgs, this);
863    
864                    if (list == null) {
865                            StringBundler query = null;
866                            String sql = null;
867    
868                            if (orderByComparator != null) {
869                                    query = new StringBundler(2 +
870                                                    (orderByComparator.getOrderByFields().length * 3));
871    
872                                    query.append(_SQL_SELECT_RATINGSSTATS);
873    
874                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
875                                            orderByComparator);
876    
877                                    sql = query.toString();
878                            }
879                            else {
880                                    sql = _SQL_SELECT_RATINGSSTATS;
881    
882                                    if (pagination) {
883                                            sql = sql.concat(RatingsStatsModelImpl.ORDER_BY_JPQL);
884                                    }
885                            }
886    
887                            Session session = null;
888    
889                            try {
890                                    session = openSession();
891    
892                                    Query q = session.createQuery(sql);
893    
894                                    if (!pagination) {
895                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
896                                                            start, end, false);
897    
898                                            Collections.sort(list);
899    
900                                            list = Collections.unmodifiableList(list);
901                                    }
902                                    else {
903                                            list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
904                                                            start, end);
905                                    }
906    
907                                    cacheResult(list);
908    
909                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
910                            }
911                            catch (Exception e) {
912                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
913    
914                                    throw processException(e);
915                            }
916                            finally {
917                                    closeSession(session);
918                            }
919                    }
920    
921                    return list;
922            }
923    
924            /**
925             * Removes all the ratings statses from the database.
926             *
927             */
928            @Override
929            public void removeAll() {
930                    for (RatingsStats ratingsStats : findAll()) {
931                            remove(ratingsStats);
932                    }
933            }
934    
935            /**
936             * Returns the number of ratings statses.
937             *
938             * @return the number of ratings statses
939             */
940            @Override
941            public int countAll() {
942                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
943                                    FINDER_ARGS_EMPTY, this);
944    
945                    if (count == null) {
946                            Session session = null;
947    
948                            try {
949                                    session = openSession();
950    
951                                    Query q = session.createQuery(_SQL_COUNT_RATINGSSTATS);
952    
953                                    count = (Long)q.uniqueResult();
954    
955                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
956                                            FINDER_ARGS_EMPTY, count);
957                            }
958                            catch (Exception e) {
959                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
960                                            FINDER_ARGS_EMPTY);
961    
962                                    throw processException(e);
963                            }
964                            finally {
965                                    closeSession(session);
966                            }
967                    }
968    
969                    return count.intValue();
970            }
971    
972            /**
973             * Initializes the ratings stats persistence.
974             */
975            public void afterPropertiesSet() {
976            }
977    
978            public void destroy() {
979                    EntityCacheUtil.removeCache(RatingsStatsImpl.class.getName());
980                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
981                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
982                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
983            }
984    
985            private static final String _SQL_SELECT_RATINGSSTATS = "SELECT ratingsStats FROM RatingsStats ratingsStats";
986            private static final String _SQL_SELECT_RATINGSSTATS_WHERE_PKS_IN = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE statsId IN (";
987            private static final String _SQL_SELECT_RATINGSSTATS_WHERE = "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ";
988            private static final String _SQL_COUNT_RATINGSSTATS = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats";
989            private static final String _SQL_COUNT_RATINGSSTATS_WHERE = "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats WHERE ";
990            private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsStats.";
991            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsStats exists with the primary key ";
992            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsStats exists with the key {";
993            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
994            private static final Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
995            private static final RatingsStats _nullRatingsStats = new RatingsStatsImpl() {
996                            @Override
997                            public Object clone() {
998                                    return this;
999                            }
1000    
1001                            @Override
1002                            public CacheModel<RatingsStats> toCacheModel() {
1003                                    return _nullRatingsStatsCacheModel;
1004                            }
1005                    };
1006    
1007            private static final CacheModel<RatingsStats> _nullRatingsStatsCacheModel = new CacheModel<RatingsStats>() {
1008                            @Override
1009                            public RatingsStats toEntityModel() {
1010                                    return _nullRatingsStats;
1011                            }
1012                    };
1013    }