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.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.ResourceConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.asset.DuplicateVocabularyException;
028    import com.liferay.portlet.asset.model.AssetVocabulary;
029    import com.liferay.portlet.asset.service.base.AssetVocabularyLocalServiceBaseImpl;
030    
031    import java.util.ArrayList;
032    import java.util.Date;
033    import java.util.HashMap;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Alvaro del Castillo
040     * @author Eduardo Lundgren
041     * @author Jorge Ferrer
042     */
043    public class AssetVocabularyLocalServiceImpl
044            extends AssetVocabularyLocalServiceBaseImpl {
045    
046            public AssetVocabulary addVocabulary(
047                            long userId, Map<Locale, String> titleMap,
048                            Map<Locale, String> descriptionMap, String settings,
049                            ServiceContext serviceContext)
050                    throws PortalException, SystemException {
051    
052                    // Vocabulary
053    
054                    User user = userPersistence.findByPrimaryKey(userId);
055                    long groupId = serviceContext.getScopeGroupId();
056                    String name = titleMap.get(LocaleUtil.getDefault());
057    
058                    Date now = new Date();
059    
060                    if (hasVocabulary(groupId, name)) {
061                            throw new DuplicateVocabularyException(
062                                    "A category vocabulary with the name " + name +
063                                            " already exists");
064                    }
065    
066                    long vocabularyId = counterLocalService.increment();
067    
068                    AssetVocabulary vocabulary = assetVocabularyPersistence.create(
069                            vocabularyId);
070    
071                    vocabulary.setUuid(serviceContext.getUuid());
072                    vocabulary.setGroupId(groupId);
073                    vocabulary.setCompanyId(user.getCompanyId());
074                    vocabulary.setUserId(user.getUserId());
075                    vocabulary.setUserName(user.getFullName());
076                    vocabulary.setCreateDate(now);
077                    vocabulary.setModifiedDate(now);
078                    vocabulary.setName(name);
079                    vocabulary.setTitleMap(titleMap);
080                    vocabulary.setDescriptionMap(descriptionMap);
081                    vocabulary.setSettings(settings);
082    
083                    assetVocabularyPersistence.update(vocabulary, false);
084    
085                    // Resources
086    
087                    if (serviceContext.getAddCommunityPermissions() ||
088                            serviceContext.getAddGuestPermissions()) {
089    
090                            addVocabularyResources(
091                                    vocabulary, serviceContext.getAddCommunityPermissions(),
092                                    serviceContext.getAddGuestPermissions());
093                    }
094                    else {
095                            addVocabularyResources(
096                                    vocabulary, serviceContext.getCommunityPermissions(),
097                                    serviceContext.getGuestPermissions());
098                    }
099    
100                    return vocabulary;
101            }
102    
103            public void addVocabularyResources(
104                            AssetVocabulary vocabulary, boolean addCommunityPermissions,
105                            boolean addGuestPermissions)
106                    throws PortalException, SystemException {
107    
108                    resourceLocalService.addResources(
109                            vocabulary.getCompanyId(), vocabulary.getGroupId(),
110                            vocabulary.getUserId(), AssetVocabulary.class.getName(),
111                            vocabulary.getVocabularyId(), false, addCommunityPermissions,
112                            addGuestPermissions);
113            }
114    
115            public void addVocabularyResources(
116                            AssetVocabulary vocabulary, String[] communityPermissions,
117                            String[] guestPermissions)
118                    throws PortalException, SystemException {
119    
120                    resourceLocalService.addModelResources(
121                            vocabulary.getCompanyId(), vocabulary.getGroupId(),
122                            vocabulary.getUserId(), AssetVocabulary.class.getName(),
123                            vocabulary.getVocabularyId(), communityPermissions,
124                            guestPermissions);
125            }
126    
127            public void deleteVocabulary(AssetVocabulary vocabulary)
128                    throws PortalException, SystemException {
129    
130                    // Vocabulary
131    
132                    assetVocabularyPersistence.remove(vocabulary);
133    
134                    // Resources
135    
136                    resourceLocalService.deleteResource(
137                            vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
138                            ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
139    
140                    // Categories
141    
142                    assetCategoryLocalService.deleteVocabularyCategories(
143                            vocabulary.getVocabularyId());
144            }
145    
146            public void deleteVocabulary(long vocabularyId)
147                    throws PortalException, SystemException {
148    
149                    AssetVocabulary vocabulary =
150                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
151    
152                    deleteVocabulary(vocabulary);
153            }
154    
155            public List<AssetVocabulary> getCompanyVocabularies(long companyId)
156                    throws SystemException {
157    
158                    return assetVocabularyPersistence.findByCompanyId(companyId);
159            }
160    
161            public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
162                    throws PortalException, SystemException {
163    
164                    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
165    
166                    for (long groupId : groupIds) {
167                            vocabularies.addAll(getGroupVocabularies(groupId));
168                    }
169    
170                    return vocabularies;
171            }
172    
173            public List<AssetVocabulary> getGroupVocabularies(long groupId)
174                    throws PortalException, SystemException {
175    
176                    List<AssetVocabulary> vocabularies =
177                            assetVocabularyPersistence.findByGroupId(groupId);
178    
179                    if (vocabularies.isEmpty()) {
180                            Group group = groupLocalService.getGroup(groupId);
181    
182                            long defaultUserId = userLocalService.getDefaultUserId(
183                                    group.getCompanyId());
184    
185                            ServiceContext serviceContext = new ServiceContext();
186    
187                            serviceContext.setScopeGroupId(groupId);
188    
189                            Map<Locale, String> titleMap = new HashMap<Locale, String>();
190    
191                            titleMap.put(
192                                    LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
193    
194                            AssetVocabulary vocabulary =
195                                    assetVocabularyLocalService.addVocabulary(
196                                            defaultUserId, titleMap, null, StringPool.BLANK,
197                                            serviceContext);
198    
199                            vocabularies = ListUtil.copy(vocabularies);
200    
201                            vocabularies.add(vocabulary);
202                    }
203    
204                    return vocabularies;
205            }
206    
207            public AssetVocabulary getGroupVocabulary(long groupId, String name)
208                    throws PortalException, SystemException {
209    
210                    return assetVocabularyPersistence.findByG_N(groupId, name);
211            }
212    
213            public AssetVocabulary getVocabulary(long vocabularyId)
214                    throws PortalException, SystemException {
215    
216                    return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
217            }
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                    String name = titleMap.get(LocaleUtil.getDefault());
226    
227                    AssetVocabulary vocabulary =
228                            assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
229    
230                    if (!vocabulary.getName().equals(name) &&
231                            hasVocabulary(vocabulary.getGroupId(), name)) {
232    
233                            throw new DuplicateVocabularyException(name);
234                    }
235    
236                    vocabulary.setModifiedDate(new Date());
237                    vocabulary.setName(name);
238                    vocabulary.setTitleMap(titleMap);
239                    vocabulary.setDescriptionMap(descriptionMap);
240                    vocabulary.setSettings(settings);
241    
242                    assetVocabularyPersistence.update(vocabulary, false);
243    
244                    return vocabulary;
245            }
246    
247            protected boolean hasVocabulary(long groupId, String name)
248                    throws SystemException {
249    
250                    if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
251                            return false;
252                    }
253                    else {
254                            return true;
255                    }
256            }
257    
258    }