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