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