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