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.util.GetterUtil;
021 import com.liferay.portal.kernel.util.ListUtil;
022 import com.liferay.portal.kernel.util.LocaleUtil;
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.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.ResourceConstants;
029 import com.liferay.portal.model.User;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portlet.asset.DuplicateCategoryException;
033 import com.liferay.portlet.asset.model.AssetCategory;
034 import com.liferay.portlet.asset.model.AssetCategoryConstants;
035 import com.liferay.portlet.asset.model.AssetCategoryProperty;
036 import com.liferay.portlet.asset.model.AssetEntry;
037 import com.liferay.portlet.asset.service.base.AssetCategoryLocalServiceBaseImpl;
038 import com.liferay.util.Autocomplete;
039
040 import java.util.Date;
041 import java.util.List;
042 import java.util.Locale;
043 import java.util.Map;
044
045
051 public class AssetCategoryLocalServiceImpl
052 extends AssetCategoryLocalServiceBaseImpl {
053
054 public AssetCategory addCategory(
055 long userId, long parentCategoryId, Map<Locale, String> titleMap,
056 long vocabularyId, String[] categoryProperties,
057 ServiceContext serviceContext)
058 throws PortalException, SystemException {
059
060
061
062 User user = userPersistence.findByPrimaryKey(userId);
063 long groupId = serviceContext.getScopeGroupId();
064 String name = titleMap.get(LocaleUtil.getDefault());
065
066 if (categoryProperties == null) {
067 categoryProperties = new String[0];
068 }
069
070 Date now = new Date();
071
072 validate(0, parentCategoryId, name, vocabularyId);
073
074 if (parentCategoryId > 0) {
075 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
076 }
077
078 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
079
080 long categoryId = counterLocalService.increment();
081
082 AssetCategory category = assetCategoryPersistence.create(categoryId);
083
084 category.setUuid(serviceContext.getUuid());
085 category.setGroupId(groupId);
086 category.setCompanyId(user.getCompanyId());
087 category.setUserId(user.getUserId());
088 category.setUserName(user.getFullName());
089 category.setCreateDate(now);
090 category.setModifiedDate(now);
091 category.setParentCategoryId(parentCategoryId);
092 category.setName(name);
093 category.setTitleMap(titleMap);
094 category.setVocabularyId(vocabularyId);
095
096 assetCategoryPersistence.update(category, false);
097
098
099
100 if (serviceContext.getAddCommunityPermissions() ||
101 serviceContext.getAddGuestPermissions()) {
102
103 addCategoryResources(
104 category, serviceContext.getAddCommunityPermissions(),
105 serviceContext.getAddGuestPermissions());
106 }
107 else {
108 addCategoryResources(
109 category, serviceContext.getCommunityPermissions(),
110 serviceContext.getGuestPermissions());
111 }
112
113
114
115 for (int i = 0; i < categoryProperties.length; i++) {
116 String[] categoryProperty = StringUtil.split(
117 categoryProperties[i], StringPool.COLON);
118
119 String key = StringPool.BLANK;
120 String value = StringPool.BLANK;
121
122 if (categoryProperty.length > 1) {
123 key = GetterUtil.getString(categoryProperty[0]);
124 value = GetterUtil.getString(categoryProperty[1]);
125 }
126
127 if (Validator.isNotNull(key)) {
128 assetCategoryPropertyLocalService.addCategoryProperty(
129 userId, categoryId, key, value);
130 }
131 }
132
133 return category;
134 }
135
136 public void addCategoryResources(
137 AssetCategory category, boolean addCommunityPermissions,
138 boolean addGuestPermissions)
139 throws PortalException, SystemException {
140
141 resourceLocalService.addResources(
142 category.getCompanyId(), category.getGroupId(),
143 category.getUserId(), AssetCategory.class.getName(),
144 category.getCategoryId(), false, addCommunityPermissions,
145 addGuestPermissions);
146 }
147
148 public void addCategoryResources(
149 AssetCategory category, String[] communityPermissions,
150 String[] guestPermissions)
151 throws PortalException, SystemException {
152
153 resourceLocalService.addModelResources(
154 category.getCompanyId(), category.getGroupId(),
155 category.getUserId(), AssetCategory.class.getName(),
156 category.getCategoryId(), communityPermissions, guestPermissions);
157 }
158
159 public void deleteCategory(AssetCategory category)
160 throws PortalException, SystemException {
161
162
163
164 assetCategoryPersistence.remove(category);
165
166
167
168 resourceLocalService.deleteResource(
169 category.getCompanyId(), AssetCategory.class.getName(),
170 ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
171
172
173
174 List<AssetCategory> categories =
175 assetCategoryPersistence.findByParentCategoryId(
176 category.getCategoryId());
177
178 for (AssetCategory curCategory : categories) {
179 deleteCategory(curCategory);
180 }
181
182
183
184 assetCategoryPropertyLocalService.deleteCategoryProperties(
185 category.getCategoryId());
186 }
187
188 public void deleteCategory(long categoryId)
189 throws PortalException, SystemException {
190
191 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
192 categoryId);
193
194 deleteCategory(category);
195 }
196
197 public void deleteVocabularyCategories(long vocabularyId)
198 throws PortalException, SystemException {
199
200 List<AssetCategory> categories =
201 assetCategoryPersistence.findByVocabularyId(vocabularyId);
202
203 for (AssetCategory category : categories) {
204 deleteCategory(category);
205 }
206 }
207
208 public List<AssetCategory> getCategories() throws SystemException {
209 return assetCategoryPersistence.findAll();
210 }
211
212 public List<AssetCategory> getCategories(long classNameId, long classPK)
213 throws SystemException {
214
215 return assetCategoryFinder.findByC_C(classNameId, classPK);
216 }
217
218 public List<AssetCategory> getCategories(String className, long classPK)
219 throws SystemException {
220
221 long classNameId = PortalUtil.getClassNameId(className);
222
223 return getCategories(classNameId, classPK);
224 }
225
226 public AssetCategory getCategory(long categoryId)
227 throws PortalException, SystemException {
228
229 return assetCategoryPersistence.findByPrimaryKey(categoryId);
230 }
231
232 public long[] getCategoryIds(String className, long classPK)
233 throws SystemException {
234
235 return getCategoryIds(getCategories(className, classPK));
236 }
237
238 public List<AssetCategory> getChildCategories(
239 long parentCategoryId, int start, int end, OrderByComparator obc)
240 throws SystemException {
241
242 return assetCategoryPersistence.findByParentCategoryId(
243 parentCategoryId, start, end, obc);
244 }
245
246 public int getChildCategoriesCount(long parentCategoryId)
247 throws SystemException {
248
249 return assetCategoryPersistence.countByParentCategoryId(
250 parentCategoryId);
251 }
252
253 public List<AssetCategory> getEntryCategories(long entryId)
254 throws SystemException {
255
256 return assetCategoryFinder.findByEntryId(entryId);
257 }
258
259 public List<AssetCategory> getVocabularyCategories(
260 long vocabularyId, int start, int end, OrderByComparator obc)
261 throws SystemException {
262
263 return assetCategoryPersistence.findByVocabularyId(
264 vocabularyId, start, end, obc);
265 }
266
267 public List<AssetCategory> getVocabularyCategories(
268 long parentCategoryId, long vocabularyId, int start, int end,
269 OrderByComparator obc)
270 throws SystemException {
271
272 return assetCategoryPersistence.findByP_V(
273 parentCategoryId, vocabularyId, start, end, obc);
274 }
275
276 public List<AssetCategory> getVocabularyRootCategories(
277 long vocabularyId, int start, int end, OrderByComparator obc)
278 throws SystemException {
279
280 return getVocabularyCategories(
281 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId,
282 start, end, obc);
283 }
284
285 public void mergeCategories(long fromCategoryId, long toCategoryId)
286 throws PortalException, SystemException {
287
288 List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
289 fromCategoryId);
290
291 assetCategoryPersistence.addAssetEntries(toCategoryId, entries);
292
293 List<AssetCategoryProperty> categoryProperties =
294 assetCategoryPropertyPersistence.findByCategoryId(fromCategoryId);
295
296 for (AssetCategoryProperty fromCategoryProperty : categoryProperties) {
297 AssetCategoryProperty toCategoryProperty =
298 assetCategoryPropertyPersistence.fetchByCA_K(
299 toCategoryId, fromCategoryProperty.getKey());
300
301 if (toCategoryProperty == null) {
302 fromCategoryProperty.setCategoryId(toCategoryId);
303
304 assetCategoryPropertyPersistence.update(
305 fromCategoryProperty, false);
306 }
307 }
308
309 deleteCategory(fromCategoryId);
310 }
311
312 public JSONArray search(
313 long groupId, String name, String[] categoryProperties, int start,
314 int end)
315 throws SystemException {
316
317 List<AssetCategory> list = assetCategoryFinder.findByG_N_P(
318 groupId, name, categoryProperties, start, end);
319
320 return Autocomplete.listToJson(list, "name", "name");
321 }
322
323 public AssetCategory updateCategory(
324 long userId, long categoryId, long parentCategoryId,
325 Map<Locale, String> titleMap, long vocabularyId,
326 String[] categoryProperties, ServiceContext serviceContext)
327 throws PortalException, SystemException {
328
329
330
331 String name = titleMap.get(LocaleUtil.getDefault());
332
333 if (categoryProperties == null) {
334 categoryProperties = new String[0];
335 }
336
337 validate(categoryId, parentCategoryId, name, vocabularyId);
338
339 if (parentCategoryId > 0) {
340 assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
341 }
342
343 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
344
345 AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
346 categoryId);
347
348 category.setModifiedDate(new Date());
349 category.setParentCategoryId(parentCategoryId);
350 category.setName(name);
351 category.setTitleMap(titleMap);
352 category.setVocabularyId(vocabularyId);
353
354 assetCategoryPersistence.update(category, false);
355
356
357
358 List<AssetCategoryProperty> oldCategoryProperties =
359 assetCategoryPropertyPersistence.findByCategoryId(categoryId);
360
361 for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
362 assetCategoryPropertyLocalService.deleteAssetCategoryProperty(
363 categoryProperty);
364 }
365
366 for (int i = 0; i < categoryProperties.length; i++) {
367 String[] categoryProperty = StringUtil.split(
368 categoryProperties[i], StringPool.COLON);
369
370 String key = StringPool.BLANK;
371
372 if (categoryProperty.length > 0) {
373 key = GetterUtil.getString(categoryProperty[0]);
374 }
375
376 String value = StringPool.BLANK;
377
378 if (categoryProperty.length > 1) {
379 value = GetterUtil.getString(categoryProperty[1]);
380 }
381
382 if (Validator.isNotNull(key)) {
383 assetCategoryPropertyLocalService.addCategoryProperty(
384 userId, categoryId, key, value);
385 }
386 }
387
388 return category;
389 }
390
391 protected long[] getCategoryIds(List <AssetCategory>categories) {
392 return StringUtil.split(
393 ListUtil.toString(categories, "categoryId"), 0L);
394 }
395
396 protected void validate(
397 long categoryId, long parentCategoryId, String name,
398 long vocabularyId)
399 throws PortalException, SystemException {
400
401 List<AssetCategory> categories = null;
402
403 if (parentCategoryId == 0) {
404 categories = assetCategoryPersistence.findByN_V(
405 name, vocabularyId);
406 }
407 else {
408 categories = assetCategoryPersistence.findByP_N(
409 parentCategoryId, name);
410 }
411
412 if ((categories.size() > 0) &&
413 (categories.get(0).getCategoryId() != categoryId)) {
414
415 StringBundler sb = new StringBundler(4);
416
417 sb.append("There is another category named ");
418 sb.append(name);
419 sb.append(" as a child of category ");
420 sb.append(parentCategoryId);
421
422 throw new DuplicateCategoryException(sb.toString());
423 }
424 }
425
426 }