001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.exception.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    /**
028     * @author Brian Wing Shun Chan
029     */
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 fetchStats(String className, long classPK) {
084                    long classNameId = classNameLocalService.getClassNameId(className);
085    
086                    RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
087                            classNameId, classPK);
088    
089                    return stats;
090            }
091    
092            @Override
093            public RatingsStats getStats(long statsId) throws PortalException {
094                    return ratingsStatsPersistence.findByPrimaryKey(statsId);
095            }
096    
097            @Override
098            public List<RatingsStats> getStats(String className, List<Long> classPKs) {
099                    long classNameId = classNameLocalService.getClassNameId(className);
100    
101                    return ratingsStatsFinder.findByC_C(classNameId, classPKs);
102            }
103    
104            @Override
105            public RatingsStats getStats(String className, long classPK) {
106                    long classNameId = classNameLocalService.getClassNameId(className);
107    
108                    RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
109                            classNameId, classPK);
110    
111                    if (stats == null) {
112                            stats = ratingsStatsLocalService.addStats(classNameId, classPK);
113                    }
114    
115                    return stats;
116            }
117    
118            private static final Log _log = LogFactoryUtil.getLog(
119                    RatingsStatsLocalServiceImpl.class);
120    
121    }