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