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 void deleteStats(String className, long classPK)
42          throws SystemException {
43  
44          long classNameId = PortalUtil.getClassNameId(className);
45  
46          try {
47              ratingsStatsPersistence.removeByC_C(classNameId, classPK);
48          }
49          catch (NoSuchStatsException nsse) {
50              _log.warn(nsse);
51          }
52  
53          ratingsEntryPersistence.removeByC_C(classNameId, classPK);
54      }
55  
56      public RatingsStats getStats(long statsId)
57          throws PortalException, SystemException {
58  
59          return ratingsStatsPersistence.findByPrimaryKey(statsId);
60      }
61  
62      public RatingsStats getStats(String className, long classPK)
63          throws SystemException {
64  
65          long classNameId = PortalUtil.getClassNameId(className);
66  
67          RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
68              classNameId, classPK);
69  
70          if (stats == null) {
71              long statsId = counterLocalService.increment();
72  
73              stats = ratingsStatsPersistence.create(statsId);
74  
75              stats.setClassNameId(classNameId);
76              stats.setClassPK(classPK);
77              stats.setTotalEntries(0);
78              stats.setTotalScore(0.0);
79              stats.setAverageScore(0.0);
80  
81              ratingsStatsPersistence.update(stats, false);
82          }
83  
84          return stats;
85      }
86  
87      private static Log _log =
88          LogFactoryUtil.getLog(RatingsStatsLocalServiceImpl.class);
89  
90  }