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             * @throws PortalException if the assetTagStats with the primary key could
088             *         not be found
089             */
090            @Override
091            public void deleteTagStats(long tagStatsId) throws PortalException {
092                    AssetTagStats tagStats = assetTagStatsPersistence.findByPrimaryKey(
093                            tagStatsId);
094    
095                    deleteTagStats(tagStats);
096            }
097    
098            /**
099             * Deletes all asset tag statistics instances associated with the asset
100             * entry matching the class name ID.
101             *
102             * @param classNameId the asset entry's class name ID
103             */
104            @Override
105            public void deleteTagStatsByClassNameId(long classNameId) {
106                    List<AssetTagStats> tagStatsList =
107                            assetTagStatsPersistence.findByClassNameId(classNameId);
108    
109                    for (AssetTagStats tagStats : tagStatsList) {
110                            deleteTagStats(tagStats);
111                    }
112            }
113    
114            /**
115             * Deletes all asset tag statistics instances associated with the tag.
116             *
117             * @param tagId the primary key of the tag
118             */
119            @Override
120            public void deleteTagStatsByTagId(long tagId) {
121                    List<AssetTagStats> tagStatsList = assetTagStatsPersistence.findByTagId(
122                            tagId);
123    
124                    for (AssetTagStats tagStats : tagStatsList) {
125                            deleteTagStats(tagStats);
126                    }
127            }
128    
129            /**
130             * Returns a range of all the asset tag statistics instances associated with
131             * the asset entry matching the class name ID.
132             *
133             * <p>
134             * Useful when paginating results. Returns a maximum of <code>end -
135             * start</code> instances. <code>start</code> and <code>end</code> are not
136             * primary keys, they are indexes in the result set. Thus, <code>0</code>
137             * refers to the first result in the set. Setting both <code>start</code>
138             * and <code>end</code> to {@link
139             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
140             * result set.
141             * </p>
142             *
143             * @param  classNameId the asset entry's class name ID
144             * @param  start the lower bound of the range of results
145             * @param  end the upper bound of the range of results (not inclusive)
146             * @return the range of asset tag statistics associated with the asset entry
147             *         matching the class name ID
148             */
149            @Override
150            public List<AssetTagStats> getTagStats(
151                    long classNameId, int start, int end) {
152    
153                    return assetTagStatsPersistence.findByClassNameId(
154                            classNameId, start, end);
155            }
156    
157            /**
158             * Returns the asset tag statistics instance with the tag and asset entry
159             * matching the class name ID
160             *
161             * @param  tagId the primary key of the tag
162             * @param  classNameId the asset entry's class name ID
163             * @return Returns the asset tag statistics instance with the tag and asset
164             *         entry  matching the class name ID
165             */
166            @Override
167            public AssetTagStats getTagStats(long tagId, long classNameId) {
168                    AssetTagStats tagStats = assetTagStatsPersistence.fetchByT_C(
169                            tagId, classNameId);
170    
171                    if (tagStats == null) {
172                            tagStats = assetTagStatsLocalService.addTagStats(
173                                    tagId, classNameId);
174                    }
175    
176                    return tagStats;
177            }
178    
179            /**
180             * Updates the asset tag statistics instance.
181             *
182             * @param  tagId the primary key of the tag
183             * @param  classNameId the asset entry's class name ID
184             * @return the updated asset tag statistics instance
185             * @throws PortalException if an asset tag with the tag ID could not be
186             *         found
187             */
188            @Override
189            public AssetTagStats updateTagStats(long tagId, long classNameId)
190                    throws PortalException {
191    
192                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
193    
194                    int assetCount = assetTagFinder.countByG_C_N(
195                            tag.getGroupId(), classNameId, tag.getName());
196    
197                    AssetTagStats tagStats = getTagStats(tagId, classNameId);
198    
199                    tagStats.setAssetCount(assetCount);
200    
201                    assetTagStatsPersistence.update(tagStats);
202    
203                    return tagStats;
204            }
205    
206            private static final Log _log = LogFactoryUtil.getLog(
207                    AssetTagStatsLocalServiceImpl.class);
208    
209    }