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)
111                    throws SystemException {
112    
113                    return assetTagPersistence.filterCountByGroupId(groupId);
114            }
115    
116            public JSONObject getJSONGroupTags(
117                            long groupId, String name, int start, int end)
118                    throws PortalException, SystemException {
119    
120                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
121    
122                    int page = end / (end - start);
123    
124                    jsonObject.put("page", page);
125    
126                    List<AssetTag> tags = new ArrayList<AssetTag>();
127                    int total = 0;
128    
129                    if (Validator.isNotNull(name)) {
130                            name = (CustomSQLUtil.keywords(name))[0];
131    
132                            tags = getTags(groupId, name, new String[0], start, end);
133                            total = getTagsCount(groupId, name, new String[0]);
134                    }
135                    else {
136                            tags = getGroupTags(groupId, start, end, null);
137                            total = getGroupTagsCount(groupId);
138                    }
139    
140                    String tagsJSON = JSONFactoryUtil.looseSerialize(tags);
141    
142                    JSONArray tagsJSONArray = JSONFactoryUtil.createJSONArray(tagsJSON);
143    
144                    jsonObject.put("tags", tagsJSONArray);
145    
146                    jsonObject.put("total", total);
147    
148                    return jsonObject;
149            }
150    
151            public AssetTag getTag(long tagId) throws PortalException, SystemException {
152                    AssetTagPermission.check(
153                            getPermissionChecker(), tagId, ActionKeys.VIEW);
154    
155                    return assetTagLocalService.getTag(tagId);
156            }
157    
158            public List<AssetTag> getTags(long groupId, long classNameId, String name)
159                    throws SystemException {
160    
161                    return assetTagFinder.filterFindByG_C_N(
162                            groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
163                            null);
164            }
165    
166            public List<AssetTag> getTags(
167                            long groupId, long classNameId, String name, int start, int end,
168                            OrderByComparator obc)
169                    throws SystemException {
170    
171                    return assetTagFinder.filterFindByG_C_N(
172                            groupId, classNameId, name, start, end, obc);
173            }
174    
175            public List<AssetTag> getTags(
176                            long groupId, String name, String[] tagProperties, int start,
177                            int end)
178                    throws SystemException {
179    
180                    return assetTagFinder.filterFindByG_N_P(
181                            groupId, name, tagProperties, start, end, null);
182            }
183    
184            public List<AssetTag> getTags(String className, long classPK)
185                    throws PortalException, SystemException {
186    
187                    return filterTags(assetTagLocalService.getTags(className, classPK));
188            }
189    
190            public int getTagsCount(long groupId, long classNameId, String name)
191                    throws SystemException {
192    
193                    return assetTagFinder.filterCountByG_C_N(groupId, classNameId, name);
194            }
195    
196            public int getTagsCount(long groupId, String name)
197                    throws SystemException {
198    
199                    return assetTagFinder.filterCountByG_N(groupId, name);
200            }
201    
202            public int getTagsCount(long groupId, String name, String[] tagProperties)
203                    throws SystemException {
204    
205                    return assetTagFinder.filterCountByG_N_P(groupId, name, tagProperties);
206            }
207    
208            public void mergeTags(
209                            long fromTagId, long toTagId, boolean overrideProperties)
210                    throws PortalException, SystemException {
211    
212                    AssetTagPermission.check(
213                            getPermissionChecker(), fromTagId, ActionKeys.VIEW);
214    
215                    AssetTagPermission.check(
216                            getPermissionChecker(), toTagId, ActionKeys.UPDATE);
217    
218                    assetTagLocalService.mergeTags(fromTagId, toTagId, overrideProperties);
219            }
220    
221            public void mergeTags(
222                            long[] fromTagIds, long toTagId, boolean overrideProperties)
223                    throws PortalException, SystemException {
224    
225                    for (long fromTagId : fromTagIds) {
226                            mergeTags(fromTagId, toTagId, overrideProperties);
227                    }
228            }
229    
230            public JSONArray search(
231                            long groupId, String name, String[] tagProperties, int start,
232                            int end)
233                    throws SystemException {
234    
235                    List<AssetTag> tags = getTags(groupId, name, tagProperties, start, end);
236    
237                    return Autocomplete.listToJson(tags, "name", "name");
238            }
239    
240            public AssetTag updateTag(
241                            long tagId, String name, String[] tagProperties,
242                            ServiceContext serviceContext)
243                    throws PortalException, SystemException {
244    
245                    AssetTagPermission.check(
246                            getPermissionChecker(), tagId, ActionKeys.UPDATE);
247    
248                    return assetTagLocalService.updateTag(
249                            getUserId(), tagId, name, tagProperties, serviceContext);
250            }
251    
252            protected List<AssetTag> filterTags(List<AssetTag> tags)
253                    throws PortalException {
254    
255                    PermissionChecker permissionChecker = getPermissionChecker();
256    
257                    tags = ListUtil.copy(tags);
258    
259                    Iterator<AssetTag> itr = tags.iterator();
260    
261                    while (itr.hasNext()) {
262                            AssetTag tag = itr.next();
263    
264                            if (!AssetTagPermission.contains(
265                                            permissionChecker, tag, ActionKeys.VIEW)) {
266    
267                                    itr.remove();
268                            }
269                    }
270    
271                    return tags;
272            }
273    
274    }