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