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.portlet.ratings.NoSuchStatsException;
022 import com.liferay.portlet.ratings.model.RatingsStats;
023 import com.liferay.portlet.ratings.service.base.RatingsStatsLocalServiceBaseImpl;
024
025 import java.util.List;
026
027
030 public class RatingsStatsLocalServiceImpl
031 extends RatingsStatsLocalServiceBaseImpl {
032
033 @Override
034 public RatingsStats addStats(long classNameId, long classPK) {
035 long statsId = counterLocalService.increment();
036
037 RatingsStats stats = ratingsStatsPersistence.create(statsId);
038
039 stats.setClassNameId(classNameId);
040 stats.setClassPK(classPK);
041 stats.setTotalEntries(0);
042 stats.setTotalScore(0.0);
043 stats.setAverageScore(0.0);
044
045 try {
046 ratingsStatsPersistence.update(stats);
047 }
048 catch (SystemException se) {
049 if (_log.isWarnEnabled()) {
050 _log.warn(
051 "Add failed, fetch {classNameId=" + classNameId +
052 ", classPK=" + classPK + "}");
053 }
054
055 stats = ratingsStatsPersistence.fetchByC_C(
056 classNameId, classPK, false);
057
058 if (stats == null) {
059 throw se;
060 }
061 }
062
063 return stats;
064 }
065
066 @Override
067 public void deleteStats(String className, long classPK) {
068 long classNameId = classNameLocalService.getClassNameId(className);
069
070 try {
071 ratingsStatsPersistence.removeByC_C(classNameId, classPK);
072 }
073 catch (NoSuchStatsException nsse) {
074 if (_log.isWarnEnabled()) {
075 _log.warn(nsse);
076 }
077 }
078
079 ratingsEntryPersistence.removeByC_C(classNameId, classPK);
080 }
081
082 @Override
083 public RatingsStats getStats(long statsId) throws PortalException {
084 return ratingsStatsPersistence.findByPrimaryKey(statsId);
085 }
086
087 @Override
088 public List<RatingsStats> getStats(String className, List<Long> classPKs) {
089 long classNameId = classNameLocalService.getClassNameId(className);
090
091 return ratingsStatsFinder.findByC_C(classNameId, classPKs);
092 }
093
094 @Override
095 public RatingsStats getStats(String className, long classPK) {
096 long classNameId = classNameLocalService.getClassNameId(className);
097
098 RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
099 classNameId, classPK);
100
101 if (stats == null) {
102 stats = ratingsStatsLocalService.addStats(classNameId, classPK);
103 }
104
105 return stats;
106 }
107
108 private static final Log _log = LogFactoryUtil.getLog(
109 RatingsStatsLocalServiceImpl.class);
110
111 }