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, false);
108
109
110
111 if (serviceContext.getAddGroupPermissions() ||
112 serviceContext.getAddGuestPermissions()) {
113
114 addVocabularyResources(
115 vocabulary, serviceContext.getAddGroupPermissions(),
116 serviceContext.getAddGuestPermissions());
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 deleteVocabulary(AssetVocabulary vocabulary)
151 throws PortalException, SystemException {
152
153
154
155 assetVocabularyPersistence.remove(vocabulary);
156
157
158
159 resourceLocalService.deleteResource(
160 vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
161 ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
162
163
164
165 assetCategoryLocalService.deleteVocabularyCategories(
166 vocabulary.getVocabularyId());
167 }
168
169 public void deleteVocabulary(long vocabularyId)
170 throws PortalException, SystemException {
171
172 AssetVocabulary vocabulary =
173 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
174
175 deleteVocabulary(vocabulary);
176 }
177
178 public List<AssetVocabulary> getCompanyVocabularies(long companyId)
179 throws SystemException {
180
181 return assetVocabularyPersistence.findByCompanyId(companyId);
182 }
183
184 public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
185 throws PortalException, SystemException {
186
187 return getGroupsVocabularies(groupIds, null);
188 }
189
190 public List<AssetVocabulary> getGroupsVocabularies(
191 long[] groupIds, String className)
192 throws PortalException, SystemException {
193
194 List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
195
196 for (long groupId : groupIds) {
197 List<AssetVocabulary> groupVocabularies = getGroupVocabularies(
198 groupId);
199
200 if (Validator.isNull(className)) {
201 vocabularies.addAll(groupVocabularies);
202
203 continue;
204 }
205
206 for (AssetVocabulary groupVocabulary: groupVocabularies) {
207 UnicodeProperties settingsProperties =
208 groupVocabulary.getSettingsProperties();
209
210 long[] selectedClassNameIds = StringUtil.split(
211 settingsProperties.getProperty("selectedClassNameIds"), 0L);
212 long classNameId = PortalUtil.getClassNameId(className);
213
214 if ((selectedClassNameIds.length == 0) ||
215 (selectedClassNameIds[0] == 0) ||
216 ArrayUtil.contains(selectedClassNameIds, classNameId)) {
217
218 vocabularies.add(groupVocabulary);
219 }
220 }
221 }
222
223 return vocabularies;
224 }
225
226 public List<AssetVocabulary> getGroupVocabularies(long groupId)
227 throws PortalException, SystemException {
228
229 return getGroupVocabularies(groupId, true);
230 }
231
232 public List<AssetVocabulary> getGroupVocabularies(
233 long groupId, boolean createDefaultVocabulary)
234 throws PortalException, SystemException {
235
236 List<AssetVocabulary> vocabularies =
237 assetVocabularyPersistence.findByGroupId(groupId);
238
239 if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
240 return vocabularies;
241 }
242
243 Group group = groupLocalService.getGroup(groupId);
244
245 long defaultUserId = userLocalService.getDefaultUserId(
246 group.getCompanyId());
247
248 Map<Locale, String> titleMap = new HashMap<Locale, String>();
249
250 titleMap.put(
251 LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
252
253 ServiceContext serviceContext = new ServiceContext();
254
255 serviceContext.setScopeGroupId(groupId);
256
257 AssetVocabulary vocabulary = assetVocabularyLocalService.addVocabulary(
258 defaultUserId, StringPool.BLANK, titleMap, null, StringPool.BLANK,
259 serviceContext);
260
261 vocabularies = new ArrayList<AssetVocabulary>();
262
263 vocabularies.add(vocabulary);
264
265 return vocabularies;
266 }
267
268 public List<AssetVocabulary> getGroupVocabularies(
269 long groupId, String name, int start, int end,
270 OrderByComparator obc)
271 throws SystemException {
272
273 return assetVocabularyFinder.findByG_N(groupId, name, start, end, obc);
274 }
275
276 public AssetVocabulary getGroupVocabulary(long groupId, String name)
277 throws PortalException, SystemException {
278
279 return assetVocabularyPersistence.findByG_N(groupId, name);
280 }
281
282 public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
283 throws PortalException, SystemException {
284
285 List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
286
287 for (long vocabularyId : vocabularyIds) {
288 AssetVocabulary vocabulary = getVocabulary(vocabularyId);
289
290 vocabularies.add(vocabulary);
291 }
292
293 return vocabularies;
294 }
295
296 public AssetVocabulary getVocabulary(long vocabularyId)
297 throws PortalException, SystemException {
298
299 return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
300 }
301
302
305 public AssetVocabulary updateVocabulary(
306 long vocabularyId, Map<Locale, String> titleMap,
307 Map<Locale, String> descriptionMap, String settings,
308 ServiceContext serviceContext)
309 throws PortalException, SystemException {
310
311 return updateVocabulary(
312 vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
313 serviceContext);
314 }
315
316 public AssetVocabulary updateVocabulary(
317 long vocabularyId, String title, Map<Locale, String> titleMap,
318 Map<Locale, String> descriptionMap, String settings,
319 ServiceContext serviceContext)
320 throws PortalException, SystemException {
321
322 long groupId = serviceContext.getScopeGroupId();
323 String name = titleMap.get(LocaleUtil.getDefault());
324
325 AssetVocabulary vocabulary =
326 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
327
328 if (!vocabulary.getName().equals(name)) {
329 validate(groupId, name);
330 }
331
332 vocabulary.setModifiedDate(new Date());
333 vocabulary.setName(name);
334 vocabulary.setTitleMap(titleMap);
335
336 if (Validator.isNotNull(title)) {
337 vocabulary.setTitle(title);
338 }
339
340 vocabulary.setDescriptionMap(descriptionMap);
341 vocabulary.setSettings(settings);
342
343 assetVocabularyPersistence.update(vocabulary, false);
344
345 return vocabulary;
346 }
347
348 protected boolean hasVocabulary(long groupId, String name)
349 throws SystemException {
350
351 if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
352 return false;
353 }
354 else {
355 return true;
356 }
357 }
358
359 protected void validate(long groupId, String name)
360 throws PortalException, SystemException {
361
362 if (Validator.isNull(name)) {
363 throw new VocabularyNameException();
364 }
365
366 if (hasVocabulary(groupId, name)) {
367 throw new DuplicateVocabularyException(
368 "A category vocabulary with the name " + name +
369 " already exists");
370 }
371 }
372
373 }