1
14
15 package com.liferay.portlet.asset.service.impl;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.json.JSONArray;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.ListUtil;
22 import com.liferay.portal.kernel.util.LocaleUtil;
23 import com.liferay.portal.kernel.util.StringBundler;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.model.ResourceConstants;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.ServiceContext;
30 import com.liferay.portal.util.PortalUtil;
31 import com.liferay.portlet.asset.DuplicateCategoryException;
32 import com.liferay.portlet.asset.model.AssetCategory;
33 import com.liferay.portlet.asset.model.AssetCategoryConstants;
34 import com.liferay.portlet.asset.model.AssetCategoryProperty;
35 import com.liferay.portlet.asset.model.AssetEntry;
36 import com.liferay.portlet.asset.service.base.AssetCategoryLocalServiceBaseImpl;
37 import com.liferay.util.Autocomplete;
38
39 import java.util.Date;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Map;
43
44
53 public class AssetCategoryLocalServiceImpl
54 extends AssetCategoryLocalServiceBaseImpl {
55
56 public AssetCategory addCategory(
57 String uuid, long userId, long parentCategoryId,
58 Map<Locale, String> titleMap, long vocabularyId,
59 String[] categoryProperties, ServiceContext serviceContext)
60 throws PortalException, SystemException {
61
62
64 User user = userPersistence.findByPrimaryKey(userId);
65 long groupId = serviceContext.getScopeGroupId();
66 String name = titleMap.get(LocaleUtil.getDefault());
67
68 if (categoryProperties == null) {
69 categoryProperties = new String[0];
70 }
71
72 Date now = new Date();
73
74 validate(0, parentCategoryId, name, vocabularyId);
75
76 if (parentCategoryId > 0) {
77 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
78 }
79
80 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
81
82 long categoryId = counterLocalService.increment();
83
84 AssetCategory category = assetCategoryPersistence.create(categoryId);
85
86 category.setUuid(uuid);
87 category.setGroupId(groupId);
88 category.setCompanyId(user.getCompanyId());
89 category.setUserId(user.getUserId());
90 category.setUserName(user.getFullName());
91 category.setCreateDate(now);
92 category.setModifiedDate(now);
93 category.setParentCategoryId(parentCategoryId);
94 category.setName(name);
95 category.setTitleMap(titleMap);
96 category.setVocabularyId(vocabularyId);
97
98 assetCategoryPersistence.update(category, false);
99
100
102 if (serviceContext.getAddCommunityPermissions() ||
103 serviceContext.getAddGuestPermissions()) {
104
105 addCategoryResources(
106 category, serviceContext.getAddCommunityPermissions(),
107 serviceContext.getAddGuestPermissions());
108 }
109 else {
110 addCategoryResources(
111 category, serviceContext.getCommunityPermissions(),
112 serviceContext.getGuestPermissions());
113 }
114
115
117 for (int i = 0; i < categoryProperties.length; i++) {
118 String[] categoryProperty = StringUtil.split(
119 categoryProperties[i], StringPool.COLON);
120
121 String key = StringPool.BLANK;
122
123 if (categoryProperty.length > 1) {
124 key = GetterUtil.getString(categoryProperty[1]);
125 }
126
127 String value = StringPool.BLANK;
128
129 if (categoryProperty.length > 2) {
130 value = GetterUtil.getString(categoryProperty[2]);
131 }
132
133 if (Validator.isNotNull(key)) {
134 assetCategoryPropertyLocalService.addCategoryProperty(
135 userId, categoryId, key, value);
136 }
137 }
138
139 return category;
140 }
141
142 public void addCategoryResources(
143 AssetCategory category, boolean addCommunityPermissions,
144 boolean addGuestPermissions)
145 throws PortalException, SystemException {
146
147 resourceLocalService.addResources(
148 category.getCompanyId(), category.getGroupId(),
149 category.getUserId(), AssetCategory.class.getName(),
150 category.getCategoryId(), false, addCommunityPermissions,
151 addGuestPermissions);
152 }
153
154 public void addCategoryResources(
155 AssetCategory category, String[] communityPermissions,
156 String[] guestPermissions)
157 throws PortalException, SystemException {
158
159 resourceLocalService.addModelResources(
160 category.getCompanyId(), category.getGroupId(),
161 category.getUserId(), AssetCategory.class.getName(),
162 category.getCategoryId(), communityPermissions, guestPermissions);
163 }
164
165 public void deleteCategory(AssetCategory category)
166 throws PortalException, SystemException {
167
168
170 assetCategoryPersistence.remove(category);
171
172
174 resourceLocalService.deleteResource(
175 category.getCompanyId(), AssetCategory.class.getName(),
176 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
177
178
180 List<AssetCategory> categories =
181 assetCategoryPersistence.findByParentCategoryId(
182 category.getCategoryId());
183
184 for (AssetCategory curCategory : categories) {
185 deleteCategory(curCategory);
186 }
187
188
190 assetCategoryPropertyLocalService.deleteCategoryProperties(
191 category.getCategoryId());
192 }
193
194 public void deleteCategory(long categoryId)
195 throws PortalException, SystemException {
196
197 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
198 categoryId);
199
200 deleteCategory(category);
201 }
202
203 public void deleteVocabularyCategories(long vocabularyId)
204 throws PortalException, SystemException {
205
206 List<AssetCategory> categories =
207 assetCategoryPersistence.findByVocabularyId(vocabularyId);
208
209 for (AssetCategory category : categories) {
210 deleteCategory(category);
211 }
212 }
213
214 public List<AssetCategory> getCategories() throws SystemException {
215 return assetCategoryPersistence.findAll();
216 }
217
218 public List<AssetCategory> getCategories(long classNameId, long classPK)
219 throws SystemException {
220
221 return assetCategoryFinder.findByC_C(classNameId, classPK);
222 }
223
224 public List<AssetCategory> getCategories(String className, long classPK)
225 throws SystemException {
226
227 long classNameId = PortalUtil.getClassNameId(className);
228
229 return getCategories(classNameId, classPK);
230 }
231
232 public AssetCategory getCategory(long categoryId)
233 throws PortalException, SystemException {
234
235 return assetCategoryPersistence.findByPrimaryKey(categoryId);
236 }
237
238 public long[] getCategoryIds(String className, long classPK)
239 throws SystemException {
240
241 return getCategoryIds(getCategories(className, classPK));
242 }
243
244 public List<AssetCategory> getChildCategories(long parentCategoryId)
245 throws SystemException {
246
247 return assetCategoryPersistence.findByParentCategoryId(
248 parentCategoryId);
249 }
250
251 public List<AssetCategory> getEntryCategories(long entryId)
252 throws SystemException {
253
254 return assetCategoryFinder.findByEntryId(entryId);
255 }
256
257 public List<AssetCategory> getVocabularyCategories(long vocabularyId)
258 throws SystemException {
259
260 return assetCategoryPersistence.findByVocabularyId(vocabularyId);
261 }
262
263 public List<AssetCategory> getVocabularyRootCategories(long vocabularyId)
264 throws SystemException {
265
266 return assetCategoryPersistence.findByP_V(
267 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId);
268 }
269
270 public void mergeCategories(long fromCategoryId, long toCategoryId)
271 throws PortalException, SystemException {
272
273 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
274 fromCategoryId);
275
276 assetCategoryPersistence.addAssetEntries(toCategoryId, entries);
277
278 List<AssetCategoryProperty> categoryProperties =
279 assetCategoryPropertyPersistence.findByCategoryId(fromCategoryId);
280
281 for (AssetCategoryProperty fromCategoryProperty : categoryProperties) {
282 AssetCategoryProperty toCategoryProperty =
283 assetCategoryPropertyPersistence.fetchByCA_K(
284 toCategoryId, fromCategoryProperty.getKey());
285
286 if (toCategoryProperty == null) {
287 fromCategoryProperty.setCategoryId(toCategoryId);
288
289 assetCategoryPropertyPersistence.update(
290 fromCategoryProperty, false);
291 }
292 }
293
294 deleteCategory(fromCategoryId);
295 }
296
297 public JSONArray search(
298 long groupId, String name, String[] categoryProperties, int start,
299 int end)
300 throws SystemException {
301
302 List<AssetCategory> list = assetCategoryFinder.findByG_N_P(
303 groupId, name, categoryProperties, start, end);
304
305 return Autocomplete.listToJson(list, "name", "name");
306 }
307
308 public AssetCategory updateCategory(
309 long userId, long categoryId, long parentCategoryId,
310 Map<Locale, String> titleMap, long vocabularyId,
311 String[] categoryProperties, ServiceContext serviceContext)
312 throws PortalException, SystemException {
313
314
316 String name = titleMap.get(LocaleUtil.getDefault());
317
318 if (categoryProperties == null) {
319 categoryProperties = new String[0];
320 }
321
322 validate(categoryId, parentCategoryId, name, vocabularyId);
323
324 if (parentCategoryId > 0) {
325 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
326 }
327
328 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
329
330 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
331 categoryId);
332
333 category.setModifiedDate(new Date());
334 category.setParentCategoryId(parentCategoryId);
335 category.setName(name);
336 category.setTitleMap(titleMap);
337 category.setVocabularyId(vocabularyId);
338
339 assetCategoryPersistence.update(category, false);
340
341
343 List<AssetCategoryProperty> oldCategoryProperties =
344 assetCategoryPropertyPersistence.findByCategoryId(categoryId);
345
346 for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
347 assetCategoryPropertyLocalService.deleteAssetCategoryProperty(
348 categoryProperty);
349 }
350
351 for (int i = 0; i < categoryProperties.length; i++) {
352 String[] categoryProperty = StringUtil.split(
353 categoryProperties[i], StringPool.COLON);
354
355 String key = StringPool.BLANK;
356
357 if (categoryProperty.length > 0) {
358 key = GetterUtil.getString(categoryProperty[0]);
359 }
360
361 String value = StringPool.BLANK;
362
363 if (categoryProperty.length > 1) {
364 value = GetterUtil.getString(categoryProperty[1]);
365 }
366
367 if (Validator.isNotNull(key)) {
368 assetCategoryPropertyLocalService.addCategoryProperty(
369 userId, categoryId, key, value);
370 }
371 }
372
373 return category;
374 }
375
376 protected long[] getCategoryIds(List <AssetCategory>categories) {
377 return StringUtil.split(
378 ListUtil.toString(categories, "categoryId"), 0L);
379 }
380
381 protected void validate(
382 long categoryId, long parentCategoryId, String name,
383 long vocabularyId)
384 throws PortalException, SystemException {
385
386 List<AssetCategory> categories = null;
387
388 if (parentCategoryId == 0) {
389 categories = assetCategoryPersistence.findByN_V(
390 name, vocabularyId);
391 }
392 else {
393 categories = assetCategoryPersistence.findByP_N(
394 parentCategoryId, name);
395 }
396
397 if ((categories.size() > 0) &&
398 (categories.get(0).getCategoryId() != categoryId)) {
399
400 StringBundler sb = new StringBundler(4);
401
402 sb.append("There is another category category named ");
403 sb.append(name);
404 sb.append(" as a child of category ");
405 sb.append(parentCategoryId);
406
407 throw new DuplicateCategoryException(sb.toString());
408 }
409 }
410
411 }