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