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