1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.ratings.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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  /**
27   * <a href="RatingsStatsLocalServiceImpl.java.html"><b><i>View Source</i></b>
28   * </a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class RatingsStatsLocalServiceImpl
33      extends RatingsStatsLocalServiceBaseImpl {
34  
35      public RatingsStats addStats(long classNameId, long classPK)
36          throws SystemException {
37  
38          long statsId = counterLocalService.increment();
39  
40          RatingsStats stats = ratingsStatsPersistence.create(statsId);
41  
42          stats.setClassNameId(classNameId);
43          stats.setClassPK(classPK);
44          stats.setTotalEntries(0);
45          stats.setTotalScore(0.0);
46          stats.setAverageScore(0.0);
47  
48          try {
49              ratingsStatsPersistence.update(stats, false);
50          }
51          catch (SystemException se) {
52              if (_log.isWarnEnabled()) {
53                  _log.warn(
54                      "Add failed, fetch {classNameId=" + classNameId +
55                          ", classPK=" + classPK + "}");
56              }
57  
58              stats = ratingsStatsPersistence.fetchByC_C(
59                  classNameId, classPK, false);
60  
61              if (stats == null) {
62                  throw se;
63              }
64          }
65  
66          return stats;
67      }
68  
69      public void deleteStats(String className, long classPK)
70          throws SystemException {
71  
72          long classNameId = PortalUtil.getClassNameId(className);
73  
74          try {
75              ratingsStatsPersistence.removeByC_C(classNameId, classPK);
76          }
77          catch (NoSuchStatsException nsse) {
78              _log.warn(nsse);
79          }
80  
81          ratingsEntryPersistence.removeByC_C(classNameId, classPK);
82      }
83  
84      public RatingsStats getStats(long statsId)
85          throws PortalException, SystemException {
86  
87          return ratingsStatsPersistence.findByPrimaryKey(statsId);
88      }
89  
90      public RatingsStats getStats(String className, long classPK)
91          throws SystemException {
92  
93          long classNameId = PortalUtil.getClassNameId(className);
94  
95          RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
96              classNameId, classPK);
97  
98          if (stats == null) {
99              stats = ratingsStatsLocalService.addStats(classNameId, classPK);
100         }
101 
102         return stats;
103     }
104 
105     private static Log _log = LogFactoryUtil.getLog(
106         RatingsStatsLocalServiceImpl.class);
107 
108 }