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