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