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