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