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