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.ArrayUtil;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.OrderByComparator;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.UnicodeProperties;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.ResourceConstants;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portal.util.PortalUtil;
031 import com.liferay.portal.util.PropsValues;
032 import com.liferay.portlet.asset.DuplicateVocabularyException;
033 import com.liferay.portlet.asset.VocabularyNameException;
034 import com.liferay.portlet.asset.model.AssetVocabulary;
035 import com.liferay.portlet.asset.service.base.AssetVocabularyLocalServiceBaseImpl;
036
037 import java.util.ArrayList;
038 import java.util.Date;
039 import java.util.HashMap;
040 import java.util.List;
041 import java.util.Locale;
042 import java.util.Map;
043
044
050 public class AssetVocabularyLocalServiceImpl
051 extends AssetVocabularyLocalServiceBaseImpl {
052
053
056 public AssetVocabulary addVocabulary(
057 long userId, Map<Locale, String> titleMap,
058 Map<Locale, String> descriptionMap, String settings,
059 ServiceContext serviceContext)
060 throws PortalException, SystemException {
061
062 return addVocabulary(
063 userId, StringPool.BLANK, titleMap, descriptionMap, settings,
064 serviceContext);
065 }
066
067 public AssetVocabulary addVocabulary(
068 long userId, String title, Map<Locale, String> titleMap,
069 Map<Locale, String> descriptionMap, String settings,
070 ServiceContext serviceContext)
071 throws PortalException, SystemException {
072
073
074
075 User user = userPersistence.findByPrimaryKey(userId);
076 long groupId = serviceContext.getScopeGroupId();
077 String name = titleMap.get(LocaleUtil.getDefault());
078
079 Date now = new Date();
080
081 validate(groupId, name);
082
083 long vocabularyId = counterLocalService.increment();
084
085 AssetVocabulary vocabulary = assetVocabularyPersistence.create(
086 vocabularyId);
087
088 vocabulary.setUuid(serviceContext.getUuid());
089 vocabulary.setGroupId(groupId);
090 vocabulary.setCompanyId(user.getCompanyId());
091 vocabulary.setUserId(user.getUserId());
092 vocabulary.setUserName(user.getFullName());
093 vocabulary.setCreateDate(now);
094 vocabulary.setModifiedDate(now);
095 vocabulary.setName(name);
096
097 if (Validator.isNotNull(title)) {
098 vocabulary.setTitle(title);
099 }
100 else {
101 vocabulary.setTitleMap(titleMap);
102 }
103
104 vocabulary.setDescriptionMap(descriptionMap);
105 vocabulary.setSettings(settings);
106
107 assetVocabularyPersistence.update(vocabulary);
108
109
110
111 if (serviceContext.isAddGroupPermissions() ||
112 serviceContext.isAddGuestPermissions()) {
113
114 addVocabularyResources(
115 vocabulary, serviceContext.isAddGroupPermissions(),
116 serviceContext.isAddGuestPermissions());
117 }
118 else {
119 addVocabularyResources(
120 vocabulary, serviceContext.getGroupPermissions(),
121 serviceContext.getGuestPermissions());
122 }
123
124 return vocabulary;
125 }
126
127 public void addVocabularyResources(
128 AssetVocabulary vocabulary, boolean addGroupPermissions,
129 boolean addGuestPermissions)
130 throws PortalException, SystemException {
131
132 resourceLocalService.addResources(
133 vocabulary.getCompanyId(), vocabulary.getGroupId(),
134 vocabulary.getUserId(), AssetVocabulary.class.getName(),
135 vocabulary.getVocabularyId(), false, addGroupPermissions,
136 addGuestPermissions);
137 }
138
139 public void addVocabularyResources(
140 AssetVocabulary vocabulary, String[] groupPermissions,
141 String[] guestPermissions)
142 throws PortalException, SystemException {
143
144 resourceLocalService.addModelResources(
145 vocabulary.getCompanyId(), vocabulary.getGroupId(),
146 vocabulary.getUserId(), AssetVocabulary.class.getName(),
147 vocabulary.getVocabularyId(), groupPermissions, guestPermissions);
148 }
149
150 public void deleteVocabularies(long groupId)
151 throws PortalException, SystemException {
152
153 List<AssetVocabulary> vocabularies =
154 assetVocabularyPersistence.findByGroupId(groupId);
155
156 for (AssetVocabulary vocabulary : vocabularies) {
157 deleteVocabulary(vocabulary);
158 }
159 }
160
161 public void deleteVocabulary(AssetVocabulary vocabulary)
162 throws PortalException, SystemException {
163
164
165
166 assetVocabularyPersistence.remove(vocabulary);
167
168
169
170 resourceLocalService.deleteResource(
171 vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
172 ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
173
174
175
176 assetCategoryLocalService.deleteVocabularyCategories(
177 vocabulary.getVocabularyId());
178 }
179
180 public void deleteVocabulary(long vocabularyId)
181 throws PortalException, SystemException {
182
183 AssetVocabulary vocabulary =
184 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
185
186 deleteVocabulary(vocabulary);
187 }
188
189 public List<AssetVocabulary> getCompanyVocabularies(long companyId)
190 throws SystemException {
191
192 return assetVocabularyPersistence.findByCompanyId(companyId);
193 }
194
195 public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
196 throws PortalException, SystemException {
197
198 return getGroupsVocabularies(groupIds, null);
199 }
200
201 public List<AssetVocabulary> getGroupsVocabularies(
202 long[] groupIds, String className)
203 throws PortalException, SystemException {
204
205 List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
206
207 groupIds = ArrayUtil.unique(groupIds);
208
209 for (long groupId : groupIds) {
210 List<AssetVocabulary> groupVocabularies = getGroupVocabularies(
211 groupId);
212
213 if (Validator.isNull(className)) {
214 vocabularies.addAll(groupVocabularies);
215
216 continue;
217 }
218
219 for (AssetVocabulary groupVocabulary : groupVocabularies) {
220 UnicodeProperties settingsProperties =
221 groupVocabulary.getSettingsProperties();
222
223 long[] selectedClassNameIds = StringUtil.split(
224 settingsProperties.getProperty("selectedClassNameIds"), 0L);
225 long classNameId = PortalUtil.getClassNameId(className);
226
227 if ((selectedClassNameIds.length == 0) ||
228 (selectedClassNameIds[0] == 0) ||
229 ArrayUtil.contains(selectedClassNameIds, classNameId)) {
230
231 vocabularies.add(groupVocabulary);
232 }
233 }
234 }
235
236 return vocabularies;
237 }
238
239 public List<AssetVocabulary> getGroupVocabularies(long groupId)
240 throws PortalException, SystemException {
241
242 return getGroupVocabularies(groupId, true);
243 }
244
245 public List<AssetVocabulary> getGroupVocabularies(
246 long groupId, boolean createDefaultVocabulary)
247 throws PortalException, SystemException {
248
249 List<AssetVocabulary> vocabularies =
250 assetVocabularyPersistence.findByGroupId(groupId);
251
252 if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
253 return vocabularies;
254 }
255
256 Group group = groupLocalService.getGroup(groupId);
257
258 long defaultUserId = userLocalService.getDefaultUserId(
259 group.getCompanyId());
260
261 Map<Locale, String> titleMap = new HashMap<Locale, String>();
262
263 titleMap.put(
264 LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
265
266 ServiceContext serviceContext = new ServiceContext();
267
268 serviceContext.setScopeGroupId(groupId);
269
270 AssetVocabulary vocabulary = assetVocabularyLocalService.addVocabulary(
271 defaultUserId, StringPool.BLANK, titleMap, null, StringPool.BLANK,
272 serviceContext);
273
274 vocabularies = new ArrayList<AssetVocabulary>();
275
276 vocabularies.add(vocabulary);
277
278 return vocabularies;
279 }
280
281 public List<AssetVocabulary> getGroupVocabularies(
282 long groupId, String name, int start, int end,
283 OrderByComparator obc)
284 throws SystemException {
285
286 return assetVocabularyFinder.findByG_N(groupId, name, start, end, obc);
287 }
288
289 public AssetVocabulary getGroupVocabulary(long groupId, String name)
290 throws PortalException, SystemException {
291
292 return assetVocabularyPersistence.findByG_N(groupId, name);
293 }
294
295 public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
296 throws PortalException, SystemException {
297
298 List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
299
300 for (long vocabularyId : vocabularyIds) {
301 AssetVocabulary vocabulary = getVocabulary(vocabularyId);
302
303 vocabularies.add(vocabulary);
304 }
305
306 return vocabularies;
307 }
308
309 public AssetVocabulary getVocabulary(long vocabularyId)
310 throws PortalException, SystemException {
311
312 return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
313 }
314
315
318 public AssetVocabulary updateVocabulary(
319 long vocabularyId, Map<Locale, String> titleMap,
320 Map<Locale, String> descriptionMap, String settings,
321 ServiceContext serviceContext)
322 throws PortalException, SystemException {
323
324 return updateVocabulary(
325 vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
326 serviceContext);
327 }
328
329 public AssetVocabulary updateVocabulary(
330 long vocabularyId, String title, Map<Locale, String> titleMap,
331 Map<Locale, String> descriptionMap, String settings,
332 ServiceContext serviceContext)
333 throws PortalException, SystemException {
334
335 long groupId = serviceContext.getScopeGroupId();
336 String name = titleMap.get(LocaleUtil.getDefault());
337
338 AssetVocabulary vocabulary =
339 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
340
341 if (!vocabulary.getName().equals(name)) {
342 validate(groupId, name);
343 }
344
345 vocabulary.setModifiedDate(new Date());
346 vocabulary.setName(name);
347 vocabulary.setTitleMap(titleMap);
348
349 if (Validator.isNotNull(title)) {
350 vocabulary.setTitle(title);
351 }
352
353 vocabulary.setDescriptionMap(descriptionMap);
354 vocabulary.setSettings(settings);
355
356 assetVocabularyPersistence.update(vocabulary);
357
358 return vocabulary;
359 }
360
361 protected boolean hasVocabulary(long groupId, String name)
362 throws SystemException {
363
364 if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
365 return false;
366 }
367 else {
368 return true;
369 }
370 }
371
372 protected void validate(long groupId, String name)
373 throws PortalException, SystemException {
374
375 if (Validator.isNull(name)) {
376 throw new VocabularyNameException();
377 }
378
379 if (hasVocabulary(groupId, name)) {
380 throw new DuplicateVocabularyException(
381 "A category vocabulary with the name " + name +
382 " already exists");
383 }
384 }
385
386 }