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.json.JSONArray;
20  import com.liferay.portal.kernel.util.ListUtil;
21  import com.liferay.portal.security.permission.ActionKeys;
22  import com.liferay.portal.security.permission.PermissionChecker;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portlet.asset.model.AssetTag;
25  import com.liferay.portlet.asset.service.base.AssetTagServiceBaseImpl;
26  import com.liferay.portlet.asset.service.permission.AssetPermission;
27  import com.liferay.portlet.asset.service.permission.AssetTagPermission;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="AssetTagServiceImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Jorge Ferrer
37   * @author Alvaro del Castillo
38   * @author Eduardo Lundgren
39   * @author Bruno Farache
40   */
41  public class AssetTagServiceImpl extends AssetTagServiceBaseImpl {
42  
43      public AssetTag addTag(
44              String name, String[] tagProperties, ServiceContext serviceContext)
45          throws PortalException, SystemException {
46  
47          AssetPermission.check(
48              getPermissionChecker(), serviceContext.getScopeGroupId(),
49              ActionKeys.ADD_TAG);
50  
51          return assetTagLocalService.addTag(
52              getUserId(), name, tagProperties, serviceContext);
53      }
54  
55      public void deleteTag(long tagId) throws PortalException, SystemException {
56          AssetTagPermission.check(
57              getPermissionChecker(), tagId, ActionKeys.DELETE);
58  
59          assetTagLocalService.deleteTag(tagId);
60      }
61  
62      public List<AssetTag> getGroupTags(long groupId)
63          throws PortalException, SystemException {
64  
65          return filterTags(
66              assetTagLocalService.getGroupTags(groupId));
67      }
68  
69      public AssetTag getTag(long tagId) throws PortalException, SystemException {
70          AssetTagPermission.check(
71              getPermissionChecker(), tagId, ActionKeys.VIEW);
72  
73          return assetTagLocalService.getTag(tagId);
74      }
75  
76      public List<AssetTag> getTags(long groupId, long classNameId, String name)
77          throws PortalException, SystemException {
78  
79          return filterTags(
80              assetTagLocalService.getTags(groupId, classNameId, name));
81      }
82  
83      public List<AssetTag> getTags(String className, long classPK)
84          throws PortalException, SystemException {
85  
86          return filterTags(assetTagLocalService.getTags(className, classPK));
87      }
88  
89      public void mergeTags(long fromTagId, long toTagId)
90          throws PortalException, SystemException {
91  
92          AssetTagPermission.check(
93              getPermissionChecker(), fromTagId, ActionKeys.VIEW);
94  
95          AssetTagPermission.check(
96              getPermissionChecker(), toTagId, ActionKeys.UPDATE);
97  
98          assetTagLocalService.mergeTags(fromTagId, toTagId);
99      }
100 
101     public JSONArray search(
102             long groupId, String name, String[] tagProperties, int start,
103             int end)
104         throws SystemException {
105 
106         return assetTagLocalService.search(
107             groupId, name, tagProperties, start, end);
108     }
109 
110     public AssetTag updateTag(
111             long tagId, String name, String[] tagProperties,
112             ServiceContext serviceContext)
113         throws PortalException, SystemException {
114 
115         AssetTagPermission.check(
116             getPermissionChecker(), tagId, ActionKeys.UPDATE);
117 
118         return assetTagLocalService.updateTag(
119             getUserId(), tagId, name, tagProperties, serviceContext);
120     }
121 
122     protected List<AssetTag> filterTags(List<AssetTag> tags)
123         throws PortalException {
124 
125         PermissionChecker permissionChecker = getPermissionChecker();
126 
127         tags = ListUtil.copy(tags);
128 
129         Iterator<AssetTag> itr = tags.iterator();
130 
131         while (itr.hasNext()) {
132             AssetTag tag = itr.next();
133 
134             if (!AssetTagPermission.contains(
135                     permissionChecker, tag, ActionKeys.VIEW)) {
136 
137                 itr.remove();
138             }
139         }
140 
141         return tags;
142     }
143 
144 }