1   /**
2    * Copyright (c) 2000-2007 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.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.ratings.NoSuchEntryException;
36  import com.liferay.portlet.ratings.model.RatingsEntry;
37  import com.liferay.portlet.ratings.model.impl.RatingsEntryImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="RatingsEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class RatingsEntryPersistenceImpl extends BasePersistence
58      implements RatingsEntryPersistence {
59      public RatingsEntry create(long entryId) {
60          RatingsEntry ratingsEntry = new RatingsEntryImpl();
61          ratingsEntry.setNew(true);
62          ratingsEntry.setPrimaryKey(entryId);
63  
64          return ratingsEntry;
65      }
66  
67      public RatingsEntry remove(long entryId)
68          throws NoSuchEntryException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              RatingsEntry ratingsEntry = (RatingsEntry)session.get(RatingsEntryImpl.class,
75                      new Long(entryId));
76  
77              if (ratingsEntry == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No RatingsEntry exists with the primary key " +
80                          entryId);
81                  }
82  
83                  throw new NoSuchEntryException(
84                      "No RatingsEntry exists with the primary key " + entryId);
85              }
86  
87              return remove(ratingsEntry);
88          }
89          catch (NoSuchEntryException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw HibernateUtil.processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public RatingsEntry remove(RatingsEntry ratingsEntry)
101         throws SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106             session.delete(ratingsEntry);
107             session.flush();
108 
109             return ratingsEntry;
110         }
111         catch (Exception e) {
112             throw HibernateUtil.processException(e);
113         }
114         finally {
115             closeSession(session);
116             FinderCache.clearCache(RatingsEntry.class.getName());
117         }
118     }
119 
120     public RatingsEntry update(
121         com.liferay.portlet.ratings.model.RatingsEntry ratingsEntry)
122         throws SystemException {
123         return update(ratingsEntry, false);
124     }
125 
126     public RatingsEntry update(
127         com.liferay.portlet.ratings.model.RatingsEntry ratingsEntry,
128         boolean merge) throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             if (merge) {
135                 session.merge(ratingsEntry);
136             }
137             else {
138                 if (ratingsEntry.isNew()) {
139                     session.save(ratingsEntry);
140                 }
141             }
142 
143             session.flush();
144             ratingsEntry.setNew(false);
145 
146             return ratingsEntry;
147         }
148         catch (Exception e) {
149             throw HibernateUtil.processException(e);
150         }
151         finally {
152             closeSession(session);
153             FinderCache.clearCache(RatingsEntry.class.getName());
154         }
155     }
156 
157     public RatingsEntry findByPrimaryKey(long entryId)
158         throws NoSuchEntryException, SystemException {
159         RatingsEntry ratingsEntry = fetchByPrimaryKey(entryId);
160 
161         if (ratingsEntry == null) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn("No RatingsEntry exists with the primary key " +
164                     entryId);
165             }
166 
167             throw new NoSuchEntryException(
168                 "No RatingsEntry exists with the primary key " + entryId);
169         }
170 
171         return ratingsEntry;
172     }
173 
174     public RatingsEntry fetchByPrimaryKey(long entryId)
175         throws SystemException {
176         Session session = null;
177 
178         try {
179             session = openSession();
180 
181             return (RatingsEntry)session.get(RatingsEntryImpl.class,
182                 new Long(entryId));
183         }
184         catch (Exception e) {
185             throw HibernateUtil.processException(e);
186         }
187         finally {
188             closeSession(session);
189         }
190     }
191 
192     public List findByC_C(long classNameId, long classPK)
193         throws SystemException {
194         String finderClassName = RatingsEntry.class.getName();
195         String finderMethodName = "findByC_C";
196         String[] finderParams = new String[] {
197                 Long.class.getName(), Long.class.getName()
198             };
199         Object[] finderArgs = new Object[] {
200                 new Long(classNameId), new Long(classPK)
201             };
202         Object result = FinderCache.getResult(finderClassName,
203                 finderMethodName, finderParams, finderArgs, getSessionFactory());
204 
205         if (result == null) {
206             Session session = null;
207 
208             try {
209                 session = openSession();
210 
211                 StringMaker query = new StringMaker();
212                 query.append(
213                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
214                 query.append("classNameId = ?");
215                 query.append(" AND ");
216                 query.append("classPK = ?");
217                 query.append(" ");
218 
219                 Query q = session.createQuery(query.toString());
220                 int queryPos = 0;
221                 q.setLong(queryPos++, classNameId);
222                 q.setLong(queryPos++, classPK);
223 
224                 List list = q.list();
225                 FinderCache.putResult(finderClassName, finderMethodName,
226                     finderParams, finderArgs, list);
227 
228                 return list;
229             }
230             catch (Exception e) {
231                 throw HibernateUtil.processException(e);
232             }
233             finally {
234                 closeSession(session);
235             }
236         }
237         else {
238             return (List)result;
239         }
240     }
241 
242     public List findByC_C(long classNameId, long classPK, int begin, int end)
243         throws SystemException {
244         return findByC_C(classNameId, classPK, begin, end, null);
245     }
246 
247     public List findByC_C(long classNameId, long classPK, int begin, int end,
248         OrderByComparator obc) throws SystemException {
249         String finderClassName = RatingsEntry.class.getName();
250         String finderMethodName = "findByC_C";
251         String[] finderParams = new String[] {
252                 Long.class.getName(), Long.class.getName(), "java.lang.Integer",
253                 "java.lang.Integer",
254                 "com.liferay.portal.kernel.util.OrderByComparator"
255             };
256         Object[] finderArgs = new Object[] {
257                 new Long(classNameId), new Long(classPK), String.valueOf(begin),
258                 String.valueOf(end), String.valueOf(obc)
259             };
260         Object result = FinderCache.getResult(finderClassName,
261                 finderMethodName, finderParams, finderArgs, getSessionFactory());
262 
263         if (result == null) {
264             Session session = null;
265 
266             try {
267                 session = openSession();
268 
269                 StringMaker query = new StringMaker();
270                 query.append(
271                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
272                 query.append("classNameId = ?");
273                 query.append(" AND ");
274                 query.append("classPK = ?");
275                 query.append(" ");
276 
277                 if (obc != null) {
278                     query.append("ORDER BY ");
279                     query.append(obc.getOrderBy());
280                 }
281 
282                 Query q = session.createQuery(query.toString());
283                 int queryPos = 0;
284                 q.setLong(queryPos++, classNameId);
285                 q.setLong(queryPos++, classPK);
286 
287                 List list = QueryUtil.list(q, getDialect(), begin, end);
288                 FinderCache.putResult(finderClassName, finderMethodName,
289                     finderParams, finderArgs, list);
290 
291                 return list;
292             }
293             catch (Exception e) {
294                 throw HibernateUtil.processException(e);
295             }
296             finally {
297                 closeSession(session);
298             }
299         }
300         else {
301             return (List)result;
302         }
303     }
304 
305     public RatingsEntry findByC_C_First(long classNameId, long classPK,
306         OrderByComparator obc) throws NoSuchEntryException, SystemException {
307         List list = findByC_C(classNameId, classPK, 0, 1, obc);
308 
309         if (list.size() == 0) {
310             StringMaker msg = new StringMaker();
311             msg.append("No RatingsEntry exists with the key ");
312             msg.append(StringPool.OPEN_CURLY_BRACE);
313             msg.append("classNameId=");
314             msg.append(classNameId);
315             msg.append(", ");
316             msg.append("classPK=");
317             msg.append(classPK);
318             msg.append(StringPool.CLOSE_CURLY_BRACE);
319             throw new NoSuchEntryException(msg.toString());
320         }
321         else {
322             return (RatingsEntry)list.get(0);
323         }
324     }
325 
326     public RatingsEntry findByC_C_Last(long classNameId, long classPK,
327         OrderByComparator obc) throws NoSuchEntryException, SystemException {
328         int count = countByC_C(classNameId, classPK);
329         List list = findByC_C(classNameId, classPK, count - 1, count, obc);
330 
331         if (list.size() == 0) {
332             StringMaker msg = new StringMaker();
333             msg.append("No RatingsEntry exists with the key ");
334             msg.append(StringPool.OPEN_CURLY_BRACE);
335             msg.append("classNameId=");
336             msg.append(classNameId);
337             msg.append(", ");
338             msg.append("classPK=");
339             msg.append(classPK);
340             msg.append(StringPool.CLOSE_CURLY_BRACE);
341             throw new NoSuchEntryException(msg.toString());
342         }
343         else {
344             return (RatingsEntry)list.get(0);
345         }
346     }
347 
348     public RatingsEntry[] findByC_C_PrevAndNext(long entryId, long classNameId,
349         long classPK, OrderByComparator obc)
350         throws NoSuchEntryException, SystemException {
351         RatingsEntry ratingsEntry = findByPrimaryKey(entryId);
352         int count = countByC_C(classNameId, classPK);
353         Session session = null;
354 
355         try {
356             session = openSession();
357 
358             StringMaker query = new StringMaker();
359             query.append(
360                 "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
361             query.append("classNameId = ?");
362             query.append(" AND ");
363             query.append("classPK = ?");
364             query.append(" ");
365 
366             if (obc != null) {
367                 query.append("ORDER BY ");
368                 query.append(obc.getOrderBy());
369             }
370 
371             Query q = session.createQuery(query.toString());
372             int queryPos = 0;
373             q.setLong(queryPos++, classNameId);
374             q.setLong(queryPos++, classPK);
375 
376             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
377                     ratingsEntry);
378             RatingsEntry[] array = new RatingsEntryImpl[3];
379             array[0] = (RatingsEntry)objArray[0];
380             array[1] = (RatingsEntry)objArray[1];
381             array[2] = (RatingsEntry)objArray[2];
382 
383             return array;
384         }
385         catch (Exception e) {
386             throw HibernateUtil.processException(e);
387         }
388         finally {
389             closeSession(session);
390         }
391     }
392 
393     public RatingsEntry findByU_C_C(long userId, long classNameId, long classPK)
394         throws NoSuchEntryException, SystemException {
395         RatingsEntry ratingsEntry = fetchByU_C_C(userId, classNameId, classPK);
396 
397         if (ratingsEntry == null) {
398             StringMaker msg = new StringMaker();
399             msg.append("No RatingsEntry exists with the key ");
400             msg.append(StringPool.OPEN_CURLY_BRACE);
401             msg.append("userId=");
402             msg.append(userId);
403             msg.append(", ");
404             msg.append("classNameId=");
405             msg.append(classNameId);
406             msg.append(", ");
407             msg.append("classPK=");
408             msg.append(classPK);
409             msg.append(StringPool.CLOSE_CURLY_BRACE);
410 
411             if (_log.isWarnEnabled()) {
412                 _log.warn(msg.toString());
413             }
414 
415             throw new NoSuchEntryException(msg.toString());
416         }
417 
418         return ratingsEntry;
419     }
420 
421     public RatingsEntry fetchByU_C_C(long userId, long classNameId, long classPK)
422         throws SystemException {
423         String finderClassName = RatingsEntry.class.getName();
424         String finderMethodName = "fetchByU_C_C";
425         String[] finderParams = new String[] {
426                 Long.class.getName(), Long.class.getName(), Long.class.getName()
427             };
428         Object[] finderArgs = new Object[] {
429                 new Long(userId), new Long(classNameId), new Long(classPK)
430             };
431         Object result = FinderCache.getResult(finderClassName,
432                 finderMethodName, finderParams, finderArgs, getSessionFactory());
433 
434         if (result == null) {
435             Session session = null;
436 
437             try {
438                 session = openSession();
439 
440                 StringMaker query = new StringMaker();
441                 query.append(
442                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
443                 query.append("userId = ?");
444                 query.append(" AND ");
445                 query.append("classNameId = ?");
446                 query.append(" AND ");
447                 query.append("classPK = ?");
448                 query.append(" ");
449 
450                 Query q = session.createQuery(query.toString());
451                 int queryPos = 0;
452                 q.setLong(queryPos++, userId);
453                 q.setLong(queryPos++, classNameId);
454                 q.setLong(queryPos++, classPK);
455 
456                 List list = q.list();
457                 FinderCache.putResult(finderClassName, finderMethodName,
458                     finderParams, finderArgs, list);
459 
460                 if (list.size() == 0) {
461                     return null;
462                 }
463                 else {
464                     return (RatingsEntry)list.get(0);
465                 }
466             }
467             catch (Exception e) {
468                 throw HibernateUtil.processException(e);
469             }
470             finally {
471                 closeSession(session);
472             }
473         }
474         else {
475             List list = (List)result;
476 
477             if (list.size() == 0) {
478                 return null;
479             }
480             else {
481                 return (RatingsEntry)list.get(0);
482             }
483         }
484     }
485 
486     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
487         throws SystemException {
488         Session session = null;
489 
490         try {
491             session = openSession();
492 
493             DynamicQuery query = queryInitializer.initialize(session);
494 
495             return query.list();
496         }
497         catch (Exception e) {
498             throw HibernateUtil.processException(e);
499         }
500         finally {
501             closeSession(session);
502         }
503     }
504 
505     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
506         int begin, int end) throws SystemException {
507         Session session = null;
508 
509         try {
510             session = openSession();
511 
512             DynamicQuery query = queryInitializer.initialize(session);
513             query.setLimit(begin, end);
514 
515             return query.list();
516         }
517         catch (Exception e) {
518             throw HibernateUtil.processException(e);
519         }
520         finally {
521             closeSession(session);
522         }
523     }
524 
525     public List findAll() throws SystemException {
526         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
527     }
528 
529     public List findAll(int begin, int end) throws SystemException {
530         return findAll(begin, end, null);
531     }
532 
533     public List findAll(int begin, int end, OrderByComparator obc)
534         throws SystemException {
535         String finderClassName = RatingsEntry.class.getName();
536         String finderMethodName = "findAll";
537         String[] finderParams = new String[] {
538                 "java.lang.Integer", "java.lang.Integer",
539                 "com.liferay.portal.kernel.util.OrderByComparator"
540             };
541         Object[] finderArgs = new Object[] {
542                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
543             };
544         Object result = FinderCache.getResult(finderClassName,
545                 finderMethodName, finderParams, finderArgs, getSessionFactory());
546 
547         if (result == null) {
548             Session session = null;
549 
550             try {
551                 session = openSession();
552 
553                 StringMaker query = new StringMaker();
554                 query.append(
555                     "FROM com.liferay.portlet.ratings.model.RatingsEntry ");
556 
557                 if (obc != null) {
558                     query.append("ORDER BY ");
559                     query.append(obc.getOrderBy());
560                 }
561 
562                 Query q = session.createQuery(query.toString());
563                 List list = QueryUtil.list(q, getDialect(), begin, end);
564 
565                 if (obc == null) {
566                     Collections.sort(list);
567                 }
568 
569                 FinderCache.putResult(finderClassName, finderMethodName,
570                     finderParams, finderArgs, list);
571 
572                 return list;
573             }
574             catch (Exception e) {
575                 throw HibernateUtil.processException(e);
576             }
577             finally {
578                 closeSession(session);
579             }
580         }
581         else {
582             return (List)result;
583         }
584     }
585 
586     public void removeByC_C(long classNameId, long classPK)
587         throws SystemException {
588         Iterator itr = findByC_C(classNameId, classPK).iterator();
589 
590         while (itr.hasNext()) {
591             RatingsEntry ratingsEntry = (RatingsEntry)itr.next();
592             remove(ratingsEntry);
593         }
594     }
595 
596     public void removeByU_C_C(long userId, long classNameId, long classPK)
597         throws NoSuchEntryException, SystemException {
598         RatingsEntry ratingsEntry = findByU_C_C(userId, classNameId, classPK);
599         remove(ratingsEntry);
600     }
601 
602     public void removeAll() throws SystemException {
603         Iterator itr = findAll().iterator();
604 
605         while (itr.hasNext()) {
606             remove((RatingsEntry)itr.next());
607         }
608     }
609 
610     public int countByC_C(long classNameId, long classPK)
611         throws SystemException {
612         String finderClassName = RatingsEntry.class.getName();
613         String finderMethodName = "countByC_C";
614         String[] finderParams = new String[] {
615                 Long.class.getName(), Long.class.getName()
616             };
617         Object[] finderArgs = new Object[] {
618                 new Long(classNameId), new Long(classPK)
619             };
620         Object result = FinderCache.getResult(finderClassName,
621                 finderMethodName, finderParams, finderArgs, getSessionFactory());
622 
623         if (result == null) {
624             Session session = null;
625 
626             try {
627                 session = openSession();
628 
629                 StringMaker query = new StringMaker();
630                 query.append("SELECT COUNT(*) ");
631                 query.append(
632                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
633                 query.append("classNameId = ?");
634                 query.append(" AND ");
635                 query.append("classPK = ?");
636                 query.append(" ");
637 
638                 Query q = session.createQuery(query.toString());
639                 int queryPos = 0;
640                 q.setLong(queryPos++, classNameId);
641                 q.setLong(queryPos++, classPK);
642 
643                 Long count = null;
644                 Iterator itr = q.list().iterator();
645 
646                 if (itr.hasNext()) {
647                     count = (Long)itr.next();
648                 }
649 
650                 if (count == null) {
651                     count = new Long(0);
652                 }
653 
654                 FinderCache.putResult(finderClassName, finderMethodName,
655                     finderParams, finderArgs, count);
656 
657                 return count.intValue();
658             }
659             catch (Exception e) {
660                 throw HibernateUtil.processException(e);
661             }
662             finally {
663                 closeSession(session);
664             }
665         }
666         else {
667             return ((Long)result).intValue();
668         }
669     }
670 
671     public int countByU_C_C(long userId, long classNameId, long classPK)
672         throws SystemException {
673         String finderClassName = RatingsEntry.class.getName();
674         String finderMethodName = "countByU_C_C";
675         String[] finderParams = new String[] {
676                 Long.class.getName(), Long.class.getName(), Long.class.getName()
677             };
678         Object[] finderArgs = new Object[] {
679                 new Long(userId), new Long(classNameId), new Long(classPK)
680             };
681         Object result = FinderCache.getResult(finderClassName,
682                 finderMethodName, finderParams, finderArgs, getSessionFactory());
683 
684         if (result == null) {
685             Session session = null;
686 
687             try {
688                 session = openSession();
689 
690                 StringMaker query = new StringMaker();
691                 query.append("SELECT COUNT(*) ");
692                 query.append(
693                     "FROM com.liferay.portlet.ratings.model.RatingsEntry WHERE ");
694                 query.append("userId = ?");
695                 query.append(" AND ");
696                 query.append("classNameId = ?");
697                 query.append(" AND ");
698                 query.append("classPK = ?");
699                 query.append(" ");
700 
701                 Query q = session.createQuery(query.toString());
702                 int queryPos = 0;
703                 q.setLong(queryPos++, userId);
704                 q.setLong(queryPos++, classNameId);
705                 q.setLong(queryPos++, classPK);
706 
707                 Long count = null;
708                 Iterator itr = q.list().iterator();
709 
710                 if (itr.hasNext()) {
711                     count = (Long)itr.next();
712                 }
713 
714                 if (count == null) {
715                     count = new Long(0);
716                 }
717 
718                 FinderCache.putResult(finderClassName, finderMethodName,
719                     finderParams, finderArgs, count);
720 
721                 return count.intValue();
722             }
723             catch (Exception e) {
724                 throw HibernateUtil.processException(e);
725             }
726             finally {
727                 closeSession(session);
728             }
729         }
730         else {
731             return ((Long)result).intValue();
732         }
733     }
734 
735     public int countAll() throws SystemException {
736         String finderClassName = RatingsEntry.class.getName();
737         String finderMethodName = "countAll";
738         String[] finderParams = new String[] {  };
739         Object[] finderArgs = new Object[] {  };
740         Object result = FinderCache.getResult(finderClassName,
741                 finderMethodName, finderParams, finderArgs, getSessionFactory());
742 
743         if (result == null) {
744             Session session = null;
745 
746             try {
747                 session = openSession();
748 
749                 StringMaker query = new StringMaker();
750                 query.append("SELECT COUNT(*) ");
751                 query.append(
752                     "FROM com.liferay.portlet.ratings.model.RatingsEntry");
753 
754                 Query q = session.createQuery(query.toString());
755                 Long count = null;
756                 Iterator itr = q.list().iterator();
757 
758                 if (itr.hasNext()) {
759                     count = (Long)itr.next();
760                 }
761 
762                 if (count == null) {
763                     count = new Long(0);
764                 }
765 
766                 FinderCache.putResult(finderClassName, finderMethodName,
767                     finderParams, finderArgs, count);
768 
769                 return count.intValue();
770             }
771             catch (Exception e) {
772                 throw HibernateUtil.processException(e);
773             }
774             finally {
775                 closeSession(session);
776             }
777         }
778         else {
779             return ((Long)result).intValue();
780         }
781     }
782 
783     protected void initDao() {
784     }
785 
786     private static Log _log = LogFactory.getLog(RatingsEntryPersistenceImpl.class);
787 }