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);
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 getStats(long statsId)
089                    throws PortalException, SystemException {
090    
091                    return ratingsStatsPersistence.findByPrimaryKey(statsId);
092            }
093    
094            @Override
095            public List<RatingsStats> getStats(String className, List<Long> classPKs)
096                    throws SystemException {
097    
098                    long classNameId = PortalUtil.getClassNameId(className);
099    
100                    return ratingsStatsFinder.findByC_C(classNameId, classPKs);
101            }
102    
103            @Override
104            public RatingsStats getStats(String className, long classPK)
105                    throws SystemException {
106    
107                    long classNameId = PortalUtil.getClassNameId(className);
108    
109                    RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
110                            classNameId, classPK);
111    
112                    if (stats == null) {
113                            stats = ratingsStatsLocalService.addStats(classNameId, classPK);
114                    }
115    
116                    return stats;
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(
120                    RatingsStatsLocalServiceImpl.class);
121    
122    }