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