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