001    /**
002     * Copyright (c) 2000-2013 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.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONArray;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
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.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.AssetVocabulary;
030    import com.liferay.portlet.asset.model.AssetVocabularyDisplay;
031    import com.liferay.portlet.asset.service.base.AssetVocabularyServiceBaseImpl;
032    import com.liferay.portlet.asset.service.permission.AssetPermission;
033    import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
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     * @author Alvaro del Castillo
044     * @author Eduardo Lundgren
045     * @author Jorge Ferrer
046     * @author Juan Fern??ndez
047     * @author Tibor Lipusz
048     */
049    public class AssetVocabularyServiceImpl extends AssetVocabularyServiceBaseImpl {
050    
051            /**
052             * @deprecated As of 6.1.0 {@link #addVocabulary(String, Map, Map, String,
053             *             ServiceContext)}
054             */
055            @Override
056            public AssetVocabulary addVocabulary(
057                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
058                            String settings, ServiceContext serviceContext)
059                    throws PortalException, SystemException {
060    
061                    return addVocabulary(
062                            StringPool.BLANK, titleMap, descriptionMap, settings,
063                            serviceContext);
064            }
065    
066            @Override
067            public AssetVocabulary addVocabulary(
068                            String title, Map<Locale, String> titleMap,
069                            Map<Locale, String> descriptionMap, String settings,
070                            ServiceContext serviceContext)
071                    throws PortalException, SystemException {
072    
073                    AssetPermission.check(
074                            getPermissionChecker(), serviceContext.getScopeGroupId(),
075                            ActionKeys.ADD_VOCABULARY);
076    
077                    return assetVocabularyLocalService.addVocabulary(
078                            getUserId(), title, titleMap, descriptionMap, settings,
079                            serviceContext);
080            }
081    
082            /**
083             * @deprecated As of 6.2.0, Replaced by {@link #deleteVocabularies(long[],
084             *             ServiceContext)}
085             */
086            @Override
087            public void deleteVocabularies(long[] vocabularyIds)
088                    throws PortalException, SystemException {
089    
090                    deleteVocabularies(vocabularyIds, null);
091            }
092    
093            @Override
094            public List<AssetVocabulary> deleteVocabularies(
095                            long[] vocabularyIds, ServiceContext serviceContext)
096                    throws PortalException, SystemException {
097    
098                    List<AssetVocabulary> failedVocabularies =
099                            new ArrayList<AssetVocabulary>();
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)
134                    throws PortalException, SystemException {
135    
136                    AssetVocabularyPermission.check(
137                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
138    
139                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
140            }
141    
142            @Override
143            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
144                    throws PortalException, SystemException {
145    
146                    return filterVocabularies(
147                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
148            }
149    
150            @Override
151            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
152                    throws PortalException, SystemException {
153    
154                    return getGroupsVocabularies(groupIds, null);
155            }
156    
157            @Override
158            public List<AssetVocabulary> getGroupsVocabularies(
159                            long[] groupIds, String className)
160                    throws PortalException, SystemException {
161    
162                    return filterVocabularies(
163                            assetVocabularyLocalService.getGroupsVocabularies(
164                                    groupIds, className));
165            }
166    
167            @Override
168            public List<AssetVocabulary> getGroupVocabularies(long groupId)
169                    throws PortalException, SystemException {
170    
171                    return filterVocabularies(
172                            assetVocabularyLocalService.getGroupVocabularies(groupId));
173            }
174    
175            @Override
176            public List<AssetVocabulary> getGroupVocabularies(
177                            long groupId, boolean createDefaultVocabulary)
178                    throws PortalException, SystemException {
179    
180                    return filterVocabularies(
181                            assetVocabularyLocalService.getGroupVocabularies(
182                                    groupId, createDefaultVocabulary));
183            }
184    
185            @Override
186            public List<AssetVocabulary> getGroupVocabularies(
187                            long groupId, int start, int end, OrderByComparator obc)
188                    throws SystemException {
189    
190                    return assetVocabularyPersistence.filterFindByGroupId(
191                            groupId, start, end, obc);
192            }
193    
194            @Override
195            public List<AssetVocabulary> getGroupVocabularies(
196                            long groupId, String name, int start, int end,
197                            OrderByComparator obc)
198                    throws SystemException {
199    
200                    return assetVocabularyFinder.filterFindByG_N(
201                            groupId, name, start, end, obc);
202            }
203    
204            @Override
205            public int getGroupVocabulariesCount(long groupId) throws SystemException {
206                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
207            }
208    
209            @Override
210            public int getGroupVocabulariesCount(long groupId, String name)
211                    throws SystemException {
212    
213                    return assetVocabularyFinder.filterCountByG_N(groupId, name);
214            }
215    
216            @Override
217            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
218                            long groupId, String name, int start, int end,
219                            boolean addDefaultVocabulary, OrderByComparator obc)
220                    throws PortalException, SystemException {
221    
222                    List<AssetVocabulary> vocabularies;
223                    int total = 0;
224    
225                    if (Validator.isNotNull(name)) {
226                            name = (CustomSQLUtil.keywords(name))[0];
227    
228                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
229                            total = getGroupVocabulariesCount(groupId, name);
230                    }
231                    else {
232                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
233                            total = getGroupVocabulariesCount(groupId);
234                    }
235    
236                    if (addDefaultVocabulary && (total == 0)) {
237                            vocabularies = new ArrayList<AssetVocabulary>();
238    
239                            vocabularies.add(
240                                    assetVocabularyLocalService.addDefaultVocabulary(groupId));
241    
242                            total = 1;
243                    }
244    
245                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
246            }
247    
248            @Override
249            public JSONObject getJSONGroupVocabularies(
250                            long groupId, String name, int start, int end,
251                            OrderByComparator obc)
252                    throws PortalException, SystemException {
253    
254                    return getJSONGroupVocabulariesDisplay
255                            (groupId, name, start, end, false, obc);
256            }
257    
258            @Override
259            public JSONObject getJSONGroupVocabulariesDisplay(
260                            long groupId, String name, int start, int end,
261                            boolean addDefaultVocabulary, OrderByComparator obc)
262                    throws PortalException, SystemException {
263    
264                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
265    
266                    int page = end / (end - start);
267    
268                    jsonObject.put("page", page);
269    
270                    AssetVocabularyDisplay assetVocabularyDisplay =
271                            getGroupVocabulariesDisplay(
272                                    groupId, name, start, end, addDefaultVocabulary, obc);
273    
274                    String vocabulariesJSON = JSONFactoryUtil.looseSerialize(
275                            assetVocabularyDisplay.getVocabularies());
276    
277                    JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
278                            vocabulariesJSON);
279    
280                    jsonObject.put("vocabularies", vocabulariesJSONArray);
281    
282                    jsonObject.put("total", assetVocabularyDisplay.getTotal());
283    
284                    return jsonObject;
285            }
286    
287            @Override
288            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
289                    throws PortalException, SystemException {
290    
291                    return filterVocabularies(
292                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
293            }
294    
295            @Override
296            public AssetVocabulary getVocabulary(long vocabularyId)
297                    throws PortalException, SystemException {
298    
299                    AssetVocabularyPermission.check(
300                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
301    
302                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
303            }
304    
305            /**
306             * @deprecated As of 6.1.0, {@link #updateVocabulary(long, String, Map, Map,
307             *             String, ServiceContext)}
308             */
309            @Override
310            public AssetVocabulary updateVocabulary(
311                            long vocabularyId, Map<Locale, String> titleMap,
312                            Map<Locale, String> descriptionMap, String settings,
313                            ServiceContext serviceContext)
314                    throws PortalException, SystemException {
315    
316                    return updateVocabulary(
317                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
318                            serviceContext);
319            }
320    
321            @Override
322            public AssetVocabulary updateVocabulary(
323                            long vocabularyId, String title, Map<Locale, String> titleMap,
324                            Map<Locale, String> descriptionMap, String settings,
325                            ServiceContext serviceContext)
326                    throws PortalException, SystemException {
327    
328                    AssetVocabularyPermission.check(
329                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
330    
331                    return assetVocabularyLocalService.updateVocabulary(
332                            vocabularyId, title, titleMap, descriptionMap, settings,
333                            serviceContext);
334            }
335    
336            protected List<AssetVocabulary> filterVocabularies(
337                            List<AssetVocabulary> vocabularies)
338                    throws PortalException {
339    
340                    PermissionChecker permissionChecker = getPermissionChecker();
341    
342                    vocabularies = ListUtil.copy(vocabularies);
343    
344                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
345    
346                    while (itr.hasNext()) {
347                            AssetVocabulary vocabulary = itr.next();
348    
349                            if (!AssetVocabularyPermission.contains(
350                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
351    
352                                    itr.remove();
353                            }
354                    }
355    
356                    return vocabularies;
357            }
358    
359    }