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