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