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.OrderByComparator;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.asset.model.AssetTag;
027    import com.liferay.portlet.asset.model.AssetTagDisplay;
028    import com.liferay.portlet.asset.service.base.AssetTagServiceBaseImpl;
029    import com.liferay.portlet.asset.service.permission.AssetPermission;
030    import com.liferay.portlet.asset.service.permission.AssetTagPermission;
031    import com.liferay.portlet.asset.util.comparator.AssetTagNameComparator;
032    import com.liferay.util.Autocomplete;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.ArrayList;
036    import java.util.List;
037    import java.util.Set;
038    import java.util.TreeSet;
039    
040    /**
041     * Provides the remote service for accessing, adding, checking, deleting,
042     * merging, and updating asset tags. Its methods include permission checks.
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            @Override
054            public AssetTag addTag(
055                            long groupId, String name, ServiceContext serviceContext)
056                    throws PortalException {
057    
058                    AssetPermission.check(
059                            getPermissionChecker(), groupId, ActionKeys.ADD_TAG);
060    
061                    return assetTagLocalService.addTag(
062                            getUserId(), groupId, name, serviceContext);
063            }
064    
065            @Override
066            public void deleteTag(long tagId) throws PortalException {
067                    AssetTagPermission.check(
068                            getPermissionChecker(), tagId, ActionKeys.DELETE);
069    
070                    assetTagLocalService.deleteTag(tagId);
071            }
072    
073            @Override
074            public void deleteTags(long[] tagIds) throws PortalException {
075                    for (long tagId : tagIds) {
076                            AssetTagPermission.check(
077                                    getPermissionChecker(), tagId, ActionKeys.DELETE);
078    
079                            assetTagLocalService.deleteTag(tagId);
080                    }
081            }
082    
083            @Override
084            public List<AssetTag> getGroupsTags(long[] groupIds) {
085                    Set<AssetTag> groupsTags = new TreeSet<>(new AssetTagNameComparator());
086    
087                    for (long groupId : groupIds) {
088                            List<AssetTag> groupTags = getGroupTags(groupId);
089    
090                            groupsTags.addAll(groupTags);
091                    }
092    
093                    return new ArrayList<>(groupsTags);
094            }
095    
096            @Override
097            public List<AssetTag> getGroupTags(long groupId) {
098                    return assetTagPersistence.findByGroupId(groupId);
099            }
100    
101            @Override
102            public List<AssetTag> getGroupTags(
103                    long groupId, int start, int end, OrderByComparator<AssetTag> obc) {
104    
105                    return assetTagPersistence.findByGroupId(groupId, start, end, obc);
106            }
107    
108            @Override
109            public int getGroupTagsCount(long groupId) {
110                    return assetTagPersistence.countByGroupId(groupId);
111            }
112    
113            @Override
114            public AssetTagDisplay getGroupTagsDisplay(
115                    long groupId, String name, int start, int end) {
116    
117                    List<AssetTag> tags = null;
118                    int total = 0;
119    
120                    if (Validator.isNotNull(name)) {
121                            name = (CustomSQLUtil.keywords(name))[0];
122    
123                            tags = getTags(groupId, name, start, end);
124                            total = getTagsCount(groupId, name);
125                    }
126                    else {
127                            tags = getGroupTags(groupId, start, end, null);
128                            total = getGroupTagsCount(groupId);
129                    }
130    
131                    return new AssetTagDisplay(tags, total, start, end);
132            }
133    
134            /**
135             * @deprecated As of 6.2.0, replaced by {@link #getGroupTagsDisplay(long,
136             *             String, int, int)}
137             */
138            @Deprecated
139            @Override
140            public JSONObject getJSONGroupTags(
141                            long groupId, String name, int start, int end)
142                    throws PortalException {
143    
144                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
145    
146                    int page = end / (end - start);
147    
148                    jsonObject.put("page", page);
149    
150                    List<AssetTag> tags = null;
151                    int total = 0;
152    
153                    if (Validator.isNotNull(name)) {
154                            name = (CustomSQLUtil.keywords(name))[0];
155    
156                            tags = getTags(groupId, name, start, end);
157                            total = getTagsCount(groupId, name);
158                    }
159                    else {
160                            tags = getGroupTags(groupId, start, end, null);
161                            total = getGroupTagsCount(groupId);
162                    }
163    
164                    String tagsJSON = JSONFactoryUtil.looseSerialize(tags);
165    
166                    JSONArray tagsJSONArray = JSONFactoryUtil.createJSONArray(tagsJSON);
167    
168                    jsonObject.put("tags", tagsJSONArray);
169    
170                    jsonObject.put("total", total);
171    
172                    return jsonObject;
173            }
174    
175            @Override
176            public AssetTag getTag(long tagId) throws PortalException {
177                    return assetTagLocalService.getTag(tagId);
178            }
179    
180            @Override
181            public List<AssetTag> getTags(long groupId, long classNameId, String name) {
182                    return assetTagFinder.findByG_C_N(
183                            groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
184                            null);
185            }
186    
187            @Override
188            public List<AssetTag> getTags(
189                    long groupId, long classNameId, String name, int start, int end,
190                    OrderByComparator<AssetTag> obc) {
191    
192                    return assetTagFinder.findByG_C_N(
193                            groupId, classNameId, name, start, end, obc);
194            }
195    
196            @Override
197            public List<AssetTag> getTags(
198                    long groupId, String name, int start, int end) {
199    
200                    return getTags(new long[] {groupId}, name, start, end);
201            }
202    
203            @Override
204            public List<AssetTag> getTags(
205                    long groupId, String name, int start, int end,
206                    OrderByComparator<AssetTag> obc) {
207    
208                    return getTags(new long[] {groupId}, name, start, end, obc);
209            }
210    
211            @Override
212            public List<AssetTag> getTags(
213                    long[] groupIds, String name, int start, int end) {
214    
215                    return getTags(
216                            groupIds, name, start, end, new AssetTagNameComparator());
217            }
218    
219            @Override
220            public List<AssetTag> getTags(
221                    long[] groupIds, String name, int start, int end,
222                    OrderByComparator<AssetTag> obc) {
223    
224                    if (Validator.isNull(name)) {
225                            return assetTagPersistence.findByGroupId(groupIds, start, end, obc);
226                    }
227    
228                    return assetTagPersistence.findByG_LikeN(
229                            groupIds, name, start, end, obc);
230            }
231    
232            @Override
233            public List<AssetTag> getTags(String className, long classPK) {
234                    return assetTagLocalService.getTags(className, classPK);
235            }
236    
237            @Override
238            public int getTagsCount(long groupId, String name) {
239                    if (Validator.isNull(name)) {
240                            return assetTagPersistence.countByGroupId(groupId);
241                    }
242    
243                    return assetTagPersistence.countByG_LikeN(groupId, name);
244            }
245    
246            @Override
247            public int getVisibleAssetsTagsCount(
248                    long groupId, long classNameId, String name) {
249    
250                    return assetTagFinder.countByG_C_N(groupId, classNameId, name);
251            }
252    
253            @Override
254            public int getVisibleAssetsTagsCount(long groupId, String name) {
255                    return assetTagFinder.countByG_N(groupId, name);
256            }
257    
258            @Override
259            public void mergeTags(long fromTagId, long toTagId) throws PortalException {
260                    AssetTagPermission.check(
261                            getPermissionChecker(), toTagId, ActionKeys.UPDATE);
262    
263                    assetTagLocalService.mergeTags(fromTagId, toTagId);
264            }
265    
266            @Override
267            public void mergeTags(long[] fromTagIds, long toTagId)
268                    throws PortalException {
269    
270                    for (long fromTagId : fromTagIds) {
271                            mergeTags(fromTagId, toTagId);
272                    }
273            }
274    
275            @Override
276            public JSONArray search(long groupId, String name, int start, int end) {
277                    return search(new long[] {groupId}, name, start, end);
278            }
279    
280            @Override
281            public JSONArray search(long[] groupIds, String name, int start, int end) {
282                    List<AssetTag> tags = getTags(groupIds, name, start, end);
283    
284                    return Autocomplete.listToJson(tags, "name", "name");
285            }
286    
287            @Override
288            public AssetTag updateTag(
289                            long tagId, String name, ServiceContext serviceContext)
290                    throws PortalException {
291    
292                    AssetTagPermission.check(
293                            getPermissionChecker(), tagId, ActionKeys.UPDATE);
294    
295                    return assetTagLocalService.updateTag(
296                            getUserId(), tagId, name, serviceContext);
297            }
298    
299    }