1   /**
2    * Copyright (c) 2000-2009 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.annotation.BeanReference;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.log.Log;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.service.persistence.BatchSessionUtil;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import com.liferay.portlet.ratings.NoSuchStatsException;
44  import com.liferay.portlet.ratings.model.RatingsStats;
45  import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
46  import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  /**
54   * <a href="RatingsStatsPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class RatingsStatsPersistenceImpl extends BasePersistenceImpl
60      implements RatingsStatsPersistence {
61      public RatingsStats create(long statsId) {
62          RatingsStats ratingsStats = new RatingsStatsImpl();
63  
64          ratingsStats.setNew(true);
65          ratingsStats.setPrimaryKey(statsId);
66  
67          return ratingsStats;
68      }
69  
70      public RatingsStats remove(long statsId)
71          throws NoSuchStatsException, SystemException {
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
78                      new Long(statsId));
79  
80              if (ratingsStats == null) {
81                  if (_log.isWarnEnabled()) {
82                      _log.warn("No RatingsStats exists with the primary key " +
83                          statsId);
84                  }
85  
86                  throw new NoSuchStatsException(
87                      "No RatingsStats exists with the primary key " + statsId);
88              }
89  
90              return remove(ratingsStats);
91          }
92          catch (NoSuchStatsException nsee) {
93              throw nsee;
94          }
95          catch (Exception e) {
96              throw processException(e);
97          }
98          finally {
99              closeSession(session);
100         }
101     }
102 
103     public RatingsStats remove(RatingsStats ratingsStats)
104         throws SystemException {
105         for (ModelListener listener : listeners) {
106             listener.onBeforeRemove(ratingsStats);
107         }
108 
109         ratingsStats = removeImpl(ratingsStats);
110 
111         for (ModelListener listener : listeners) {
112             listener.onAfterRemove(ratingsStats);
113         }
114 
115         return ratingsStats;
116     }
117 
118     protected RatingsStats removeImpl(RatingsStats ratingsStats)
119         throws SystemException {
120         Session session = null;
121 
122         try {
123             session = openSession();
124 
125             if (BatchSessionUtil.isEnabled()) {
126                 Object staleObject = session.get(RatingsStatsImpl.class,
127                         ratingsStats.getPrimaryKeyObj());
128 
129                 if (staleObject != null) {
130                     session.evict(staleObject);
131                 }
132             }
133 
134             session.delete(ratingsStats);
135 
136             session.flush();
137 
138             return ratingsStats;
139         }
140         catch (Exception e) {
141             throw processException(e);
142         }
143         finally {
144             closeSession(session);
145 
146             FinderCacheUtil.clearCache(RatingsStats.class.getName());
147         }
148     }
149 
150     /**
151      * @deprecated Use <code>update(RatingsStats ratingsStats, boolean merge)</code>.
152      */
153     public RatingsStats update(RatingsStats ratingsStats)
154         throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(RatingsStats ratingsStats) method. Use update(RatingsStats ratingsStats, boolean merge) instead.");
158         }
159 
160         return update(ratingsStats, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        ratingsStats the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when ratingsStats is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public RatingsStats update(RatingsStats ratingsStats, boolean merge)
177         throws SystemException {
178         boolean isNew = ratingsStats.isNew();
179 
180         for (ModelListener listener : listeners) {
181             if (isNew) {
182                 listener.onBeforeCreate(ratingsStats);
183             }
184             else {
185                 listener.onBeforeUpdate(ratingsStats);
186             }
187         }
188 
189         ratingsStats = updateImpl(ratingsStats, merge);
190 
191         for (ModelListener listener : listeners) {
192             if (isNew) {
193                 listener.onAfterCreate(ratingsStats);
194             }
195             else {
196                 listener.onAfterUpdate(ratingsStats);
197             }
198         }
199 
200         return ratingsStats;
201     }
202 
203     public RatingsStats updateImpl(
204         com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
205         boolean merge) throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             BatchSessionUtil.update(session, ratingsStats, merge);
212 
213             ratingsStats.setNew(false);
214 
215             return ratingsStats;
216         }
217         catch (Exception e) {
218             throw processException(e);
219         }
220         finally {
221             closeSession(session);
222 
223             FinderCacheUtil.clearCache(RatingsStats.class.getName());
224         }
225     }
226 
227     public RatingsStats findByPrimaryKey(long statsId)
228         throws NoSuchStatsException, SystemException {
229         RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
230 
231         if (ratingsStats == null) {
232             if (_log.isWarnEnabled()) {
233                 _log.warn("No RatingsStats exists with the primary key " +
234                     statsId);
235             }
236 
237             throw new NoSuchStatsException(
238                 "No RatingsStats exists with the primary key " + statsId);
239         }
240 
241         return ratingsStats;
242     }
243 
244     public RatingsStats fetchByPrimaryKey(long statsId)
245         throws SystemException {
246         Session session = null;
247 
248         try {
249             session = openSession();
250 
251             return (RatingsStats)session.get(RatingsStatsImpl.class,
252                 new Long(statsId));
253         }
254         catch (Exception e) {
255             throw processException(e);
256         }
257         finally {
258             closeSession(session);
259         }
260     }
261 
262     public RatingsStats findByC_C(long classNameId, long classPK)
263         throws NoSuchStatsException, SystemException {
264         RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
265 
266         if (ratingsStats == null) {
267             StringBuilder msg = new StringBuilder();
268 
269             msg.append("No RatingsStats exists with the key {");
270 
271             msg.append("classNameId=" + classNameId);
272 
273             msg.append(", ");
274             msg.append("classPK=" + classPK);
275 
276             msg.append(StringPool.CLOSE_CURLY_BRACE);
277 
278             if (_log.isWarnEnabled()) {
279                 _log.warn(msg.toString());
280             }
281 
282             throw new NoSuchStatsException(msg.toString());
283         }
284 
285         return ratingsStats;
286     }
287 
288     public RatingsStats fetchByC_C(long classNameId, long classPK)
289         throws SystemException {
290         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
291         String finderClassName = RatingsStats.class.getName();
292         String finderMethodName = "fetchByC_C";
293         String[] finderParams = new String[] {
294                 Long.class.getName(), Long.class.getName()
295             };
296         Object[] finderArgs = new Object[] {
297                 new Long(classNameId), new Long(classPK)
298             };
299 
300         Object result = null;
301 
302         if (finderClassNameCacheEnabled) {
303             result = FinderCacheUtil.getResult(finderClassName,
304                     finderMethodName, finderParams, finderArgs, this);
305         }
306 
307         if (result == null) {
308             Session session = null;
309 
310             try {
311                 session = openSession();
312 
313                 StringBuilder query = new StringBuilder();
314 
315                 query.append(
316                     "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
317 
318                 query.append("classNameId = ?");
319 
320                 query.append(" AND ");
321 
322                 query.append("classPK = ?");
323 
324                 query.append(" ");
325 
326                 Query q = session.createQuery(query.toString());
327 
328                 QueryPos qPos = QueryPos.getInstance(q);
329 
330                 qPos.add(classNameId);
331 
332                 qPos.add(classPK);
333 
334                 List<RatingsStats> list = q.list();
335 
336                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
337                     finderClassName, finderMethodName, finderParams,
338                     finderArgs, list);
339 
340                 if (list.size() == 0) {
341                     return null;
342                 }
343                 else {
344                     return list.get(0);
345                 }
346             }
347             catch (Exception e) {
348                 throw processException(e);
349             }
350             finally {
351                 closeSession(session);
352             }
353         }
354         else {
355             List<RatingsStats> list = (List<RatingsStats>)result;
356 
357             if (list.size() == 0) {
358                 return null;
359             }
360             else {
361                 return list.get(0);
362             }
363         }
364     }
365 
366     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
367         throws SystemException {
368         Session session = null;
369 
370         try {
371             session = openSession();
372 
373             dynamicQuery.compile(session);
374 
375             return dynamicQuery.list();
376         }
377         catch (Exception e) {
378             throw processException(e);
379         }
380         finally {
381             closeSession(session);
382         }
383     }
384 
385     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
386         int start, int end) throws SystemException {
387         Session session = null;
388 
389         try {
390             session = openSession();
391 
392             dynamicQuery.setLimit(start, end);
393 
394             dynamicQuery.compile(session);
395 
396             return dynamicQuery.list();
397         }
398         catch (Exception e) {
399             throw processException(e);
400         }
401         finally {
402             closeSession(session);
403         }
404     }
405 
406     public List<RatingsStats> findAll() throws SystemException {
407         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
408     }
409 
410     public List<RatingsStats> findAll(int start, int end)
411         throws SystemException {
412         return findAll(start, end, null);
413     }
414 
415     public List<RatingsStats> findAll(int start, int end, OrderByComparator obc)
416         throws SystemException {
417         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
418         String finderClassName = RatingsStats.class.getName();
419         String finderMethodName = "findAll";
420         String[] finderParams = new String[] {
421                 "java.lang.Integer", "java.lang.Integer",
422                 "com.liferay.portal.kernel.util.OrderByComparator"
423             };
424         Object[] finderArgs = new Object[] {
425                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
426             };
427 
428         Object result = null;
429 
430         if (finderClassNameCacheEnabled) {
431             result = FinderCacheUtil.getResult(finderClassName,
432                     finderMethodName, finderParams, finderArgs, this);
433         }
434 
435         if (result == null) {
436             Session session = null;
437 
438             try {
439                 session = openSession();
440 
441                 StringBuilder query = new StringBuilder();
442 
443                 query.append(
444                     "FROM com.liferay.portlet.ratings.model.RatingsStats ");
445 
446                 if (obc != null) {
447                     query.append("ORDER BY ");
448                     query.append(obc.getOrderBy());
449                 }
450 
451                 Query q = session.createQuery(query.toString());
452 
453                 List<RatingsStats> list = null;
454 
455                 if (obc == null) {
456                     list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
457                             start, end, false);
458 
459                     Collections.sort(list);
460                 }
461                 else {
462                     list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
463                             start, end);
464                 }
465 
466                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
467                     finderClassName, finderMethodName, finderParams,
468                     finderArgs, list);
469 
470                 return list;
471             }
472             catch (Exception e) {
473                 throw processException(e);
474             }
475             finally {
476                 closeSession(session);
477             }
478         }
479         else {
480             return (List<RatingsStats>)result;
481         }
482     }
483 
484     public void removeByC_C(long classNameId, long classPK)
485         throws NoSuchStatsException, SystemException {
486         RatingsStats ratingsStats = findByC_C(classNameId, classPK);
487 
488         remove(ratingsStats);
489     }
490 
491     public void removeAll() throws SystemException {
492         for (RatingsStats ratingsStats : findAll()) {
493             remove(ratingsStats);
494         }
495     }
496 
497     public int countByC_C(long classNameId, long classPK)
498         throws SystemException {
499         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
500         String finderClassName = RatingsStats.class.getName();
501         String finderMethodName = "countByC_C";
502         String[] finderParams = new String[] {
503                 Long.class.getName(), Long.class.getName()
504             };
505         Object[] finderArgs = new Object[] {
506                 new Long(classNameId), new Long(classPK)
507             };
508 
509         Object result = null;
510 
511         if (finderClassNameCacheEnabled) {
512             result = FinderCacheUtil.getResult(finderClassName,
513                     finderMethodName, finderParams, finderArgs, this);
514         }
515 
516         if (result == null) {
517             Session session = null;
518 
519             try {
520                 session = openSession();
521 
522                 StringBuilder query = new StringBuilder();
523 
524                 query.append("SELECT COUNT(*) ");
525                 query.append(
526                     "FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
527 
528                 query.append("classNameId = ?");
529 
530                 query.append(" AND ");
531 
532                 query.append("classPK = ?");
533 
534                 query.append(" ");
535 
536                 Query q = session.createQuery(query.toString());
537 
538                 QueryPos qPos = QueryPos.getInstance(q);
539 
540                 qPos.add(classNameId);
541 
542                 qPos.add(classPK);
543 
544                 Long count = null;
545 
546                 Iterator<Long> itr = q.list().iterator();
547 
548                 if (itr.hasNext()) {
549                     count = itr.next();
550                 }
551 
552                 if (count == null) {
553                     count = new Long(0);
554                 }
555 
556                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
557                     finderClassName, finderMethodName, finderParams,
558                     finderArgs, count);
559 
560                 return count.intValue();
561             }
562             catch (Exception e) {
563                 throw processException(e);
564             }
565             finally {
566                 closeSession(session);
567             }
568         }
569         else {
570             return ((Long)result).intValue();
571         }
572     }
573 
574     public int countAll() throws SystemException {
575         boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
576         String finderClassName = RatingsStats.class.getName();
577         String finderMethodName = "countAll";
578         String[] finderParams = new String[] {  };
579         Object[] finderArgs = new Object[] {  };
580 
581         Object result = null;
582 
583         if (finderClassNameCacheEnabled) {
584             result = FinderCacheUtil.getResult(finderClassName,
585                     finderMethodName, finderParams, finderArgs, this);
586         }
587 
588         if (result == null) {
589             Session session = null;
590 
591             try {
592                 session = openSession();
593 
594                 Query q = session.createQuery(
595                         "SELECT COUNT(*) FROM com.liferay.portlet.ratings.model.RatingsStats");
596 
597                 Long count = null;
598 
599                 Iterator<Long> itr = q.list().iterator();
600 
601                 if (itr.hasNext()) {
602                     count = itr.next();
603                 }
604 
605                 if (count == null) {
606                     count = new Long(0);
607                 }
608 
609                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
610                     finderClassName, finderMethodName, finderParams,
611                     finderArgs, count);
612 
613                 return count.intValue();
614             }
615             catch (Exception e) {
616                 throw processException(e);
617             }
618             finally {
619                 closeSession(session);
620             }
621         }
622         else {
623             return ((Long)result).intValue();
624         }
625     }
626 
627     public void afterPropertiesSet() {
628         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
629                     com.liferay.portal.util.PropsUtil.get(
630                         "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
631 
632         if (listenerClassNames.length > 0) {
633             try {
634                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
635 
636                 for (String listenerClassName : listenerClassNames) {
637                     listenersList.add((ModelListener)Class.forName(
638                             listenerClassName).newInstance());
639                 }
640 
641                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
642             }
643             catch (Exception e) {
644                 _log.error(e);
645             }
646         }
647     }
648 
649     @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence.impl")
650     protected com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence ratingsEntryPersistence;
651     @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence.impl")
652     protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
653     private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
654 }