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.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    import java.util.List;
026    
027    /**
028     * Provides the local service for accessing, adding, deleting, and updating
029     * asset tag statistics.
030     *
031     * @author Jorge Ferrer
032     */
033    public class AssetTagStatsLocalServiceImpl
034            extends AssetTagStatsLocalServiceBaseImpl {
035    
036            /**
037             * Adds an asset tag statistics instance.
038             *
039             * @param  tagId the primary key of the tag
040             * @param  classNameId the asset entry's class name ID
041             * @return the asset tag statistics instance
042             */
043            @Override
044            public AssetTagStats addTagStats(long tagId, long classNameId) {
045                    long tagStatsId = counterLocalService.increment();
046    
047                    AssetTagStats tagStats = assetTagStatsPersistence.create(tagStatsId);
048    
049                    tagStats.setTagId(tagId);
050                    tagStats.setClassNameId(classNameId);
051    
052                    try {
053                            assetTagStatsPersistence.update(tagStats);
054                    }
055                    catch (SystemException se) {
056                            if (_log.isWarnEnabled()) {
057                                    _log.warn(
058                                            "Add failed, fetch {tagId=" + tagId + ", classNameId=" +
059                                                    classNameId + "}");
060                            }
061    
062                            tagStats = assetTagStatsPersistence.fetchByT_C(
063                                    tagId, classNameId, false);
064    
065                            if (tagStats == null) {
066                                    throw se;
067                            }
068                    }
069    
070                    return tagStats;
071            }
072    
073            /**
074             * Deletes the asset tag statistics instance.
075             *
076             * @param tagStats the asset tag statistics instance
077             */
078            @Override
079            public void deleteTagStats(AssetTagStats tagStats) {
080                    assetTagStatsPersistence.remove(tagStats);
081            }
082    
083            /**
084             * Deletes the asset tag statistics instance matching the tag statistics ID.
085             *
086             * @param tagStatsId the primary key of the asset tag statistics instance
087             */
088            @Override
089            public void deleteTagStats(long tagStatsId) throws PortalException {
090                    AssetTagStats tagStats = assetTagStatsPersistence.findByPrimaryKey(
091                            tagStatsId);
092    
093                    deleteTagStats(tagStats);
094            }
095    
096            /**
097             * Deletes all asset tag statistics instances associated with the asset
098             * entry matching the class name ID.
099             *
100             * @param classNameId the asset entry's class name ID
101             */
102            @Override
103            public void deleteTagStatsByClassNameId(long classNameId) {
104                    List<AssetTagStats> tagStatsList =
105                            assetTagStatsPersistence.findByClassNameId(classNameId);
106    
107                    for (AssetTagStats tagStats : tagStatsList) {
108                            deleteTagStats(tagStats);
109                    }
110            }
111    
112            /**
113             * Deletes all asset tag statistics instances associated with the tag.
114             *
115             * @param tagId the primary key of the tag
116             */
117            @Override
118            public void deleteTagStatsByTagId(long tagId) {
119                    List<AssetTagStats> tagStatsList = assetTagStatsPersistence.findByTagId(
120                            tagId);
121    
122                    for (AssetTagStats tagStats : tagStatsList) {
123                            deleteTagStats(tagStats);
124                    }
125            }
126    
127            /**
128             * Returns a range of all the asset tag statistics instances associated with
129             * the asset entry matching the class name ID.
130             *
131             * <p>
132             * Useful when paginating results. Returns a maximum of <code>end -
133             * start</code> instances. <code>start</code> and <code>end</code> are not
134             * primary keys, they are indexes in the result set. Thus, <code>0</code>
135             * refers to the first result in the set. Setting both <code>start</code>
136             * and <code>end</code> to {@link
137             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
138             * result set.
139             * </p>
140             *
141             * @param  classNameId the asset entry's class name ID
142             * @param  start the lower bound of the range of results
143             * @param  end the upper bound of the range of results (not inclusive)
144             * @return the range of asset tag statistics associated with the asset entry
145             *         matching the class name ID
146             */
147            @Override
148            public List<AssetTagStats> getTagStats(
149                    long classNameId, int start, int end) {
150    
151                    return assetTagStatsPersistence.findByClassNameId(
152                            classNameId, start, end);
153            }
154    
155            /**
156             * Returns the asset tag statistics instance with the tag and asset entry
157             * matching the class name ID
158             *
159             * @param  tagId the primary key of the tag
160             * @param  classNameId the asset entry's class name ID
161             * @return Returns the asset tag statistics instance with the tag and asset
162             *         entry  matching the class name ID
163             */
164            @Override
165            public AssetTagStats getTagStats(long tagId, long classNameId) {
166                    AssetTagStats tagStats = assetTagStatsPersistence.fetchByT_C(
167                            tagId, classNameId);
168    
169                    if (tagStats == null) {
170                            tagStats = assetTagStatsLocalService.addTagStats(
171                                    tagId, classNameId);
172                    }
173    
174                    return tagStats;
175            }
176    
177            /**
178             * Updates the asset tag statistics instance.
179             *
180             * @param  tagId the primary key of the tag
181             * @param  classNameId the asset entry's class name ID
182             * @return the updated asset tag statistics instance
183             */
184            @Override
185            public AssetTagStats updateTagStats(long tagId, long classNameId)
186                    throws PortalException {
187    
188                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
189    
190                    int assetCount = assetTagFinder.countByG_C_N(
191                            tag.getGroupId(), classNameId, tag.getName());
192    
193                    AssetTagStats tagStats = getTagStats(tagId, classNameId);
194    
195                    tagStats.setAssetCount(assetCount);
196    
197                    assetTagStatsPersistence.update(tagStats);
198    
199                    return tagStats;
200            }
201    
202            private static final Log _log = LogFactoryUtil.getLog(
203                    AssetTagStatsLocalServiceImpl.class);
204    
205    }