001
014
015 package com.liferay.portlet.ratings.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.util.PortalUtil;
022 import com.liferay.portlet.ratings.NoSuchStatsException;
023 import com.liferay.portlet.ratings.model.RatingsStats;
024 import com.liferay.portlet.ratings.service.base.RatingsStatsLocalServiceBaseImpl;
025
026 import java.util.List;
027
028
031 public class RatingsStatsLocalServiceImpl
032 extends RatingsStatsLocalServiceBaseImpl {
033
034 @Override
035 public RatingsStats addStats(long classNameId, long classPK)
036 throws SystemException {
037
038 long statsId = counterLocalService.increment();
039
040 RatingsStats stats = ratingsStatsPersistence.create(statsId);
041
042 stats.setClassNameId(classNameId);
043 stats.setClassPK(classPK);
044 stats.setTotalEntries(0);
045 stats.setTotalScore(0.0);
046 stats.setAverageScore(0.0);
047
048 try {
049 ratingsStatsPersistence.update(stats);
050 }
051 catch (SystemException se) {
052 if (_log.isWarnEnabled()) {
053 _log.warn(
054 "Add failed, fetch {classNameId=" + classNameId +
055 ", classPK=" + classPK + "}");
056 }
057
058 stats = ratingsStatsPersistence.fetchByC_C(
059 classNameId, classPK, false);
060
061 if (stats == null) {
062 throw se;
063 }
064 }
065
066 return stats;
067 }
068
069 @Override
070 public void deleteStats(String className, long classPK)
071 throws SystemException {
072
073 long classNameId = PortalUtil.getClassNameId(className);
074
075 try {
076 ratingsStatsPersistence.removeByC_C(classNameId, classPK);
077 }
078 catch (NoSuchStatsException nsse) {
079 if (_log.isWarnEnabled()) {
080 _log.warn(nsse);
081 }
082 }
083
084 ratingsEntryPersistence.removeByC_C(classNameId, classPK);
085 }
086
087 @Override
088 public RatingsStats fetchStats(String className, long classPK)
089 throws SystemException {
090
091 long classNameId = PortalUtil.getClassNameId(className);
092
093 RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
094 classNameId, classPK);
095
096 return stats;
097 }
098
099 @Override
100 public RatingsStats getStats(long statsId)
101 throws PortalException, SystemException {
102
103 return ratingsStatsPersistence.findByPrimaryKey(statsId);
104 }
105
106 @Override
107 public List<RatingsStats> getStats(String className, List<Long> classPKs)
108 throws SystemException {
109
110 long classNameId = PortalUtil.getClassNameId(className);
111
112 return ratingsStatsFinder.findByC_C(classNameId, classPKs);
113 }
114
115 @Override
116 public RatingsStats getStats(String className, long classPK)
117 throws SystemException {
118
119 long classNameId = PortalUtil.getClassNameId(className);
120
121 RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
122 classNameId, classPK);
123
124 if (stats == null) {
125 stats = ratingsStatsLocalService.addStats(classNameId, classPK);
126 }
127
128 return stats;
129 }
130
131 private static Log _log = LogFactoryUtil.getLog(
132 RatingsStatsLocalServiceImpl.class);
133
134 }