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