1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.ratings.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.ratings.NoSuchStatsException;
23  import com.liferay.portlet.ratings.model.RatingsStats;
24  import com.liferay.portlet.ratings.service.base.RatingsStatsLocalServiceBaseImpl;
25  
26  import java.util.List;
27  
28  /**
29   * <a href="RatingsStatsLocalServiceImpl.java.html"><b><i>View Source</i></b>
30   * </a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class RatingsStatsLocalServiceImpl
35      extends RatingsStatsLocalServiceBaseImpl {
36  
37      public RatingsStats addStats(long classNameId, long classPK)
38          throws SystemException {
39  
40          long statsId = counterLocalService.increment();
41  
42          RatingsStats stats = ratingsStatsPersistence.create(statsId);
43  
44          stats.setClassNameId(classNameId);
45          stats.setClassPK(classPK);
46          stats.setTotalEntries(0);
47          stats.setTotalScore(0.0);
48          stats.setAverageScore(0.0);
49  
50          try {
51              ratingsStatsPersistence.update(stats, false);
52          }
53          catch (SystemException se) {
54              if (_log.isWarnEnabled()) {
55                  _log.warn(
56                      "Add failed, fetch {classNameId=" + classNameId +
57                          ", classPK=" + classPK + "}");
58              }
59  
60              stats = ratingsStatsPersistence.fetchByC_C(
61                  classNameId, classPK, false);
62  
63              if (stats == null) {
64                  throw se;
65              }
66          }
67  
68          return stats;
69      }
70  
71      public void deleteStats(String className, long classPK)
72          throws SystemException {
73  
74          long classNameId = PortalUtil.getClassNameId(className);
75  
76          try {
77              ratingsStatsPersistence.removeByC_C(classNameId, classPK);
78          }
79          catch (NoSuchStatsException nsse) {
80              _log.warn(nsse);
81          }
82  
83          ratingsEntryPersistence.removeByC_C(classNameId, classPK);
84      }
85  
86      public RatingsStats getStats(long statsId)
87          throws PortalException, SystemException {
88  
89          return ratingsStatsPersistence.findByPrimaryKey(statsId);
90      }
91  
92      public List<RatingsStats> getStats(String className, List<Long> classPKs)
93          throws SystemException {
94  
95          long classNameId = PortalUtil.getClassNameId(className);
96  
97          return ratingsStatsFinder.findByC_C(classNameId, classPKs);
98      }
99  
100     public RatingsStats getStats(String className, long classPK)
101         throws SystemException {
102 
103         long classNameId = PortalUtil.getClassNameId(className);
104 
105         RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
106             classNameId, classPK);
107 
108         if (stats == null) {
109             stats = ratingsStatsLocalService.addStats(classNameId, classPK);
110         }
111 
112         return stats;
113     }
114 
115     private static Log _log = LogFactoryUtil.getLog(
116         RatingsStatsLocalServiceImpl.class);
117 
118 }