001    /**
002     * Copyright (c) 2000-2012 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.service.base.AssetVocabularyServiceBaseImpl;
031    import com.liferay.portlet.asset.service.permission.AssetPermission;
032    import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.util.Iterator;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Alvaro del Castillo
042     * @author Eduardo Lundgren
043     * @author Jorge Ferrer
044     * @author Juan Fernández
045     */
046    public class AssetVocabularyServiceImpl extends AssetVocabularyServiceBaseImpl {
047    
048            /**
049             * @deprecated
050             */
051            public AssetVocabulary addVocabulary(
052                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
053                            String settings, ServiceContext serviceContext)
054                    throws PortalException, SystemException {
055    
056                    return addVocabulary(
057                            StringPool.BLANK, titleMap, descriptionMap, settings,
058                            serviceContext);
059            }
060    
061            public AssetVocabulary addVocabulary(
062                            String title, Map<Locale, String> titleMap,
063                            Map<Locale, String> descriptionMap, String settings,
064                            ServiceContext serviceContext)
065                    throws PortalException, SystemException {
066    
067                    AssetPermission.check(
068                            getPermissionChecker(), serviceContext.getScopeGroupId(),
069                            ActionKeys.ADD_VOCABULARY);
070    
071                    return assetVocabularyLocalService.addVocabulary(
072                            getUserId(), title, titleMap, descriptionMap, settings,
073                            serviceContext);
074            }
075    
076            public void deleteVocabularies(long[] vocabularyIds)
077                    throws PortalException, SystemException {
078    
079                    PermissionChecker permissionChecker = getPermissionChecker();
080    
081                    for (long vocabularyId : vocabularyIds) {
082                            AssetVocabularyPermission.check(
083                                    permissionChecker, vocabularyId, ActionKeys.DELETE);
084    
085                            assetVocabularyLocalService.deleteVocabulary(vocabularyId);
086                    }
087            }
088    
089            public void deleteVocabulary(long vocabularyId)
090                    throws PortalException, SystemException {
091    
092                    AssetVocabularyPermission.check(
093                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
094    
095                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
096            }
097    
098            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
099                    throws PortalException, SystemException {
100    
101                    return filterVocabularies(
102                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
103            }
104    
105            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
106                    throws PortalException, SystemException {
107    
108                    return getGroupsVocabularies(groupIds, null);
109            }
110    
111            public List<AssetVocabulary> getGroupsVocabularies(
112                            long[] groupIds, String className)
113                    throws PortalException, SystemException {
114    
115                    return filterVocabularies(
116                            assetVocabularyLocalService.getGroupsVocabularies(
117                                    groupIds, className));
118            }
119    
120            public List<AssetVocabulary> getGroupVocabularies(long groupId)
121                    throws PortalException, SystemException {
122    
123                    return filterVocabularies(
124                            assetVocabularyLocalService.getGroupVocabularies(groupId));
125            }
126    
127            public List<AssetVocabulary> getGroupVocabularies(
128                            long groupId, boolean createDefaultVocabulary)
129                    throws PortalException, SystemException {
130    
131                    return filterVocabularies(
132                            assetVocabularyLocalService.getGroupVocabularies(
133                                    groupId, createDefaultVocabulary));
134            }
135    
136            public List<AssetVocabulary> getGroupVocabularies(
137                            long groupId, int start, int end, OrderByComparator obc)
138                    throws SystemException {
139    
140                    return assetVocabularyPersistence.filterFindByGroupId(
141                            groupId, start, end, obc);
142            }
143    
144            public List<AssetVocabulary> getGroupVocabularies(
145                            long groupId, String name, int start, int end,
146                            OrderByComparator obc)
147                    throws SystemException {
148    
149                    return assetVocabularyFinder.filterFindByG_N(
150                            groupId, name, start, end, obc);
151            }
152    
153            public int getGroupVocabulariesCount(long groupId) throws SystemException {
154                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
155            }
156    
157            public int getGroupVocabulariesCount(long groupId, String name)
158                    throws SystemException {
159    
160                    return assetVocabularyFinder.filterCountByG_N(groupId, name);
161            }
162    
163            public JSONObject getJSONGroupVocabularies(
164                            long groupId, String name, int start, int end,
165                            OrderByComparator obc)
166                    throws PortalException, SystemException {
167    
168                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
169    
170                    int page = end / (end - start);
171    
172                    jsonObject.put("page", page);
173    
174                    List<AssetVocabulary> vocabularies;
175                    int total = 0;
176    
177                    if (Validator.isNotNull(name)) {
178                            name = (CustomSQLUtil.keywords(name))[0];
179    
180                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
181                            total = getGroupVocabulariesCount(groupId, name);
182                    }
183                    else {
184                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
185                            total = getGroupVocabulariesCount(groupId);
186                    }
187    
188                    String vocabulariesJSON = JSONFactoryUtil.looseSerialize(vocabularies);
189    
190                    JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
191                            vocabulariesJSON);
192    
193                    jsonObject.put("vocabularies", vocabulariesJSONArray);
194    
195                    jsonObject.put("total", total);
196    
197                    return jsonObject;
198            }
199    
200            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
201                    throws PortalException, SystemException {
202    
203                    return filterVocabularies(
204                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
205            }
206    
207            public AssetVocabulary getVocabulary(long vocabularyId)
208                    throws PortalException, SystemException {
209    
210                    AssetVocabularyPermission.check(
211                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
212    
213                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
214            }
215    
216            /**
217             * @deprecated
218             */
219            public AssetVocabulary updateVocabulary(
220                            long vocabularyId, Map<Locale, String> titleMap,
221                            Map<Locale, String> descriptionMap, String settings,
222                            ServiceContext serviceContext)
223                    throws PortalException, SystemException {
224    
225                    return updateVocabulary(
226                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
227                            serviceContext);
228            }
229    
230            public AssetVocabulary updateVocabulary(
231                            long vocabularyId, String title, Map<Locale, String> titleMap,
232                            Map<Locale, String> descriptionMap, String settings,
233                            ServiceContext serviceContext)
234                    throws PortalException, SystemException {
235    
236                    AssetVocabularyPermission.check(
237                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
238    
239                    return assetVocabularyLocalService.updateVocabulary(
240                            vocabularyId, title, titleMap, descriptionMap, settings,
241                            serviceContext);
242            }
243    
244            protected List<AssetVocabulary> filterVocabularies(
245                            List<AssetVocabulary> vocabularies)
246                    throws PortalException {
247    
248                    PermissionChecker permissionChecker = getPermissionChecker();
249    
250                    vocabularies = ListUtil.copy(vocabularies);
251    
252                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
253    
254                    while (itr.hasNext()) {
255                            AssetVocabulary vocabulary = itr.next();
256    
257                            if (!AssetVocabularyPermission.contains(
258                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
259    
260                                    itr.remove();
261                            }
262                    }
263    
264                    return vocabularies;
265            }
266    
267    }