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            /**
161             * @deprecated As of 7.0.0, with no direct replacement
162             */
163            @Deprecated
164            @Override
165            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
166                    throws PortalException {
167    
168                    return filterVocabularies(
169                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
170            }
171    
172            @Override
173            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds) {
174                    return getGroupsVocabularies(groupIds, null);
175            }
176    
177            @Override
178            public List<AssetVocabulary> getGroupsVocabularies(
179                    long[] groupIds, String className) {
180    
181                    return getGroupsVocabularies(
182                            groupIds, className, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
183            }
184    
185            @Override
186            public List<AssetVocabulary> getGroupsVocabularies(
187                    long[] groupIds, String className, long classTypePK) {
188    
189                    List<AssetVocabulary> vocabularies =
190                            assetVocabularyPersistence.filterFindByGroupId(groupIds);
191    
192                    if (Validator.isNull(className)) {
193                            return vocabularies;
194                    }
195    
196                    return AssetUtil.filterVocabularies(
197                            vocabularies, className, classTypePK);
198            }
199    
200            @Override
201            public List<AssetVocabulary> getGroupVocabularies(long groupId)
202                    throws PortalException {
203    
204                    return getGroupVocabularies(groupId, true);
205            }
206    
207            @Override
208            public List<AssetVocabulary> getGroupVocabularies(
209                            long groupId, boolean createDefaultVocabulary)
210                    throws PortalException {
211    
212                    List<AssetVocabulary> vocabularies =
213                            assetVocabularyPersistence.filterFindByGroupId(groupId);
214    
215                    if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
216                            return vocabularies;
217                    }
218    
219                    vocabularies = new ArrayList<AssetVocabulary>();
220    
221                    AssetVocabulary vocabulary =
222                            assetVocabularyLocalService.addDefaultVocabulary(groupId);
223    
224                    vocabularies.add(vocabulary);
225    
226                    return vocabularies;
227            }
228    
229            @Override
230            public List<AssetVocabulary> getGroupVocabularies(
231                            long groupId, boolean createDefaultVocabulary, int start, int end,
232                            OrderByComparator<AssetVocabulary> obc)
233                    throws PortalException {
234    
235                    List<AssetVocabulary> vocabularies = getGroupVocabularies(
236                            groupId, start, end, obc);
237    
238                    if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
239                            return vocabularies;
240                    }
241    
242                    vocabularies = new ArrayList<AssetVocabulary>();
243    
244                    AssetVocabulary vocabulary =
245                            assetVocabularyLocalService.addDefaultVocabulary(groupId);
246    
247                    vocabularies.add(vocabulary);
248    
249                    return vocabularies;
250            }
251    
252            @Override
253            public List<AssetVocabulary> getGroupVocabularies(
254                    long groupId, int start, int end,
255                    OrderByComparator<AssetVocabulary> obc) {
256    
257                    return assetVocabularyPersistence.filterFindByGroupId(
258                            groupId, start, end, obc);
259            }
260    
261            @Override
262            public List<AssetVocabulary> getGroupVocabularies(
263                    long groupId, String name, int start, int end,
264                    OrderByComparator<AssetVocabulary> obc) {
265    
266                    return assetVocabularyPersistence.filterFindByG_LikeN(
267                            groupId, name, start, end, obc);
268            }
269    
270            @Override
271            public List<AssetVocabulary> getGroupVocabularies(long[] groupIds) {
272                    return assetVocabularyPersistence.filterFindByGroupId(groupIds);
273            }
274    
275            @Override
276            public int getGroupVocabulariesCount(long groupId) {
277                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
278            }
279    
280            @Override
281            public int getGroupVocabulariesCount(long groupId, String name) {
282                    return assetVocabularyPersistence.filterCountByG_LikeN(groupId, name);
283            }
284    
285            @Override
286            public int getGroupVocabulariesCount(long[] groupIds) {
287                    return assetVocabularyPersistence.filterCountByGroupId(groupIds);
288            }
289    
290            @Override
291            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
292                            long groupId, String name, int start, int end,
293                            boolean addDefaultVocabulary,
294                            OrderByComparator<AssetVocabulary> obc)
295                    throws PortalException {
296    
297                    List<AssetVocabulary> vocabularies;
298                    int total = 0;
299    
300                    if (Validator.isNotNull(name)) {
301                            name = (CustomSQLUtil.keywords(name))[0];
302    
303                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
304                            total = getGroupVocabulariesCount(groupId, name);
305                    }
306                    else {
307                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
308                            total = getGroupVocabulariesCount(groupId);
309                    }
310    
311                    if (addDefaultVocabulary && (total == 0) &&
312                            (assetVocabularyPersistence.countByGroupId(groupId) == 0)) {
313    
314                            vocabularies = new ArrayList<AssetVocabulary>();
315    
316                            vocabularies.add(
317                                    assetVocabularyLocalService.addDefaultVocabulary(groupId));
318    
319                            total = 1;
320                    }
321    
322                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
323            }
324    
325            @Override
326            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
327                            long groupId, String name, int start, int end,
328                            OrderByComparator<AssetVocabulary> obc)
329                    throws PortalException {
330    
331                    return getGroupVocabulariesDisplay(
332                            groupId, name, start, end, false, obc);
333            }
334    
335            /**
336             * @deprecated As of 6.2.0, with no direct replacement
337             */
338            @Deprecated
339            @Override
340            public JSONObject getJSONGroupVocabularies(
341                            long groupId, String name, int start, int end,
342                            OrderByComparator<AssetVocabulary> obc)
343                    throws PortalException {
344    
345                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
346    
347                    int page = end / (end - start);
348    
349                    jsonObject.put("page", page);
350    
351                    List<AssetVocabulary> vocabularies;
352                    int total = 0;
353    
354                    if (Validator.isNotNull(name)) {
355                            name = (CustomSQLUtil.keywords(name))[0];
356    
357                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
358                            total = getGroupVocabulariesCount(groupId, name);
359                    }
360                    else {
361                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
362                            total = getGroupVocabulariesCount(groupId);
363                    }
364    
365                    String vocabulariesJSON = JSONFactoryUtil.looseSerialize(vocabularies);
366    
367                    JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
368                            vocabulariesJSON);
369    
370                    jsonObject.put("vocabularies", vocabulariesJSONArray);
371    
372                    jsonObject.put("total", total);
373    
374                    return jsonObject;
375            }
376    
377            /**
378             * @deprecated As of 7.0.0, replaced by {@link
379             *             #AssetUtil.filterVocabularyIds(PermissionChecker, long[])}
380             */
381            @Deprecated
382            @Override
383            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
384                    throws PortalException {
385    
386                    return filterVocabularies(
387                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
388            }
389    
390            @Override
391            public AssetVocabulary getVocabulary(long vocabularyId)
392                    throws PortalException {
393    
394                    AssetVocabularyPermission.check(
395                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
396    
397                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
398            }
399    
400            @Override
401            public AssetVocabularyDisplay searchVocabulariesDisplay(
402                            long groupId, String title, int start, int end,
403                            boolean addDefaultVocabulary)
404                    throws PortalException {
405    
406                    User user = getUser();
407    
408                    BaseModelSearchResult<AssetVocabulary> baseModelSearchResult =
409                            assetVocabularyLocalService.searchVocabularies(
410                                    user.getCompanyId(), groupId, title, start, end);
411    
412                    List<AssetVocabulary> vocabularies =
413                            baseModelSearchResult.getBaseModels();
414                    int total = baseModelSearchResult.getLength();
415    
416                    if (addDefaultVocabulary && (total == 0)) {
417                            total = assetVocabularyPersistence.countByGroupId(groupId);
418    
419                            if (total == 0) {
420                                    vocabularies = new ArrayList<AssetVocabulary>(1);
421    
422                                    AssetVocabulary defaultVocabulary =
423                                            assetVocabularyLocalService.addDefaultVocabulary(groupId);
424    
425                                    vocabularies.add(defaultVocabulary);
426    
427                                    total = 1;
428                            }
429                    }
430    
431                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
432            }
433    
434            /**
435             * @deprecated As of 6.1.0, {@link #updateVocabulary(long, String, Map, Map,
436             *             String, ServiceContext)}
437             */
438            @Deprecated
439            @Override
440            public AssetVocabulary updateVocabulary(
441                            long vocabularyId, Map<Locale, String> titleMap,
442                            Map<Locale, String> descriptionMap, String settings,
443                            ServiceContext serviceContext)
444                    throws PortalException {
445    
446                    return updateVocabulary(
447                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
448                            serviceContext);
449            }
450    
451            @Override
452            public AssetVocabulary updateVocabulary(
453                            long vocabularyId, String title, Map<Locale, String> titleMap,
454                            Map<Locale, String> descriptionMap, String settings,
455                            ServiceContext serviceContext)
456                    throws PortalException {
457    
458                    AssetVocabularyPermission.check(
459                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
460    
461                    return assetVocabularyLocalService.updateVocabulary(
462                            vocabularyId, title, titleMap, descriptionMap, settings,
463                            serviceContext);
464            }
465    
466            /**
467             * @deprecated As of 7.0.0, with no direct replacement
468             */
469            @Deprecated
470            protected List<AssetVocabulary> filterVocabularies(
471                            List<AssetVocabulary> vocabularies)
472                    throws PortalException {
473    
474                    PermissionChecker permissionChecker = getPermissionChecker();
475    
476                    vocabularies = ListUtil.copy(vocabularies);
477    
478                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
479    
480                    while (itr.hasNext()) {
481                            AssetVocabulary vocabulary = itr.next();
482    
483                            if (!AssetVocabularyPermission.contains(
484                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
485    
486                                    itr.remove();
487                            }
488                    }
489    
490                    return vocabularies;
491            }
492    
493    }