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