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