1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.ratings.NoSuchStatsException;
31  import com.liferay.portlet.ratings.model.RatingsStats;
32  import com.liferay.portlet.ratings.service.base.RatingsStatsLocalServiceBaseImpl;
33  
34  /**
35   * <a href="RatingsStatsLocalServiceImpl.java.html"><b><i>View Source</i></b>
36   * </a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class RatingsStatsLocalServiceImpl
41      extends RatingsStatsLocalServiceBaseImpl {
42  
43      public RatingsStats addStats(long classNameId, long classPK)
44          throws SystemException {
45  
46          long statsId = counterLocalService.increment();
47  
48          RatingsStats stats = ratingsStatsPersistence.create(statsId);
49  
50          stats.setClassNameId(classNameId);
51          stats.setClassPK(classPK);
52          stats.setTotalEntries(0);
53          stats.setTotalScore(0.0);
54          stats.setAverageScore(0.0);
55  
56          try {
57              ratingsStatsPersistence.update(stats, false);
58          }
59          catch (SystemException se) {
60              if (_log.isWarnEnabled()) {
61                  _log.warn(
62                      "Add failed, fetch {classNameId=" + classNameId +
63                          ", classPK=" + classPK + "}");
64              }
65  
66              stats = ratingsStatsPersistence.fetchByC_C(
67                  classNameId, classPK, false);
68  
69              if (stats == null) {
70                  throw se;
71              }
72          }
73  
74          return stats;
75      }
76  
77      public void deleteStats(String className, long classPK)
78          throws SystemException {
79  
80          long classNameId = PortalUtil.getClassNameId(className);
81  
82          try {
83              ratingsStatsPersistence.removeByC_C(classNameId, classPK);
84          }
85          catch (NoSuchStatsException nsse) {
86              _log.warn(nsse);
87          }
88  
89          ratingsEntryPersistence.removeByC_C(classNameId, classPK);
90      }
91  
92      public RatingsStats getStats(long statsId)
93          throws PortalException, SystemException {
94  
95          return ratingsStatsPersistence.findByPrimaryKey(statsId);
96      }
97  
98      public RatingsStats getStats(String className, long classPK)
99          throws SystemException {
100 
101         long classNameId = PortalUtil.getClassNameId(className);
102 
103         RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
104             classNameId, classPK);
105 
106         if (stats == null) {
107             stats = ratingsStatsLocalService.addStats(classNameId, classPK);
108         }
109 
110         return stats;
111     }
112 
113     private static Log _log =
114         LogFactoryUtil.getLog(RatingsStatsLocalServiceImpl.class);
115 
116 }