1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.ratings.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  import com.liferay.portal.util.PropsUtil;
38  
39  import com.liferay.portlet.ratings.NoSuchStatsException;
40  import com.liferay.portlet.ratings.model.RatingsStats;
41  import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
42  import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
43  
44  import com.liferay.util.dao.hibernate.QueryUtil;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.hibernate.Query;
50  import org.hibernate.Session;
51  
52  import java.util.Collections;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  /**
57   * <a href="RatingsStatsPersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class RatingsStatsPersistenceImpl extends BasePersistence
63      implements RatingsStatsPersistence {
64      public RatingsStats create(long statsId) {
65          RatingsStats ratingsStats = new RatingsStatsImpl();
66  
67          ratingsStats.setNew(true);
68          ratingsStats.setPrimaryKey(statsId);
69  
70          return ratingsStats;
71      }
72  
73      public RatingsStats remove(long statsId)
74          throws NoSuchStatsException, SystemException {
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
81                      new Long(statsId));
82  
83              if (ratingsStats == null) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn("No RatingsStats exists with the primary key " +
86                          statsId);
87                  }
88  
89                  throw new NoSuchStatsException(
90                      "No RatingsStats exists with the primary key " + statsId);
91              }
92  
93              return remove(ratingsStats);
94          }
95          catch (NoSuchStatsException nsee) {
96              throw nsee;
97          }
98          catch (Exception e) {
99              throw HibernateUtil.processException(e);
100         }
101         finally {
102             closeSession(session);
103         }
104     }
105 
106     public RatingsStats remove(RatingsStats ratingsStats)
107         throws SystemException {
108         ModelListener listener = _getListener();
109 
110         if (listener != null) {
111             listener.onBeforeRemove(ratingsStats);
112         }
113 
114         ratingsStats = removeImpl(ratingsStats);
115 
116         if (listener != null) {
117             listener.onAfterRemove(ratingsStats);
118         }
119 
120         return ratingsStats;
121     }
122 
123     protected RatingsStats removeImpl(RatingsStats ratingsStats)
124         throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             session.delete(ratingsStats);
131 
132             session.flush();
133 
134             return ratingsStats;
135         }
136         catch (Exception e) {
137             throw HibernateUtil.processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCache.clearCache(RatingsStats.class.getName());
143         }
144     }
145 
146     public RatingsStats update(RatingsStats ratingsStats)
147         throws SystemException {
148         return update(ratingsStats, false);
149     }
150 
151     public RatingsStats update(RatingsStats ratingsStats, boolean merge)
152         throws SystemException {
153         ModelListener listener = _getListener();
154 
155         boolean isNew = ratingsStats.isNew();
156 
157         if (listener != null) {
158             if (isNew) {
159                 listener.onBeforeCreate(ratingsStats);
160             }
161             else {
162                 listener.onBeforeUpdate(ratingsStats);
163             }
164         }
165 
166         ratingsStats = updateImpl(ratingsStats, merge);
167 
168         if (listener != null) {
169             if (isNew) {
170                 listener.onAfterCreate(ratingsStats);
171             }
172             else {
173                 listener.onAfterUpdate(ratingsStats);
174             }
175         }
176 
177         return ratingsStats;
178     }
179 
180     public RatingsStats updateImpl(
181         com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
182         boolean merge) throws SystemException {
183         Session session = null;
184 
185         try {
186             session = openSession();
187 
188             if (merge) {
189                 session.merge(ratingsStats);
190             }
191             else {
192                 if (ratingsStats.isNew()) {
193                     session.save(ratingsStats);
194                 }
195             }
196 
197             session.flush();
198 
199             ratingsStats.setNew(false);
200 
201             return ratingsStats;
202         }
203         catch (Exception e) {
204             throw HibernateUtil.processException(e);
205         }
206         finally {
207             closeSession(session);
208 
209             FinderCache.clearCache(RatingsStats.class.getName());
210         }
211     }
212 
213     public RatingsStats findByPrimaryKey(long statsId)
214         throws NoSuchStatsException, SystemException {
215         RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
216 
217         if (ratingsStats == null) {
218             if (_log.isWarnEnabled()) {
219                 _log.warn("No RatingsStats exists with the primary key " +
220                     statsId);
221             }
222 
223             throw new NoSuchStatsException(
224                 "No RatingsStats exists with the primary key " + statsId);
225         }
226 
227         return ratingsStats;
228     }
229 
230     public RatingsStats fetchByPrimaryKey(long statsId)
231         throws SystemException {
232         Session session = null;
233 
234         try {
235             session = openSession();
236 
237             return (RatingsStats)session.get(RatingsStatsImpl.class,
238                 new Long(statsId));
239         }
240         catch (Exception e) {
241             throw HibernateUtil.processException(e);
242         }
243         finally {
244             closeSession(session);
245         }
246     }
247 
248     public RatingsStats findByC_C(long classNameId, long classPK)
249         throws NoSuchStatsException, SystemException {
250         RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
251 
252         if (ratingsStats == null) {
253             StringMaker msg = new StringMaker();
254 
255             msg.append("No RatingsStats exists with the key {");
256 
257             msg.append("classNameId=" + classNameId);
258 
259             msg.append(", ");
260             msg.append("classPK=" + classPK);
261 
262             msg.append(StringPool.CLOSE_CURLY_BRACE);
263 
264             if (_log.isWarnEnabled()) {
265                 _log.warn(msg.toString());
266             }
267 
268             throw new NoSuchStatsException(msg.toString());
269         }
270 
271         return ratingsStats;
272     }
273 
274     public RatingsStats fetchByC_C(long classNameId, long classPK)
275         throws SystemException {
276         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
277         String finderClassName = RatingsStats.class.getName();
278         String finderMethodName = "fetchByC_C";
279         String[] finderParams = new String[] {
280                 Long.class.getName(), Long.class.getName()
281             };
282         Object[] finderArgs = new Object[] {
283                 new Long(classNameId), new Long(classPK)
284             };
285 
286         Object result = null;
287 
288         if (finderClassNameCacheEnabled) {
289             result = FinderCache.getResult(finderClassName, finderMethodName,
290                     finderParams, finderArgs, getSessionFactory());
291         }
292 
293         if (result == null) {
294             Session session = null;
295 
296             try {
297                 session = openSession();
298 
299                 StringMaker query = new StringMaker();
300 
301                 query.append(
302                     "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
303 
304                 query.append("classNameId = ?");
305 
306                 query.append(" AND ");
307 
308                 query.append("classPK = ?");
309 
310                 query.append(" ");
311 
312                 Query q = session.createQuery(query.toString());
313 
314                 int queryPos = 0;
315 
316                 q.setLong(queryPos++, classNameId);
317 
318                 q.setLong(queryPos++, classPK);
319 
320                 List list = q.list();
321 
322                 FinderCache.putResult(finderClassNameCacheEnabled,
323                     finderClassName, finderMethodName, finderParams,
324                     finderArgs, list);
325 
326                 if (list.size() == 0) {
327                     return null;
328                 }
329                 else {
330                     return (RatingsStats)list.get(0);
331                 }
332             }
333             catch (Exception e) {
334                 throw HibernateUtil.processException(e);
335             }
336             finally {
337                 closeSession(session);
338             }
339         }
340         else {
341             List list = (List)result;
342 
343             if (list.size() == 0) {
344                 return null;
345             }
346             else {
347                 return (RatingsStats)list.get(0);
348             }
349         }
350     }
351 
352     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
353         throws SystemException {
354         Session session = null;
355 
356         try {
357             session = openSession();
358 
359             DynamicQuery query = queryInitializer.initialize(session);
360 
361             return query.list();
362         }
363         catch (Exception e) {
364             throw HibernateUtil.processException(e);
365         }
366         finally {
367             closeSession(session);
368         }
369     }
370 
371     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
372         int begin, int end) throws SystemException {
373         Session session = null;
374 
375         try {
376             session = openSession();
377 
378             DynamicQuery query = queryInitializer.initialize(session);
379 
380             query.setLimit(begin, end);
381 
382             return query.list();
383         }
384         catch (Exception e) {
385             throw HibernateUtil.processException(e);
386         }
387         finally {
388             closeSession(session);
389         }
390     }
391 
392     public List findAll() throws SystemException {
393         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
394     }
395 
396     public List findAll(int begin, int end) throws SystemException {
397         return findAll(begin, end, null);
398     }
399 
400     public List findAll(int begin, int end, OrderByComparator obc)
401         throws SystemException {
402         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
403         String finderClassName = RatingsStats.class.getName();
404         String finderMethodName = "findAll";
405         String[] finderParams = new String[] {
406                 "java.lang.Integer", "java.lang.Integer",
407                 "com.liferay.portal.kernel.util.OrderByComparator"
408             };
409         Object[] finderArgs = new Object[] {
410                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
411             };
412 
413         Object result = null;
414 
415         if (finderClassNameCacheEnabled) {
416             result = FinderCache.getResult(finderClassName, finderMethodName,
417                     finderParams, finderArgs, getSessionFactory());
418         }
419 
420         if (result == null) {
421             Session session = null;
422 
423             try {
424                 session = openSession();
425 
426                 StringMaker query = new StringMaker();
427 
428                 query.append(
429                     "FROM com.liferay.portlet.ratings.model.RatingsStats ");
430 
431                 if (obc != null) {
432                     query.append("ORDER BY ");
433                     query.append(obc.getOrderBy());
434                 }
435 
436                 Query q = session.createQuery(query.toString());
437 
438                 List list = QueryUtil.list(q, getDialect(), begin, end);
439 
440                 if (obc == null) {
441                     Collections.sort(list);
442                 }
443 
444                 FinderCache.putResult(finderClassNameCacheEnabled,
445                     finderClassName, finderMethodName, finderParams,
446                     finderArgs, list);
447 
448                 return list;
449             }
450             catch (Exception e) {
451                 throw HibernateUtil.processException(e);
452             }
453             finally {
454                 closeSession(session);
455             }
456         }
457         else {
458             return (List)result;
459         }
460     }
461 
462     public void removeByC_C(long classNameId, long classPK)
463         throws NoSuchStatsException, SystemException {
464         RatingsStats ratingsStats = findByC_C(classNameId, classPK);
465 
466         remove(ratingsStats);
467     }
468 
469     public void removeAll() throws SystemException {
470         Iterator itr = findAll().iterator();
471 
472         while (itr.hasNext()) {
473             remove((RatingsStats)itr.next());
474         }
475     }
476 
477     public int countByC_C(long classNameId, long classPK)
478         throws SystemException {
479         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
480         String finderClassName = RatingsStats.class.getName();
481         String finderMethodName = "countByC_C";
482         String[] finderParams = new String[] {
483                 Long.class.getName(), Long.class.getName()
484             };
485         Object[] finderArgs = new Object[] {
486                 new Long(classNameId), new Long(classPK)
487             };
488 
489         Object result = null;
490 
491         if (finderClassNameCacheEnabled) {
492             result = FinderCache.getResult(finderClassName, finderMethodName,
493                     finderParams, finderArgs, getSessionFactory());
494         }
495 
496         if (result == null) {
497             Session session = null;
498 
499             try {
500                 session = openSession();
501 
502                 StringMaker query = new StringMaker();
503 
504                 query.append("SELECT COUNT(*) ");
505                 query.append(
506                     "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
507 
508                 query.append("classNameId = ?");
509 
510                 query.append(" AND ");
511 
512                 query.append("classPK = ?");
513 
514                 query.append(" ");
515 
516                 Query q = session.createQuery(query.toString());
517 
518                 int queryPos = 0;
519 
520                 q.setLong(queryPos++, classNameId);
521 
522                 q.setLong(queryPos++, classPK);
523 
524                 Long count = null;
525 
526                 Iterator itr = q.list().iterator();
527 
528                 if (itr.hasNext()) {
529                     count = (Long)itr.next();
530                 }
531 
532                 if (count == null) {
533                     count = new Long(0);
534                 }
535 
536                 FinderCache.putResult(finderClassNameCacheEnabled,
537                     finderClassName, finderMethodName, finderParams,
538                     finderArgs, count);
539 
540                 return count.intValue();
541             }
542             catch (Exception e) {
543                 throw HibernateUtil.processException(e);
544             }
545             finally {
546                 closeSession(session);
547             }
548         }
549         else {
550             return ((Long)result).intValue();
551         }
552     }
553 
554     public int countAll() throws SystemException {
555         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
556         String finderClassName = RatingsStats.class.getName();
557         String finderMethodName = "countAll";
558         String[] finderParams = new String[] {  };
559         Object[] finderArgs = new Object[] {  };
560 
561         Object result = null;
562 
563         if (finderClassNameCacheEnabled) {
564             result = FinderCache.getResult(finderClassName, finderMethodName,
565                     finderParams, finderArgs, getSessionFactory());
566         }
567 
568         if (result == null) {
569             Session session = null;
570 
571             try {
572                 session = openSession();
573 
574                 Query q = session.createQuery(
575                         "SELECT COUNT(*) FROM com.liferay.portlet.ratings.model.RatingsStats");
576 
577                 Long count = null;
578 
579                 Iterator itr = q.list().iterator();
580 
581                 if (itr.hasNext()) {
582                     count = (Long)itr.next();
583                 }
584 
585                 if (count == null) {
586                     count = new Long(0);
587                 }
588 
589                 FinderCache.putResult(finderClassNameCacheEnabled,
590                     finderClassName, finderMethodName, finderParams,
591                     finderArgs, count);
592 
593                 return count.intValue();
594             }
595             catch (Exception e) {
596                 throw HibernateUtil.processException(e);
597             }
598             finally {
599                 closeSession(session);
600             }
601         }
602         else {
603             return ((Long)result).intValue();
604         }
605     }
606 
607     protected void initDao() {
608     }
609 
610     private static ModelListener _getListener() {
611         if (Validator.isNotNull(_LISTENER)) {
612             try {
613                 return (ModelListener)Class.forName(_LISTENER).newInstance();
614             }
615             catch (Exception e) {
616                 _log.error(e);
617             }
618         }
619 
620         return null;
621     }
622 
623     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
624                 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats"));
625     private static Log _log = LogFactory.getLog(RatingsStatsPersistenceImpl.class);
626 }