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.asset.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.asset.model.AssetTag;
022    import com.liferay.portlet.asset.model.AssetTagStats;
023    import com.liferay.portlet.asset.service.base.AssetTagStatsLocalServiceBaseImpl;
024    
025    /**
026     * @author Jorge Ferrer
027     */
028    public class AssetTagStatsLocalServiceImpl
029            extends AssetTagStatsLocalServiceBaseImpl {
030    
031            public AssetTagStats addTagStats(long tagId, long classNameId)
032                    throws SystemException {
033    
034                    long tagStatsId = counterLocalService.increment();
035    
036                    AssetTagStats tagStats = assetTagStatsPersistence.create(tagStatsId);
037    
038                    tagStats.setTagId(tagId);
039                    tagStats.setClassNameId(classNameId);
040    
041                    try {
042                            assetTagStatsPersistence.update(tagStats, false);
043                    }
044                    catch (SystemException se) {
045                            if (_log.isWarnEnabled()) {
046                                    _log.warn(
047                                            "Add failed, fetch {tagId=" + tagId + ", classNameId=" +
048                                                    classNameId + "}");
049                            }
050    
051                            tagStats = assetTagStatsPersistence.fetchByT_C(
052                                    tagId, classNameId, false);
053    
054                            if (tagStats == null) {
055                                    throw se;
056                            }
057                    }
058    
059                    return tagStats;
060            }
061    
062            public void deleteTagStatsByClassNameId(long classNameId)
063                    throws SystemException {
064    
065                    assetTagStatsPersistence.removeByClassNameId(classNameId);
066            }
067    
068            public void deleteTagStatsByTagId(long tagId)
069                    throws SystemException {
070    
071                    assetTagStatsPersistence.removeByTagId(tagId);
072            }
073    
074            public AssetTagStats getTagStats(long tagId, long classNameId)
075                    throws SystemException {
076    
077                    AssetTagStats tagStats = assetTagStatsPersistence.fetchByT_C(
078                            tagId, classNameId);
079    
080                    if (tagStats == null) {
081                            tagStats = assetTagStatsLocalService.addTagStats(
082                                    tagId, classNameId);
083                    }
084    
085                    return tagStats;
086            }
087    
088            public AssetTagStats updateTagStats(long tagId, long classNameId)
089                    throws PortalException, SystemException {
090    
091                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
092    
093                    int assetCount = assetTagFinder.countByG_C_N(
094                            tag.getGroupId(), classNameId, tag.getName());
095    
096                    AssetTagStats tagStats = getTagStats(tagId, classNameId);
097    
098                    tagStats.setAssetCount(assetCount);
099    
100                    assetTagStatsPersistence.update(tagStats, false);
101    
102                    return tagStats;
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(
106                    AssetTagStatsLocalServiceImpl.class);
107    
108    }