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