001    /**
002     * Copyright (c) 2000-present 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.json.JSONArray;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portlet.asset.model.AssetTag;
029    import com.liferay.portlet.asset.model.AssetTagDisplay;
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     * Provides the remote service for accessing, adding, checking, deleting,
045     * merging, and updating asset tags. Its methods include permission checks.
046     *
047     * @author Brian Wing Shun Chan
048     * @author Jorge Ferrer
049     * @author Alvaro del Castillo
050     * @author Eduardo Lundgren
051     * @author Bruno Farache
052     * @author Juan Fern??ndez
053     */
054    public class AssetTagServiceImpl extends AssetTagServiceBaseImpl {
055    
056            @Override
057            public AssetTag addTag(
058                            String name, String[] tagProperties, ServiceContext serviceContext)
059                    throws PortalException {
060    
061                    AssetPermission.check(
062                            getPermissionChecker(), serviceContext.getScopeGroupId(),
063                            ActionKeys.ADD_TAG);
064    
065                    return assetTagLocalService.addTag(
066                            getUserId(), name, tagProperties, serviceContext);
067            }
068    
069            @Override
070            public void deleteTag(long tagId) throws PortalException {
071                    AssetTagPermission.check(
072                            getPermissionChecker(), tagId, ActionKeys.DELETE);
073    
074                    assetTagLocalService.deleteTag(tagId);
075            }
076    
077            @Override
078            public void deleteTags(long[] tagIds) throws PortalException {
079                    for (long tagId : tagIds) {
080                            AssetTagPermission.check(
081                                    getPermissionChecker(), tagId, ActionKeys.DELETE);
082    
083                            assetTagLocalService.deleteTag(tagId);
084                    }
085            }
086    
087            @Override
088            public List<AssetTag> getGroupsTags(long[] groupIds) {
089                    Set<AssetTag> groupsTags = new TreeSet<AssetTag>(
090                            new AssetTagNameComparator());
091    
092                    for (long groupId : groupIds) {
093                            List<AssetTag> groupTags = getGroupTags(groupId);
094    
095                            groupsTags.addAll(groupTags);
096                    }
097    
098                    return new ArrayList<AssetTag>(groupsTags);
099            }
100    
101            @Override
102            public List<AssetTag> getGroupTags(long groupId) {
103                    return assetTagPersistence.filterFindByGroupId(groupId);
104            }
105    
106            @Override
107            public List<AssetTag> getGroupTags(
108                    long groupId, int start, int end, OrderByComparator<AssetTag> obc) {
109    
110                    return assetTagPersistence.filterFindByGroupId(
111                            groupId, start, end, obc);
112            }
113    
114            @Override
115            public int getGroupTagsCount(long groupId) {
116                    return assetTagPersistence.filterCountByGroupId(groupId);
117            }
118    
119            @Override
120            public AssetTagDisplay getGroupTagsDisplay(
121                    long groupId, String name, int start, int end) {
122    
123                    List<AssetTag> tags = null;
124                    int total = 0;
125    
126                    if (Validator.isNotNull(name)) {
127                            name = (CustomSQLUtil.keywords(name))[0];
128    
129                            tags = getTags(groupId, name, new String[0], start, end);
130                            total = getTagsCount(groupId, name, new String[0]);
131                    }
132                    else {
133                            tags = getGroupTags(groupId, start, end, null);
134                            total = getGroupTagsCount(groupId);
135                    }
136    
137                    return new AssetTagDisplay(tags, total, start, end);
138            }
139    
140            /**
141             * @deprecated As of 6.2.0, replaced by {@link #getGroupTagsDisplay(long,
142             *             String, int, int)}
143             */
144            @Deprecated
145            @Override
146            public JSONObject getJSONGroupTags(
147                            long groupId, String name, int start, int end)
148                    throws PortalException {
149    
150                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
151    
152                    int page = end / (end - start);
153    
154                    jsonObject.put("page", page);
155    
156                    List<AssetTag> tags = null;
157                    int total = 0;
158    
159                    if (Validator.isNotNull(name)) {
160                            name = (CustomSQLUtil.keywords(name))[0];
161    
162                            tags = getTags(groupId, name, new String[0], start, end);
163                            total = getTagsCount(groupId, name, new String[0]);
164                    }
165                    else {
166                            tags = getGroupTags(groupId, start, end, null);
167                            total = getGroupTagsCount(groupId);
168                    }
169    
170                    String tagsJSON = JSONFactoryUtil.looseSerialize(tags);
171    
172                    JSONArray tagsJSONArray = JSONFactoryUtil.createJSONArray(tagsJSON);
173    
174                    jsonObject.put("tags", tagsJSONArray);
175    
176                    jsonObject.put("total", total);
177    
178                    return jsonObject;
179            }
180    
181            @Override
182            public AssetTag getTag(long tagId) throws PortalException {
183                    AssetTagPermission.check(
184                            getPermissionChecker(), tagId, ActionKeys.VIEW);
185    
186                    return assetTagLocalService.getTag(tagId);
187            }
188    
189            @Override
190            public List<AssetTag> getTags(long groupId, long classNameId, String name) {
191                    return assetTagFinder.filterFindByG_C_N(
192                            groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
193                            null);
194            }
195    
196            @Override
197            public List<AssetTag> getTags(
198                    long groupId, long classNameId, String name, int start, int end,
199                    OrderByComparator<AssetTag> obc) {
200    
201                    return assetTagFinder.filterFindByG_C_N(
202                            groupId, classNameId, name, start, end, obc);
203            }
204    
205            @Override
206            public List<AssetTag> getTags(
207                    long groupId, String name, String[] tagProperties, int start, int end) {
208    
209                    return getTags(new long[] {groupId}, name, tagProperties, start, end);
210            }
211    
212            @Override
213            public List<AssetTag> getTags(
214                    long[] groupIds, String name, String[] tagProperties, int start,
215                    int end) {
216    
217                    return assetTagFinder.filterFindByG_N_P(
218                            groupIds, name, tagProperties, start, end, null);
219            }
220    
221            @Override
222            public List<AssetTag> getTags(String className, long classPK)
223                    throws PortalException {
224    
225                    return filterTags(assetTagLocalService.getTags(className, classPK));
226            }
227    
228            @Override
229            public int getTagsCount(long groupId, long classNameId, String name) {
230                    return assetTagFinder.filterCountByG_C_N(groupId, classNameId, name);
231            }
232    
233            @Override
234            public int getTagsCount(long groupId, String name) {
235                    return assetTagFinder.filterCountByG_N(groupId, name);
236            }
237    
238            @Override
239            public int getTagsCount(long groupId, String name, String[] tagProperties) {
240                    return assetTagFinder.filterCountByG_N_P(groupId, name, tagProperties);
241            }
242    
243            @Override
244            public void mergeTags(
245                            long fromTagId, long toTagId, boolean overrideProperties)
246                    throws PortalException {
247    
248                    AssetTagPermission.check(
249                            getPermissionChecker(), fromTagId, ActionKeys.VIEW);
250    
251                    AssetTagPermission.check(
252                            getPermissionChecker(), toTagId, ActionKeys.UPDATE);
253    
254                    assetTagLocalService.mergeTags(fromTagId, toTagId, overrideProperties);
255            }
256    
257            @Override
258            public void mergeTags(
259                            long[] fromTagIds, long toTagId, boolean overrideProperties)
260                    throws PortalException {
261    
262                    for (long fromTagId : fromTagIds) {
263                            mergeTags(fromTagId, toTagId, overrideProperties);
264                    }
265            }
266    
267            @Override
268            public JSONArray search(
269                    long groupId, String name, String[] tagProperties, int start, int end) {
270    
271                    return search(new long[] {groupId}, name, tagProperties, start, end);
272            }
273    
274            @Override
275            public JSONArray search(
276                    long[] groupIds, String name, String[] tagProperties, int start,
277                    int end) {
278    
279                    List<AssetTag> tags = getTags(
280                            groupIds, name, tagProperties, start, end);
281    
282                    return Autocomplete.listToJson(tags, "name", "name");
283            }
284    
285            @Override
286            public AssetTag updateTag(
287                            long tagId, String name, String[] tagProperties,
288                            ServiceContext serviceContext)
289                    throws PortalException {
290    
291                    AssetTagPermission.check(
292                            getPermissionChecker(), tagId, ActionKeys.UPDATE);
293    
294                    return assetTagLocalService.updateTag(
295                            getUserId(), tagId, name, tagProperties, serviceContext);
296            }
297    
298            protected List<AssetTag> filterTags(List<AssetTag> tags)
299                    throws PortalException {
300    
301                    PermissionChecker permissionChecker = getPermissionChecker();
302    
303                    tags = ListUtil.copy(tags);
304    
305                    Iterator<AssetTag> itr = tags.iterator();
306    
307                    while (itr.hasNext()) {
308                            AssetTag tag = itr.next();
309    
310                            if (!AssetTagPermission.contains(
311                                            permissionChecker, tag, ActionKeys.VIEW)) {
312    
313                                    itr.remove();
314                            }
315                    }
316    
317                    return tags;
318            }
319    
320    }