001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.ratings.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.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.blogs.service.persistence.BlogsEntryPersistence;
043    import com.liferay.portlet.blogs.service.persistence.BlogsStatsUserPersistence;
044    import com.liferay.portlet.ratings.NoSuchEntryException;
045    import com.liferay.portlet.ratings.model.RatingsEntry;
046    import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
047    import com.liferay.portlet.ratings.model.impl.RatingsEntryModelImpl;
048    import com.liferay.portlet.social.service.persistence.SocialEquityLogPersistence;
049    
050    import java.io.Serializable;
051    
052    import java.util.ArrayList;
053    import java.util.Collections;
054    import java.util.List;
055    
056    /**
057     * @author    Brian Wing Shun Chan
058     * @see       RatingsEntryPersistence
059     * @see       RatingsEntryUtil
060     * @generated
061     */
062    public class RatingsEntryPersistenceImpl extends BasePersistenceImpl<RatingsEntry>
063            implements RatingsEntryPersistence {
064            public static final String FINDER_CLASS_NAME_ENTITY = RatingsEntryImpl.class.getName();
065            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
066                    ".List";
067            public static final FinderPath FINDER_PATH_FIND_BY_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
068                            RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
069                            "findByC_C",
070                            new String[] {
071                                    Long.class.getName(), Long.class.getName(),
072                                    
073                            "java.lang.Integer", "java.lang.Integer",
074                                    "com.liferay.portal.kernel.util.OrderByComparator"
075                            });
076            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
077                            RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
078                            "countByC_C",
079                            new String[] { Long.class.getName(), Long.class.getName() });
080            public static final FinderPath FINDER_PATH_FETCH_BY_U_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
081                            RatingsEntryModelImpl.FINDER_CACHE_ENABLED,
082                            FINDER_CLASS_NAME_ENTITY, "fetchByU_C_C",
083                            new String[] {
084                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
085                            });
086            public static final FinderPath FINDER_PATH_COUNT_BY_U_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
087                            RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
088                            "countByU_C_C",
089                            new String[] {
090                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
091                            });
092            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
093                            RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
094                            "findAll", new String[0]);
095            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
096                            RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
097                            "countAll", new String[0]);
098    
099            public void cacheResult(RatingsEntry ratingsEntry) {
100                    EntityCacheUtil.putResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
101                            RatingsEntryImpl.class, ratingsEntry.getPrimaryKey(), ratingsEntry);
102    
103                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
104                            new Object[] {
105                                    new Long(ratingsEntry.getUserId()),
106                                    new Long(ratingsEntry.getClassNameId()),
107                                    new Long(ratingsEntry.getClassPK())
108                            }, ratingsEntry);
109            }
110    
111            public void cacheResult(List<RatingsEntry> ratingsEntries) {
112                    for (RatingsEntry ratingsEntry : ratingsEntries) {
113                            if (EntityCacheUtil.getResult(
114                                                    RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
115                                                    RatingsEntryImpl.class, ratingsEntry.getPrimaryKey(),
116                                                    this) == null) {
117                                    cacheResult(ratingsEntry);
118                            }
119                    }
120            }
121    
122            public void clearCache() {
123                    CacheRegistryUtil.clear(RatingsEntryImpl.class.getName());
124                    EntityCacheUtil.clearCache(RatingsEntryImpl.class.getName());
125                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
126                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
127            }
128    
129            public void clearCache(RatingsEntry ratingsEntry) {
130                    EntityCacheUtil.removeResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
131                            RatingsEntryImpl.class, ratingsEntry.getPrimaryKey());
132    
133                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_C_C,
134                            new Object[] {
135                                    new Long(ratingsEntry.getUserId()),
136                                    new Long(ratingsEntry.getClassNameId()),
137                                    new Long(ratingsEntry.getClassPK())
138                            });
139            }
140    
141            public RatingsEntry create(long entryId) {
142                    RatingsEntry ratingsEntry = new RatingsEntryImpl();
143    
144                    ratingsEntry.setNew(true);
145                    ratingsEntry.setPrimaryKey(entryId);
146    
147                    return ratingsEntry;
148            }
149    
150            public RatingsEntry remove(Serializable primaryKey)
151                    throws NoSuchModelException, SystemException {
152                    return remove(((Long)primaryKey).longValue());
153            }
154    
155            public RatingsEntry remove(long entryId)
156                    throws NoSuchEntryException, SystemException {
157                    Session session = null;
158    
159                    try {
160                            session = openSession();
161    
162                            RatingsEntry ratingsEntry = (RatingsEntry)session.get(RatingsEntryImpl.class,
163                                            new Long(entryId));
164    
165                            if (ratingsEntry == null) {
166                                    if (_log.isWarnEnabled()) {
167                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + entryId);
168                                    }
169    
170                                    throw new NoSuchEntryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
171                                            entryId);
172                            }
173    
174                            return remove(ratingsEntry);
175                    }
176                    catch (NoSuchEntryException nsee) {
177                            throw nsee;
178                    }
179                    catch (Exception e) {
180                            throw processException(e);
181                    }
182                    finally {
183                            closeSession(session);
184                    }
185            }
186    
187            protected RatingsEntry removeImpl(RatingsEntry ratingsEntry)
188                    throws SystemException {
189                    ratingsEntry = toUnwrappedModel(ratingsEntry);
190    
191                    Session session = null;
192    
193                    try {
194                            session = openSession();
195    
196                            if (ratingsEntry.isCachedModel() || BatchSessionUtil.isEnabled()) {
197                                    Object staleObject = session.get(RatingsEntryImpl.class,
198                                                    ratingsEntry.getPrimaryKeyObj());
199    
200                                    if (staleObject != null) {
201                                            session.evict(staleObject);
202                                    }
203                            }
204    
205                            session.delete(ratingsEntry);
206    
207                            session.flush();
208                    }
209                    catch (Exception e) {
210                            throw processException(e);
211                    }
212                    finally {
213                            closeSession(session);
214                    }
215    
216                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
217    
218                    RatingsEntryModelImpl ratingsEntryModelImpl = (RatingsEntryModelImpl)ratingsEntry;
219    
220                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_C_C,
221                            new Object[] {
222                                    new Long(ratingsEntryModelImpl.getOriginalUserId()),
223                                    new Long(ratingsEntryModelImpl.getOriginalClassNameId()),
224                                    new Long(ratingsEntryModelImpl.getOriginalClassPK())
225                            });
226    
227                    EntityCacheUtil.removeResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
228                            RatingsEntryImpl.class, ratingsEntry.getPrimaryKey());
229    
230                    return ratingsEntry;
231            }
232    
233            public RatingsEntry updateImpl(
234                    com.liferay.portlet.ratings.model.RatingsEntry ratingsEntry,
235                    boolean merge) throws SystemException {
236                    ratingsEntry = toUnwrappedModel(ratingsEntry);
237    
238                    boolean isNew = ratingsEntry.isNew();
239    
240                    RatingsEntryModelImpl ratingsEntryModelImpl = (RatingsEntryModelImpl)ratingsEntry;
241    
242                    Session session = null;
243    
244                    try {
245                            session = openSession();
246    
247                            BatchSessionUtil.update(session, ratingsEntry, merge);
248    
249                            ratingsEntry.setNew(false);
250                    }
251                    catch (Exception e) {
252                            throw processException(e);
253                    }
254                    finally {
255                            closeSession(session);
256                    }
257    
258                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
259    
260                    EntityCacheUtil.putResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
261                            RatingsEntryImpl.class, ratingsEntry.getPrimaryKey(), ratingsEntry);
262    
263                    if (!isNew &&
264                                    ((ratingsEntry.getUserId() != ratingsEntryModelImpl.getOriginalUserId()) ||
265                                    (ratingsEntry.getClassNameId() != ratingsEntryModelImpl.getOriginalClassNameId()) ||
266                                    (ratingsEntry.getClassPK() != ratingsEntryModelImpl.getOriginalClassPK()))) {
267                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_C_C,
268                                    new Object[] {
269                                            new Long(ratingsEntryModelImpl.getOriginalUserId()),
270                                            new Long(ratingsEntryModelImpl.getOriginalClassNameId()),
271                                            new Long(ratingsEntryModelImpl.getOriginalClassPK())
272                                    });
273                    }
274    
275                    if (isNew ||
276                                    ((ratingsEntry.getUserId() != ratingsEntryModelImpl.getOriginalUserId()) ||
277                                    (ratingsEntry.getClassNameId() != ratingsEntryModelImpl.getOriginalClassNameId()) ||
278                                    (ratingsEntry.getClassPK() != ratingsEntryModelImpl.getOriginalClassPK()))) {
279                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
280                                    new Object[] {
281                                            new Long(ratingsEntry.getUserId()),
282                                            new Long(ratingsEntry.getClassNameId()),
283                                            new Long(ratingsEntry.getClassPK())
284                                    }, ratingsEntry);
285                    }
286    
287                    return ratingsEntry;
288            }
289    
290            protected RatingsEntry toUnwrappedModel(RatingsEntry ratingsEntry) {
291                    if (ratingsEntry instanceof RatingsEntryImpl) {
292                            return ratingsEntry;
293                    }
294    
295                    RatingsEntryImpl ratingsEntryImpl = new RatingsEntryImpl();
296    
297                    ratingsEntryImpl.setNew(ratingsEntry.isNew());
298                    ratingsEntryImpl.setPrimaryKey(ratingsEntry.getPrimaryKey());
299    
300                    ratingsEntryImpl.setEntryId(ratingsEntry.getEntryId());
301                    ratingsEntryImpl.setCompanyId(ratingsEntry.getCompanyId());
302                    ratingsEntryImpl.setUserId(ratingsEntry.getUserId());
303                    ratingsEntryImpl.setUserName(ratingsEntry.getUserName());
304                    ratingsEntryImpl.setCreateDate(ratingsEntry.getCreateDate());
305                    ratingsEntryImpl.setModifiedDate(ratingsEntry.getModifiedDate());
306                    ratingsEntryImpl.setClassNameId(ratingsEntry.getClassNameId());
307                    ratingsEntryImpl.setClassPK(ratingsEntry.getClassPK());
308                    ratingsEntryImpl.setScore(ratingsEntry.getScore());
309    
310                    return ratingsEntryImpl;
311            }
312    
313            public RatingsEntry findByPrimaryKey(Serializable primaryKey)
314                    throws NoSuchModelException, SystemException {
315                    return findByPrimaryKey(((Long)primaryKey).longValue());
316            }
317    
318            public RatingsEntry findByPrimaryKey(long entryId)
319                    throws NoSuchEntryException, SystemException {
320                    RatingsEntry ratingsEntry = fetchByPrimaryKey(entryId);
321    
322                    if (ratingsEntry == null) {
323                            if (_log.isWarnEnabled()) {
324                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + entryId);
325                            }
326    
327                            throw new NoSuchEntryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
328                                    entryId);
329                    }
330    
331                    return ratingsEntry;
332            }
333    
334            public RatingsEntry fetchByPrimaryKey(Serializable primaryKey)
335                    throws SystemException {
336                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
337            }
338    
339            public RatingsEntry fetchByPrimaryKey(long entryId)
340                    throws SystemException {
341                    RatingsEntry ratingsEntry = (RatingsEntry)EntityCacheUtil.getResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
342                                    RatingsEntryImpl.class, entryId, this);
343    
344                    if (ratingsEntry == null) {
345                            Session session = null;
346    
347                            try {
348                                    session = openSession();
349    
350                                    ratingsEntry = (RatingsEntry)session.get(RatingsEntryImpl.class,
351                                                    new Long(entryId));
352                            }
353                            catch (Exception e) {
354                                    throw processException(e);
355                            }
356                            finally {
357                                    if (ratingsEntry != null) {
358                                            cacheResult(ratingsEntry);
359                                    }
360    
361                                    closeSession(session);
362                            }
363                    }
364    
365                    return ratingsEntry;
366            }
367    
368            public List<RatingsEntry> findByC_C(long classNameId, long classPK)
369                    throws SystemException {
370                    return findByC_C(classNameId, classPK, QueryUtil.ALL_POS,
371                            QueryUtil.ALL_POS, null);
372            }
373    
374            public List<RatingsEntry> findByC_C(long classNameId, long classPK,
375                    int start, int end) throws SystemException {
376                    return findByC_C(classNameId, classPK, start, end, null);
377            }
378    
379            public List<RatingsEntry> findByC_C(long classNameId, long classPK,
380                    int start, int end, OrderByComparator orderByComparator)
381                    throws SystemException {
382                    Object[] finderArgs = new Object[] {
383                                    classNameId, classPK,
384                                    
385                                    String.valueOf(start), String.valueOf(end),
386                                    String.valueOf(orderByComparator)
387                            };
388    
389                    List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_C,
390                                    finderArgs, this);
391    
392                    if (list == null) {
393                            Session session = null;
394    
395                            try {
396                                    session = openSession();
397    
398                                    StringBundler query = null;
399    
400                                    if (orderByComparator != null) {
401                                            query = new StringBundler(4 +
402                                                            (orderByComparator.getOrderByFields().length * 3));
403                                    }
404                                    else {
405                                            query = new StringBundler(3);
406                                    }
407    
408                                    query.append(_SQL_SELECT_RATINGSENTRY_WHERE);
409    
410                                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
411    
412                                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
413    
414                                    if (orderByComparator != null) {
415                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
416                                                    orderByComparator);
417                                    }
418    
419                                    String sql = query.toString();
420    
421                                    Query q = session.createQuery(sql);
422    
423                                    QueryPos qPos = QueryPos.getInstance(q);
424    
425                                    qPos.add(classNameId);
426    
427                                    qPos.add(classPK);
428    
429                                    list = (List<RatingsEntry>)QueryUtil.list(q, getDialect(),
430                                                    start, end);
431                            }
432                            catch (Exception e) {
433                                    throw processException(e);
434                            }
435                            finally {
436                                    if (list == null) {
437                                            list = new ArrayList<RatingsEntry>();
438                                    }
439    
440                                    cacheResult(list);
441    
442                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_C, finderArgs,
443                                            list);
444    
445                                    closeSession(session);
446                            }
447                    }
448    
449                    return list;
450            }
451    
452            public RatingsEntry findByC_C_First(long classNameId, long classPK,
453                    OrderByComparator orderByComparator)
454                    throws NoSuchEntryException, SystemException {
455                    List<RatingsEntry> list = findByC_C(classNameId, classPK, 0, 1,
456                                    orderByComparator);
457    
458                    if (list.isEmpty()) {
459                            StringBundler msg = new StringBundler(6);
460    
461                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
462    
463                            msg.append("classNameId=");
464                            msg.append(classNameId);
465    
466                            msg.append(", classPK=");
467                            msg.append(classPK);
468    
469                            msg.append(StringPool.CLOSE_CURLY_BRACE);
470    
471                            throw new NoSuchEntryException(msg.toString());
472                    }
473                    else {
474                            return list.get(0);
475                    }
476            }
477    
478            public RatingsEntry findByC_C_Last(long classNameId, long classPK,
479                    OrderByComparator orderByComparator)
480                    throws NoSuchEntryException, SystemException {
481                    int count = countByC_C(classNameId, classPK);
482    
483                    List<RatingsEntry> list = findByC_C(classNameId, classPK, count - 1,
484                                    count, orderByComparator);
485    
486                    if (list.isEmpty()) {
487                            StringBundler msg = new StringBundler(6);
488    
489                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
490    
491                            msg.append("classNameId=");
492                            msg.append(classNameId);
493    
494                            msg.append(", classPK=");
495                            msg.append(classPK);
496    
497                            msg.append(StringPool.CLOSE_CURLY_BRACE);
498    
499                            throw new NoSuchEntryException(msg.toString());
500                    }
501                    else {
502                            return list.get(0);
503                    }
504            }
505    
506            public RatingsEntry[] findByC_C_PrevAndNext(long entryId, long classNameId,
507                    long classPK, OrderByComparator orderByComparator)
508                    throws NoSuchEntryException, SystemException {
509                    RatingsEntry ratingsEntry = findByPrimaryKey(entryId);
510    
511                    Session session = null;
512    
513                    try {
514                            session = openSession();
515    
516                            RatingsEntry[] array = new RatingsEntryImpl[3];
517    
518                            array[0] = getByC_C_PrevAndNext(session, ratingsEntry, classNameId,
519                                            classPK, orderByComparator, true);
520    
521                            array[1] = ratingsEntry;
522    
523                            array[2] = getByC_C_PrevAndNext(session, ratingsEntry, classNameId,
524                                            classPK, orderByComparator, false);
525    
526                            return array;
527                    }
528                    catch (Exception e) {
529                            throw processException(e);
530                    }
531                    finally {
532                            closeSession(session);
533                    }
534            }
535    
536            protected RatingsEntry getByC_C_PrevAndNext(Session session,
537                    RatingsEntry ratingsEntry, long classNameId, long classPK,
538                    OrderByComparator orderByComparator, boolean previous) {
539                    StringBundler query = null;
540    
541                    if (orderByComparator != null) {
542                            query = new StringBundler(6 +
543                                            (orderByComparator.getOrderByFields().length * 6));
544                    }
545                    else {
546                            query = new StringBundler(3);
547                    }
548    
549                    query.append(_SQL_SELECT_RATINGSENTRY_WHERE);
550    
551                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
552    
553                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
554    
555                    if (orderByComparator != null) {
556                            String[] orderByFields = orderByComparator.getOrderByFields();
557    
558                            if (orderByFields.length > 0) {
559                                    query.append(WHERE_AND);
560                            }
561    
562                            for (int i = 0; i < orderByFields.length; i++) {
563                                    query.append(_ORDER_BY_ENTITY_ALIAS);
564                                    query.append(orderByFields[i]);
565    
566                                    if ((i + 1) < orderByFields.length) {
567                                            if (orderByComparator.isAscending() ^ previous) {
568                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
569                                            }
570                                            else {
571                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
572                                            }
573                                    }
574                                    else {
575                                            if (orderByComparator.isAscending() ^ previous) {
576                                                    query.append(WHERE_GREATER_THAN);
577                                            }
578                                            else {
579                                                    query.append(WHERE_LESSER_THAN);
580                                            }
581                                    }
582                            }
583    
584                            query.append(ORDER_BY_CLAUSE);
585    
586                            for (int i = 0; i < orderByFields.length; i++) {
587                                    query.append(_ORDER_BY_ENTITY_ALIAS);
588                                    query.append(orderByFields[i]);
589    
590                                    if ((i + 1) < orderByFields.length) {
591                                            if (orderByComparator.isAscending() ^ previous) {
592                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
593                                            }
594                                            else {
595                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
596                                            }
597                                    }
598                                    else {
599                                            if (orderByComparator.isAscending() ^ previous) {
600                                                    query.append(ORDER_BY_ASC);
601                                            }
602                                            else {
603                                                    query.append(ORDER_BY_DESC);
604                                            }
605                                    }
606                            }
607                    }
608    
609                    String sql = query.toString();
610    
611                    Query q = session.createQuery(sql);
612    
613                    q.setFirstResult(0);
614                    q.setMaxResults(2);
615    
616                    QueryPos qPos = QueryPos.getInstance(q);
617    
618                    qPos.add(classNameId);
619    
620                    qPos.add(classPK);
621    
622                    if (orderByComparator != null) {
623                            Object[] values = orderByComparator.getOrderByValues(ratingsEntry);
624    
625                            for (Object value : values) {
626                                    qPos.add(value);
627                            }
628                    }
629    
630                    List<RatingsEntry> list = q.list();
631    
632                    if (list.size() == 2) {
633                            return list.get(1);
634                    }
635                    else {
636                            return null;
637                    }
638            }
639    
640            public RatingsEntry findByU_C_C(long userId, long classNameId, long classPK)
641                    throws NoSuchEntryException, SystemException {
642                    RatingsEntry ratingsEntry = fetchByU_C_C(userId, classNameId, classPK);
643    
644                    if (ratingsEntry == null) {
645                            StringBundler msg = new StringBundler(8);
646    
647                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
648    
649                            msg.append("userId=");
650                            msg.append(userId);
651    
652                            msg.append(", classNameId=");
653                            msg.append(classNameId);
654    
655                            msg.append(", classPK=");
656                            msg.append(classPK);
657    
658                            msg.append(StringPool.CLOSE_CURLY_BRACE);
659    
660                            if (_log.isWarnEnabled()) {
661                                    _log.warn(msg.toString());
662                            }
663    
664                            throw new NoSuchEntryException(msg.toString());
665                    }
666    
667                    return ratingsEntry;
668            }
669    
670            public RatingsEntry fetchByU_C_C(long userId, long classNameId, long classPK)
671                    throws SystemException {
672                    return fetchByU_C_C(userId, classNameId, classPK, true);
673            }
674    
675            public RatingsEntry fetchByU_C_C(long userId, long classNameId,
676                    long classPK, boolean retrieveFromCache) throws SystemException {
677                    Object[] finderArgs = new Object[] { userId, classNameId, classPK };
678    
679                    Object result = null;
680    
681                    if (retrieveFromCache) {
682                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_C_C,
683                                            finderArgs, this);
684                    }
685    
686                    if (result == null) {
687                            Session session = null;
688    
689                            try {
690                                    session = openSession();
691    
692                                    StringBundler query = new StringBundler(4);
693    
694                                    query.append(_SQL_SELECT_RATINGSENTRY_WHERE);
695    
696                                    query.append(_FINDER_COLUMN_U_C_C_USERID_2);
697    
698                                    query.append(_FINDER_COLUMN_U_C_C_CLASSNAMEID_2);
699    
700                                    query.append(_FINDER_COLUMN_U_C_C_CLASSPK_2);
701    
702                                    String sql = query.toString();
703    
704                                    Query q = session.createQuery(sql);
705    
706                                    QueryPos qPos = QueryPos.getInstance(q);
707    
708                                    qPos.add(userId);
709    
710                                    qPos.add(classNameId);
711    
712                                    qPos.add(classPK);
713    
714                                    List<RatingsEntry> list = q.list();
715    
716                                    result = list;
717    
718                                    RatingsEntry ratingsEntry = null;
719    
720                                    if (list.isEmpty()) {
721                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
722                                                    finderArgs, list);
723                                    }
724                                    else {
725                                            ratingsEntry = list.get(0);
726    
727                                            cacheResult(ratingsEntry);
728    
729                                            if ((ratingsEntry.getUserId() != userId) ||
730                                                            (ratingsEntry.getClassNameId() != classNameId) ||
731                                                            (ratingsEntry.getClassPK() != classPK)) {
732                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
733                                                            finderArgs, ratingsEntry);
734                                            }
735                                    }
736    
737                                    return ratingsEntry;
738                            }
739                            catch (Exception e) {
740                                    throw processException(e);
741                            }
742                            finally {
743                                    if (result == null) {
744                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
745                                                    finderArgs, new ArrayList<RatingsEntry>());
746                                    }
747    
748                                    closeSession(session);
749                            }
750                    }
751                    else {
752                            if (result instanceof List<?>) {
753                                    return null;
754                            }
755                            else {
756                                    return (RatingsEntry)result;
757                            }
758                    }
759            }
760    
761            public List<RatingsEntry> findAll() throws SystemException {
762                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
763            }
764    
765            public List<RatingsEntry> findAll(int start, int end)
766                    throws SystemException {
767                    return findAll(start, end, null);
768            }
769    
770            public List<RatingsEntry> findAll(int start, int end,
771                    OrderByComparator orderByComparator) throws SystemException {
772                    Object[] finderArgs = new Object[] {
773                                    String.valueOf(start), String.valueOf(end),
774                                    String.valueOf(orderByComparator)
775                            };
776    
777                    List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
778                                    finderArgs, this);
779    
780                    if (list == null) {
781                            Session session = null;
782    
783                            try {
784                                    session = openSession();
785    
786                                    StringBundler query = null;
787                                    String sql = null;
788    
789                                    if (orderByComparator != null) {
790                                            query = new StringBundler(2 +
791                                                            (orderByComparator.getOrderByFields().length * 3));
792    
793                                            query.append(_SQL_SELECT_RATINGSENTRY);
794    
795                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
796                                                    orderByComparator);
797    
798                                            sql = query.toString();
799                                    }
800                                    else {
801                                            sql = _SQL_SELECT_RATINGSENTRY;
802                                    }
803    
804                                    Query q = session.createQuery(sql);
805    
806                                    if (orderByComparator == null) {
807                                            list = (List<RatingsEntry>)QueryUtil.list(q, getDialect(),
808                                                            start, end, false);
809    
810                                            Collections.sort(list);
811                                    }
812                                    else {
813                                            list = (List<RatingsEntry>)QueryUtil.list(q, getDialect(),
814                                                            start, end);
815                                    }
816                            }
817                            catch (Exception e) {
818                                    throw processException(e);
819                            }
820                            finally {
821                                    if (list == null) {
822                                            list = new ArrayList<RatingsEntry>();
823                                    }
824    
825                                    cacheResult(list);
826    
827                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
828    
829                                    closeSession(session);
830                            }
831                    }
832    
833                    return list;
834            }
835    
836            public void removeByC_C(long classNameId, long classPK)
837                    throws SystemException {
838                    for (RatingsEntry ratingsEntry : findByC_C(classNameId, classPK)) {
839                            remove(ratingsEntry);
840                    }
841            }
842    
843            public void removeByU_C_C(long userId, long classNameId, long classPK)
844                    throws NoSuchEntryException, SystemException {
845                    RatingsEntry ratingsEntry = findByU_C_C(userId, classNameId, classPK);
846    
847                    remove(ratingsEntry);
848            }
849    
850            public void removeAll() throws SystemException {
851                    for (RatingsEntry ratingsEntry : findAll()) {
852                            remove(ratingsEntry);
853                    }
854            }
855    
856            public int countByC_C(long classNameId, long classPK)
857                    throws SystemException {
858                    Object[] finderArgs = new Object[] { classNameId, classPK };
859    
860                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
861                                    finderArgs, this);
862    
863                    if (count == null) {
864                            Session session = null;
865    
866                            try {
867                                    session = openSession();
868    
869                                    StringBundler query = new StringBundler(3);
870    
871                                    query.append(_SQL_COUNT_RATINGSENTRY_WHERE);
872    
873                                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
874    
875                                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
876    
877                                    String sql = query.toString();
878    
879                                    Query q = session.createQuery(sql);
880    
881                                    QueryPos qPos = QueryPos.getInstance(q);
882    
883                                    qPos.add(classNameId);
884    
885                                    qPos.add(classPK);
886    
887                                    count = (Long)q.uniqueResult();
888                            }
889                            catch (Exception e) {
890                                    throw processException(e);
891                            }
892                            finally {
893                                    if (count == null) {
894                                            count = Long.valueOf(0);
895                                    }
896    
897                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
898                                            count);
899    
900                                    closeSession(session);
901                            }
902                    }
903    
904                    return count.intValue();
905            }
906    
907            public int countByU_C_C(long userId, long classNameId, long classPK)
908                    throws SystemException {
909                    Object[] finderArgs = new Object[] { userId, classNameId, classPK };
910    
911                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_C_C,
912                                    finderArgs, this);
913    
914                    if (count == null) {
915                            Session session = null;
916    
917                            try {
918                                    session = openSession();
919    
920                                    StringBundler query = new StringBundler(4);
921    
922                                    query.append(_SQL_COUNT_RATINGSENTRY_WHERE);
923    
924                                    query.append(_FINDER_COLUMN_U_C_C_USERID_2);
925    
926                                    query.append(_FINDER_COLUMN_U_C_C_CLASSNAMEID_2);
927    
928                                    query.append(_FINDER_COLUMN_U_C_C_CLASSPK_2);
929    
930                                    String sql = query.toString();
931    
932                                    Query q = session.createQuery(sql);
933    
934                                    QueryPos qPos = QueryPos.getInstance(q);
935    
936                                    qPos.add(userId);
937    
938                                    qPos.add(classNameId);
939    
940                                    qPos.add(classPK);
941    
942                                    count = (Long)q.uniqueResult();
943                            }
944                            catch (Exception e) {
945                                    throw processException(e);
946                            }
947                            finally {
948                                    if (count == null) {
949                                            count = Long.valueOf(0);
950                                    }
951    
952                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_C_C,
953                                            finderArgs, count);
954    
955                                    closeSession(session);
956                            }
957                    }
958    
959                    return count.intValue();
960            }
961    
962            public int countAll() throws SystemException {
963                    Object[] finderArgs = new Object[0];
964    
965                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
966                                    finderArgs, this);
967    
968                    if (count == null) {
969                            Session session = null;
970    
971                            try {
972                                    session = openSession();
973    
974                                    Query q = session.createQuery(_SQL_COUNT_RATINGSENTRY);
975    
976                                    count = (Long)q.uniqueResult();
977                            }
978                            catch (Exception e) {
979                                    throw processException(e);
980                            }
981                            finally {
982                                    if (count == null) {
983                                            count = Long.valueOf(0);
984                                    }
985    
986                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
987                                            count);
988    
989                                    closeSession(session);
990                            }
991                    }
992    
993                    return count.intValue();
994            }
995    
996            public void afterPropertiesSet() {
997                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
998                                            com.liferay.portal.util.PropsUtil.get(
999                                                    "value.object.listener.com.liferay.portlet.ratings.model.RatingsEntry")));
1000    
1001                    if (listenerClassNames.length > 0) {
1002                            try {
1003                                    List<ModelListener<RatingsEntry>> listenersList = new ArrayList<ModelListener<RatingsEntry>>();
1004    
1005                                    for (String listenerClassName : listenerClassNames) {
1006                                            listenersList.add((ModelListener<RatingsEntry>)InstanceFactory.newInstance(
1007                                                            listenerClassName));
1008                                    }
1009    
1010                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1011                            }
1012                            catch (Exception e) {
1013                                    _log.error(e);
1014                            }
1015                    }
1016            }
1017    
1018            @BeanReference(type = RatingsEntryPersistence.class)
1019            protected RatingsEntryPersistence ratingsEntryPersistence;
1020            @BeanReference(type = RatingsStatsPersistence.class)
1021            protected RatingsStatsPersistence ratingsStatsPersistence;
1022            @BeanReference(type = ResourcePersistence.class)
1023            protected ResourcePersistence resourcePersistence;
1024            @BeanReference(type = UserPersistence.class)
1025            protected UserPersistence userPersistence;
1026            @BeanReference(type = BlogsEntryPersistence.class)
1027            protected BlogsEntryPersistence blogsEntryPersistence;
1028            @BeanReference(type = BlogsStatsUserPersistence.class)
1029            protected BlogsStatsUserPersistence blogsStatsUserPersistence;
1030            @BeanReference(type = SocialEquityLogPersistence.class)
1031            protected SocialEquityLogPersistence socialEquityLogPersistence;
1032            private static final String _SQL_SELECT_RATINGSENTRY = "SELECT ratingsEntry FROM RatingsEntry ratingsEntry";
1033            private static final String _SQL_SELECT_RATINGSENTRY_WHERE = "SELECT ratingsEntry FROM RatingsEntry ratingsEntry WHERE ";
1034            private static final String _SQL_COUNT_RATINGSENTRY = "SELECT COUNT(ratingsEntry) FROM RatingsEntry ratingsEntry";
1035            private static final String _SQL_COUNT_RATINGSENTRY_WHERE = "SELECT COUNT(ratingsEntry) FROM RatingsEntry ratingsEntry WHERE ";
1036            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "ratingsEntry.classNameId = ? AND ";
1037            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "ratingsEntry.classPK = ?";
1038            private static final String _FINDER_COLUMN_U_C_C_USERID_2 = "ratingsEntry.userId = ? AND ";
1039            private static final String _FINDER_COLUMN_U_C_C_CLASSNAMEID_2 = "ratingsEntry.classNameId = ? AND ";
1040            private static final String _FINDER_COLUMN_U_C_C_CLASSPK_2 = "ratingsEntry.classPK = ?";
1041            private static final String _ORDER_BY_ENTITY_ALIAS = "ratingsEntry.";
1042            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No RatingsEntry exists with the primary key ";
1043            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No RatingsEntry exists with the key {";
1044            private static Log _log = LogFactoryUtil.getLog(RatingsEntryPersistenceImpl.class);
1045    }