001    /**
002     * Copyright (c) 2000-2012 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.json.JSONArray;
021    import com.liferay.portal.kernel.json.JSONFactoryUtil;
022    import com.liferay.portal.kernel.json.JSONObject;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portlet.asset.model.AssetTag;
030    import com.liferay.portlet.asset.service.base.AssetTagServiceBaseImpl;
031    import com.liferay.portlet.asset.service.permission.AssetPermission;
032    import com.liferay.portlet.asset.service.permission.AssetTagPermission;
033    import com.liferay.portlet.asset.util.comparator.AssetTagNameComparator;
034    import com.liferay.util.Autocomplete;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.ArrayList;
038    import java.util.Iterator;
039    import java.util.List;
040    import java.util.Set;
041    import java.util.TreeSet;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Jorge Ferrer
046     * @author Alvaro del Castillo
047     * @author Eduardo Lundgren
048     * @author Bruno Farache
049     * @author Juan Fernández
050     */
051    public class AssetTagServiceImpl extends AssetTagServiceBaseImpl {
052    
053            public AssetTag addTag(
054                            String name, String[] tagProperties, ServiceContext serviceContext)
055                    throws PortalException, SystemException {
056    
057                    AssetPermission.check(
058                            getPermissionChecker(), serviceContext.getScopeGroupId(),
059                            ActionKeys.ADD_TAG);
060    
061                    return assetTagLocalService.addTag(
062                            getUserId(), name, tagProperties, serviceContext);
063            }
064    
065            public void deleteTag(long tagId) throws PortalException, SystemException {
066                    AssetTagPermission.check(
067                            getPermissionChecker(), tagId, ActionKeys.DELETE);
068    
069                    assetTagLocalService.deleteTag(tagId);
070            }
071    
072            public void deleteTags(long[] tagIds)
073                    throws PortalException, SystemException {
074    
075                    for (long tagId : tagIds) {
076                            AssetTagPermission.check(
077                                    getPermissionChecker(), tagId, ActionKeys.DELETE);
078    
079                            assetTagLocalService.deleteTag(tagId);
080                    }
081            }
082    
083            public List<AssetTag> getGroupsTags(long[] groupIds)
084                    throws SystemException {
085    
086                    Set<AssetTag> groupsTags = new TreeSet<AssetTag>(
087                            new AssetTagNameComparator());
088    
089                    for (long groupId : groupIds) {
090                            List<AssetTag> groupTags = getGroupTags(groupId);
091    
092                            groupsTags.addAll(groupTags);
093                    }
094    
095                    return new ArrayList<AssetTag>(groupsTags);
096            }
097    
098            public List<AssetTag> getGroupTags(long groupId) throws SystemException {
099                    return assetTagPersistence.filterFindByGroupId(groupId);
100            }
101    
102            public List<AssetTag> getGroupTags(
103                            long groupId, int start, int end, OrderByComparator obc)
104                    throws SystemException {
105    
106                    return assetTagPersistence.filterFindByGroupId(
107                            groupId, start, end, obc);
108            }
109    
110            public int getGroupTagsCount(long groupId) throws SystemException {
111                    return assetTagPersistence.filterCountByGroupId(groupId);
112            }
113    
114            public JSONObject getJSONGroupTags(
115                            long groupId, String name, int start, int end)
116                    throws PortalException, SystemException {
117    
118                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
119    
120                    int page = end / (end - start);
121    
122                    jsonObject.put("page", page);
123    
124                    List<AssetTag> tags = new ArrayList<AssetTag>();
125                    int total = 0;
126    
127                    if (Validator.isNotNull(name)) {
128                            name = (CustomSQLUtil.keywords(name))[0];
129    
130                            tags = getTags(groupId, name, new String[0], start, end);
131                            total = getTagsCount(groupId, name, new String[0]);
132                    }
133                    else {
134                            tags = getGroupTags(groupId, start, end, null);
135                            total = getGroupTagsCount(groupId);
136                    }
137    
138                    String tagsJSON = JSONFactoryUtil.looseSerialize(tags);
139    
140                    JSONArray tagsJSONArray = JSONFactoryUtil.createJSONArray(tagsJSON);
141    
142                    jsonObject.put("tags", tagsJSONArray);
143    
144                    jsonObject.put("total", total);
145    
146                    return jsonObject;
147            }
148    
149            public AssetTag getTag(long tagId) throws PortalException, SystemException {
150                    AssetTagPermission.check(
151                            getPermissionChecker(), tagId, ActionKeys.VIEW);
152    
153                    return assetTagLocalService.getTag(tagId);
154            }
155    
156            public List<AssetTag> getTags(long groupId, long classNameId, String name)
157                    throws SystemException {
158    
159                    return assetTagFinder.filterFindByG_C_N(
160                            groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
161                            null);
162            }
163    
164            public List<AssetTag> getTags(
165                            long groupId, long classNameId, String name, int start, int end,
166                            OrderByComparator obc)
167                    throws SystemException {
168    
169                    return assetTagFinder.filterFindByG_C_N(
170                            groupId, classNameId, name, start, end, obc);
171            }
172    
173            public List<AssetTag> getTags(
174                            long groupId, String name, String[] tagProperties, int start,
175                            int end)
176                    throws SystemException {
177    
178                    return assetTagFinder.filterFindByG_N_P(
179                            groupId, name, tagProperties, start, end, null);
180            }
181    
182            public List<AssetTag> getTags(String className, long classPK)
183                    throws PortalException, SystemException {
184    
185                    return filterTags(assetTagLocalService.getTags(className, classPK));
186            }
187    
188            public int getTagsCount(long groupId, long classNameId, String name)
189                    throws SystemException {
190    
191                    return assetTagFinder.filterCountByG_C_N(groupId, classNameId, name);
192            }
193    
194            public int getTagsCount(long groupId, String name) throws SystemException {
195                    return assetTagFinder.filterCountByG_N(groupId, name);
196            }
197    
198            public int getTagsCount(long groupId, String name, String[] tagProperties)
199                    throws SystemException {
200    
201                    return assetTagFinder.filterCountByG_N_P(groupId, name, tagProperties);
202            }
203    
204            public void mergeTags(
205                            long fromTagId, long toTagId, boolean overrideProperties)
206                    throws PortalException, SystemException {
207    
208                    AssetTagPermission.check(
209                            getPermissionChecker(), fromTagId, ActionKeys.VIEW);
210    
211                    AssetTagPermission.check(
212                            getPermissionChecker(), toTagId, ActionKeys.UPDATE);
213    
214                    assetTagLocalService.mergeTags(fromTagId, toTagId, overrideProperties);
215            }
216    
217            public void mergeTags(
218                            long[] fromTagIds, long toTagId, boolean overrideProperties)
219                    throws PortalException, SystemException {
220    
221                    for (long fromTagId : fromTagIds) {
222                            mergeTags(fromTagId, toTagId, overrideProperties);
223                    }
224            }
225    
226            public JSONArray search(
227                            long groupId, String name, String[] tagProperties, int start,
228                            int end)
229                    throws SystemException {
230    
231                    List<AssetTag> tags = getTags(groupId, name, tagProperties, start, end);
232    
233                    return Autocomplete.listToJson(tags, "name", "name");
234            }
235    
236            public AssetTag updateTag(
237                            long tagId, String name, String[] tagProperties,
238                            ServiceContext serviceContext)
239                    throws PortalException, SystemException {
240    
241                    AssetTagPermission.check(
242                            getPermissionChecker(), tagId, ActionKeys.UPDATE);
243    
244                    return assetTagLocalService.updateTag(
245                            getUserId(), tagId, name, tagProperties, serviceContext);
246            }
247    
248            protected List<AssetTag> filterTags(List<AssetTag> tags)
249                    throws PortalException {
250    
251                    PermissionChecker permissionChecker = getPermissionChecker();
252    
253                    tags = ListUtil.copy(tags);
254    
255                    Iterator<AssetTag> itr = tags.iterator();
256    
257                    while (itr.hasNext()) {
258                            AssetTag tag = itr.next();
259    
260                            if (!AssetTagPermission.contains(
261                                            permissionChecker, tag, ActionKeys.VIEW)) {
262    
263                                    itr.remove();
264                            }
265                    }
266    
267                    return tags;
268            }
269    
270    }