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, false);
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 void addVocabularyResources(
128                            AssetVocabulary vocabulary, boolean addGroupPermissions,
129                            boolean addGuestPermissions)
130                    throws PortalException, SystemException {
131    
132                    resourceLocalService.addResources(
133                            vocabulary.getCompanyId(), vocabulary.getGroupId(),
134                            vocabulary.getUserId(), AssetVocabulary.class.getName(),
135                            vocabulary.getVocabularyId(), false, addGroupPermissions,
136                            addGuestPermissions);
137            }
138    
139            public void addVocabularyResources(
140                            AssetVocabulary vocabulary, String[] groupPermissions,
141                            String[] guestPermissions)
142                    throws PortalException, SystemException {
143    
144                    resourceLocalService.addModelResources(
145                            vocabulary.getCompanyId(), vocabulary.getGroupId(),
146                            vocabulary.getUserId(), AssetVocabulary.class.getName(),
147                            vocabulary.getVocabularyId(), groupPermissions, guestPermissions);
148            }
149    
150            public void deleteVocabularies(long groupId)
151                    throws PortalException, SystemException {
152    
153                    List<AssetVocabulary> vocabularies =
154                            assetVocabularyPersistence.findByGroupId(groupId);
155    
156                    for (AssetVocabulary vocabulary : vocabularies) {
157                            deleteVocabulary(vocabulary);
158                    }
159            }
160    
161            public void deleteVocabulary(AssetVocabulary vocabulary)
162                    throws PortalException, SystemException {
163    
164                    // Vocabulary
165    
166                    assetVocabularyPersistence.remove(vocabulary);
167    
168                    // Resources
169    
170                    resourceLocalService.deleteResource(
171                            vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
172                            ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
173    
174                    // Categories
175    
176                    assetCategoryLocalService.deleteVocabularyCategories(
177                            vocabulary.getVocabularyId());
178            }
179    
180            public void deleteVocabulary(long vocabularyId)
181                    throws PortalException, SystemException {
182    
183                    AssetVocabulary vocabulary =
184                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
185    
186                    deleteVocabulary(vocabulary);
187            }
188    
189            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
190                    throws SystemException {
191    
192                    return assetVocabularyPersistence.findByCompanyId(companyId);
193            }
194    
195            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
196                    throws PortalException, SystemException {
197    
198                    return getGroupsVocabularies(groupIds, null);
199            }
200    
201            public List<AssetVocabulary> getGroupsVocabularies(
202                            long[] groupIds, String className)
203                    throws PortalException, SystemException {
204    
205                    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
206    
207                    for (long groupId : groupIds) {
208                            List<AssetVocabulary> groupVocabularies = getGroupVocabularies(
209                                    groupId);
210    
211                            if (Validator.isNull(className)) {
212                                    vocabularies.addAll(groupVocabularies);
213    
214                                    continue;
215                            }
216    
217                            for (AssetVocabulary groupVocabulary: groupVocabularies) {
218                                    UnicodeProperties settingsProperties =
219                                            groupVocabulary.getSettingsProperties();
220    
221                                    long[] selectedClassNameIds = StringUtil.split(
222                                            settingsProperties.getProperty("selectedClassNameIds"), 0L);
223                                    long classNameId = PortalUtil.getClassNameId(className);
224    
225                                    if ((selectedClassNameIds.length == 0) ||
226                                            (selectedClassNameIds[0] == 0) ||
227                                            ArrayUtil.contains(selectedClassNameIds, classNameId)) {
228    
229                                            vocabularies.add(groupVocabulary);
230                                    }
231                            }
232                    }
233    
234                    return vocabularies;
235            }
236    
237            public List<AssetVocabulary> getGroupVocabularies(long groupId)
238                    throws PortalException, SystemException {
239    
240                    return getGroupVocabularies(groupId, true);
241            }
242    
243            public List<AssetVocabulary> getGroupVocabularies(
244                            long groupId, boolean createDefaultVocabulary)
245                    throws PortalException, SystemException {
246    
247                    List<AssetVocabulary> vocabularies =
248                            assetVocabularyPersistence.findByGroupId(groupId);
249    
250                    if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
251                            return vocabularies;
252                    }
253    
254                    Group group = groupLocalService.getGroup(groupId);
255    
256                    long defaultUserId = userLocalService.getDefaultUserId(
257                            group.getCompanyId());
258    
259                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
260    
261                    titleMap.put(
262                            LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
263    
264                    ServiceContext serviceContext = new ServiceContext();
265    
266                    serviceContext.setScopeGroupId(groupId);
267    
268                    AssetVocabulary vocabulary = assetVocabularyLocalService.addVocabulary(
269                            defaultUserId, StringPool.BLANK, titleMap, null, StringPool.BLANK,
270                            serviceContext);
271    
272                    vocabularies = new ArrayList<AssetVocabulary>();
273    
274                    vocabularies.add(vocabulary);
275    
276                    return vocabularies;
277            }
278    
279            public List<AssetVocabulary> getGroupVocabularies(
280                            long groupId, String name, int start, int end,
281                            OrderByComparator obc)
282                    throws SystemException {
283    
284                    return assetVocabularyFinder.findByG_N(groupId, name, start, end, obc);
285            }
286    
287            public AssetVocabulary getGroupVocabulary(long groupId, String name)
288                    throws PortalException, SystemException {
289    
290                    return assetVocabularyPersistence.findByG_N(groupId, name);
291            }
292    
293            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
294                    throws PortalException, SystemException {
295    
296                    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
297    
298                    for (long vocabularyId : vocabularyIds) {
299                            AssetVocabulary vocabulary = getVocabulary(vocabularyId);
300    
301                            vocabularies.add(vocabulary);
302                    }
303    
304                    return vocabularies;
305            }
306    
307            public AssetVocabulary getVocabulary(long vocabularyId)
308                    throws PortalException, SystemException {
309    
310                    return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
311            }
312    
313            /**
314             * @deprecated
315             */
316            public AssetVocabulary updateVocabulary(
317                            long vocabularyId, Map<Locale, String> titleMap,
318                            Map<Locale, String> descriptionMap, String settings,
319                            ServiceContext serviceContext)
320                    throws PortalException, SystemException {
321    
322                    return updateVocabulary(
323                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
324                            serviceContext);
325            }
326    
327            public AssetVocabulary updateVocabulary(
328                            long vocabularyId, String title, Map<Locale, String> titleMap,
329                            Map<Locale, String> descriptionMap, String settings,
330                            ServiceContext serviceContext)
331                    throws PortalException, SystemException {
332    
333                    long groupId = serviceContext.getScopeGroupId();
334                    String name = titleMap.get(LocaleUtil.getDefault());
335    
336                    AssetVocabulary vocabulary =
337                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
338    
339                    if (!vocabulary.getName().equals(name)) {
340                            validate(groupId, name);
341                    }
342    
343                    vocabulary.setModifiedDate(new Date());
344                    vocabulary.setName(name);
345                    vocabulary.setTitleMap(titleMap);
346    
347                    if (Validator.isNotNull(title)) {
348                            vocabulary.setTitle(title);
349                    }
350    
351                    vocabulary.setDescriptionMap(descriptionMap);
352                    vocabulary.setSettings(settings);
353    
354                    assetVocabularyPersistence.update(vocabulary, false);
355    
356                    return vocabulary;
357            }
358    
359            protected boolean hasVocabulary(long groupId, String name)
360                    throws SystemException {
361    
362                    if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
363                            return false;
364                    }
365                    else {
366                            return true;
367                    }
368            }
369    
370            protected void validate(long groupId, String name)
371                    throws PortalException, SystemException {
372    
373                    if (Validator.isNull(name)) {
374                            throw new VocabularyNameException();
375                    }
376    
377                    if (hasVocabulary(groupId, name)) {
378                            throw new DuplicateVocabularyException(
379                                    "A category vocabulary with the name " + name +
380                                            " already exists");
381                    }
382            }
383    
384    }