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