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