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