001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.asset.service.impl;
016    
017    import com.liferay.portal.kernel.cache.ThreadLocalCachable;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.ModelHintsUtil;
030    import com.liferay.portal.model.ResourceConstants;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portlet.asset.AssetCategoryNameException;
035    import com.liferay.portlet.asset.DuplicateCategoryException;
036    import com.liferay.portlet.asset.model.AssetCategory;
037    import com.liferay.portlet.asset.model.AssetCategoryConstants;
038    import com.liferay.portlet.asset.model.AssetCategoryProperty;
039    import com.liferay.portlet.asset.model.AssetEntry;
040    import com.liferay.portlet.asset.service.base.AssetCategoryLocalServiceBaseImpl;
041    
042    import java.util.Date;
043    import java.util.Iterator;
044    import java.util.List;
045    import java.util.Locale;
046    import java.util.Map;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Alvaro del Castillo
051     * @author Jorge Ferrer
052     * @author Bruno Farache
053     */
054    public class AssetCategoryLocalServiceImpl
055            extends AssetCategoryLocalServiceBaseImpl {
056    
057            public AssetCategory addCategory(
058                            long userId, long parentCategoryId, Map<Locale, String> titleMap,
059                            Map<Locale, String> descriptionMap, long vocabularyId,
060                            String[] categoryProperties, ServiceContext serviceContext)
061                    throws PortalException, SystemException {
062    
063                    // Category
064    
065                    User user = userPersistence.findByPrimaryKey(userId);
066                    long groupId = serviceContext.getScopeGroupId();
067    
068                    String name = titleMap.get(LocaleUtil.getDefault());
069    
070                    name = ModelHintsUtil.trimString(
071                            AssetCategory.class.getName(), "name", name);
072    
073                    if (categoryProperties == null) {
074                            categoryProperties = new String[0];
075                    }
076    
077                    Date now = new Date();
078    
079                    validate(0, parentCategoryId, name, vocabularyId);
080    
081                    if (parentCategoryId > 0) {
082                            assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
083                    }
084    
085                    assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
086    
087                    long categoryId = counterLocalService.increment();
088    
089                    AssetCategory category = assetCategoryPersistence.create(categoryId);
090    
091                    category.setUuid(serviceContext.getUuid());
092                    category.setGroupId(groupId);
093                    category.setCompanyId(user.getCompanyId());
094                    category.setUserId(user.getUserId());
095                    category.setUserName(user.getFullName());
096                    category.setCreateDate(now);
097                    category.setModifiedDate(now);
098                    category.setParentCategoryId(parentCategoryId);
099                    category.setName(name);
100                    category.setTitleMap(titleMap);
101                    category.setDescriptionMap(descriptionMap);
102                    category.setVocabularyId(vocabularyId);
103    
104                    assetCategoryPersistence.update(category);
105    
106                    // Resources
107    
108                    if (serviceContext.isAddGroupPermissions() ||
109                            serviceContext.isAddGuestPermissions()) {
110    
111                            addCategoryResources(
112                                    category, serviceContext.isAddGroupPermissions(),
113                                    serviceContext.isAddGuestPermissions());
114                    }
115                    else {
116                            addCategoryResources(
117                                    category, serviceContext.getGroupPermissions(),
118                                    serviceContext.getGuestPermissions());
119                    }
120    
121                    // Properties
122    
123                    for (int i = 0; i < categoryProperties.length; i++) {
124                            String[] categoryProperty = StringUtil.split(
125                                    categoryProperties[i], CharPool.COLON);
126    
127                            String key = StringPool.BLANK;
128                            String value = StringPool.BLANK;
129    
130                            if (categoryProperty.length > 1) {
131                                    key = GetterUtil.getString(categoryProperty[0]);
132                                    value = GetterUtil.getString(categoryProperty[1]);
133                            }
134    
135                            if (Validator.isNotNull(key)) {
136                                    assetCategoryPropertyLocalService.addCategoryProperty(
137                                            userId, categoryId, key, value);
138                            }
139                    }
140    
141                    return category;
142            }
143    
144            public void addCategoryResources(
145                            AssetCategory category, boolean addGroupPermissions,
146                            boolean addGuestPermissions)
147                    throws PortalException, SystemException {
148    
149                    resourceLocalService.addResources(
150                            category.getCompanyId(), category.getGroupId(),
151                            category.getUserId(), AssetCategory.class.getName(),
152                            category.getCategoryId(), false, addGroupPermissions,
153                            addGuestPermissions);
154            }
155    
156            public void addCategoryResources(
157                            AssetCategory category, String[] groupPermissions,
158                            String[] guestPermissions)
159                    throws PortalException, SystemException {
160    
161                    resourceLocalService.addModelResources(
162                            category.getCompanyId(), category.getGroupId(),
163                            category.getUserId(), AssetCategory.class.getName(),
164                            category.getCategoryId(), groupPermissions, guestPermissions);
165            }
166    
167            public void deleteCategory(AssetCategory category)
168                    throws PortalException, SystemException {
169    
170                    // Entries
171    
172                    List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
173                            category.getCategoryId());
174    
175                    // Category
176    
177                    assetCategoryPersistence.remove(category);
178    
179                    // Resources
180    
181                    resourceLocalService.deleteResource(
182                            category.getCompanyId(), AssetCategory.class.getName(),
183                            ResourceConstants.SCOPE_INDIVIDUAL, category.getCategoryId());
184    
185                    // Categories
186    
187                    List<AssetCategory> categories =
188                            assetCategoryPersistence.findByParentCategoryId(
189                                    category.getCategoryId());
190    
191                    for (AssetCategory curCategory : categories) {
192                            deleteCategory(curCategory);
193                    }
194    
195                    // Properties
196    
197                    assetCategoryPropertyLocalService.deleteCategoryProperties(
198                            category.getCategoryId());
199    
200                    // Indexer
201    
202                    assetEntryLocalService.reindex(entries);
203            }
204    
205            public void deleteCategory(long categoryId)
206                    throws PortalException, SystemException {
207    
208                    AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
209                            categoryId);
210    
211                    deleteCategory(category);
212            }
213    
214            public void deleteVocabularyCategories(long vocabularyId)
215                    throws PortalException, SystemException {
216    
217                    List<AssetCategory> categories =
218                            assetCategoryPersistence.findByVocabularyId(vocabularyId);
219    
220                    for (AssetCategory category : categories) {
221                            deleteCategory(category);
222                    }
223            }
224    
225            public AssetCategory fetchCategory(long categoryId) throws SystemException {
226                    return assetCategoryPersistence.fetchByPrimaryKey(categoryId);
227            }
228    
229            public List<AssetCategory> getCategories() throws SystemException {
230                    return assetCategoryPersistence.findAll();
231            }
232    
233            @ThreadLocalCachable
234            public List<AssetCategory> getCategories(long classNameId, long classPK)
235                    throws SystemException {
236    
237                    return assetCategoryFinder.findByC_C(classNameId, classPK);
238            }
239    
240            public List<AssetCategory> getCategories(String className, long classPK)
241                    throws SystemException {
242    
243                    long classNameId = PortalUtil.getClassNameId(className);
244    
245                    return getCategories(classNameId, classPK);
246            }
247    
248            public AssetCategory getCategory(long categoryId)
249                    throws PortalException, SystemException {
250    
251                    return assetCategoryPersistence.findByPrimaryKey(categoryId);
252            }
253    
254            public long[] getCategoryIds(String className, long classPK)
255                    throws SystemException {
256    
257                    return getCategoryIds(getCategories(className, classPK));
258            }
259    
260            public String[] getCategoryNames() throws SystemException {
261                    return getCategoryNames(getCategories());
262            }
263    
264            public String[] getCategoryNames(long classNameId, long classPK)
265                    throws SystemException {
266    
267                    return getCategoryNames(getCategories(classNameId, classPK));
268            }
269    
270            public String[] getCategoryNames(String className, long classPK)
271                    throws SystemException {
272    
273                    return getCategoryNames(getCategories(className, classPK));
274            }
275    
276            public List<AssetCategory> getChildCategories(long parentCategoryId)
277                    throws SystemException {
278    
279                    return assetCategoryPersistence.findByParentCategoryId(
280                            parentCategoryId);
281            }
282    
283            public List<AssetCategory> getChildCategories(
284                            long parentCategoryId, int start, int end, OrderByComparator obc)
285                    throws SystemException {
286    
287                    return assetCategoryPersistence.findByParentCategoryId(
288                            parentCategoryId, start, end, obc);
289            }
290    
291            public int getChildCategoriesCount(long parentCategoryId)
292                    throws SystemException {
293    
294                    return assetCategoryPersistence.countByParentCategoryId(
295                            parentCategoryId);
296            }
297    
298            public List<AssetCategory> getEntryCategories(long entryId)
299                    throws SystemException {
300    
301                    return assetCategoryFinder.findByEntryId(entryId);
302            }
303    
304            public List<AssetCategory> getVocabularyCategories(
305                            long vocabularyId, int start, int end, OrderByComparator obc)
306                    throws SystemException {
307    
308                    return assetCategoryPersistence.findByVocabularyId(
309                            vocabularyId, start, end, obc);
310            }
311    
312            public List<AssetCategory> getVocabularyCategories(
313                            long parentCategoryId, long vocabularyId, int start, int end,
314                            OrderByComparator obc)
315                    throws SystemException {
316    
317                    return assetCategoryPersistence.findByP_V(
318                            parentCategoryId, vocabularyId, start, end, obc);
319            }
320    
321            public int getVocabularyCategoriesCount(long vocabularyId)
322                    throws SystemException {
323    
324                    return assetCategoryPersistence.countByVocabularyId(vocabularyId);
325            }
326    
327            public List<AssetCategory> getVocabularyRootCategories(
328                            long vocabularyId, int start, int end, OrderByComparator obc)
329                    throws SystemException {
330    
331                    return getVocabularyCategories(
332                            AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID, vocabularyId,
333                            start, end, obc);
334            }
335    
336            public void mergeCategories(long fromCategoryId, long toCategoryId)
337                    throws PortalException, SystemException {
338    
339                    List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
340                            fromCategoryId);
341    
342                    assetCategoryPersistence.addAssetEntries(toCategoryId, entries);
343    
344                    List<AssetCategoryProperty> categoryProperties =
345                            assetCategoryPropertyPersistence.findByCategoryId(fromCategoryId);
346    
347                    for (AssetCategoryProperty fromCategoryProperty : categoryProperties) {
348                            AssetCategoryProperty toCategoryProperty =
349                                    assetCategoryPropertyPersistence.fetchByCA_K(
350                                            toCategoryId, fromCategoryProperty.getKey());
351    
352                            if (toCategoryProperty == null) {
353                                    fromCategoryProperty.setCategoryId(toCategoryId);
354    
355                                    assetCategoryPropertyPersistence.update(fromCategoryProperty);
356                            }
357                    }
358    
359                    deleteCategory(fromCategoryId);
360            }
361    
362            public AssetCategory moveCategory(
363                            long categoryId, long parentCategoryId, long vocabularyId,
364                            ServiceContext serviceContext)
365                    throws PortalException, SystemException {
366    
367                    AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
368                            categoryId);
369    
370                    validate(
371                            categoryId, parentCategoryId, category.getName(), vocabularyId);
372    
373                    if (parentCategoryId > 0) {
374                            assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
375                    }
376    
377                    if (vocabularyId != category.getVocabularyId()) {
378                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
379    
380                            category.setVocabularyId(vocabularyId);
381    
382                            updateChildrenVocabularyId(category, vocabularyId);
383                    }
384    
385                    category.setModifiedDate(new Date());
386                    category.setParentCategoryId(parentCategoryId);
387    
388                    assetCategoryPersistence.update(category);
389    
390                    return category;
391            }
392    
393            public void rebuildTree(long groupId, boolean force)
394                    throws SystemException {
395    
396                    assetCategoryPersistence.rebuildTree(groupId, force);
397            }
398    
399            public List<AssetCategory> search(
400                            long groupId, String name, String[] categoryProperties, int start,
401                            int end)
402                    throws SystemException {
403    
404                    return assetCategoryFinder.findByG_N_P(
405                            groupId, name, categoryProperties, start, end);
406            }
407    
408            public AssetCategory updateCategory(
409                            long userId, long categoryId, long parentCategoryId,
410                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
411                            long vocabularyId, String[] categoryProperties,
412                            ServiceContext serviceContext)
413                    throws PortalException, SystemException {
414    
415                    // Category
416    
417                    String name = titleMap.get(LocaleUtil.getDefault());
418    
419                    name = ModelHintsUtil.trimString(
420                            AssetCategory.class.getName(), "name", name);
421    
422                    if (categoryProperties == null) {
423                            categoryProperties = new String[0];
424                    }
425    
426                    validate(categoryId, parentCategoryId, name, vocabularyId);
427    
428                    if (parentCategoryId > 0) {
429                            assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
430                    }
431    
432                    AssetCategory category = assetCategoryPersistence.findByPrimaryKey(
433                            categoryId);
434    
435                    String oldName = category.getName();
436    
437                    if (vocabularyId != category.getVocabularyId()) {
438                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
439    
440                            parentCategoryId =
441                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
442    
443                            category.setVocabularyId(vocabularyId);
444    
445                            updateChildrenVocabularyId(category, vocabularyId);
446                    }
447    
448                    category.setModifiedDate(new Date());
449                    category.setParentCategoryId(parentCategoryId);
450                    category.setName(name);
451                    category.setTitleMap(titleMap);
452                    category.setDescriptionMap(descriptionMap);
453    
454                    assetCategoryPersistence.update(category);
455    
456                    // Properties
457    
458                    List<AssetCategoryProperty> oldCategoryProperties =
459                            assetCategoryPropertyPersistence.findByCategoryId(categoryId);
460    
461                    for (int i = 0; i < categoryProperties.length; i++) {
462                            String[] categoryProperty = StringUtil.split(
463                                    categoryProperties[i], CharPool.COLON);
464    
465                            String key = StringPool.BLANK;
466    
467                            if (categoryProperty.length > 0) {
468                                    key = GetterUtil.getString(categoryProperty[0]);
469                            }
470    
471                            String value = StringPool.BLANK;
472    
473                            if (categoryProperty.length > 1) {
474                                    value = GetterUtil.getString(categoryProperty[1]);
475                            }
476    
477                            if (Validator.isNotNull(key)) {
478                                    boolean addCategoryProperty = true;
479    
480                                    Iterator<AssetCategoryProperty> iterator =
481                                            oldCategoryProperties.iterator();
482    
483                                    while (iterator.hasNext()) {
484                                            AssetCategoryProperty oldCategoryProperty = iterator.next();
485    
486                                            if ((userId == oldCategoryProperty.getUserId()) &&
487                                                    (categoryId == oldCategoryProperty.getCategoryId()) &&
488                                                    key.equals(oldCategoryProperty.getKey()) &&
489                                                    value.equals(oldCategoryProperty.getValue())) {
490    
491                                                    addCategoryProperty = false;
492    
493                                                    iterator.remove();
494    
495                                                    break;
496                                            }
497                                    }
498    
499                                    if (addCategoryProperty) {
500                                            assetCategoryPropertyLocalService.addCategoryProperty(
501                                                    userId, categoryId, key, value);
502                                    }
503                            }
504                    }
505    
506                    for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
507                            assetCategoryPropertyLocalService.deleteAssetCategoryProperty(
508                                    categoryProperty);
509                    }
510    
511                    // Indexer
512    
513                    if (!oldName.equals(name)) {
514                            List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(
515                                    category.getCategoryId());
516    
517                            assetEntryLocalService.reindex(entries);
518                    }
519    
520                    return category;
521            }
522    
523            protected long[] getCategoryIds(List<AssetCategory> categories) {
524                    return StringUtil.split(
525                            ListUtil.toString(categories, AssetCategory.CATEGORY_ID_ACCESSOR),
526                            0L);
527            }
528    
529            protected String[] getCategoryNames(List<AssetCategory> categories) {
530                    return StringUtil.split(
531                            ListUtil.toString(categories, AssetCategory.NAME_ACCESSOR));
532            }
533    
534            protected void updateChildrenVocabularyId(
535                            AssetCategory category, long vocabularyId)
536                    throws SystemException {
537    
538                    List<AssetCategory> childrenCategories =
539                            assetCategoryPersistence.findByParentCategoryId(
540                                    category.getCategoryId());
541    
542                    if (!childrenCategories.isEmpty()) {
543                            for (AssetCategory childCategory : childrenCategories) {
544                                    childCategory.setVocabularyId(vocabularyId);
545                                    childCategory.setModifiedDate(new Date());
546    
547                                    assetCategoryPersistence.update(childCategory);
548    
549                                    updateChildrenVocabularyId (childCategory, vocabularyId);
550                            }
551                    }
552            }
553    
554            protected void validate(
555                            long categoryId, long parentCategoryId, String name,
556                            long vocabularyId)
557                    throws PortalException, SystemException {
558    
559                    if (Validator.isNull(name)) {
560                            throw new AssetCategoryNameException();
561                    }
562    
563                    AssetCategory category = assetCategoryPersistence.fetchByP_N_V(
564                            parentCategoryId, name, vocabularyId);
565    
566                    if ((category != null) && (category.getCategoryId() != categoryId)) {
567                            StringBundler sb = new StringBundler(4);
568    
569                            sb.append("There is another category named ");
570                            sb.append(name);
571                            sb.append(" as a child of category ");
572                            sb.append(parentCategoryId);
573    
574                            throw new DuplicateCategoryException(sb.toString());
575                    }
576            }
577    
578    }