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.NoSuchStatsException;
36  import com.liferay.portlet.ratings.model.RatingsStats;
37  import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
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="RatingsStatsPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class RatingsStatsPersistenceImpl extends BasePersistence
58      implements RatingsStatsPersistence {
59      public RatingsStats create(long statsId) {
60          RatingsStats ratingsStats = new RatingsStatsImpl();
61          ratingsStats.setNew(true);
62          ratingsStats.setPrimaryKey(statsId);
63  
64          return ratingsStats;
65      }
66  
67      public RatingsStats remove(long statsId)
68          throws NoSuchStatsException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
75                      new Long(statsId));
76  
77              if (ratingsStats == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No RatingsStats exists with the primary key " +
80                          statsId);
81                  }
82  
83                  throw new NoSuchStatsException(
84                      "No RatingsStats exists with the primary key " + statsId);
85              }
86  
87              return remove(ratingsStats);
88          }
89          catch (NoSuchStatsException 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 RatingsStats remove(RatingsStats ratingsStats)
101         throws SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106             session.delete(ratingsStats);
107             session.flush();
108 
109             return ratingsStats;
110         }
111         catch (Exception e) {
112             throw HibernateUtil.processException(e);
113         }
114         finally {
115             closeSession(session);
116             FinderCache.clearCache(RatingsStats.class.getName());
117         }
118     }
119 
120     public RatingsStats update(
121         com.liferay.portlet.ratings.model.RatingsStats ratingsStats)
122         throws SystemException {
123         return update(ratingsStats, false);
124     }
125 
126     public RatingsStats update(
127         com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
128         boolean merge) throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             if (merge) {
135                 session.merge(ratingsStats);
136             }
137             else {
138                 if (ratingsStats.isNew()) {
139                     session.save(ratingsStats);
140                 }
141             }
142 
143             session.flush();
144             ratingsStats.setNew(false);
145 
146             return ratingsStats;
147         }
148         catch (Exception e) {
149             throw HibernateUtil.processException(e);
150         }
151         finally {
152             closeSession(session);
153             FinderCache.clearCache(RatingsStats.class.getName());
154         }
155     }
156 
157     public RatingsStats findByPrimaryKey(long statsId)
158         throws NoSuchStatsException, SystemException {
159         RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
160 
161         if (ratingsStats == null) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn("No RatingsStats exists with the primary key " +
164                     statsId);
165             }
166 
167             throw new NoSuchStatsException(
168                 "No RatingsStats exists with the primary key " + statsId);
169         }
170 
171         return ratingsStats;
172     }
173 
174     public RatingsStats fetchByPrimaryKey(long statsId)
175         throws SystemException {
176         Session session = null;
177 
178         try {
179             session = openSession();
180 
181             return (RatingsStats)session.get(RatingsStatsImpl.class,
182                 new Long(statsId));
183         }
184         catch (Exception e) {
185             throw HibernateUtil.processException(e);
186         }
187         finally {
188             closeSession(session);
189         }
190     }
191 
192     public RatingsStats findByC_C(long classNameId, long classPK)
193         throws NoSuchStatsException, SystemException {
194         RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
195 
196         if (ratingsStats == null) {
197             StringMaker msg = new StringMaker();
198             msg.append("No RatingsStats exists with the key ");
199             msg.append(StringPool.OPEN_CURLY_BRACE);
200             msg.append("classNameId=");
201             msg.append(classNameId);
202             msg.append(", ");
203             msg.append("classPK=");
204             msg.append(classPK);
205             msg.append(StringPool.CLOSE_CURLY_BRACE);
206 
207             if (_log.isWarnEnabled()) {
208                 _log.warn(msg.toString());
209             }
210 
211             throw new NoSuchStatsException(msg.toString());
212         }
213 
214         return ratingsStats;
215     }
216 
217     public RatingsStats fetchByC_C(long classNameId, long classPK)
218         throws SystemException {
219         String finderClassName = RatingsStats.class.getName();
220         String finderMethodName = "fetchByC_C";
221         String[] finderParams = new String[] {
222                 Long.class.getName(), Long.class.getName()
223             };
224         Object[] finderArgs = new Object[] {
225                 new Long(classNameId), new Long(classPK)
226             };
227         Object result = FinderCache.getResult(finderClassName,
228                 finderMethodName, finderParams, finderArgs, getSessionFactory());
229 
230         if (result == null) {
231             Session session = null;
232 
233             try {
234                 session = openSession();
235 
236                 StringMaker query = new StringMaker();
237                 query.append(
238                     "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
239                 query.append("classNameId = ?");
240                 query.append(" AND ");
241                 query.append("classPK = ?");
242                 query.append(" ");
243 
244                 Query q = session.createQuery(query.toString());
245                 int queryPos = 0;
246                 q.setLong(queryPos++, classNameId);
247                 q.setLong(queryPos++, classPK);
248 
249                 List list = q.list();
250                 FinderCache.putResult(finderClassName, finderMethodName,
251                     finderParams, finderArgs, list);
252 
253                 if (list.size() == 0) {
254                     return null;
255                 }
256                 else {
257                     return (RatingsStats)list.get(0);
258                 }
259             }
260             catch (Exception e) {
261                 throw HibernateUtil.processException(e);
262             }
263             finally {
264                 closeSession(session);
265             }
266         }
267         else {
268             List list = (List)result;
269 
270             if (list.size() == 0) {
271                 return null;
272             }
273             else {
274                 return (RatingsStats)list.get(0);
275             }
276         }
277     }
278 
279     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
280         throws SystemException {
281         Session session = null;
282 
283         try {
284             session = openSession();
285 
286             DynamicQuery query = queryInitializer.initialize(session);
287 
288             return query.list();
289         }
290         catch (Exception e) {
291             throw HibernateUtil.processException(e);
292         }
293         finally {
294             closeSession(session);
295         }
296     }
297 
298     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
299         int begin, int end) throws SystemException {
300         Session session = null;
301 
302         try {
303             session = openSession();
304 
305             DynamicQuery query = queryInitializer.initialize(session);
306             query.setLimit(begin, end);
307 
308             return query.list();
309         }
310         catch (Exception e) {
311             throw HibernateUtil.processException(e);
312         }
313         finally {
314             closeSession(session);
315         }
316     }
317 
318     public List findAll() throws SystemException {
319         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
320     }
321 
322     public List findAll(int begin, int end) throws SystemException {
323         return findAll(begin, end, null);
324     }
325 
326     public List findAll(int begin, int end, OrderByComparator obc)
327         throws SystemException {
328         String finderClassName = RatingsStats.class.getName();
329         String finderMethodName = "findAll";
330         String[] finderParams = new String[] {
331                 "java.lang.Integer", "java.lang.Integer",
332                 "com.liferay.portal.kernel.util.OrderByComparator"
333             };
334         Object[] finderArgs = new Object[] {
335                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
336             };
337         Object result = FinderCache.getResult(finderClassName,
338                 finderMethodName, finderParams, finderArgs, getSessionFactory());
339 
340         if (result == null) {
341             Session session = null;
342 
343             try {
344                 session = openSession();
345 
346                 StringMaker query = new StringMaker();
347                 query.append(
348                     "FROM com.liferay.portlet.ratings.model.RatingsStats ");
349 
350                 if (obc != null) {
351                     query.append("ORDER BY ");
352                     query.append(obc.getOrderBy());
353                 }
354 
355                 Query q = session.createQuery(query.toString());
356                 List list = QueryUtil.list(q, getDialect(), begin, end);
357 
358                 if (obc == null) {
359                     Collections.sort(list);
360                 }
361 
362                 FinderCache.putResult(finderClassName, finderMethodName,
363                     finderParams, finderArgs, list);
364 
365                 return list;
366             }
367             catch (Exception e) {
368                 throw HibernateUtil.processException(e);
369             }
370             finally {
371                 closeSession(session);
372             }
373         }
374         else {
375             return (List)result;
376         }
377     }
378 
379     public void removeByC_C(long classNameId, long classPK)
380         throws NoSuchStatsException, SystemException {
381         RatingsStats ratingsStats = findByC_C(classNameId, classPK);
382         remove(ratingsStats);
383     }
384 
385     public void removeAll() throws SystemException {
386         Iterator itr = findAll().iterator();
387 
388         while (itr.hasNext()) {
389             remove((RatingsStats)itr.next());
390         }
391     }
392 
393     public int countByC_C(long classNameId, long classPK)
394         throws SystemException {
395         String finderClassName = RatingsStats.class.getName();
396         String finderMethodName = "countByC_C";
397         String[] finderParams = new String[] {
398                 Long.class.getName(), Long.class.getName()
399             };
400         Object[] finderArgs = new Object[] {
401                 new Long(classNameId), new Long(classPK)
402             };
403         Object result = FinderCache.getResult(finderClassName,
404                 finderMethodName, finderParams, finderArgs, getSessionFactory());
405 
406         if (result == null) {
407             Session session = null;
408 
409             try {
410                 session = openSession();
411 
412                 StringMaker query = new StringMaker();
413                 query.append("SELECT COUNT(*) ");
414                 query.append(
415                     "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
416                 query.append("classNameId = ?");
417                 query.append(" AND ");
418                 query.append("classPK = ?");
419                 query.append(" ");
420 
421                 Query q = session.createQuery(query.toString());
422                 int queryPos = 0;
423                 q.setLong(queryPos++, classNameId);
424                 q.setLong(queryPos++, classPK);
425 
426                 Long count = null;
427                 Iterator itr = q.list().iterator();
428 
429                 if (itr.hasNext()) {
430                     count = (Long)itr.next();
431                 }
432 
433                 if (count == null) {
434                     count = new Long(0);
435                 }
436 
437                 FinderCache.putResult(finderClassName, finderMethodName,
438                     finderParams, finderArgs, count);
439 
440                 return count.intValue();
441             }
442             catch (Exception e) {
443                 throw HibernateUtil.processException(e);
444             }
445             finally {
446                 closeSession(session);
447             }
448         }
449         else {
450             return ((Long)result).intValue();
451         }
452     }
453 
454     public int countAll() throws SystemException {
455         String finderClassName = RatingsStats.class.getName();
456         String finderMethodName = "countAll";
457         String[] finderParams = new String[] {  };
458         Object[] finderArgs = new Object[] {  };
459         Object result = FinderCache.getResult(finderClassName,
460                 finderMethodName, finderParams, finderArgs, getSessionFactory());
461 
462         if (result == null) {
463             Session session = null;
464 
465             try {
466                 session = openSession();
467 
468                 StringMaker query = new StringMaker();
469                 query.append("SELECT COUNT(*) ");
470                 query.append(
471                     "FROM com.liferay.portlet.ratings.model.RatingsStats");
472 
473                 Query q = session.createQuery(query.toString());
474                 Long count = null;
475                 Iterator itr = q.list().iterator();
476 
477                 if (itr.hasNext()) {
478                     count = (Long)itr.next();
479                 }
480 
481                 if (count == null) {
482                     count = new Long(0);
483                 }
484 
485                 FinderCache.putResult(finderClassName, finderMethodName,
486                     finderParams, finderArgs, count);
487 
488                 return count.intValue();
489             }
490             catch (Exception e) {
491                 throw HibernateUtil.processException(e);
492             }
493             finally {
494                 closeSession(session);
495             }
496         }
497         else {
498             return ((Long)result).intValue();
499         }
500     }
501 
502     protected void initDao() {
503     }
504 
505     private static Log _log = LogFactory.getLog(RatingsStatsPersistenceImpl.class);
506 }