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