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