001    /**
002     * Copyright (c) 2000-2010 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.util.ListUtil;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.asset.model.AssetVocabulary;
024    import com.liferay.portlet.asset.service.base.AssetVocabularyServiceBaseImpl;
025    import com.liferay.portlet.asset.service.permission.AssetPermission;
026    import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
027    
028    import java.util.Iterator;
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    
033    /**
034     * @author Alvaro del Castillo
035     * @author Eduardo Lundgren
036     * @author Jorge Ferrer
037     */
038    public class AssetVocabularyServiceImpl
039            extends AssetVocabularyServiceBaseImpl {
040    
041            public AssetVocabulary addVocabulary(
042                            Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
043                            String settings, ServiceContext serviceContext)
044                    throws PortalException, SystemException {
045    
046                    AssetPermission.check(
047                            getPermissionChecker(), serviceContext.getScopeGroupId(),
048                            ActionKeys.ADD_VOCABULARY);
049    
050                    return assetVocabularyLocalService.addVocabulary(
051                            getUserId(), titleMap, descriptionMap, settings, serviceContext);
052            }
053    
054            public void deleteVocabulary(long vocabularyId)
055                    throws PortalException, SystemException {
056    
057                    AssetVocabularyPermission.check(
058                            getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
059    
060                    assetVocabularyLocalService.deleteVocabulary(vocabularyId);
061            }
062    
063            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
064                    throws PortalException, SystemException {
065    
066                    return filterVocabularies(
067                            assetVocabularyLocalService.getCompanyVocabularies(companyId));
068            }
069    
070            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
071                    throws PortalException, SystemException {
072    
073                    return filterVocabularies(
074                            assetVocabularyLocalService.getGroupsVocabularies(groupIds));
075            }
076    
077            public List<AssetVocabulary> getGroupVocabularies(long groupId)
078                    throws PortalException, SystemException {
079    
080                    return filterVocabularies(
081                            assetVocabularyLocalService.getGroupVocabularies(groupId));
082            }
083    
084            public AssetVocabulary getVocabulary(long vocabularyId)
085                    throws PortalException, SystemException {
086    
087                    AssetVocabularyPermission.check(
088                            getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
089    
090                    return assetVocabularyLocalService.getVocabulary(vocabularyId);
091            }
092    
093            public AssetVocabulary updateVocabulary(
094                            long vocabularyId, Map<Locale, String> titleMap,
095                            Map<Locale, String> descriptionMap, String settings,
096                            ServiceContext serviceContext)
097                    throws PortalException, SystemException {
098    
099                    AssetVocabularyPermission.check(
100                            getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
101    
102                    return assetVocabularyLocalService.updateVocabulary(
103                            vocabularyId, titleMap, descriptionMap, settings, serviceContext);
104            }
105    
106            protected List<AssetVocabulary> filterVocabularies(
107                            List<AssetVocabulary> vocabularies)
108                    throws PortalException {
109    
110                    PermissionChecker permissionChecker = getPermissionChecker();
111    
112                    vocabularies = ListUtil.copy(vocabularies);
113    
114                    Iterator<AssetVocabulary> itr = vocabularies.iterator();
115    
116                    while (itr.hasNext()) {
117                            AssetVocabulary vocabulary = itr.next();
118    
119                            if (!AssetVocabularyPermission.contains(
120                                            permissionChecker, vocabulary, ActionKeys.VIEW)) {
121    
122                                    itr.remove();
123                            }
124                    }
125    
126                    return vocabularies;
127            }
128    
129    }