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