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