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