001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
206                            groupId, name, tagProperties);
207            }
208    
209            public void mergeTags(
210                            long fromTagId, long toTagId, boolean overrideProperties)
211                    throws PortalException, SystemException {
212    
213                    AssetTagPermission.check(
214                            getPermissionChecker(), fromTagId, ActionKeys.VIEW);
215    
216                    AssetTagPermission.check(
217                            getPermissionChecker(), toTagId, ActionKeys.UPDATE);
218    
219                    assetTagLocalService.mergeTags(fromTagId, toTagId, overrideProperties);
220            }
221    
222            public void mergeTags(
223                            long[] fromTagIds, long toTagId, boolean overrideProperties)
224                    throws PortalException, SystemException {
225    
226                    for (long fromTagId : fromTagIds) {
227                            mergeTags(fromTagId, toTagId, overrideProperties);
228                    }
229            }
230    
231            public JSONArray search(
232                            long groupId, String name, String[] tagProperties, int start,
233                            int end)
234                    throws SystemException {
235    
236                    List<AssetTag> tags = getTags(
237                            groupId, name, tagProperties, start, end);
238    
239                    return Autocomplete.listToJson(tags, "name", "name");
240            }
241    
242            public AssetTag updateTag(
243                            long tagId, String name, String[] tagProperties,
244                            ServiceContext serviceContext)
245                    throws PortalException, SystemException {
246    
247                    AssetTagPermission.check(
248                            getPermissionChecker(), tagId, ActionKeys.UPDATE);
249    
250                    return assetTagLocalService.updateTag(
251                            getUserId(), tagId, name, tagProperties, serviceContext);
252            }
253    
254            protected List<AssetTag> filterTags(List<AssetTag> tags)
255                    throws PortalException {
256    
257                    PermissionChecker permissionChecker = getPermissionChecker();
258    
259                    tags = ListUtil.copy(tags);
260    
261                    Iterator<AssetTag> itr = tags.iterator();
262    
263                    while (itr.hasNext()) {
264                            AssetTag tag = itr.next();
265    
266                            if (!AssetTagPermission.contains(
267                                            permissionChecker, tag, ActionKeys.VIEW)) {
268    
269                                    itr.remove();
270                            }
271                    }
272    
273                    return tags;
274            }
275    
276    }