001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.json.JSONArray;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.security.permission.ActionKeys;
028 import com.liferay.portal.security.permission.PermissionChecker;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portlet.asset.model.AssetCategory;
031 import com.liferay.portlet.asset.model.AssetVocabulary;
032 import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
033 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
034 import com.liferay.util.Autocomplete;
035 import com.liferay.util.dao.orm.CustomSQLUtil;
036
037 import java.util.ArrayList;
038 import java.util.Collections;
039 import java.util.Iterator;
040 import java.util.List;
041 import java.util.Locale;
042 import java.util.Map;
043
044
051 public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
052
053 public AssetCategory addCategory(
054 long parentCategoryId, Map<Locale, String> titleMap,
055 Map<Locale, String> descriptionMap, long vocabularyId,
056 String[] categoryProperties, ServiceContext serviceContext)
057 throws PortalException, SystemException {
058
059 AssetCategoryPermission.check(
060 getPermissionChecker(), serviceContext.getScopeGroupId(),
061 parentCategoryId, ActionKeys.ADD_CATEGORY);
062
063 return assetCategoryLocalService.addCategory(
064 getUserId(), parentCategoryId, titleMap, descriptionMap,
065 vocabularyId, categoryProperties, serviceContext);
066 }
067
068 public void deleteCategories(long[] categoryIds)
069 throws PortalException, SystemException {
070
071 PermissionChecker permissionChecker = getPermissionChecker();
072
073 for (long categoryId : categoryIds) {
074 AssetCategory category = assetCategoryPersistence.fetchByPrimaryKey(
075 categoryId);
076
077 if (category == null) {
078 continue;
079 }
080
081 AssetCategoryPermission.check(
082 permissionChecker, categoryId, ActionKeys.DELETE);
083
084 assetCategoryLocalService.deleteCategory(category);
085 }
086 }
087
088 public void deleteCategory(long categoryId)
089 throws PortalException, SystemException {
090
091 AssetCategoryPermission.check(
092 getPermissionChecker(), categoryId, ActionKeys.DELETE);
093
094 assetCategoryLocalService.deleteCategory(categoryId);
095 }
096
097 public List<AssetCategory> getCategories(String className, long classPK)
098 throws PortalException, SystemException {
099
100 return filterCategories(
101 assetCategoryLocalService.getCategories(className, classPK));
102 }
103
104 public AssetCategory getCategory(long categoryId)
105 throws PortalException, SystemException {
106
107 AssetCategoryPermission.check(
108 getPermissionChecker(), categoryId, ActionKeys.VIEW);
109
110 return assetCategoryLocalService.getCategory(categoryId);
111 }
112
113 public List<AssetCategory> getChildCategories(long parentCategoryId)
114 throws PortalException, SystemException {
115
116 return filterCategories(
117 assetCategoryLocalService.getChildCategories(parentCategoryId));
118 }
119
120 public List<AssetCategory> getChildCategories(
121 long parentCategoryId, int start, int end, OrderByComparator obc)
122 throws PortalException, SystemException {
123
124 return filterCategories(
125 assetCategoryLocalService.getChildCategories(
126 parentCategoryId, start, end, obc));
127 }
128
129 public JSONArray getJSONSearch(
130 long groupId, String name, long[] vocabularyIds, int start, int end)
131 throws PortalException, SystemException {
132
133 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
134
135 for (AssetVocabulary vocabulary :
136 assetVocabularyService.getVocabularies(vocabularyIds)) {
137
138 List<AssetCategory> vocabularyCategory =
139 assetCategoryFinder.findByG_N_V(
140 groupId, name, vocabulary.getVocabularyId(), start, end,
141 null);
142
143 JSONArray vocabularyCategoryJSONArray = toJSONArray(
144 vocabularyCategory);
145
146 for (int i = 0; i < vocabularyCategoryJSONArray.length(); ++i) {
147 JSONObject vocabularyCategoryJSONObject =
148 vocabularyCategoryJSONArray.getJSONObject(i);
149
150 jsonArray.put(vocabularyCategoryJSONObject);
151 }
152 }
153
154 return jsonArray;
155 }
156
157 public JSONObject getJSONVocabularyCategories(
158 long vocabularyId, int start, int end, OrderByComparator obc)
159 throws PortalException, SystemException {
160
161 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
162
163 List<AssetCategory> categories = filterCategories(
164 assetCategoryLocalService.getVocabularyCategories(
165 vocabularyId, start, end, obc));
166
167 jsonObject.put("categories", toJSONArray(categories));
168 jsonObject.put("total", categories.size());
169
170 return jsonObject;
171 }
172
173 public JSONObject getJSONVocabularyCategories(
174 long groupId, String name, long vocabularyId, int start, int end,
175 OrderByComparator obc)
176 throws PortalException, SystemException {
177
178 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
179
180 int page = 0;
181
182 if ((end > 0) && (start > 0)) {
183 page = end / (end - start);
184 }
185
186 jsonObject.put("page", page);
187
188 List<AssetCategory> categories;
189 int total = 0;
190
191 if (Validator.isNotNull(name)) {
192 name = (CustomSQLUtil.keywords(name))[0];
193
194 categories = getVocabularyCategories(
195 groupId, name, vocabularyId, start, end, obc);
196 total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
197 }
198 else {
199 categories = getVocabularyCategories(vocabularyId, start, end, obc);
200 total = getVocabularyCategoriesCount(groupId, vocabularyId);
201 }
202
203 jsonObject.put("categories", toJSONArray(categories));
204 jsonObject.put("total", total);
205
206 return jsonObject;
207 }
208
209 public List<AssetCategory> getVocabularyCategories(
210 long vocabularyId, int start, int end, OrderByComparator obc)
211 throws PortalException, SystemException {
212
213 return filterCategories(
214 assetCategoryLocalService.getVocabularyCategories(
215 vocabularyId, start, end, obc));
216 }
217
218 public List<AssetCategory> getVocabularyCategories(
219 long parentCategoryId, long vocabularyId, int start, int end,
220 OrderByComparator obc)
221 throws PortalException, SystemException {
222
223 return filterCategories(
224 assetCategoryLocalService.getVocabularyCategories(
225 parentCategoryId, vocabularyId, start, end, obc));
226 }
227
228 public List<AssetCategory> getVocabularyCategories(
229 long groupId, String name, long vocabularyId, int start, int end,
230 OrderByComparator obc)
231 throws SystemException {
232
233 return assetCategoryFinder.filterFindByG_N_V(
234 groupId, name, vocabularyId, start, end, obc);
235 }
236
237 public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
238 throws SystemException {
239
240 return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
241 }
242
243 public int getVocabularyCategoriesCount(
244 long groupId, String name, long vocabularyId)
245 throws SystemException {
246
247 return assetCategoryFinder.filterCountByG_N_V(
248 groupId, name, vocabularyId);
249 }
250
251 public List<AssetCategory> getVocabularyRootCategories(
252 long vocabularyId, int start, int end, OrderByComparator obc)
253 throws PortalException, SystemException {
254
255 return filterCategories(
256 assetCategoryLocalService.getVocabularyRootCategories(
257 vocabularyId, start, end, obc));
258 }
259
260 public AssetCategory moveCategory(
261 long categoryId, long parentCategoryId, long vocabularyId,
262 ServiceContext serviceContext)
263 throws PortalException, SystemException {
264
265 AssetCategoryPermission.check(
266 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
267
268 return assetCategoryLocalService.moveCategory(
269 categoryId, parentCategoryId, vocabularyId, serviceContext);
270 }
271
272 public List<AssetCategory> search(
273 long groupId, String keywords, long vocabularyId, int start,
274 int end, OrderByComparator obc)
275 throws PortalException, SystemException {
276
277 return filterCategories(
278 assetCategoryFinder.findByG_N_V(
279 groupId, CustomSQLUtil.keywords(keywords)[0], vocabularyId,
280 start, end, obc));
281 }
282
283 public JSONArray search(
284 long groupId, String name, String[] categoryProperties, int start,
285 int end)
286 throws PortalException, SystemException {
287
288 List<AssetCategory> categories = assetCategoryLocalService.search(
289 groupId, name, categoryProperties, start, end);
290
291 categories = filterCategories(categories);
292
293 return Autocomplete.listToJson(categories, "name", "name");
294 }
295
296 public AssetCategory updateCategory(
297 long categoryId, long parentCategoryId,
298 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
299 long vocabularyId, String[] categoryProperties,
300 ServiceContext serviceContext)
301 throws PortalException, SystemException {
302
303 AssetCategoryPermission.check(
304 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
305
306 return assetCategoryLocalService.updateCategory(
307 getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
308 vocabularyId, categoryProperties, serviceContext);
309 }
310
311 protected List<AssetCategory> filterCategories(
312 List<AssetCategory> categories)
313 throws PortalException {
314
315 PermissionChecker permissionChecker = getPermissionChecker();
316
317 categories = ListUtil.copy(categories);
318
319 Iterator<AssetCategory> itr = categories.iterator();
320
321 while (itr.hasNext()) {
322 AssetCategory category = itr.next();
323
324 if (!AssetCategoryPermission.contains(
325 permissionChecker, category, ActionKeys.VIEW)) {
326
327 itr.remove();
328 }
329 }
330
331 return categories;
332 }
333
334 protected JSONArray toJSONArray(List<AssetCategory> categories)
335 throws PortalException, SystemException {
336
337 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
338
339 for (AssetCategory category : categories) {
340 String categoryJSON = JSONFactoryUtil.looseSerialize(category);
341
342 JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(
343 categoryJSON);
344
345 List<String> names = new ArrayList<String>();
346
347 AssetCategory curCategory = category;
348
349 while (curCategory.getParentCategoryId() > 0) {
350 AssetCategory parentCategory = getCategory(
351 curCategory.getParentCategoryId());
352
353 names.add(parentCategory.getName());
354 names.add(
355 StringPool.SPACE + StringPool.GREATER_THAN +
356 StringPool.SPACE);
357
358 curCategory = parentCategory;
359 }
360
361 Collections.reverse(names);
362
363 AssetVocabulary vocabulary = assetVocabularyService.getVocabulary(
364 category.getVocabularyId());
365
366 StringBundler sb = new StringBundler(1 + names.size());
367
368 sb.append(vocabulary.getName());
369 sb.append(names.toArray(new String[names.size()]));
370
371 categoryJSONObject.put("path", sb.toString());
372
373 jsonArray.put(categoryJSONObject);
374 }
375
376 return jsonArray;
377 }
378
379 }