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