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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.Group;
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.portal.util.PropsValues;
032    import com.liferay.portlet.asset.DuplicateVocabularyException;
033    import com.liferay.portlet.asset.VocabularyNameException;
034    import com.liferay.portlet.asset.model.AssetVocabulary;
035    import com.liferay.portlet.asset.service.base.AssetVocabularyLocalServiceBaseImpl;
036    
037    import java.util.ArrayList;
038    import java.util.Date;
039    import java.util.HashMap;
040    import java.util.List;
041    import java.util.Locale;
042    import java.util.Map;
043    
044    /**
045     * @author Alvaro del Castillo
046     * @author Eduardo Lundgren
047     * @author Jorge Ferrer
048     * @author Juan Fernández
049     */
050    public class AssetVocabularyLocalServiceImpl
051            extends AssetVocabularyLocalServiceBaseImpl {
052    
053            /**
054             * @deprecated
055             */
056            public AssetVocabulary addVocabulary(
057                            long userId, Map<Locale, String> titleMap,
058                            Map<Locale, String> descriptionMap, String settings,
059                            ServiceContext serviceContext)
060                    throws PortalException, SystemException {
061    
062                    return addVocabulary(
063                            userId, StringPool.BLANK, titleMap, descriptionMap, settings,
064                            serviceContext);
065            }
066    
067            public AssetVocabulary addVocabulary(
068                            long userId, String title, Map<Locale, String> titleMap,
069                            Map<Locale, String> descriptionMap, String settings,
070                            ServiceContext serviceContext)
071                    throws PortalException, SystemException {
072    
073                    // Vocabulary
074    
075                    User user = userPersistence.findByPrimaryKey(userId);
076                    long groupId = serviceContext.getScopeGroupId();
077                    String name = titleMap.get(LocaleUtil.getDefault());
078    
079                    Date now = new Date();
080    
081                    validate(groupId, name);
082    
083                    long vocabularyId = counterLocalService.increment();
084    
085                    AssetVocabulary vocabulary = assetVocabularyPersistence.create(
086                            vocabularyId);
087    
088                    vocabulary.setUuid(serviceContext.getUuid());
089                    vocabulary.setGroupId(groupId);
090                    vocabulary.setCompanyId(user.getCompanyId());
091                    vocabulary.setUserId(user.getUserId());
092                    vocabulary.setUserName(user.getFullName());
093                    vocabulary.setCreateDate(now);
094                    vocabulary.setModifiedDate(now);
095                    vocabulary.setName(name);
096    
097                    if (Validator.isNotNull(title)) {
098                            vocabulary.setTitle(title);
099                    }
100                    else {
101                            vocabulary.setTitleMap(titleMap);
102                    }
103    
104                    vocabulary.setDescriptionMap(descriptionMap);
105                    vocabulary.setSettings(settings);
106    
107                    assetVocabularyPersistence.update(vocabulary);
108    
109                    // Resources
110    
111                    if (serviceContext.isAddGroupPermissions() ||
112                            serviceContext.isAddGuestPermissions()) {
113    
114                            addVocabularyResources(
115                                    vocabulary, serviceContext.isAddGroupPermissions(),
116                                    serviceContext.isAddGuestPermissions());
117                    }
118                    else {
119                            addVocabularyResources(
120                                    vocabulary, serviceContext.getGroupPermissions(),
121                                    serviceContext.getGuestPermissions());
122                    }
123    
124                    return vocabulary;
125            }
126    
127            public AssetVocabulary addVocabulary(
128                            long userId, String title, ServiceContext serviceContext)
129                    throws PortalException, SystemException {
130    
131                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
132    
133                    Locale locale = LocaleUtil.getDefault();
134    
135                    titleMap.put(locale, title);
136    
137                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
138    
139                    descriptionMap.put(locale, StringPool.BLANK);
140    
141                    return addVocabulary(
142                            userId, title, titleMap, descriptionMap, null, serviceContext);
143            }
144    
145            public void addVocabularyResources(
146                            AssetVocabulary vocabulary, boolean addGroupPermissions,
147                            boolean addGuestPermissions)
148                    throws PortalException, SystemException {
149    
150                    resourceLocalService.addResources(
151                            vocabulary.getCompanyId(), vocabulary.getGroupId(),
152                            vocabulary.getUserId(), AssetVocabulary.class.getName(),
153                            vocabulary.getVocabularyId(), false, addGroupPermissions,
154                            addGuestPermissions);
155            }
156    
157            public void addVocabularyResources(
158                            AssetVocabulary vocabulary, String[] groupPermissions,
159                            String[] guestPermissions)
160                    throws PortalException, SystemException {
161    
162                    resourceLocalService.addModelResources(
163                            vocabulary.getCompanyId(), vocabulary.getGroupId(),
164                            vocabulary.getUserId(), AssetVocabulary.class.getName(),
165                            vocabulary.getVocabularyId(), groupPermissions, guestPermissions);
166            }
167    
168            public void deleteVocabularies(long groupId)
169                    throws PortalException, SystemException {
170    
171                    List<AssetVocabulary> vocabularies =
172                            assetVocabularyPersistence.findByGroupId(groupId);
173    
174                    for (AssetVocabulary vocabulary : vocabularies) {
175                            deleteVocabulary(vocabulary);
176                    }
177            }
178    
179            public void deleteVocabulary(AssetVocabulary vocabulary)
180                    throws PortalException, SystemException {
181    
182                    // Vocabulary
183    
184                    assetVocabularyPersistence.remove(vocabulary);
185    
186                    // Resources
187    
188                    resourceLocalService.deleteResource(
189                            vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
190                            ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
191    
192                    // Categories
193    
194                    assetCategoryLocalService.deleteVocabularyCategories(
195                            vocabulary.getVocabularyId());
196            }
197    
198            public void deleteVocabulary(long vocabularyId)
199                    throws PortalException, SystemException {
200    
201                    AssetVocabulary vocabulary =
202                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
203    
204                    deleteVocabulary(vocabulary);
205            }
206    
207            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
208                    throws SystemException {
209    
210                    return assetVocabularyPersistence.findByCompanyId(companyId);
211            }
212    
213            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
214                    throws PortalException, SystemException {
215    
216                    return getGroupsVocabularies(groupIds, null);
217            }
218    
219            public List<AssetVocabulary> getGroupsVocabularies(
220                            long[] groupIds, String className)
221                    throws PortalException, SystemException {
222    
223                    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
224    
225                    groupIds = ArrayUtil.unique(groupIds);
226    
227                    for (long groupId : groupIds) {
228                            List<AssetVocabulary> groupVocabularies = getGroupVocabularies(
229                                    groupId);
230    
231                            if (Validator.isNull(className)) {
232                                    vocabularies.addAll(groupVocabularies);
233    
234                                    continue;
235                            }
236    
237                            for (AssetVocabulary groupVocabulary : groupVocabularies) {
238                                    UnicodeProperties settingsProperties =
239                                            groupVocabulary.getSettingsProperties();
240    
241                                    long[] selectedClassNameIds = StringUtil.split(
242                                            settingsProperties.getProperty("selectedClassNameIds"), 0L);
243                                    long classNameId = PortalUtil.getClassNameId(className);
244    
245                                    if ((selectedClassNameIds.length == 0) ||
246                                            (selectedClassNameIds[0] == 0) ||
247                                            ArrayUtil.contains(selectedClassNameIds, classNameId)) {
248    
249                                            vocabularies.add(groupVocabulary);
250                                    }
251                            }
252                    }
253    
254                    return vocabularies;
255            }
256    
257            public List<AssetVocabulary> getGroupVocabularies(long groupId)
258                    throws PortalException, SystemException {
259    
260                    return getGroupVocabularies(groupId, true);
261            }
262    
263            public List<AssetVocabulary> getGroupVocabularies(
264                            long groupId, boolean createDefaultVocabulary)
265                    throws PortalException, SystemException {
266    
267                    List<AssetVocabulary> vocabularies =
268                            assetVocabularyPersistence.findByGroupId(groupId);
269    
270                    if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
271                            return vocabularies;
272                    }
273    
274                    Group group = groupLocalService.getGroup(groupId);
275    
276                    long defaultUserId = userLocalService.getDefaultUserId(
277                            group.getCompanyId());
278    
279                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
280    
281                    titleMap.put(
282                            LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
283    
284                    ServiceContext serviceContext = new ServiceContext();
285    
286                    serviceContext.setScopeGroupId(groupId);
287    
288                    AssetVocabulary vocabulary = assetVocabularyLocalService.addVocabulary(
289                            defaultUserId, StringPool.BLANK, titleMap, null, StringPool.BLANK,
290                            serviceContext);
291    
292                    vocabularies = new ArrayList<AssetVocabulary>();
293    
294                    vocabularies.add(vocabulary);
295    
296                    return vocabularies;
297            }
298    
299            public List<AssetVocabulary> getGroupVocabularies(
300                            long groupId, String name, int start, int end,
301                            OrderByComparator obc)
302                    throws SystemException {
303    
304                    return assetVocabularyFinder.findByG_N(groupId, name, start, end, obc);
305            }
306    
307            public AssetVocabulary getGroupVocabulary(long groupId, String name)
308                    throws PortalException, SystemException {
309    
310                    return assetVocabularyPersistence.findByG_N(groupId, name);
311            }
312    
313            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
314                    throws PortalException, SystemException {
315    
316                    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
317    
318                    for (long vocabularyId : vocabularyIds) {
319                            AssetVocabulary vocabulary = getVocabulary(vocabularyId);
320    
321                            vocabularies.add(vocabulary);
322                    }
323    
324                    return vocabularies;
325            }
326    
327            public AssetVocabulary getVocabulary(long vocabularyId)
328                    throws PortalException, SystemException {
329    
330                    return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
331            }
332    
333            /**
334             * @deprecated
335             */
336            public AssetVocabulary updateVocabulary(
337                            long vocabularyId, Map<Locale, String> titleMap,
338                            Map<Locale, String> descriptionMap, String settings,
339                            ServiceContext serviceContext)
340                    throws PortalException, SystemException {
341    
342                    return updateVocabulary(
343                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
344                            serviceContext);
345            }
346    
347            public AssetVocabulary updateVocabulary(
348                            long vocabularyId, String title, Map<Locale, String> titleMap,
349                            Map<Locale, String> descriptionMap, String settings,
350                            ServiceContext serviceContext)
351                    throws PortalException, SystemException {
352    
353                    long groupId = serviceContext.getScopeGroupId();
354                    String name = titleMap.get(LocaleUtil.getDefault());
355    
356                    AssetVocabulary vocabulary =
357                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
358    
359                    if (!vocabulary.getName().equals(name)) {
360                            validate(groupId, name);
361                    }
362    
363                    vocabulary.setModifiedDate(new Date());
364                    vocabulary.setName(name);
365                    vocabulary.setTitleMap(titleMap);
366    
367                    if (Validator.isNotNull(title)) {
368                            vocabulary.setTitle(title);
369                    }
370    
371                    vocabulary.setDescriptionMap(descriptionMap);
372                    vocabulary.setSettings(settings);
373    
374                    assetVocabularyPersistence.update(vocabulary);
375    
376                    return vocabulary;
377            }
378    
379            protected boolean hasVocabulary(long groupId, String name)
380                    throws SystemException {
381    
382                    if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
383                            return false;
384                    }
385                    else {
386                            return true;
387                    }
388            }
389    
390            protected void validate(long groupId, String name)
391                    throws PortalException, SystemException {
392    
393                    if (Validator.isNull(name)) {
394                            throw new VocabularyNameException();
395                    }
396    
397                    if (hasVocabulary(groupId, name)) {
398                            throw new DuplicateVocabularyException(
399                                    "A category vocabulary with the name " + name +
400                                            " already exists");
401                    }
402            }
403    
404    }