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
055             */
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            public AssetVocabulary addVocabulary(
067                            String title, Map<Locale, String> titleMap,
068                            Map<Locale, String> descriptionMap, String settings,
069                            ServiceContext serviceContext)
070                    throws PortalException, SystemException {
071    
072                    AssetPermission.check(
073                            getPermissionChecker(), serviceContext.getScopeGroupId(),
074                            ActionKeys.ADD_VOCABULARY);
075    
076                    return assetVocabularyLocalService.addVocabulary(
077                            getUserId(), title, titleMap, descriptionMap, settings,
078                            serviceContext);
079            }
080    
081            public AssetVocabulary addVocabulary(
082                            String title, ServiceContext serviceContext)
083                    throws PortalException, SystemException {
084    
085                    AssetPermission.check(
086                            getPermissionChecker(), serviceContext.getScopeGroupId(),
087                            ActionKeys.ADD_VOCABULARY);
088    
089                    return assetVocabularyLocalService.addVocabulary(
090                            getUserId(), title, serviceContext);
091            }
092    
093            public List<AssetVocabulary> deleteVocabularies(
094                            long[] vocabularyIds, ServiceContext serviceContext)
095                    throws PortalException, SystemException {
096    
097                    List<AssetVocabulary> failedVocabularies =
098                            new ArrayList<AssetVocabulary>();
099    
100                    for (long vocabularyId : vocabularyIds) {
101                            try {
102                                    AssetVocabularyPermission.check(
103                                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
104    
105                                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
106                            }
107                            catch (PortalException pe) {
108                                    if (serviceContext.isFailOnPortalException()) {
109                                            throw pe;
110                                    }
111    
112                                    AssetVocabulary vocabulary =
113                                            assetVocabularyPersistence.fetchByPrimaryKey(vocabularyId);
114    
115                                    if (vocabulary == null) {
116                                            vocabulary = assetVocabularyPersistence.create(
117                                                    vocabularyId);
118                                    }
119    
120                                    failedVocabularies.add(vocabulary);
121                            }
122                    }
123    
124                    return failedVocabularies;
125            }
126    
127            public void deleteVocabulary(long vocabularyId)
128                    throws PortalException, SystemException {
129    
130                    AssetVocabularyPermission.check(
131                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
132    
133                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
134            }
135    
136            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
137                    throws PortalException, SystemException {
138    
139                    return filterVocabularies(
140                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
141            }
142    
143            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
144                    throws PortalException, SystemException {
145    
146                    return getGroupsVocabularies(groupIds, null);
147            }
148    
149            public List<AssetVocabulary> getGroupsVocabularies(
150                            long[] groupIds, String className)
151                    throws PortalException, SystemException {
152    
153                    return filterVocabularies(
154                            assetVocabularyLocalService.getGroupsVocabularies(
155                                    groupIds, className));
156            }
157    
158            public List<AssetVocabulary> getGroupVocabularies(long groupId)
159                    throws PortalException, SystemException {
160    
161                    return filterVocabularies(
162                            assetVocabularyLocalService.getGroupVocabularies(groupId));
163            }
164    
165            public List<AssetVocabulary> getGroupVocabularies(
166                            long groupId, boolean createDefaultVocabulary)
167                    throws PortalException, SystemException {
168    
169                    return filterVocabularies(
170                            assetVocabularyLocalService.getGroupVocabularies(
171                                    groupId, createDefaultVocabulary));
172            }
173    
174            public List<AssetVocabulary> getGroupVocabularies(
175                            long groupId, int start, int end, OrderByComparator obc)
176                    throws SystemException {
177    
178                    return assetVocabularyPersistence.filterFindByGroupId(
179                            groupId, start, end, obc);
180            }
181    
182            public List<AssetVocabulary> getGroupVocabularies(
183                            long groupId, String name, int start, int end,
184                            OrderByComparator obc)
185                    throws SystemException {
186    
187                    return assetVocabularyFinder.filterFindByG_N(
188                            groupId, name, start, end, obc);
189            }
190    
191            public int getGroupVocabulariesCount(long groupId) throws SystemException {
192                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
193            }
194    
195            public int getGroupVocabulariesCount(long groupId, String name)
196                    throws SystemException {
197    
198                    return assetVocabularyFinder.filterCountByG_N(groupId, name);
199            }
200    
201            public AssetVocabularyDisplay getGroupVocabulariesDisplay(
202                            long groupId, String name, int start, int end,
203                            OrderByComparator obc)
204                    throws SystemException {
205    
206                    List<AssetVocabulary> vocabularies;
207                    int total = 0;
208    
209                    if (Validator.isNotNull(name)) {
210                            name = (CustomSQLUtil.keywords(name))[0];
211    
212                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
213                            total = getGroupVocabulariesCount(groupId, name);
214                    }
215                    else {
216                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
217                            total = getGroupVocabulariesCount(groupId);
218                    }
219    
220                    return new AssetVocabularyDisplay(vocabularies, total, start, end);
221            }
222    
223            @Deprecated
224            public JSONObject getJSONGroupVocabularies(
225                            long groupId, String name, int start, int end,
226                            OrderByComparator obc)
227                    throws PortalException, SystemException {
228    
229                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
230    
231                    int page = end / (end - start);
232    
233                    jsonObject.put("page", page);
234    
235                    List<AssetVocabulary> vocabularies;
236                    int total = 0;
237    
238                    if (Validator.isNotNull(name)) {
239                            name = (CustomSQLUtil.keywords(name))[0];
240    
241                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
242                            total = getGroupVocabulariesCount(groupId, name);
243                    }
244                    else {
245                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
246                            total = getGroupVocabulariesCount(groupId);
247                    }
248    
249                    String vocabulariesJSON = JSONFactoryUtil.looseSerialize(vocabularies);
250    
251                    JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
252                            vocabulariesJSON);
253    
254                    jsonObject.put("vocabularies", vocabulariesJSONArray);
255    
256                    jsonObject.put("total", total);
257    
258                    return jsonObject;
259            }
260    
261            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
262                    throws PortalException, SystemException {
263    
264                    return filterVocabularies(
265                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
266            }
267    
268            public AssetVocabulary getVocabulary(long vocabularyId)
269                    throws PortalException, SystemException {
270    
271                    AssetVocabularyPermission.check(
272                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
273    
274                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
275            }
276    
277            /**
278             * @deprecated As of 6.1.0
279             */
280            public AssetVocabulary updateVocabulary(
281                            long vocabularyId, Map<Locale, String> titleMap,
282                            Map<Locale, String> descriptionMap, String settings,
283                            ServiceContext serviceContext)
284                    throws PortalException, SystemException {
285    
286                    return updateVocabulary(
287                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
288                            serviceContext);
289            }
290    
291            public AssetVocabulary updateVocabulary(
292                            long vocabularyId, String title, Map<Locale, String> titleMap,
293                            Map<Locale, String> descriptionMap, String settings,
294                            ServiceContext serviceContext)
295                    throws PortalException, SystemException {
296    
297                    AssetVocabularyPermission.check(
298                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
299    
300                    return assetVocabularyLocalService.updateVocabulary(
301                            vocabularyId, title, titleMap, descriptionMap, settings,
302                            serviceContext);
303            }
304    
305            protected List<AssetVocabulary> filterVocabularies(
306                            List<AssetVocabulary> vocabularies)
307                    throws PortalException {
308    
309                    PermissionChecker permissionChecker = getPermissionChecker();
310    
311                    vocabularies = ListUtil.copy(vocabularies);
312    
313                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
314    
315                    while (itr.hasNext()) {
316                            AssetVocabulary vocabulary = itr.next();
317    
318                            if (!AssetVocabularyPermission.contains(
319                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
320    
321                                    itr.remove();
322                            }
323                    }
324    
325                    return vocabularies;
326            }
327    
328    }