1   /**
2    * Copyright (c) 2000-2008 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.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  import com.liferay.portal.util.PropsUtil;
38  
39  import com.liferay.portlet.ratings.NoSuchEntryException;
40  import com.liferay.portlet.ratings.model.RatingsEntry;
41  import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
42  import com.liferay.portlet.ratings.model.impl.RatingsEntryModelImpl;
43  
44  import com.liferay.util.dao.hibernate.QueryUtil;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.hibernate.Query;
50  import org.hibernate.Session;
51  
52  import java.util.ArrayList;
53  import java.util.Collections;
54  import java.util.Iterator;
55  import java.util.List;
56  
57  /**
58   * <a href="RatingsEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class RatingsEntryPersistenceImpl extends BasePersistence
64      implements RatingsEntryPersistence {
65      public RatingsEntry create(long entryId) {
66          RatingsEntry ratingsEntry = new RatingsEntryImpl();
67  
68          ratingsEntry.setNew(true);
69          ratingsEntry.setPrimaryKey(entryId);
70  
71          return ratingsEntry;
72      }
73  
74      public RatingsEntry remove(long entryId)
75          throws NoSuchEntryException, SystemException {
76          Session session = null;
77  
78          try {
79              session = openSession();
80  
81              RatingsEntry ratingsEntry = (RatingsEntry)session.get(RatingsEntryImpl.class,
82                      new Long(entryId));
83  
84              if (ratingsEntry == null) {
85                  if (_log.isWarnEnabled()) {
86                      _log.warn("No RatingsEntry exists with the primary key " +
87                          entryId);
88                  }
89  
90                  throw new NoSuchEntryException(
91                      "No RatingsEntry exists with the primary key " + entryId);
92              }
93  
94              return remove(ratingsEntry);
95          }
96          catch (NoSuchEntryException nsee) {
97              throw nsee;
98          }
99          catch (Exception e) {
100             throw HibernateUtil.processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public RatingsEntry remove(RatingsEntry ratingsEntry)
108         throws SystemException {
109         if (_listeners != null) {
110             for (ModelListener listener : _listeners) {
111                 listener.onBeforeRemove(ratingsEntry);
112             }
113         }
114 
115         ratingsEntry = removeImpl(ratingsEntry);
116 
117         if (_listeners != null) {
118             for (ModelListener listener : _listeners) {
119                 listener.onAfterRemove(ratingsEntry);
120             }
121         }
122 
123         return ratingsEntry;
124     }
125 
126     protected RatingsEntry removeImpl(RatingsEntry ratingsEntry)
127         throws SystemException {
128         Session session = null;
129 
130         try {
131             session = openSession();
132 
133             session.delete(ratingsEntry);
134 
135             session.flush();
136 
137             return ratingsEntry;
138         }
139         catch (Exception e) {
140             throw HibernateUtil.processException(e);
141         }
142         finally {
143             closeSession(session);
144 
145             FinderCache.clearCache(RatingsEntry.class.getName());
146         }
147     }
148 
149     /**
150      * @deprecated Use <code>update(RatingsEntry ratingsEntry, boolean merge)</code>.
151      */
152     public RatingsEntry update(RatingsEntry ratingsEntry)
153         throws SystemException {
154         if (_log.isWarnEnabled()) {
155             _log.warn(
156                 "Using the deprecated update(RatingsEntry ratingsEntry) method. Use update(RatingsEntry ratingsEntry, boolean merge) instead.");
157         }
158 
159         return update(ratingsEntry, false);
160     }
161 
162     /**
163      * Add, update, or merge, the entity. This method also calls the model
164      * listeners to trigger the proper events associated with adding, deleting,
165      * or updating an entity.
166      *
167      * @param        ratingsEntry the entity to add, update, or merge
168      * @param        merge boolean value for whether to merge the entity. The
169      *                default value is false. Setting merge to true is more
170      *                expensive and should only be true when ratingsEntry is
171      *                transient. See LEP-5473 for a detailed discussion of this
172      *                method.
173      * @return        true if the portlet can be displayed via Ajax
174      */
175     public RatingsEntry update(RatingsEntry ratingsEntry, boolean merge)
176         throws SystemException {
177         boolean isNew = ratingsEntry.isNew();
178 
179         if (_listeners != null) {
180             for (ModelListener listener : _listeners) {
181                 if (isNew) {
182                     listener.onBeforeCreate(ratingsEntry);
183                 }
184                 else {
185                     listener.onBeforeUpdate(ratingsEntry);
186                 }
187             }
188         }
189 
190         ratingsEntry = updateImpl(ratingsEntry, merge);
191 
192         if (_listeners != null) {
193             for (ModelListener listener : _listeners) {
194                 if (isNew) {
195                     listener.onAfterCreate(ratingsEntry);
196                 }
197                 else {
198                     listener.onAfterUpdate(ratingsEntry);
199                 }
200             }
201         }
202 
203         return ratingsEntry;
204     }
205 
206     public RatingsEntry updateImpl(
207         com.liferay.portlet.ratings.model.RatingsEntry ratingsEntry,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             if (merge) {
215                 session.merge(ratingsEntry);
216             }
217             else {
218                 if (ratingsEntry.isNew()) {
219                     session.save(ratingsEntry);
220                 }
221             }
222 
223             session.flush();
224 
225             ratingsEntry.setNew(false);
226 
227             return ratingsEntry;
228         }
229         catch (Exception e) {
230             throw HibernateUtil.processException(e);
231         }
232         finally {
233             closeSession(session);
234 
235             FinderCache.clearCache(RatingsEntry.class.getName());
236         }
237     }
238 
239     public RatingsEntry findByPrimaryKey(long entryId)
240         throws NoSuchEntryException, SystemException {
241         RatingsEntry ratingsEntry = fetchByPrimaryKey(entryId);
242 
243         if (ratingsEntry == null) {
244             if (_log.isWarnEnabled()) {
245                 _log.warn("No RatingsEntry exists with the primary key " +
246                     entryId);
247             }
248 
249             throw new NoSuchEntryException(
250                 "No RatingsEntry exists with the primary key " + entryId);
251         }
252 
253         return ratingsEntry;
254     }
255 
256     public RatingsEntry fetchByPrimaryKey(long entryId)
257         throws SystemException {
258         Session session = null;
259 
260         try {
261             session = openSession();
262 
263             return (RatingsEntry)session.get(RatingsEntryImpl.class,
264                 new Long(entryId));
265         }
266         catch (Exception e) {
267             throw HibernateUtil.processException(e);
268         }
269         finally {
270             closeSession(session);
271         }
272     }
273 
274     public List<RatingsEntry> findByC_C(long classNameId, long classPK)
275         throws SystemException {
276         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
277         String finderClassName = RatingsEntry.class.getName();
278         String finderMethodName = "findByC_C";
279         String[] finderParams = new String[] {
280                 Long.class.getName(), Long.class.getName()
281             };
282         Object[] finderArgs = new Object[] {
283                 new Long(classNameId), new Long(classPK)
284             };
285 
286         Object result = null;
287 
288         if (finderClassNameCacheEnabled) {
289             result = FinderCache.getResult(finderClassName, finderMethodName,
290                     finderParams, finderArgs, getSessionFactory());
291         }
292 
293         if (result == null) {
294             Session session = null;
295 
296             try {
297                 session = openSession();
298 
299                 StringMaker query = new StringMaker();
300 
301                 query.append(
302                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
303 
304                 query.append("classNameId = ?");
305 
306                 query.append(" AND ");
307 
308                 query.append("classPK = ?");
309 
310                 query.append(" ");
311 
312                 Query q = session.createQuery(query.toString());
313 
314                 int queryPos = 0;
315 
316                 q.setLong(queryPos++, classNameId);
317 
318                 q.setLong(queryPos++, classPK);
319 
320                 List<RatingsEntry> list = q.list();
321 
322                 FinderCache.putResult(finderClassNameCacheEnabled,
323                     finderClassName, finderMethodName, finderParams,
324                     finderArgs, list);
325 
326                 return list;
327             }
328             catch (Exception e) {
329                 throw HibernateUtil.processException(e);
330             }
331             finally {
332                 closeSession(session);
333             }
334         }
335         else {
336             return (List<RatingsEntry>)result;
337         }
338     }
339 
340     public List<RatingsEntry> findByC_C(long classNameId, long classPK,
341         int begin, int end) throws SystemException {
342         return findByC_C(classNameId, classPK, begin, end, null);
343     }
344 
345     public List<RatingsEntry> findByC_C(long classNameId, long classPK,
346         int begin, int end, OrderByComparator obc) throws SystemException {
347         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
348         String finderClassName = RatingsEntry.class.getName();
349         String finderMethodName = "findByC_C";
350         String[] finderParams = new String[] {
351                 Long.class.getName(), Long.class.getName(),
352                 
353                 "java.lang.Integer", "java.lang.Integer",
354                 "com.liferay.portal.kernel.util.OrderByComparator"
355             };
356         Object[] finderArgs = new Object[] {
357                 new Long(classNameId), new Long(classPK),
358                 
359                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
360             };
361 
362         Object result = null;
363 
364         if (finderClassNameCacheEnabled) {
365             result = FinderCache.getResult(finderClassName, finderMethodName,
366                     finderParams, finderArgs, getSessionFactory());
367         }
368 
369         if (result == null) {
370             Session session = null;
371 
372             try {
373                 session = openSession();
374 
375                 StringMaker query = new StringMaker();
376 
377                 query.append(
378                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
379 
380                 query.append("classNameId = ?");
381 
382                 query.append(" AND ");
383 
384                 query.append("classPK = ?");
385 
386                 query.append(" ");
387 
388                 if (obc != null) {
389                     query.append("ORDER BY ");
390                     query.append(obc.getOrderBy());
391                 }
392 
393                 Query q = session.createQuery(query.toString());
394 
395                 int queryPos = 0;
396 
397                 q.setLong(queryPos++, classNameId);
398 
399                 q.setLong(queryPos++, classPK);
400 
401                 List<RatingsEntry> list = (List<RatingsEntry>)QueryUtil.list(q,
402                         getDialect(), begin, end);
403 
404                 FinderCache.putResult(finderClassNameCacheEnabled,
405                     finderClassName, finderMethodName, finderParams,
406                     finderArgs, list);
407 
408                 return list;
409             }
410             catch (Exception e) {
411                 throw HibernateUtil.processException(e);
412             }
413             finally {
414                 closeSession(session);
415             }
416         }
417         else {
418             return (List<RatingsEntry>)result;
419         }
420     }
421 
422     public RatingsEntry findByC_C_First(long classNameId, long classPK,
423         OrderByComparator obc) throws NoSuchEntryException, SystemException {
424         List<RatingsEntry> list = findByC_C(classNameId, classPK, 0, 1, obc);
425 
426         if (list.size() == 0) {
427             StringMaker msg = new StringMaker();
428 
429             msg.append("No RatingsEntry exists with the key {");
430 
431             msg.append("classNameId=" + classNameId);
432 
433             msg.append(", ");
434             msg.append("classPK=" + classPK);
435 
436             msg.append(StringPool.CLOSE_CURLY_BRACE);
437 
438             throw new NoSuchEntryException(msg.toString());
439         }
440         else {
441             return list.get(0);
442         }
443     }
444 
445     public RatingsEntry findByC_C_Last(long classNameId, long classPK,
446         OrderByComparator obc) throws NoSuchEntryException, SystemException {
447         int count = countByC_C(classNameId, classPK);
448 
449         List<RatingsEntry> list = findByC_C(classNameId, classPK, count - 1,
450                 count, obc);
451 
452         if (list.size() == 0) {
453             StringMaker msg = new StringMaker();
454 
455             msg.append("No RatingsEntry exists with the key {");
456 
457             msg.append("classNameId=" + classNameId);
458 
459             msg.append(", ");
460             msg.append("classPK=" + classPK);
461 
462             msg.append(StringPool.CLOSE_CURLY_BRACE);
463 
464             throw new NoSuchEntryException(msg.toString());
465         }
466         else {
467             return list.get(0);
468         }
469     }
470 
471     public RatingsEntry[] findByC_C_PrevAndNext(long entryId, long classNameId,
472         long classPK, OrderByComparator obc)
473         throws NoSuchEntryException, SystemException {
474         RatingsEntry ratingsEntry = findByPrimaryKey(entryId);
475 
476         int count = countByC_C(classNameId, classPK);
477 
478         Session session = null;
479 
480         try {
481             session = openSession();
482 
483             StringMaker query = new StringMaker();
484 
485             query.append(
486                 "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
487 
488             query.append("classNameId = ?");
489 
490             query.append(" AND ");
491 
492             query.append("classPK = ?");
493 
494             query.append(" ");
495 
496             if (obc != null) {
497                 query.append("ORDER BY ");
498                 query.append(obc.getOrderBy());
499             }
500 
501             Query q = session.createQuery(query.toString());
502 
503             int queryPos = 0;
504 
505             q.setLong(queryPos++, classNameId);
506 
507             q.setLong(queryPos++, classPK);
508 
509             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
510                     ratingsEntry);
511 
512             RatingsEntry[] array = new RatingsEntryImpl[3];
513 
514             array[0] = (RatingsEntry)objArray[0];
515             array[1] = (RatingsEntry)objArray[1];
516             array[2] = (RatingsEntry)objArray[2];
517 
518             return array;
519         }
520         catch (Exception e) {
521             throw HibernateUtil.processException(e);
522         }
523         finally {
524             closeSession(session);
525         }
526     }
527 
528     public RatingsEntry findByU_C_C(long userId, long classNameId, long classPK)
529         throws NoSuchEntryException, SystemException {
530         RatingsEntry ratingsEntry = fetchByU_C_C(userId, classNameId, classPK);
531 
532         if (ratingsEntry == null) {
533             StringMaker msg = new StringMaker();
534 
535             msg.append("No RatingsEntry exists with the key {");
536 
537             msg.append("userId=" + userId);
538 
539             msg.append(", ");
540             msg.append("classNameId=" + classNameId);
541 
542             msg.append(", ");
543             msg.append("classPK=" + classPK);
544 
545             msg.append(StringPool.CLOSE_CURLY_BRACE);
546 
547             if (_log.isWarnEnabled()) {
548                 _log.warn(msg.toString());
549             }
550 
551             throw new NoSuchEntryException(msg.toString());
552         }
553 
554         return ratingsEntry;
555     }
556 
557     public RatingsEntry fetchByU_C_C(long userId, long classNameId, long classPK)
558         throws SystemException {
559         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
560         String finderClassName = RatingsEntry.class.getName();
561         String finderMethodName = "fetchByU_C_C";
562         String[] finderParams = new String[] {
563                 Long.class.getName(), Long.class.getName(), Long.class.getName()
564             };
565         Object[] finderArgs = new Object[] {
566                 new Long(userId), new Long(classNameId), new Long(classPK)
567             };
568 
569         Object result = null;
570 
571         if (finderClassNameCacheEnabled) {
572             result = FinderCache.getResult(finderClassName, finderMethodName,
573                     finderParams, finderArgs, getSessionFactory());
574         }
575 
576         if (result == null) {
577             Session session = null;
578 
579             try {
580                 session = openSession();
581 
582                 StringMaker query = new StringMaker();
583 
584                 query.append(
585                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
586 
587                 query.append("userId = ?");
588 
589                 query.append(" AND ");
590 
591                 query.append("classNameId = ?");
592 
593                 query.append(" AND ");
594 
595                 query.append("classPK = ?");
596 
597                 query.append(" ");
598 
599                 Query q = session.createQuery(query.toString());
600 
601                 int queryPos = 0;
602 
603                 q.setLong(queryPos++, userId);
604 
605                 q.setLong(queryPos++, classNameId);
606 
607                 q.setLong(queryPos++, classPK);
608 
609                 List<RatingsEntry> list = q.list();
610 
611                 FinderCache.putResult(finderClassNameCacheEnabled,
612                     finderClassName, finderMethodName, finderParams,
613                     finderArgs, list);
614 
615                 if (list.size() == 0) {
616                     return null;
617                 }
618                 else {
619                     return list.get(0);
620                 }
621             }
622             catch (Exception e) {
623                 throw HibernateUtil.processException(e);
624             }
625             finally {
626                 closeSession(session);
627             }
628         }
629         else {
630             List<RatingsEntry> list = (List<RatingsEntry>)result;
631 
632             if (list.size() == 0) {
633                 return null;
634             }
635             else {
636                 return list.get(0);
637             }
638         }
639     }
640 
641     public List<RatingsEntry> findWithDynamicQuery(
642         DynamicQueryInitializer queryInitializer) throws SystemException {
643         Session session = null;
644 
645         try {
646             session = openSession();
647 
648             DynamicQuery query = queryInitializer.initialize(session);
649 
650             return query.list();
651         }
652         catch (Exception e) {
653             throw HibernateUtil.processException(e);
654         }
655         finally {
656             closeSession(session);
657         }
658     }
659 
660     public List<RatingsEntry> findWithDynamicQuery(
661         DynamicQueryInitializer queryInitializer, int begin, int end)
662         throws SystemException {
663         Session session = null;
664 
665         try {
666             session = openSession();
667 
668             DynamicQuery query = queryInitializer.initialize(session);
669 
670             query.setLimit(begin, end);
671 
672             return query.list();
673         }
674         catch (Exception e) {
675             throw HibernateUtil.processException(e);
676         }
677         finally {
678             closeSession(session);
679         }
680     }
681 
682     public List<RatingsEntry> findAll() throws SystemException {
683         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
684     }
685 
686     public List<RatingsEntry> findAll(int begin, int end)
687         throws SystemException {
688         return findAll(begin, end, null);
689     }
690 
691     public List<RatingsEntry> findAll(int begin, int end, OrderByComparator obc)
692         throws SystemException {
693         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
694         String finderClassName = RatingsEntry.class.getName();
695         String finderMethodName = "findAll";
696         String[] finderParams = new String[] {
697                 "java.lang.Integer", "java.lang.Integer",
698                 "com.liferay.portal.kernel.util.OrderByComparator"
699             };
700         Object[] finderArgs = new Object[] {
701                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
702             };
703 
704         Object result = null;
705 
706         if (finderClassNameCacheEnabled) {
707             result = FinderCache.getResult(finderClassName, finderMethodName,
708                     finderParams, finderArgs, getSessionFactory());
709         }
710 
711         if (result == null) {
712             Session session = null;
713 
714             try {
715                 session = openSession();
716 
717                 StringMaker query = new StringMaker();
718 
719                 query.append(
720                     "FROM com.liferay.portlet.ratings.model.RatingsEntry ");
721 
722                 if (obc != null) {
723                     query.append("ORDER BY ");
724                     query.append(obc.getOrderBy());
725                 }
726 
727                 Query q = session.createQuery(query.toString());
728 
729                 List<RatingsEntry> list = (List<RatingsEntry>)QueryUtil.list(q,
730                         getDialect(), begin, end);
731 
732                 if (obc == null) {
733                     Collections.sort(list);
734                 }
735 
736                 FinderCache.putResult(finderClassNameCacheEnabled,
737                     finderClassName, finderMethodName, finderParams,
738                     finderArgs, list);
739 
740                 return list;
741             }
742             catch (Exception e) {
743                 throw HibernateUtil.processException(e);
744             }
745             finally {
746                 closeSession(session);
747             }
748         }
749         else {
750             return (List<RatingsEntry>)result;
751         }
752     }
753 
754     public void removeByC_C(long classNameId, long classPK)
755         throws SystemException {
756         for (RatingsEntry ratingsEntry : findByC_C(classNameId, classPK)) {
757             remove(ratingsEntry);
758         }
759     }
760 
761     public void removeByU_C_C(long userId, long classNameId, long classPK)
762         throws NoSuchEntryException, SystemException {
763         RatingsEntry ratingsEntry = findByU_C_C(userId, classNameId, classPK);
764 
765         remove(ratingsEntry);
766     }
767 
768     public void removeAll() throws SystemException {
769         for (RatingsEntry ratingsEntry : findAll()) {
770             remove(ratingsEntry);
771         }
772     }
773 
774     public int countByC_C(long classNameId, long classPK)
775         throws SystemException {
776         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
777         String finderClassName = RatingsEntry.class.getName();
778         String finderMethodName = "countByC_C";
779         String[] finderParams = new String[] {
780                 Long.class.getName(), Long.class.getName()
781             };
782         Object[] finderArgs = new Object[] {
783                 new Long(classNameId), new Long(classPK)
784             };
785 
786         Object result = null;
787 
788         if (finderClassNameCacheEnabled) {
789             result = FinderCache.getResult(finderClassName, finderMethodName,
790                     finderParams, finderArgs, getSessionFactory());
791         }
792 
793         if (result == null) {
794             Session session = null;
795 
796             try {
797                 session = openSession();
798 
799                 StringMaker query = new StringMaker();
800 
801                 query.append("SELECT COUNT(*) ");
802                 query.append(
803                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
804 
805                 query.append("classNameId = ?");
806 
807                 query.append(" AND ");
808 
809                 query.append("classPK = ?");
810 
811                 query.append(" ");
812 
813                 Query q = session.createQuery(query.toString());
814 
815                 int queryPos = 0;
816 
817                 q.setLong(queryPos++, classNameId);
818 
819                 q.setLong(queryPos++, classPK);
820 
821                 Long count = null;
822 
823                 Iterator<Long> itr = q.list().iterator();
824 
825                 if (itr.hasNext()) {
826                     count = itr.next();
827                 }
828 
829                 if (count == null) {
830                     count = new Long(0);
831                 }
832 
833                 FinderCache.putResult(finderClassNameCacheEnabled,
834                     finderClassName, finderMethodName, finderParams,
835                     finderArgs, count);
836 
837                 return count.intValue();
838             }
839             catch (Exception e) {
840                 throw HibernateUtil.processException(e);
841             }
842             finally {
843                 closeSession(session);
844             }
845         }
846         else {
847             return ((Long)result).intValue();
848         }
849     }
850 
851     public int countByU_C_C(long userId, long classNameId, long classPK)
852         throws SystemException {
853         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
854         String finderClassName = RatingsEntry.class.getName();
855         String finderMethodName = "countByU_C_C";
856         String[] finderParams = new String[] {
857                 Long.class.getName(), Long.class.getName(), Long.class.getName()
858             };
859         Object[] finderArgs = new Object[] {
860                 new Long(userId), new Long(classNameId), new Long(classPK)
861             };
862 
863         Object result = null;
864 
865         if (finderClassNameCacheEnabled) {
866             result = FinderCache.getResult(finderClassName, finderMethodName,
867                     finderParams, finderArgs, getSessionFactory());
868         }
869 
870         if (result == null) {
871             Session session = null;
872 
873             try {
874                 session = openSession();
875 
876                 StringMaker query = new StringMaker();
877 
878                 query.append("SELECT COUNT(*) ");
879                 query.append(
880                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
881 
882                 query.append("userId = ?");
883 
884                 query.append(" AND ");
885 
886                 query.append("classNameId = ?");
887 
888                 query.append(" AND ");
889 
890                 query.append("classPK = ?");
891 
892                 query.append(" ");
893 
894                 Query q = session.createQuery(query.toString());
895 
896                 int queryPos = 0;
897 
898                 q.setLong(queryPos++, userId);
899 
900                 q.setLong(queryPos++, classNameId);
901 
902                 q.setLong(queryPos++, classPK);
903 
904                 Long count = null;
905 
906                 Iterator<Long> itr = q.list().iterator();
907 
908                 if (itr.hasNext()) {
909                     count = itr.next();
910                 }
911 
912                 if (count == null) {
913                     count = new Long(0);
914                 }
915 
916                 FinderCache.putResult(finderClassNameCacheEnabled,
917                     finderClassName, finderMethodName, finderParams,
918                     finderArgs, count);
919 
920                 return count.intValue();
921             }
922             catch (Exception e) {
923                 throw HibernateUtil.processException(e);
924             }
925             finally {
926                 closeSession(session);
927             }
928         }
929         else {
930             return ((Long)result).intValue();
931         }
932     }
933 
934     public int countAll() throws SystemException {
935         boolean finderClassNameCacheEnabled = RatingsEntryModelImpl.CACHE_ENABLED;
936         String finderClassName = RatingsEntry.class.getName();
937         String finderMethodName = "countAll";
938         String[] finderParams = new String[] {  };
939         Object[] finderArgs = new Object[] {  };
940 
941         Object result = null;
942 
943         if (finderClassNameCacheEnabled) {
944             result = FinderCache.getResult(finderClassName, finderMethodName,
945                     finderParams, finderArgs, getSessionFactory());
946         }
947 
948         if (result == null) {
949             Session session = null;
950 
951             try {
952                 session = openSession();
953 
954                 Query q = session.createQuery(
955                         "SELECT COUNT(*) FROM com.liferay.portlet.ratings.model.RatingsEntry");
956 
957                 Long count = null;
958 
959                 Iterator<Long> itr = q.list().iterator();
960 
961                 if (itr.hasNext()) {
962                     count = itr.next();
963                 }
964 
965                 if (count == null) {
966                     count = new Long(0);
967                 }
968 
969                 FinderCache.putResult(finderClassNameCacheEnabled,
970                     finderClassName, finderMethodName, finderParams,
971                     finderArgs, count);
972 
973                 return count.intValue();
974             }
975             catch (Exception e) {
976                 throw HibernateUtil.processException(e);
977             }
978             finally {
979                 closeSession(session);
980             }
981         }
982         else {
983             return ((Long)result).intValue();
984         }
985     }
986 
987     protected void initDao() {
988         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
989                     PropsUtil.get(
990                         "value.object.listener.com.liferay.portlet.ratings.model.RatingsEntry")));
991 
992         if (listenerClassNames.length > 0) {
993             try {
994                 List<ModelListener> listeners = new ArrayList<ModelListener>();
995 
996                 for (String listenerClassName : listenerClassNames) {
997                     listeners.add((ModelListener)Class.forName(
998                             listenerClassName).newInstance());
999                 }
1000
1001                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1002            }
1003            catch (Exception e) {
1004                _log.error(e);
1005            }
1006        }
1007    }
1008
1009    private static Log _log = LogFactory.getLog(RatingsEntryPersistenceImpl.class);
1010    private ModelListener[] _listeners;
1011}