001    /**
002     * Copyright (c) 2000-2010 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            public RatingsStats addStats(long classNameId, long classPK)
035                    throws SystemException {
036    
037                    long statsId = counterLocalService.increment();
038    
039                    RatingsStats stats = ratingsStatsPersistence.create(statsId);
040    
041                    stats.setClassNameId(classNameId);
042                    stats.setClassPK(classPK);
043                    stats.setTotalEntries(0);
044                    stats.setTotalScore(0.0);
045                    stats.setAverageScore(0.0);
046    
047                    try {
048                            ratingsStatsPersistence.update(stats, false);
049                    }
050                    catch (SystemException se) {
051                            if (_log.isWarnEnabled()) {
052                                    _log.warn(
053                                            "Add failed, fetch {classNameId=" + classNameId +
054                                                    ", classPK=" + classPK + "}");
055                            }
056    
057                            stats = ratingsStatsPersistence.fetchByC_C(
058                                    classNameId, classPK, false);
059    
060                            if (stats == null) {
061                                    throw se;
062                            }
063                    }
064    
065                    return stats;
066            }
067    
068            public void deleteStats(String className, long classPK)
069                    throws SystemException {
070    
071                    long classNameId = PortalUtil.getClassNameId(className);
072    
073                    try {
074                            ratingsStatsPersistence.removeByC_C(classNameId, classPK);
075                    }
076                    catch (NoSuchStatsException nsse) {
077                            _log.warn(nsse);
078                    }
079    
080                    ratingsEntryPersistence.removeByC_C(classNameId, classPK);
081            }
082    
083            public RatingsStats getStats(long statsId)
084                    throws PortalException, SystemException {
085    
086                    return ratingsStatsPersistence.findByPrimaryKey(statsId);
087            }
088    
089            public List<RatingsStats> getStats(String className, List<Long> classPKs)
090                    throws SystemException {
091    
092                    long classNameId = PortalUtil.getClassNameId(className);
093    
094                    return ratingsStatsFinder.findByC_C(classNameId, classPKs);
095            }
096    
097            public RatingsStats getStats(String className, long classPK)
098                    throws SystemException {
099    
100                    long classNameId = PortalUtil.getClassNameId(className);
101    
102                    RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
103                            classNameId, classPK);
104    
105                    if (stats == null) {
106                            stats = ratingsStatsLocalService.addStats(classNameId, classPK);
107                    }
108    
109                    return stats;
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    RatingsStatsLocalServiceImpl.class);
114    
115    }