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