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