001    /**
002     * Copyright (c) 2000-2013 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.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    /**
029     * @author Brian Wing Shun Chan
030     */
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, false);
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                            _log.warn(nsse);
080                    }
081    
082                    ratingsEntryPersistence.removeByC_C(classNameId, classPK);
083            }
084    
085            @Override
086            public RatingsStats getStats(long statsId)
087                    throws PortalException, SystemException {
088    
089                    return ratingsStatsPersistence.findByPrimaryKey(statsId);
090            }
091    
092            @Override
093            public List<RatingsStats> getStats(String className, List<Long> classPKs)
094                    throws SystemException {
095    
096                    long classNameId = PortalUtil.getClassNameId(className);
097    
098                    return ratingsStatsFinder.findByC_C(classNameId, classPKs);
099            }
100    
101            @Override
102            public RatingsStats getStats(String className, long classPK)
103                    throws SystemException {
104    
105                    long classNameId = PortalUtil.getClassNameId(className);
106    
107                    RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
108                            classNameId, classPK);
109    
110                    if (stats == null) {
111                            stats = ratingsStatsLocalService.addStats(classNameId, classPK);
112                    }
113    
114                    return stats;
115            }
116    
117            private static Log _log = LogFactoryUtil.getLog(
118                    RatingsStatsLocalServiceImpl.class);
119    
120    }