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, int start, int end, OrderByComparator obc)
129                    throws SystemException {
130    
131                    return assetVocabularyPersistence.filterFindByGroupId(
132                            groupId, start, end, obc);
133            }
134    
135            public List<AssetVocabulary> getGroupVocabularies(
136                            long groupId, String name, int start, int end,
137                            OrderByComparator obc)
138                    throws SystemException {
139    
140                    return assetVocabularyFinder.filterFindByG_N(
141                            groupId, name, start, end, obc);
142            }
143    
144            public int getGroupVocabulariesCount(long groupId) throws SystemException {
145                    return assetVocabularyPersistence.filterCountByGroupId(groupId);
146            }
147    
148            public int getGroupVocabulariesCount(long groupId, String name)
149                    throws SystemException {
150    
151                    return assetVocabularyFinder.filterCountByG_N(groupId, name);
152            }
153    
154            public JSONObject getJSONGroupVocabularies(
155                            long groupId, String name, int start, int end,
156                            OrderByComparator obc)
157                    throws PortalException, SystemException {
158    
159                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
160    
161                    int page = end / (end - start);
162    
163                    jsonObject.put("page", page);
164    
165                    List<AssetVocabulary> vocabularies;
166                    int total = 0;
167    
168                    if (Validator.isNotNull(name)) {
169                            name = (CustomSQLUtil.keywords(name))[0];
170    
171                            vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
172                            total = getGroupVocabulariesCount(groupId, name);
173                    }
174                    else {
175                            vocabularies = getGroupVocabularies(groupId, start, end, obc);
176                            total = getGroupVocabulariesCount(groupId);
177                    }
178    
179                    String vocabulariesJSON = JSONFactoryUtil.looseSerialize(vocabularies);
180    
181                    JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
182                            vocabulariesJSON);
183    
184                    jsonObject.put("vocabularies", vocabulariesJSONArray);
185    
186                    jsonObject.put("total", total);
187    
188                    return jsonObject;
189            }
190    
191            public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
192                    throws PortalException, SystemException {
193    
194                    return filterVocabularies(
195                            assetVocabularyLocalService.getVocabularies(vocabularyIds));
196            }
197    
198            public AssetVocabulary getVocabulary(long vocabularyId)
199                    throws PortalException, SystemException {
200    
201                    AssetVocabularyPermission.check(
202                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
203    
204                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
205            }
206    
207            /**
208             * @deprecated
209             */
210            public AssetVocabulary updateVocabulary(
211                            long vocabularyId, Map<Locale, String> titleMap,
212                            Map<Locale, String> descriptionMap, String settings,
213                            ServiceContext serviceContext)
214                    throws PortalException, SystemException {
215    
216                    return updateVocabulary(
217                            vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
218                            serviceContext);
219            }
220    
221            public AssetVocabulary updateVocabulary(
222                            long vocabularyId, String title, Map<Locale, String> titleMap,
223                            Map<Locale, String> descriptionMap, String settings,
224                            ServiceContext serviceContext)
225                    throws PortalException, SystemException {
226    
227                    AssetVocabularyPermission.check(
228                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
229    
230                    return assetVocabularyLocalService.updateVocabulary(
231                            vocabularyId, title, titleMap, descriptionMap, settings,
232                            serviceContext);
233            }
234    
235            protected List<AssetVocabulary> filterVocabularies(
236                            List<AssetVocabulary> vocabularies)
237                    throws PortalException {
238    
239                    PermissionChecker permissionChecker = getPermissionChecker();
240    
241                    vocabularies = ListUtil.copy(vocabularies);
242    
243                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
244    
245                    while (itr.hasNext()) {
246                            AssetVocabulary vocabulary = itr.next();
247    
248                            if (!AssetVocabularyPermission.contains(
249                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
250    
251                                    itr.remove();
252                            }
253                    }
254    
255                    return vocabularies;
256            }
257    
258    }