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