001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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
088             * 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            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            public void deleteTagStatsByClassNameId(long classNameId)
112                    throws SystemException {
113    
114                    List<AssetTagStats> tagStatsList =
115                            assetTagStatsPersistence.findByClassNameId(classNameId);
116    
117                    for (AssetTagStats tagStats : tagStatsList) {
118                            deleteTagStats(tagStats);
119                    }
120            }
121    
122            /**
123             * Deletes all asset tag statistics instances associated with the tag.
124             *
125             * @param  tagId the primary key of the tag
126             * @throws SystemException if a system exception occurred
127             */
128            public void deleteTagStatsByTagId(long tagId)
129                    throws SystemException {
130    
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
141             * with 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
150             * full 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
157             *         entry matching the class name ID
158             * @throws SystemException if a system exception occurred
159             */
160            public List<AssetTagStats> getTagStats(long classNameId, int start, int end)
161                    throws SystemException {
162    
163                    return assetTagStatsPersistence.findByClassNameId(
164                            classNameId, start, end);
165            }
166    
167            /**
168             * Returns the asset tag statistics instance with the tag and asset entry
169             * matching the class name ID
170             *
171             * @param  tagId the primary key of the tag
172             * @param  classNameId the asset entry's class name ID
173             * @return Returns the asset tag statistics instance with the tag and asset
174             *         entry  matching the class name ID
175             * @throws SystemException if a system exception occurred
176             */
177            public AssetTagStats getTagStats(long tagId, long classNameId)
178                    throws SystemException {
179    
180                    AssetTagStats tagStats = assetTagStatsPersistence.fetchByT_C(
181                            tagId, classNameId);
182    
183                    if (tagStats == null) {
184                            tagStats = assetTagStatsLocalService.addTagStats(
185                                    tagId, classNameId);
186                    }
187    
188                    return tagStats;
189            }
190    
191            /**
192             * Updates the asset tag statistics instance.
193             *
194             * @param  tagId the primary key of the tag
195             * @param  classNameId the asset entry's class name ID
196             * @return the updated asset tag statistics instance
197             * @throws PortalException if an asset tag with the tag ID could not be
198             *         found
199             * @throws SystemException if a system exception occurred
200             */
201            public AssetTagStats updateTagStats(long tagId, long classNameId)
202                    throws PortalException, SystemException {
203    
204                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
205    
206                    int assetCount = assetTagFinder.countByG_C_N(
207                            tag.getGroupId(), classNameId, tag.getName());
208    
209                    AssetTagStats tagStats = getTagStats(tagId, classNameId);
210    
211                    tagStats.setAssetCount(assetCount);
212    
213                    assetTagStatsPersistence.update(tagStats, false);
214    
215                    return tagStats;
216            }
217    
218            private static Log _log = LogFactoryUtil.getLog(
219                    AssetTagStatsLocalServiceImpl.class);
220    
221    }