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.asset.kernel.model.AssetCategoryConstants;
018    import com.liferay.asset.kernel.model.AssetVocabulary;
019    import com.liferay.asset.kernel.model.AssetVocabularyDisplay;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.model.User;
022    import com.liferay.portal.kernel.search.BaseModelSearchResult;
023    import com.liferay.portal.kernel.search.Sort;
024    import com.liferay.portal.kernel.security.permission.ActionKeys;
025    import com.liferay.portal.kernel.security.permission.PermissionChecker;
026    import com.liferay.portal.kernel.service.ServiceContext;
027    import com.liferay.portal.kernel.util.ListUtil;
028    import com.liferay.portal.kernel.util.OrderByComparator;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portlet.asset.service.base.AssetVocabularyServiceBaseImpl;
031    import com.liferay.portlet.asset.service.permission.AssetPermission;
032    import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
033    import com.liferay.portlet.asset.util.AssetUtil;
034    import com.liferay.util.dao.orm.CustomSQLUtil;
035    
036    import java.util.ArrayList;
037    import java.util.Iterator;
038    import java.util.List;
039    import java.util.Locale;
040    import java.util.Map;
041    
042    /**
043     * Provides the remote service for accessing, adding, deleting, and updating
044     * asset vocabularies. Its methods include permission checks.
045     *
046     * @author Alvaro del Castillo
047     * @author Eduardo Lundgren
048     * @author Jorge Ferrer
049     * @author Juan Fernández
050     */
051    public class AssetVocabularyServiceImpl extends AssetVocabularyServiceBaseImpl {
052    
053            @Override
054            public AssetVocabulary addVocabulary(
055                            long groupId, String title, Map<Locale, String> titleMap,
056                            Map<Locale, String> descriptionMap, String settings,
057                            ServiceContext serviceContext)
058                    throws PortalException {
059    
060                    AssetPermission.check(
061                            getPermissionChecker(), groupId, ActionKeys.ADD_VOCABULARY);
062    
063                    return assetVocabularyLocalService.addVocabulary(
064                            getUserId(), groupId, title, titleMap, descriptionMap, settings,
065                            serviceContext);
066            }
067    
068            @Override
069            public AssetVocabulary addVocabulary(
070                            long groupId, String title, ServiceContext serviceContext)
071                    throws PortalException {
072    
073                    AssetPermission.check(
074                            getPermissionChecker(), groupId, ActionKeys.ADD_VOCABULARY);
075    
076                    return assetVocabularyLocalService.addVocabulary(
077                            getUserId(), groupId, title, serviceContext);
078            }
079    
080            @Override
081            public List<AssetVocabulary> deleteVocabularies(
082                            long[] vocabularyIds, ServiceContext serviceContext)
083                    throws PortalException {
084    
085                    List<AssetVocabulary> failedVocabularies = new ArrayList<>();
086    
087                    for (long vocabularyId : vocabularyIds) {
088                            try {
089                                    AssetVocabularyPermission.check(
090                                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
091    
092                                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
093                            }
094                            catch (PortalException pe) {
095                                    if (serviceContext == null) {
096                                            return null;
097                                    }
098    
099                                    if (serviceContext.isFailOnPortalException()) {
100                                            throw pe;
101                                    }
102    
103                                    AssetVocabulary vocabulary =
104                                            assetVocabularyPersistence.fetchByPrimaryKey(vocabularyId);
105    
106                                    if (vocabulary == null) {
107                                            vocabulary = assetVocabularyPersistence.create(
108                                                    vocabularyId);
109                                    }
110    
111                                    failedVocabularies.add(vocabulary);
112                            }
113                    }
114    
115                    return failedVocabularies;
116            }
117    
118            @Override
119            public void deleteVocabulary(long vocabularyId) throws PortalException {
120                    AssetVocabularyPermission.check(
121                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
122    
123                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
124            }
125    
126            @Override
127            public AssetVocabulary fetchVocabulary(long vocabularyId)
128                    throws PortalException {
129    
130                    AssetVocabulary vocabulary =
131                            assetVocabularyLocalService.fetchAssetVocabulary(vocabularyId);
132    
133                    if (vocabulary != null) {
134                            AssetVocabularyPermission.check(
135                                    getPermissionChecker(), vocabulary, ActionKeys.VIEW);
136                    }
137    
138                    return vocabulary;
139            }
140    
141            /**
142             * @deprecated As of 7.0.0, with no direct replacement
143             */
144            @Deprecated
145            @Override
146            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
147                    throws PortalException {
148    
149                    return filterVocabularies(
150                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
151            }
152    
153            @Override
154            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds) {
155                    return getGroupsVocabularies(groupIds, null);
156            }
157    
158            @Override
159            public List<AssetVocabulary> getGroupsVocabularies(
160                    long[] groupIds, String className) {
161    
162                    return getGroupsVocabularies(
163                            groupIds, className, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
164            }
165    
166            @Override
167            public List<AssetVocabulary> getGroupsVocabularies(
168                    long[] groupIds, String className, long classTypePK) {
169    
170                    List<AssetVocabulary> vocabularies =
171                            assetVocabularyPersistence.filterFindByGroupId(groupIds);
172    
173                    if (Validator.isNull(className)) {
174                            return vocabularies;
175                    }
176    
177                    return AssetUtil.filterVocabularies(
178                            vocabularies, className, classTypePK);
179            }
180    
181            @Override
182            public List<AssetVocabulary> getGroupVocabularies(long groupId)
183                    throws PortalException {
184    
185                    return getGroupVocabularies(groupId, true);
186            }
187    
188            @Override
189            public List<AssetVocabulary> getGroupVocabularies(
190                            long groupId, boolean createDefaultVocabulary)
191                    throws PortalException {
192    
193                    List<AssetVocabulary> vocabularies =
194                            assetVocabularyPersistence.filterFindByGroupId(groupId);
195    
196                    if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
197                            return vocabularies;
198                    }
199    
200                    vocabularies = new ArrayList<>();
201    
202                    AssetVocabulary vocabulary =
203                            assetVocabularyLocalService.addDefaultVocabulary(groupId);
204    
205                    vocabularies.add(vocabulary);
206    
207                    return vocabularies;
208            }
209    
210            @Override
211            public List<AssetVocabulary> getGroupVocabularies(
212                            long groupId, boolean createDefaultVocabulary, int start, int end,
213                            OrderByComparator<AssetVocabulary> obc)
214                    throws PortalException {
215    
216                    List<AssetVocabulary> vocabularies = getGroupVocabularies(
217                            groupId, start, end, obc);
218    
219                    if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
220                            return vocabularies;
221                    }
222    
223                    int count = assetVocabularyLocalService.getGroupVocabulariesCount(
224                            new long[] {groupId});
225    
226                    if (count> 0) {
227                            return vocabularies;
228                    }
229    
230                    vocabularies = new ArrayList<>();
231    
232                    AssetVocabulary vocabulary =
233                            assetVocabularyLocalService.addDefaultVocabulary(groupId);
234    
235                    vocabularies.add(vocabulary);
236    
237                    return vocabularies;
238            }
239    
240            @Override
241            public List<AssetVocabulary> getGroupVocabularies(
242                    long groupId, int start, int end,
243                    OrderByComparator<AssetVocabulary> obc) {
244    
245                    return assetVocabularyPersistence.filterFindByGroupId(
246                            groupId, start, end, obc);
247            }
248    
249            @Override
250            public List<AssetVocabulary> getGroupVocabularies(
251                    long groupId, String name, int start, int end,
252                    OrderByComparator<AssetVocabulary> obc) {
253    
254                    return assetVocabularyPersistence.filterFindByG_LikeN(
255                            groupId, name, start, end, obc);
256            }
257    
258            @Override
259            public List<AssetVocabulary> getGroupVocabularies(long[] groupIds) {
260                    return assetVocabularyPersistence.filterFindByGroupId(groupIds);
261            }
262    
263            @Override
264            public int getGroupVocabulariesCount(long groupId) {
265                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
266            }
267    
268            @Override
269            public int getGroupVocabulariesCount(long groupId, String name) {
270                    return assetVocabularyPersistence.filterCountByG_LikeN(groupId, name);
271            }
272    
273            @Override
274            public int getGroupVocabulariesCount(long[] groupIds) {
275                    return assetVocabularyPersistence.filterCountByGroupId(groupIds);
276            }
277    
278            @Override
279            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
280                            long groupId, String name, int start, int end,
281                            boolean addDefaultVocabulary,
282                            OrderByComparator<AssetVocabulary> obc)
283                    throws PortalException {
284    
285                    List<AssetVocabulary> vocabularies;
286                    int total = 0;
287    
288                    if (Validator.isNotNull(name)) {
289                            name = (CustomSQLUtil.keywords(name))[0];
290    
291                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
292                            total = getGroupVocabulariesCount(groupId, name);
293                    }
294                    else {
295                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
296                            total = getGroupVocabulariesCount(groupId);
297                    }
298    
299                    if (addDefaultVocabulary && (total == 0) &&
300                            (assetVocabularyPersistence.countByGroupId(groupId) == 0)) {
301    
302                            vocabularies = new ArrayList<>();
303    
304                            vocabularies.add(
305                                    assetVocabularyLocalService.addDefaultVocabulary(groupId));
306    
307                            total = 1;
308                    }
309    
310                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
311            }
312    
313            @Override
314            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
315                            long groupId, String name, int start, int end,
316                            OrderByComparator<AssetVocabulary> obc)
317                    throws PortalException {
318    
319                    return getGroupVocabulariesDisplay(
320                            groupId, name, start, end, false, obc);
321            }
322    
323            /**
324             * @deprecated As of 7.0.0, replaced by {@link
325             *             AssetUtil#filterVocabularyIds(PermissionChecker, long[])}
326             */
327            @Deprecated
328            @Override
329            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
330                    throws PortalException {
331    
332                    return filterVocabularies(
333                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
334            }
335    
336            @Override
337            public AssetVocabulary getVocabulary(long vocabularyId)
338                    throws PortalException {
339    
340                    AssetVocabularyPermission.check(
341                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
342    
343                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
344            }
345    
346            @Override
347            public AssetVocabularyDisplay searchVocabulariesDisplay(
348                            long groupId, String title, boolean addDefaultVocabulary, int start,
349                            int end)
350                    throws PortalException {
351    
352                    return searchVocabulariesDisplay(
353                            groupId, title, addDefaultVocabulary, start, end, null);
354            }
355    
356            @Override
357            public AssetVocabularyDisplay searchVocabulariesDisplay(
358                            long groupId, String title, boolean addDefaultVocabulary, int start,
359                            int end, Sort sort)
360                    throws PortalException {
361    
362                    User user = getUser();
363    
364                    BaseModelSearchResult<AssetVocabulary> baseModelSearchResult =
365                            assetVocabularyLocalService.searchVocabularies(
366                                    user.getCompanyId(), groupId, title, start, end, sort);
367    
368                    List<AssetVocabulary> vocabularies =
369                            baseModelSearchResult.getBaseModels();
370                    int total = baseModelSearchResult.getLength();
371    
372                    if (addDefaultVocabulary && (total == 0)) {
373                            total = assetVocabularyPersistence.countByGroupId(groupId);
374    
375                            if (total == 0) {
376                                    vocabularies = new ArrayList<>(1);
377    
378                                    AssetVocabulary defaultVocabulary =
379                                            assetVocabularyLocalService.addDefaultVocabulary(groupId);
380    
381                                    vocabularies.add(defaultVocabulary);
382    
383                                    total = 1;
384                            }
385                    }
386    
387                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
388            }
389    
390            @Override
391            public AssetVocabulary updateVocabulary(
392                            long vocabularyId, String title, Map<Locale, String> titleMap,
393                            Map<Locale, String> descriptionMap, String settings,
394                            ServiceContext serviceContext)
395                    throws PortalException {
396    
397                    AssetVocabularyPermission.check(
398                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
399    
400                    return assetVocabularyLocalService.updateVocabulary(
401                            vocabularyId, title, titleMap, descriptionMap, settings,
402                            serviceContext);
403            }
404    
405            /**
406             * @deprecated As of 7.0.0, with no direct replacement
407             */
408            @Deprecated
409            protected List<AssetVocabulary> filterVocabularies(
410                            List<AssetVocabulary> vocabularies)
411                    throws PortalException {
412    
413                    PermissionChecker permissionChecker = getPermissionChecker();
414    
415                    vocabularies = ListUtil.copy(vocabularies);
416    
417                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
418    
419                    while (itr.hasNext()) {
420                            AssetVocabulary vocabulary = itr.next();
421    
422                            if (!AssetVocabularyPermission.contains(
423                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
424    
425                                    itr.remove();
426                            }
427                    }
428    
429                    return vocabularies;
430            }
431    
432    }