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