001    /**
002     * Copyright (c) 2000-2010 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.json.JSONArray;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.asset.model.AssetTag;
025    import com.liferay.portlet.asset.service.base.AssetTagServiceBaseImpl;
026    import com.liferay.portlet.asset.service.permission.AssetPermission;
027    import com.liferay.portlet.asset.service.permission.AssetTagPermission;
028    import com.liferay.util.Autocomplete;
029    
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Jorge Ferrer
036     * @author Alvaro del Castillo
037     * @author Eduardo Lundgren
038     * @author Bruno Farache
039     */
040    public class AssetTagServiceImpl extends AssetTagServiceBaseImpl {
041    
042            public AssetTag addTag(
043                            String name, String[] tagProperties, ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    AssetPermission.check(
047                            getPermissionChecker(), serviceContext.getScopeGroupId(),
048                            ActionKeys.ADD_TAG);
049    
050                    return assetTagLocalService.addTag(
051                            getUserId(), name, tagProperties, serviceContext);
052            }
053    
054            public void deleteTag(long tagId) throws PortalException, SystemException {
055                    AssetTagPermission.check(
056                            getPermissionChecker(), tagId, ActionKeys.DELETE);
057    
058                    assetTagLocalService.deleteTag(tagId);
059            }
060    
061            public List<AssetTag> getGroupTags(long groupId)
062                    throws PortalException, SystemException {
063    
064                    return filterTags(
065                            assetTagLocalService.getGroupTags(groupId));
066            }
067    
068            public AssetTag getTag(long tagId) throws PortalException, SystemException {
069                    AssetTagPermission.check(
070                            getPermissionChecker(), tagId, ActionKeys.VIEW);
071    
072                    return assetTagLocalService.getTag(tagId);
073            }
074    
075            public List<AssetTag> getTags(long groupId, long classNameId, String name)
076                    throws PortalException, SystemException {
077    
078                    return filterTags(
079                            assetTagLocalService.getTags(groupId, classNameId, name));
080            }
081    
082            public List<AssetTag> getTags(String className, long classPK)
083                    throws PortalException, SystemException {
084    
085                    return filterTags(assetTagLocalService.getTags(className, classPK));
086            }
087    
088            public void mergeTags(long fromTagId, long toTagId)
089                    throws PortalException, SystemException {
090    
091                    AssetTagPermission.check(
092                            getPermissionChecker(), fromTagId, ActionKeys.VIEW);
093    
094                    AssetTagPermission.check(
095                            getPermissionChecker(), toTagId, ActionKeys.UPDATE);
096    
097                    assetTagLocalService.mergeTags(fromTagId, toTagId);
098            }
099    
100            public JSONArray search(
101                            long groupId, String name, String[] tagProperties, int start,
102                            int end)
103                    throws PortalException, SystemException {
104    
105                    List<AssetTag> tags = assetTagLocalService.search(
106                            groupId, name, tagProperties, start, end);
107    
108                    tags = filterTags(tags);
109    
110                    return Autocomplete.listToJson(tags, "name", "name");
111            }
112    
113            public AssetTag updateTag(
114                            long tagId, String name, String[] tagProperties,
115                            ServiceContext serviceContext)
116                    throws PortalException, SystemException {
117    
118                    AssetTagPermission.check(
119                            getPermissionChecker(), tagId, ActionKeys.UPDATE);
120    
121                    return assetTagLocalService.updateTag(
122                            getUserId(), tagId, name, tagProperties, serviceContext);
123            }
124    
125            protected List<AssetTag> filterTags(List<AssetTag> tags)
126                    throws PortalException {
127    
128                    PermissionChecker permissionChecker = getPermissionChecker();
129    
130                    tags = ListUtil.copy(tags);
131    
132                    Iterator<AssetTag> itr = tags.iterator();
133    
134                    while (itr.hasNext()) {
135                            AssetTag tag = itr.next();
136    
137                            if (!AssetTagPermission.contains(
138                                            permissionChecker, tag, ActionKeys.VIEW)) {
139    
140                                    itr.remove();
141                            }
142                    }
143    
144                    return tags;
145            }
146    
147    }