1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portlet.asset.model.AssetTag;
22  import com.liferay.portlet.asset.model.AssetTagStats;
23  import com.liferay.portlet.asset.service.base.AssetTagStatsLocalServiceBaseImpl;
24  
25  /**
26   * <a href="AssetTagStatsLocalServiceImpl.java.html"><b><i>View Source</i></b>
27   * </a>
28   *
29   * @author Jorge Ferrer
30   */
31  public class AssetTagStatsLocalServiceImpl
32      extends AssetTagStatsLocalServiceBaseImpl {
33  
34      public AssetTagStats addTagStats(long tagId, long classNameId)
35          throws SystemException {
36  
37          long tagStatsId = counterLocalService.increment();
38  
39          AssetTagStats tagStats = assetTagStatsPersistence.create(tagStatsId);
40  
41          tagStats.setTagId(tagId);
42          tagStats.setClassNameId(classNameId);
43  
44          try {
45              assetTagStatsPersistence.update(tagStats, false);
46          }
47          catch (SystemException se) {
48              if (_log.isWarnEnabled()) {
49                  _log.warn(
50                      "Add failed, fetch {tagId=" + tagId + ", classNameId=" +
51                          classNameId + "}");
52              }
53  
54              tagStats = assetTagStatsPersistence.fetchByT_C(
55                  tagId, classNameId, false);
56  
57              if (tagStats == null) {
58                  throw se;
59              }
60          }
61  
62          return tagStats;
63      }
64  
65      public void deleteTagStatsByClassNameId(long classNameId)
66          throws SystemException {
67  
68          assetTagStatsPersistence.removeByClassNameId(classNameId);
69      }
70  
71      public void deleteTagStatsByTagId(long tagId)
72          throws SystemException {
73  
74          assetTagStatsPersistence.removeByTagId(tagId);
75      }
76  
77      public AssetTagStats getTagStats(long tagId, long classNameId)
78          throws SystemException {
79  
80          AssetTagStats tagStats = assetTagStatsPersistence.fetchByT_C(
81              tagId, classNameId);
82  
83          if (tagStats == null) {
84              tagStats = assetTagStatsLocalService.addTagStats(
85                  tagId, classNameId);
86          }
87  
88          return tagStats;
89      }
90  
91      public AssetTagStats updateTagStats(long tagId, long classNameId)
92          throws PortalException, SystemException {
93  
94          AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
95  
96          int assetCount = assetTagFinder.countByG_C_N(
97              tag.getGroupId(), classNameId, tag.getName());
98  
99          AssetTagStats tagStats = getTagStats(tagId, classNameId);
100 
101         tagStats.setAssetCount(assetCount);
102 
103         assetTagStatsPersistence.update(tagStats, false);
104 
105         return tagStats;
106     }
107 
108     private static Log _log = LogFactoryUtil.getLog(
109         AssetTagStatsLocalServiceImpl.class);
110 
111 }