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     * Provides the remote service for accessing, adding, deleting, and updating
044     * asset vocabularies. Its methods include permission checks.
045     *
046     * @author Alvaro del Castillo
047     * @author Eduardo Lundgren
048     * @author Jorge Ferrer
049     * @author Juan Fern??ndez
050     */
051    public class AssetVocabularyServiceImpl extends AssetVocabularyServiceBaseImpl {
052    
053            /**
054             * @deprecated As of 6.1.0 {@link #addVocabulary(String, Map, Map, String,
055             *             ServiceContext)}
056             */
057            @Override
058            public AssetVocabulary addVocabulary(
059                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
060                            String settings, ServiceContext serviceContext)
061                    throws PortalException, SystemException {
062    
063                    return addVocabulary(
064                            StringPool.BLANK, titleMap, descriptionMap, settings,
065                            serviceContext);
066            }
067    
068            @Override
069            public AssetVocabulary addVocabulary(
070                            String title, Map<Locale, String> titleMap,
071                            Map<Locale, String> descriptionMap, String settings,
072                            ServiceContext serviceContext)
073                    throws PortalException, SystemException {
074    
075                    AssetPermission.check(
076                            getPermissionChecker(), serviceContext.getScopeGroupId(),
077                            ActionKeys.ADD_VOCABULARY);
078    
079                    return assetVocabularyLocalService.addVocabulary(
080                            getUserId(), title, titleMap, descriptionMap, settings,
081                            serviceContext);
082            }
083    
084            @Override
085            public AssetVocabulary addVocabulary(
086                            String title, ServiceContext serviceContext)
087                    throws PortalException, SystemException {
088    
089                    AssetPermission.check(
090                            getPermissionChecker(), serviceContext.getScopeGroupId(),
091                            ActionKeys.ADD_VOCABULARY);
092    
093                    return assetVocabularyLocalService.addVocabulary(
094                            getUserId(), title, serviceContext);
095            }
096    
097            /**
098             * @deprecated As of 6.2.0, Replaced by {@link #deleteVocabularies(long[],
099             *             ServiceContext)}
100             */
101            @Override
102            public void deleteVocabularies(long[] vocabularyIds)
103                    throws PortalException, SystemException {
104    
105                    deleteVocabularies(vocabularyIds, null);
106            }
107    
108            @Override
109            public List<AssetVocabulary> deleteVocabularies(
110                            long[] vocabularyIds, ServiceContext serviceContext)
111                    throws PortalException, SystemException {
112    
113                    List<AssetVocabulary> failedVocabularies =
114                            new ArrayList<AssetVocabulary>();
115    
116                    for (long vocabularyId : vocabularyIds) {
117                            try {
118                                    AssetVocabularyPermission.check(
119                                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
120    
121                                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
122                            }
123                            catch (PortalException pe) {
124                                    if (serviceContext == null) {
125                                            return null;
126                                    }
127    
128                                    if (serviceContext.isFailOnPortalException()) {
129                                            throw pe;
130                                    }
131    
132                                    AssetVocabulary vocabulary =
133                                            assetVocabularyPersistence.fetchByPrimaryKey(vocabularyId);
134    
135                                    if (vocabulary == null) {
136                                            vocabulary = assetVocabularyPersistence.create(
137                                                    vocabularyId);
138                                    }
139    
140                                    failedVocabularies.add(vocabulary);
141                            }
142                    }
143    
144                    return failedVocabularies;
145            }
146    
147            @Override
148            public void deleteVocabulary(long vocabularyId)
149                    throws PortalException, SystemException {
150    
151                    AssetVocabularyPermission.check(
152                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
153    
154                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
155            }
156    
157            @Override
158            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
159                    throws PortalException, SystemException {
160    
161                    return filterVocabularies(
162                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
163            }
164    
165            @Override
166            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
167                    throws PortalException, SystemException {
168    
169                    return getGroupsVocabularies(groupIds, null);
170            }
171    
172            @Override
173            public List<AssetVocabulary> getGroupsVocabularies(
174                            long[] groupIds, String className)
175                    throws PortalException, SystemException {
176    
177                    return filterVocabularies(
178                            assetVocabularyLocalService.getGroupsVocabularies(
179                                    groupIds, className));
180            }
181    
182            @Override
183            public List<AssetVocabulary> getGroupVocabularies(long groupId)
184                    throws PortalException, SystemException {
185    
186                    return filterVocabularies(
187                            assetVocabularyLocalService.getGroupVocabularies(groupId));
188            }
189    
190            @Override
191            public List<AssetVocabulary> getGroupVocabularies(
192                            long groupId, boolean createDefaultVocabulary)
193                    throws PortalException, SystemException {
194    
195                    return filterVocabularies(
196                            assetVocabularyLocalService.getGroupVocabularies(
197                                    groupId, createDefaultVocabulary));
198            }
199    
200            @Override
201            public List<AssetVocabulary> getGroupVocabularies(
202                            long groupId, int start, int end, OrderByComparator obc)
203                    throws SystemException {
204    
205                    return assetVocabularyPersistence.filterFindByGroupId(
206                            groupId, start, end, obc);
207            }
208    
209            @Override
210            public List<AssetVocabulary> getGroupVocabularies(
211                            long groupId, String name, int start, int end,
212                            OrderByComparator obc)
213                    throws SystemException {
214    
215                    return assetVocabularyPersistence.filterFindByG_LikeN(
216                            groupId, name, start, end, obc);
217            }
218    
219            @Override
220            public int getGroupVocabulariesCount(long groupId) throws SystemException {
221                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
222            }
223    
224            @Override
225            public int getGroupVocabulariesCount(long groupId, String name)
226                    throws SystemException {
227    
228                    return assetVocabularyPersistence.filterCountByG_LikeN(groupId, name);
229            }
230    
231            @Override
232            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
233                            long groupId, String name, int start, int end,
234                            boolean addDefaultVocabulary, OrderByComparator obc)
235                    throws PortalException, SystemException {
236    
237                    List<AssetVocabulary> vocabularies;
238                    int total = 0;
239    
240                    if (Validator.isNotNull(name)) {
241                            name = (CustomSQLUtil.keywords(name))[0];
242    
243                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
244                            total = getGroupVocabulariesCount(groupId, name);
245                    }
246                    else {
247                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
248                            total = getGroupVocabulariesCount(groupId);
249                    }
250    
251                    if (addDefaultVocabulary && (total == 0)) {
252                            vocabularies = new ArrayList<AssetVocabulary>();
253    
254                            vocabularies.add(
255                                    assetVocabularyLocalService.addDefaultVocabulary(groupId));
256    
257                            total = 1;
258                    }
259    
260                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
261            }
262    
263            @Override
264            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
265                            long groupId, String name, int start, int end,
266                            OrderByComparator obc)
267                    throws PortalException, SystemException {
268    
269                    return getGroupVocabulariesDisplay(
270                            groupId, name, start, end, false, obc);
271            }
272    
273            /**
274             * @deprecated As of 6.2.0, with no direct replacement
275             */
276            @Override
277            public JSONObject getJSONGroupVocabularies(
278                            long groupId, String name, int start, int end,
279                            OrderByComparator obc)
280                    throws PortalException, SystemException {
281    
282                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
283    
284                    int page = end / (end - start);
285    
286                    jsonObject.put("page", page);
287    
288                    List<AssetVocabulary> vocabularies;
289                    int total = 0;
290    
291                    if (Validator.isNotNull(name)) {
292                            name = (CustomSQLUtil.keywords(name))[0];
293    
294                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
295                            total = getGroupVocabulariesCount(groupId, name);
296                    }
297                    else {
298                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
299                            total = getGroupVocabulariesCount(groupId);
300                    }
301    
302                    String vocabulariesJSON = JSONFactoryUtil.looseSerialize(vocabularies);
303    
304                    JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
305                            vocabulariesJSON);
306    
307                    jsonObject.put("vocabularies", vocabulariesJSONArray);
308    
309                    jsonObject.put("total", total);
310    
311                    return jsonObject;
312            }
313    
314            @Override
315            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
316                    throws PortalException, SystemException {
317    
318                    return filterVocabularies(
319                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
320            }
321    
322            @Override
323            public AssetVocabulary getVocabulary(long vocabularyId)
324                    throws PortalException, SystemException {
325    
326                    AssetVocabularyPermission.check(
327                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
328    
329                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
330            }
331    
332            /**
333             * @deprecated As of 6.1.0, {@link #updateVocabulary(long, String, Map, Map,
334             *             String, ServiceContext)}
335             */
336            @Override
337            public AssetVocabulary updateVocabulary(
338                            long vocabularyId, Map<Locale, String> titleMap,
339                            Map<Locale, String> descriptionMap, String settings,
340                            ServiceContext serviceContext)
341                    throws PortalException, SystemException {
342    
343                    return updateVocabulary(
344                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
345                            serviceContext);
346            }
347    
348            @Override
349            public AssetVocabulary updateVocabulary(
350                            long vocabularyId, String title, Map<Locale, String> titleMap,
351                            Map<Locale, String> descriptionMap, String settings,
352                            ServiceContext serviceContext)
353                    throws PortalException, SystemException {
354    
355                    AssetVocabularyPermission.check(
356                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
357    
358                    return assetVocabularyLocalService.updateVocabulary(
359                            vocabularyId, title, titleMap, descriptionMap, settings,
360                            serviceContext);
361            }
362    
363            protected List<AssetVocabulary> filterVocabularies(
364                            List<AssetVocabulary> vocabularies)
365                    throws PortalException {
366    
367                    PermissionChecker permissionChecker = getPermissionChecker();
368    
369                    vocabularies = ListUtil.copy(vocabularies);
370    
371                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
372    
373                    while (itr.hasNext()) {
374                            AssetVocabulary vocabulary = itr.next();
375    
376                            if (!AssetVocabularyPermission.contains(
377                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
378    
379                                    itr.remove();
380                            }
381                    }
382    
383                    return vocabularies;
384            }
385    
386    }