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