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