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