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