1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.ratings.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.annotation.BeanReference;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderPath;
32  import com.liferay.portal.kernel.dao.orm.Query;
33  import com.liferay.portal.kernel.dao.orm.QueryPos;
34  import com.liferay.portal.kernel.dao.orm.QueryUtil;
35  import com.liferay.portal.kernel.dao.orm.Session;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.OrderByComparator;
40  import com.liferay.portal.kernel.util.StringPool;
41  import com.liferay.portal.kernel.util.StringUtil;
42  import com.liferay.portal.model.ModelListener;
43  import com.liferay.portal.service.persistence.BatchSessionUtil;
44  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
45  
46  import com.liferay.portlet.ratings.NoSuchEntryException;
47  import com.liferay.portlet.ratings.model.RatingsEntry;
48  import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
49  import com.liferay.portlet.ratings.model.impl.RatingsEntryModelImpl;
50  
51  import java.util.ArrayList;
52  import java.util.Collections;
53  import java.util.List;
54  
55  /**
56   * <a href="RatingsEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * <p>
59   * ServiceBuilder generated this class. Modifications in this class will be
60   * overwritten the next time is generated.
61   * </p>
62   *
63   * @author    Brian Wing Shun Chan
64   * @see       RatingsEntryPersistence
65   * @see       RatingsEntryUtil
66   * @generated
67   */
68  public class RatingsEntryPersistenceImpl extends BasePersistenceImpl
69      implements RatingsEntryPersistence {
70      public static final String FINDER_CLASS_NAME_ENTITY = RatingsEntryImpl.class.getName();
71      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
72          ".List";
73      public static final FinderPath FINDER_PATH_FIND_BY_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
74              RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
75              "findByC_C",
76              new String[] { Long.class.getName(), Long.class.getName() });
77      public static final FinderPath FINDER_PATH_FIND_BY_OBC_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
78              RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
79              "findByC_C",
80              new String[] {
81                  Long.class.getName(), Long.class.getName(),
82                  
83              "java.lang.Integer", "java.lang.Integer",
84                  "com.liferay.portal.kernel.util.OrderByComparator"
85              });
86      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
87              RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
88              "countByC_C",
89              new String[] { Long.class.getName(), Long.class.getName() });
90      public static final FinderPath FINDER_PATH_FETCH_BY_U_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
91              RatingsEntryModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_ENTITY, "fetchByU_C_C",
93              new String[] {
94                  Long.class.getName(), Long.class.getName(), Long.class.getName()
95              });
96      public static final FinderPath FINDER_PATH_COUNT_BY_U_C_C = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
97              RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
98              "countByU_C_C",
99              new String[] {
100                 Long.class.getName(), Long.class.getName(), Long.class.getName()
101             });
102     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
103             RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
104             "findAll", new String[0]);
105     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
106             RatingsEntryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
107             "countAll", new String[0]);
108 
109     public void cacheResult(RatingsEntry ratingsEntry) {
110         EntityCacheUtil.putResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
111             RatingsEntryImpl.class, ratingsEntry.getPrimaryKey(), ratingsEntry);
112 
113         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
114             new Object[] {
115                 new Long(ratingsEntry.getUserId()),
116                 new Long(ratingsEntry.getClassNameId()),
117                 new Long(ratingsEntry.getClassPK())
118             }, ratingsEntry);
119     }
120 
121     public void cacheResult(List<RatingsEntry> ratingsEntries) {
122         for (RatingsEntry ratingsEntry : ratingsEntries) {
123             if (EntityCacheUtil.getResult(
124                         RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
125                         RatingsEntryImpl.class, ratingsEntry.getPrimaryKey(),
126                         this) == null) {
127                 cacheResult(ratingsEntry);
128             }
129         }
130     }
131 
132     public void clearCache() {
133         CacheRegistry.clear(RatingsEntryImpl.class.getName());
134         EntityCacheUtil.clearCache(RatingsEntryImpl.class.getName());
135         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
136         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
137     }
138 
139     public RatingsEntry create(long entryId) {
140         RatingsEntry ratingsEntry = new RatingsEntryImpl();
141 
142         ratingsEntry.setNew(true);
143         ratingsEntry.setPrimaryKey(entryId);
144 
145         return ratingsEntry;
146     }
147 
148     public RatingsEntry remove(long entryId)
149         throws NoSuchEntryException, SystemException {
150         Session session = null;
151 
152         try {
153             session = openSession();
154 
155             RatingsEntry ratingsEntry = (RatingsEntry)session.get(RatingsEntryImpl.class,
156                     new Long(entryId));
157 
158             if (ratingsEntry == null) {
159                 if (_log.isWarnEnabled()) {
160                     _log.warn("No RatingsEntry exists with the primary key " +
161                         entryId);
162                 }
163 
164                 throw new NoSuchEntryException(
165                     "No RatingsEntry exists with the primary key " + entryId);
166             }
167 
168             return remove(ratingsEntry);
169         }
170         catch (NoSuchEntryException nsee) {
171             throw nsee;
172         }
173         catch (Exception e) {
174             throw processException(e);
175         }
176         finally {
177             closeSession(session);
178         }
179     }
180 
181     public RatingsEntry remove(RatingsEntry ratingsEntry)
182         throws SystemException {
183         for (ModelListener<RatingsEntry> listener : listeners) {
184             listener.onBeforeRemove(ratingsEntry);
185         }
186 
187         ratingsEntry = removeImpl(ratingsEntry);
188 
189         for (ModelListener<RatingsEntry> listener : listeners) {
190             listener.onAfterRemove(ratingsEntry);
191         }
192 
193         return ratingsEntry;
194     }
195 
196     protected RatingsEntry removeImpl(RatingsEntry ratingsEntry)
197         throws SystemException {
198         ratingsEntry = toUnwrappedModel(ratingsEntry);
199 
200         Session session = null;
201 
202         try {
203             session = openSession();
204 
205             if (ratingsEntry.isCachedModel() || BatchSessionUtil.isEnabled()) {
206                 Object staleObject = session.get(RatingsEntryImpl.class,
207                         ratingsEntry.getPrimaryKeyObj());
208 
209                 if (staleObject != null) {
210                     session.evict(staleObject);
211                 }
212             }
213 
214             session.delete(ratingsEntry);
215 
216             session.flush();
217         }
218         catch (Exception e) {
219             throw processException(e);
220         }
221         finally {
222             closeSession(session);
223         }
224 
225         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
226 
227         RatingsEntryModelImpl ratingsEntryModelImpl = (RatingsEntryModelImpl)ratingsEntry;
228 
229         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_C_C,
230             new Object[] {
231                 new Long(ratingsEntryModelImpl.getOriginalUserId()),
232                 new Long(ratingsEntryModelImpl.getOriginalClassNameId()),
233                 new Long(ratingsEntryModelImpl.getOriginalClassPK())
234             });
235 
236         EntityCacheUtil.removeResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
237             RatingsEntryImpl.class, ratingsEntry.getPrimaryKey());
238 
239         return ratingsEntry;
240     }
241 
242     /**
243      * @deprecated Use {@link #update(RatingsEntry, boolean merge)}.
244      */
245     public RatingsEntry update(RatingsEntry ratingsEntry)
246         throws SystemException {
247         if (_log.isWarnEnabled()) {
248             _log.warn(
249                 "Using the deprecated update(RatingsEntry ratingsEntry) method. Use update(RatingsEntry ratingsEntry, boolean merge) instead.");
250         }
251 
252         return update(ratingsEntry, false);
253     }
254 
255     /**
256      * Add, update, or merge, the entity. This method also calls the model
257      * listeners to trigger the proper events associated with adding, deleting,
258      * or updating an entity.
259      *
260      * @param  ratingsEntry the entity to add, update, or merge
261      * @param  merge boolean value for whether to merge the entity. The default
262      *         value is false. Setting merge to true is more expensive and
263      *         should only be true when ratingsEntry is transient. See
264      *         LEP-5473 for a detailed discussion of this method.
265      * @return the entity that was added, updated, or merged
266      */
267     public RatingsEntry update(RatingsEntry ratingsEntry, boolean merge)
268         throws SystemException {
269         boolean isNew = ratingsEntry.isNew();
270 
271         for (ModelListener<RatingsEntry> listener : listeners) {
272             if (isNew) {
273                 listener.onBeforeCreate(ratingsEntry);
274             }
275             else {
276                 listener.onBeforeUpdate(ratingsEntry);
277             }
278         }
279 
280         ratingsEntry = updateImpl(ratingsEntry, merge);
281 
282         for (ModelListener<RatingsEntry> listener : listeners) {
283             if (isNew) {
284                 listener.onAfterCreate(ratingsEntry);
285             }
286             else {
287                 listener.onAfterUpdate(ratingsEntry);
288             }
289         }
290 
291         return ratingsEntry;
292     }
293 
294     public RatingsEntry updateImpl(
295         com.liferay.portlet.ratings.model.RatingsEntry ratingsEntry,
296         boolean merge) throws SystemException {
297         ratingsEntry = toUnwrappedModel(ratingsEntry);
298 
299         boolean isNew = ratingsEntry.isNew();
300 
301         RatingsEntryModelImpl ratingsEntryModelImpl = (RatingsEntryModelImpl)ratingsEntry;
302 
303         Session session = null;
304 
305         try {
306             session = openSession();
307 
308             BatchSessionUtil.update(session, ratingsEntry, merge);
309 
310             ratingsEntry.setNew(false);
311         }
312         catch (Exception e) {
313             throw processException(e);
314         }
315         finally {
316             closeSession(session);
317         }
318 
319         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
320 
321         EntityCacheUtil.putResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
322             RatingsEntryImpl.class, ratingsEntry.getPrimaryKey(), ratingsEntry);
323 
324         if (!isNew &&
325                 ((ratingsEntry.getUserId() != ratingsEntryModelImpl.getOriginalUserId()) ||
326                 (ratingsEntry.getClassNameId() != ratingsEntryModelImpl.getOriginalClassNameId()) ||
327                 (ratingsEntry.getClassPK() != ratingsEntryModelImpl.getOriginalClassPK()))) {
328             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_U_C_C,
329                 new Object[] {
330                     new Long(ratingsEntryModelImpl.getOriginalUserId()),
331                     new Long(ratingsEntryModelImpl.getOriginalClassNameId()),
332                     new Long(ratingsEntryModelImpl.getOriginalClassPK())
333                 });
334         }
335 
336         if (isNew ||
337                 ((ratingsEntry.getUserId() != ratingsEntryModelImpl.getOriginalUserId()) ||
338                 (ratingsEntry.getClassNameId() != ratingsEntryModelImpl.getOriginalClassNameId()) ||
339                 (ratingsEntry.getClassPK() != ratingsEntryModelImpl.getOriginalClassPK()))) {
340             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
341                 new Object[] {
342                     new Long(ratingsEntry.getUserId()),
343                     new Long(ratingsEntry.getClassNameId()),
344                     new Long(ratingsEntry.getClassPK())
345                 }, ratingsEntry);
346         }
347 
348         return ratingsEntry;
349     }
350 
351     protected RatingsEntry toUnwrappedModel(RatingsEntry ratingsEntry) {
352         if (ratingsEntry instanceof RatingsEntryImpl) {
353             return ratingsEntry;
354         }
355 
356         RatingsEntryImpl ratingsEntryImpl = new RatingsEntryImpl();
357 
358         ratingsEntryImpl.setNew(ratingsEntry.isNew());
359         ratingsEntryImpl.setPrimaryKey(ratingsEntry.getPrimaryKey());
360 
361         ratingsEntryImpl.setEntryId(ratingsEntry.getEntryId());
362         ratingsEntryImpl.setCompanyId(ratingsEntry.getCompanyId());
363         ratingsEntryImpl.setUserId(ratingsEntry.getUserId());
364         ratingsEntryImpl.setUserName(ratingsEntry.getUserName());
365         ratingsEntryImpl.setCreateDate(ratingsEntry.getCreateDate());
366         ratingsEntryImpl.setModifiedDate(ratingsEntry.getModifiedDate());
367         ratingsEntryImpl.setClassNameId(ratingsEntry.getClassNameId());
368         ratingsEntryImpl.setClassPK(ratingsEntry.getClassPK());
369         ratingsEntryImpl.setScore(ratingsEntry.getScore());
370 
371         return ratingsEntryImpl;
372     }
373 
374     public RatingsEntry findByPrimaryKey(long entryId)
375         throws NoSuchEntryException, SystemException {
376         RatingsEntry ratingsEntry = fetchByPrimaryKey(entryId);
377 
378         if (ratingsEntry == null) {
379             if (_log.isWarnEnabled()) {
380                 _log.warn("No RatingsEntry exists with the primary key " +
381                     entryId);
382             }
383 
384             throw new NoSuchEntryException(
385                 "No RatingsEntry exists with the primary key " + entryId);
386         }
387 
388         return ratingsEntry;
389     }
390 
391     public RatingsEntry fetchByPrimaryKey(long entryId)
392         throws SystemException {
393         RatingsEntry ratingsEntry = (RatingsEntry)EntityCacheUtil.getResult(RatingsEntryModelImpl.ENTITY_CACHE_ENABLED,
394                 RatingsEntryImpl.class, entryId, this);
395 
396         if (ratingsEntry == null) {
397             Session session = null;
398 
399             try {
400                 session = openSession();
401 
402                 ratingsEntry = (RatingsEntry)session.get(RatingsEntryImpl.class,
403                         new Long(entryId));
404             }
405             catch (Exception e) {
406                 throw processException(e);
407             }
408             finally {
409                 if (ratingsEntry != null) {
410                     cacheResult(ratingsEntry);
411                 }
412 
413                 closeSession(session);
414             }
415         }
416 
417         return ratingsEntry;
418     }
419 
420     public List<RatingsEntry> findByC_C(long classNameId, long classPK)
421         throws SystemException {
422         Object[] finderArgs = new Object[] {
423                 new Long(classNameId), new Long(classPK)
424             };
425 
426         List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_C,
427                 finderArgs, this);
428 
429         if (list == null) {
430             Session session = null;
431 
432             try {
433                 session = openSession();
434 
435                 StringBuilder query = new StringBuilder();
436 
437                 query.append(
438                     "SELECT ratingsEntry FROM RatingsEntry ratingsEntry WHERE ");
439 
440                 query.append("ratingsEntry.classNameId = ?");
441 
442                 query.append(" AND ");
443 
444                 query.append("ratingsEntry.classPK = ?");
445 
446                 query.append(" ");
447 
448                 Query q = session.createQuery(query.toString());
449 
450                 QueryPos qPos = QueryPos.getInstance(q);
451 
452                 qPos.add(classNameId);
453 
454                 qPos.add(classPK);
455 
456                 list = q.list();
457             }
458             catch (Exception e) {
459                 throw processException(e);
460             }
461             finally {
462                 if (list == null) {
463                     list = new ArrayList<RatingsEntry>();
464                 }
465 
466                 cacheResult(list);
467 
468                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_C, finderArgs,
469                     list);
470 
471                 closeSession(session);
472             }
473         }
474 
475         return list;
476     }
477 
478     public List<RatingsEntry> findByC_C(long classNameId, long classPK,
479         int start, int end) throws SystemException {
480         return findByC_C(classNameId, classPK, start, end, null);
481     }
482 
483     public List<RatingsEntry> findByC_C(long classNameId, long classPK,
484         int start, int end, OrderByComparator obc) throws SystemException {
485         Object[] finderArgs = new Object[] {
486                 new Long(classNameId), new Long(classPK),
487                 
488                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
489             };
490 
491         List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_C_C,
492                 finderArgs, this);
493 
494         if (list == null) {
495             Session session = null;
496 
497             try {
498                 session = openSession();
499 
500                 StringBuilder query = new StringBuilder();
501 
502                 query.append(
503                     "SELECT ratingsEntry FROM RatingsEntry ratingsEntry WHERE ");
504 
505                 query.append("ratingsEntry.classNameId = ?");
506 
507                 query.append(" AND ");
508 
509                 query.append("ratingsEntry.classPK = ?");
510 
511                 query.append(" ");
512 
513                 if (obc != null) {
514                     query.append("ORDER BY ");
515 
516                     String[] orderByFields = obc.getOrderByFields();
517 
518                     for (int i = 0; i < orderByFields.length; i++) {
519                         query.append("ratingsEntry.");
520                         query.append(orderByFields[i]);
521 
522                         if (obc.isAscending()) {
523                             query.append(" ASC");
524                         }
525                         else {
526                             query.append(" DESC");
527                         }
528 
529                         if ((i + 1) < orderByFields.length) {
530                             query.append(", ");
531                         }
532                     }
533                 }
534 
535                 Query q = session.createQuery(query.toString());
536 
537                 QueryPos qPos = QueryPos.getInstance(q);
538 
539                 qPos.add(classNameId);
540 
541                 qPos.add(classPK);
542 
543                 list = (List<RatingsEntry>)QueryUtil.list(q, getDialect(),
544                         start, end);
545             }
546             catch (Exception e) {
547                 throw processException(e);
548             }
549             finally {
550                 if (list == null) {
551                     list = new ArrayList<RatingsEntry>();
552                 }
553 
554                 cacheResult(list);
555 
556                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_C,
557                     finderArgs, list);
558 
559                 closeSession(session);
560             }
561         }
562 
563         return list;
564     }
565 
566     public RatingsEntry findByC_C_First(long classNameId, long classPK,
567         OrderByComparator obc) throws NoSuchEntryException, SystemException {
568         List<RatingsEntry> list = findByC_C(classNameId, classPK, 0, 1, obc);
569 
570         if (list.isEmpty()) {
571             StringBuilder msg = new StringBuilder();
572 
573             msg.append("No RatingsEntry exists with the key {");
574 
575             msg.append("classNameId=" + classNameId);
576 
577             msg.append(", ");
578             msg.append("classPK=" + classPK);
579 
580             msg.append(StringPool.CLOSE_CURLY_BRACE);
581 
582             throw new NoSuchEntryException(msg.toString());
583         }
584         else {
585             return list.get(0);
586         }
587     }
588 
589     public RatingsEntry findByC_C_Last(long classNameId, long classPK,
590         OrderByComparator obc) throws NoSuchEntryException, SystemException {
591         int count = countByC_C(classNameId, classPK);
592 
593         List<RatingsEntry> list = findByC_C(classNameId, classPK, count - 1,
594                 count, obc);
595 
596         if (list.isEmpty()) {
597             StringBuilder msg = new StringBuilder();
598 
599             msg.append("No RatingsEntry exists with the key {");
600 
601             msg.append("classNameId=" + classNameId);
602 
603             msg.append(", ");
604             msg.append("classPK=" + classPK);
605 
606             msg.append(StringPool.CLOSE_CURLY_BRACE);
607 
608             throw new NoSuchEntryException(msg.toString());
609         }
610         else {
611             return list.get(0);
612         }
613     }
614 
615     public RatingsEntry[] findByC_C_PrevAndNext(long entryId, long classNameId,
616         long classPK, OrderByComparator obc)
617         throws NoSuchEntryException, SystemException {
618         RatingsEntry ratingsEntry = findByPrimaryKey(entryId);
619 
620         int count = countByC_C(classNameId, classPK);
621 
622         Session session = null;
623 
624         try {
625             session = openSession();
626 
627             StringBuilder query = new StringBuilder();
628 
629             query.append(
630                 "SELECT ratingsEntry FROM RatingsEntry ratingsEntry WHERE ");
631 
632             query.append("ratingsEntry.classNameId = ?");
633 
634             query.append(" AND ");
635 
636             query.append("ratingsEntry.classPK = ?");
637 
638             query.append(" ");
639 
640             if (obc != null) {
641                 query.append("ORDER BY ");
642 
643                 String[] orderByFields = obc.getOrderByFields();
644 
645                 for (int i = 0; i < orderByFields.length; i++) {
646                     query.append("ratingsEntry.");
647                     query.append(orderByFields[i]);
648 
649                     if (obc.isAscending()) {
650                         query.append(" ASC");
651                     }
652                     else {
653                         query.append(" DESC");
654                     }
655 
656                     if ((i + 1) < orderByFields.length) {
657                         query.append(", ");
658                     }
659                 }
660             }
661 
662             Query q = session.createQuery(query.toString());
663 
664             QueryPos qPos = QueryPos.getInstance(q);
665 
666             qPos.add(classNameId);
667 
668             qPos.add(classPK);
669 
670             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
671                     ratingsEntry);
672 
673             RatingsEntry[] array = new RatingsEntryImpl[3];
674 
675             array[0] = (RatingsEntry)objArray[0];
676             array[1] = (RatingsEntry)objArray[1];
677             array[2] = (RatingsEntry)objArray[2];
678 
679             return array;
680         }
681         catch (Exception e) {
682             throw processException(e);
683         }
684         finally {
685             closeSession(session);
686         }
687     }
688 
689     public RatingsEntry findByU_C_C(long userId, long classNameId, long classPK)
690         throws NoSuchEntryException, SystemException {
691         RatingsEntry ratingsEntry = fetchByU_C_C(userId, classNameId, classPK);
692 
693         if (ratingsEntry == null) {
694             StringBuilder msg = new StringBuilder();
695 
696             msg.append("No RatingsEntry exists with the key {");
697 
698             msg.append("userId=" + userId);
699 
700             msg.append(", ");
701             msg.append("classNameId=" + classNameId);
702 
703             msg.append(", ");
704             msg.append("classPK=" + classPK);
705 
706             msg.append(StringPool.CLOSE_CURLY_BRACE);
707 
708             if (_log.isWarnEnabled()) {
709                 _log.warn(msg.toString());
710             }
711 
712             throw new NoSuchEntryException(msg.toString());
713         }
714 
715         return ratingsEntry;
716     }
717 
718     public RatingsEntry fetchByU_C_C(long userId, long classNameId, long classPK)
719         throws SystemException {
720         return fetchByU_C_C(userId, classNameId, classPK, true);
721     }
722 
723     public RatingsEntry fetchByU_C_C(long userId, long classNameId,
724         long classPK, boolean retrieveFromCache) throws SystemException {
725         Object[] finderArgs = new Object[] {
726                 new Long(userId), new Long(classNameId), new Long(classPK)
727             };
728 
729         Object result = null;
730 
731         if (retrieveFromCache) {
732             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_U_C_C,
733                     finderArgs, this);
734         }
735 
736         if (result == null) {
737             Session session = null;
738 
739             try {
740                 session = openSession();
741 
742                 StringBuilder query = new StringBuilder();
743 
744                 query.append(
745                     "SELECT ratingsEntry FROM RatingsEntry ratingsEntry WHERE ");
746 
747                 query.append("ratingsEntry.userId = ?");
748 
749                 query.append(" AND ");
750 
751                 query.append("ratingsEntry.classNameId = ?");
752 
753                 query.append(" AND ");
754 
755                 query.append("ratingsEntry.classPK = ?");
756 
757                 query.append(" ");
758 
759                 Query q = session.createQuery(query.toString());
760 
761                 QueryPos qPos = QueryPos.getInstance(q);
762 
763                 qPos.add(userId);
764 
765                 qPos.add(classNameId);
766 
767                 qPos.add(classPK);
768 
769                 List<RatingsEntry> list = q.list();
770 
771                 result = list;
772 
773                 RatingsEntry ratingsEntry = null;
774 
775                 if (list.isEmpty()) {
776                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
777                         finderArgs, list);
778                 }
779                 else {
780                     ratingsEntry = list.get(0);
781 
782                     cacheResult(ratingsEntry);
783 
784                     if ((ratingsEntry.getUserId() != userId) ||
785                             (ratingsEntry.getClassNameId() != classNameId) ||
786                             (ratingsEntry.getClassPK() != classPK)) {
787                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
788                             finderArgs, ratingsEntry);
789                     }
790                 }
791 
792                 return ratingsEntry;
793             }
794             catch (Exception e) {
795                 throw processException(e);
796             }
797             finally {
798                 if (result == null) {
799                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_U_C_C,
800                         finderArgs, new ArrayList<RatingsEntry>());
801                 }
802 
803                 closeSession(session);
804             }
805         }
806         else {
807             if (result instanceof List<?>) {
808                 return null;
809             }
810             else {
811                 return (RatingsEntry)result;
812             }
813         }
814     }
815 
816     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
817         throws SystemException {
818         Session session = null;
819 
820         try {
821             session = openSession();
822 
823             dynamicQuery.compile(session);
824 
825             return dynamicQuery.list();
826         }
827         catch (Exception e) {
828             throw processException(e);
829         }
830         finally {
831             closeSession(session);
832         }
833     }
834 
835     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
836         int start, int end) throws SystemException {
837         Session session = null;
838 
839         try {
840             session = openSession();
841 
842             dynamicQuery.setLimit(start, end);
843 
844             dynamicQuery.compile(session);
845 
846             return dynamicQuery.list();
847         }
848         catch (Exception e) {
849             throw processException(e);
850         }
851         finally {
852             closeSession(session);
853         }
854     }
855 
856     public List<RatingsEntry> findAll() throws SystemException {
857         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
858     }
859 
860     public List<RatingsEntry> findAll(int start, int end)
861         throws SystemException {
862         return findAll(start, end, null);
863     }
864 
865     public List<RatingsEntry> findAll(int start, int end, OrderByComparator obc)
866         throws SystemException {
867         Object[] finderArgs = new Object[] {
868                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
869             };
870 
871         List<RatingsEntry> list = (List<RatingsEntry>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
872                 finderArgs, this);
873 
874         if (list == null) {
875             Session session = null;
876 
877             try {
878                 session = openSession();
879 
880                 StringBuilder query = new StringBuilder();
881 
882                 query.append(
883                     "SELECT ratingsEntry FROM RatingsEntry ratingsEntry ");
884 
885                 if (obc != null) {
886                     query.append("ORDER BY ");
887 
888                     String[] orderByFields = obc.getOrderByFields();
889 
890                     for (int i = 0; i < orderByFields.length; i++) {
891                         query.append("ratingsEntry.");
892                         query.append(orderByFields[i]);
893 
894                         if (obc.isAscending()) {
895                             query.append(" ASC");
896                         }
897                         else {
898                             query.append(" DESC");
899                         }
900 
901                         if ((i + 1) < orderByFields.length) {
902                             query.append(", ");
903                         }
904                     }
905                 }
906 
907                 Query q = session.createQuery(query.toString());
908 
909                 if (obc == null) {
910                     list = (List<RatingsEntry>)QueryUtil.list(q, getDialect(),
911                             start, end, false);
912 
913                     Collections.sort(list);
914                 }
915                 else {
916                     list = (List<RatingsEntry>)QueryUtil.list(q, getDialect(),
917                             start, end);
918                 }
919             }
920             catch (Exception e) {
921                 throw processException(e);
922             }
923             finally {
924                 if (list == null) {
925                     list = new ArrayList<RatingsEntry>();
926                 }
927 
928                 cacheResult(list);
929 
930                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
931 
932                 closeSession(session);
933             }
934         }
935 
936         return list;
937     }
938 
939     public void removeByC_C(long classNameId, long classPK)
940         throws SystemException {
941         for (RatingsEntry ratingsEntry : findByC_C(classNameId, classPK)) {
942             remove(ratingsEntry);
943         }
944     }
945 
946     public void removeByU_C_C(long userId, long classNameId, long classPK)
947         throws NoSuchEntryException, SystemException {
948         RatingsEntry ratingsEntry = findByU_C_C(userId, classNameId, classPK);
949 
950         remove(ratingsEntry);
951     }
952 
953     public void removeAll() throws SystemException {
954         for (RatingsEntry ratingsEntry : findAll()) {
955             remove(ratingsEntry);
956         }
957     }
958 
959     public int countByC_C(long classNameId, long classPK)
960         throws SystemException {
961         Object[] finderArgs = new Object[] {
962                 new Long(classNameId), new Long(classPK)
963             };
964 
965         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
966                 finderArgs, this);
967 
968         if (count == null) {
969             Session session = null;
970 
971             try {
972                 session = openSession();
973 
974                 StringBuilder query = new StringBuilder();
975 
976                 query.append("SELECT COUNT(ratingsEntry) ");
977                 query.append("FROM RatingsEntry ratingsEntry WHERE ");
978 
979                 query.append("ratingsEntry.classNameId = ?");
980 
981                 query.append(" AND ");
982 
983                 query.append("ratingsEntry.classPK = ?");
984 
985                 query.append(" ");
986 
987                 Query q = session.createQuery(query.toString());
988 
989                 QueryPos qPos = QueryPos.getInstance(q);
990 
991                 qPos.add(classNameId);
992 
993                 qPos.add(classPK);
994 
995                 count = (Long)q.uniqueResult();
996             }
997             catch (Exception e) {
998                 throw processException(e);
999             }
1000            finally {
1001                if (count == null) {
1002                    count = Long.valueOf(0);
1003                }
1004
1005                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1006                    count);
1007
1008                closeSession(session);
1009            }
1010        }
1011
1012        return count.intValue();
1013    }
1014
1015    public int countByU_C_C(long userId, long classNameId, long classPK)
1016        throws SystemException {
1017        Object[] finderArgs = new Object[] {
1018                new Long(userId), new Long(classNameId), new Long(classPK)
1019            };
1020
1021        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_U_C_C,
1022                finderArgs, this);
1023
1024        if (count == null) {
1025            Session session = null;
1026
1027            try {
1028                session = openSession();
1029
1030                StringBuilder query = new StringBuilder();
1031
1032                query.append("SELECT COUNT(ratingsEntry) ");
1033                query.append("FROM RatingsEntry ratingsEntry WHERE ");
1034
1035                query.append("ratingsEntry.userId = ?");
1036
1037                query.append(" AND ");
1038
1039                query.append("ratingsEntry.classNameId = ?");
1040
1041                query.append(" AND ");
1042
1043                query.append("ratingsEntry.classPK = ?");
1044
1045                query.append(" ");
1046
1047                Query q = session.createQuery(query.toString());
1048
1049                QueryPos qPos = QueryPos.getInstance(q);
1050
1051                qPos.add(userId);
1052
1053                qPos.add(classNameId);
1054
1055                qPos.add(classPK);
1056
1057                count = (Long)q.uniqueResult();
1058            }
1059            catch (Exception e) {
1060                throw processException(e);
1061            }
1062            finally {
1063                if (count == null) {
1064                    count = Long.valueOf(0);
1065                }
1066
1067                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_U_C_C,
1068                    finderArgs, count);
1069
1070                closeSession(session);
1071            }
1072        }
1073
1074        return count.intValue();
1075    }
1076
1077    public int countAll() throws SystemException {
1078        Object[] finderArgs = new Object[0];
1079
1080        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1081                finderArgs, this);
1082
1083        if (count == null) {
1084            Session session = null;
1085
1086            try {
1087                session = openSession();
1088
1089                Query q = session.createQuery(
1090                        "SELECT COUNT(ratingsEntry) FROM RatingsEntry ratingsEntry");
1091
1092                count = (Long)q.uniqueResult();
1093            }
1094            catch (Exception e) {
1095                throw processException(e);
1096            }
1097            finally {
1098                if (count == null) {
1099                    count = Long.valueOf(0);
1100                }
1101
1102                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1103                    count);
1104
1105                closeSession(session);
1106            }
1107        }
1108
1109        return count.intValue();
1110    }
1111
1112    public void afterPropertiesSet() {
1113        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1114                    com.liferay.portal.util.PropsUtil.get(
1115                        "value.object.listener.com.liferay.portlet.ratings.model.RatingsEntry")));
1116
1117        if (listenerClassNames.length > 0) {
1118            try {
1119                List<ModelListener<RatingsEntry>> listenersList = new ArrayList<ModelListener<RatingsEntry>>();
1120
1121                for (String listenerClassName : listenerClassNames) {
1122                    listenersList.add((ModelListener<RatingsEntry>)Class.forName(
1123                            listenerClassName).newInstance());
1124                }
1125
1126                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1127            }
1128            catch (Exception e) {
1129                _log.error(e);
1130            }
1131        }
1132    }
1133
1134    @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence.impl")
1135    protected com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence ratingsEntryPersistence;
1136    @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence.impl")
1137    protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
1138    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1139    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1140    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1141    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1142    @BeanReference(name = "com.liferay.portlet.blogs.service.persistence.BlogsEntryPersistence.impl")
1143    protected com.liferay.portlet.blogs.service.persistence.BlogsEntryPersistence blogsEntryPersistence;
1144    @BeanReference(name = "com.liferay.portlet.blogs.service.persistence.BlogsStatsUserPersistence.impl")
1145    protected com.liferay.portlet.blogs.service.persistence.BlogsStatsUserPersistence blogsStatsUserPersistence;
1146    private static Log _log = LogFactoryUtil.getLog(RatingsEntryPersistenceImpl.class);
1147}