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.json.JSONArray;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.search.BaseModelSearchResult;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.security.permission.ActionKeys;
027 import com.liferay.portal.security.permission.PermissionChecker;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portlet.asset.model.AssetCategoryConstants;
030 import com.liferay.portlet.asset.model.AssetVocabulary;
031 import com.liferay.portlet.asset.model.AssetVocabularyDisplay;
032 import com.liferay.portlet.asset.service.base.AssetVocabularyServiceBaseImpl;
033 import com.liferay.portlet.asset.service.permission.AssetPermission;
034 import com.liferay.portlet.asset.service.permission.AssetVocabularyPermission;
035 import com.liferay.portlet.asset.util.AssetUtil;
036 import com.liferay.util.dao.orm.CustomSQLUtil;
037
038 import java.util.ArrayList;
039 import java.util.Iterator;
040 import java.util.List;
041 import java.util.Locale;
042 import java.util.Map;
043
044
053 public class AssetVocabularyServiceImpl extends AssetVocabularyServiceBaseImpl {
054
055 @Override
056 public AssetVocabulary addVocabulary(
057 long groupId, String title, Map<Locale, String> titleMap,
058 Map<Locale, String> descriptionMap, String settings,
059 ServiceContext serviceContext)
060 throws PortalException {
061
062 AssetPermission.check(
063 getPermissionChecker(), groupId, ActionKeys.ADD_VOCABULARY);
064
065 return assetVocabularyLocalService.addVocabulary(
066 getUserId(), groupId, title, titleMap, descriptionMap, settings,
067 serviceContext);
068 }
069
070 @Override
071 public AssetVocabulary addVocabulary(
072 long groupId, String title, ServiceContext serviceContext)
073 throws PortalException {
074
075 AssetPermission.check(
076 getPermissionChecker(), groupId, ActionKeys.ADD_VOCABULARY);
077
078 return assetVocabularyLocalService.addVocabulary(
079 getUserId(), groupId, title, serviceContext);
080 }
081
082
086 @Deprecated
087 @Override
088 public void deleteVocabularies(long[] vocabularyIds)
089 throws PortalException {
090
091 deleteVocabularies(vocabularyIds, null);
092 }
093
094 @Override
095 public List<AssetVocabulary> deleteVocabularies(
096 long[] vocabularyIds, ServiceContext serviceContext)
097 throws PortalException {
098
099 List<AssetVocabulary> failedVocabularies = new ArrayList<>();
100
101 for (long vocabularyId : vocabularyIds) {
102 try {
103 AssetVocabularyPermission.check(
104 getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
105
106 assetVocabularyLocalService.deleteVocabulary(vocabularyId);
107 }
108 catch (PortalException pe) {
109 if (serviceContext == null) {
110 return null;
111 }
112
113 if (serviceContext.isFailOnPortalException()) {
114 throw pe;
115 }
116
117 AssetVocabulary vocabulary =
118 assetVocabularyPersistence.fetchByPrimaryKey(vocabularyId);
119
120 if (vocabulary == null) {
121 vocabulary = assetVocabularyPersistence.create(
122 vocabularyId);
123 }
124
125 failedVocabularies.add(vocabulary);
126 }
127 }
128
129 return failedVocabularies;
130 }
131
132 @Override
133 public void deleteVocabulary(long vocabularyId) throws PortalException {
134 AssetVocabularyPermission.check(
135 getPermissionChecker(), vocabularyId, ActionKeys.DELETE);
136
137 assetVocabularyLocalService.deleteVocabulary(vocabularyId);
138 }
139
140 @Override
141 public AssetVocabulary fetchVocabulary(long vocabularyId)
142 throws PortalException {
143
144 AssetVocabulary vocabulary =
145 assetVocabularyLocalService.fetchAssetVocabulary(vocabularyId);
146
147 if (vocabulary != null) {
148 AssetVocabularyPermission.check(
149 getPermissionChecker(), vocabulary, ActionKeys.VIEW);
150 }
151
152 return vocabulary;
153 }
154
155
158 @Deprecated
159 @Override
160 public List<AssetVocabulary> getCompanyVocabularies(long companyId)
161 throws PortalException {
162
163 return filterVocabularies(
164 assetVocabularyLocalService.getCompanyVocabularies(companyId));
165 }
166
167 @Override
168 public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds) {
169 return getGroupsVocabularies(groupIds, null);
170 }
171
172 @Override
173 public List<AssetVocabulary> getGroupsVocabularies(
174 long[] groupIds, String className) {
175
176 return getGroupsVocabularies(
177 groupIds, className, AssetCategoryConstants.ALL_CLASS_TYPE_PK);
178 }
179
180 @Override
181 public List<AssetVocabulary> getGroupsVocabularies(
182 long[] groupIds, String className, long classTypePK) {
183
184 List<AssetVocabulary> vocabularies =
185 assetVocabularyPersistence.filterFindByGroupId(groupIds);
186
187 if (Validator.isNull(className)) {
188 return vocabularies;
189 }
190
191 return AssetUtil.filterVocabularies(
192 vocabularies, className, classTypePK);
193 }
194
195 @Override
196 public List<AssetVocabulary> getGroupVocabularies(long groupId)
197 throws PortalException {
198
199 return getGroupVocabularies(groupId, true);
200 }
201
202 @Override
203 public List<AssetVocabulary> getGroupVocabularies(
204 long groupId, boolean createDefaultVocabulary)
205 throws PortalException {
206
207 List<AssetVocabulary> vocabularies =
208 assetVocabularyPersistence.filterFindByGroupId(groupId);
209
210 if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
211 return vocabularies;
212 }
213
214 vocabularies = new ArrayList<>();
215
216 AssetVocabulary vocabulary =
217 assetVocabularyLocalService.addDefaultVocabulary(groupId);
218
219 vocabularies.add(vocabulary);
220
221 return vocabularies;
222 }
223
224 @Override
225 public List<AssetVocabulary> getGroupVocabularies(
226 long groupId, boolean createDefaultVocabulary, int start, int end,
227 OrderByComparator<AssetVocabulary> obc)
228 throws PortalException {
229
230 List<AssetVocabulary> vocabularies = getGroupVocabularies(
231 groupId, start, end, obc);
232
233 if (!vocabularies.isEmpty() || !createDefaultVocabulary) {
234 return vocabularies;
235 }
236
237 int count = assetVocabularyLocalService.getGroupVocabulariesCount(
238 new long[] {groupId});
239
240 if (count> 0) {
241 return vocabularies;
242 }
243
244 vocabularies = new ArrayList<>();
245
246 AssetVocabulary vocabulary =
247 assetVocabularyLocalService.addDefaultVocabulary(groupId);
248
249 vocabularies.add(vocabulary);
250
251 return vocabularies;
252 }
253
254 @Override
255 public List<AssetVocabulary> getGroupVocabularies(
256 long groupId, int start, int end,
257 OrderByComparator<AssetVocabulary> obc) {
258
259 return assetVocabularyPersistence.filterFindByGroupId(
260 groupId, start, end, obc);
261 }
262
263 @Override
264 public List<AssetVocabulary> getGroupVocabularies(
265 long groupId, String name, int start, int end,
266 OrderByComparator<AssetVocabulary> obc) {
267
268 return assetVocabularyPersistence.filterFindByG_LikeN(
269 groupId, name, start, end, obc);
270 }
271
272 @Override
273 public List<AssetVocabulary> getGroupVocabularies(long[] groupIds) {
274 return assetVocabularyPersistence.filterFindByGroupId(groupIds);
275 }
276
277 @Override
278 public int getGroupVocabulariesCount(long groupId) {
279 return assetVocabularyPersistence.filterCountByGroupId(groupId);
280 }
281
282 @Override
283 public int getGroupVocabulariesCount(long groupId, String name) {
284 return assetVocabularyPersistence.filterCountByG_LikeN(groupId, name);
285 }
286
287 @Override
288 public int getGroupVocabulariesCount(long[] groupIds) {
289 return assetVocabularyPersistence.filterCountByGroupId(groupIds);
290 }
291
292 @Override
293 public AssetVocabularyDisplay getGroupVocabulariesDisplay(
294 long groupId, String name, int start, int end,
295 boolean addDefaultVocabulary,
296 OrderByComparator<AssetVocabulary> obc)
297 throws PortalException {
298
299 List<AssetVocabulary> vocabularies;
300 int total = 0;
301
302 if (Validator.isNotNull(name)) {
303 name = (CustomSQLUtil.keywords(name))[0];
304
305 vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
306 total = getGroupVocabulariesCount(groupId, name);
307 }
308 else {
309 vocabularies = getGroupVocabularies(groupId, start, end, obc);
310 total = getGroupVocabulariesCount(groupId);
311 }
312
313 if (addDefaultVocabulary && (total == 0) &&
314 (assetVocabularyPersistence.countByGroupId(groupId) == 0)) {
315
316 vocabularies = new ArrayList<>();
317
318 vocabularies.add(
319 assetVocabularyLocalService.addDefaultVocabulary(groupId));
320
321 total = 1;
322 }
323
324 return new AssetVocabularyDisplay(vocabularies, total, start, end);
325 }
326
327 @Override
328 public AssetVocabularyDisplay getGroupVocabulariesDisplay(
329 long groupId, String name, int start, int end,
330 OrderByComparator<AssetVocabulary> obc)
331 throws PortalException {
332
333 return getGroupVocabulariesDisplay(
334 groupId, name, start, end, false, obc);
335 }
336
337
340 @Deprecated
341 @Override
342 public JSONObject getJSONGroupVocabularies(
343 long groupId, String name, int start, int end,
344 OrderByComparator<AssetVocabulary> obc)
345 throws PortalException {
346
347 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
348
349 int page = end / (end - start);
350
351 jsonObject.put("page", page);
352
353 List<AssetVocabulary> vocabularies;
354 int total = 0;
355
356 if (Validator.isNotNull(name)) {
357 name = (CustomSQLUtil.keywords(name))[0];
358
359 vocabularies = getGroupVocabularies(groupId, name, start, end, obc);
360 total = getGroupVocabulariesCount(groupId, name);
361 }
362 else {
363 vocabularies = getGroupVocabularies(groupId, start, end, obc);
364 total = getGroupVocabulariesCount(groupId);
365 }
366
367 String vocabulariesJSON = JSONFactoryUtil.looseSerialize(vocabularies);
368
369 JSONArray vocabulariesJSONArray = JSONFactoryUtil.createJSONArray(
370 vocabulariesJSON);
371
372 jsonObject.put("vocabularies", vocabulariesJSONArray);
373
374 jsonObject.put("total", total);
375
376 return jsonObject;
377 }
378
379
383 @Deprecated
384 @Override
385 public List<AssetVocabulary> getVocabularies(long[] vocabularyIds)
386 throws PortalException {
387
388 return filterVocabularies(
389 assetVocabularyLocalService.getVocabularies(vocabularyIds));
390 }
391
392 @Override
393 public AssetVocabulary getVocabulary(long vocabularyId)
394 throws PortalException {
395
396 AssetVocabularyPermission.check(
397 getPermissionChecker(), vocabularyId, ActionKeys.VIEW);
398
399 return assetVocabularyLocalService.getVocabulary(vocabularyId);
400 }
401
402 @Override
403 public AssetVocabularyDisplay searchVocabulariesDisplay(
404 long groupId, String title, int start, int end,
405 boolean addDefaultVocabulary)
406 throws PortalException {
407
408 User user = getUser();
409
410 BaseModelSearchResult<AssetVocabulary> baseModelSearchResult =
411 assetVocabularyLocalService.searchVocabularies(
412 user.getCompanyId(), groupId, title, start, end);
413
414 List<AssetVocabulary> vocabularies =
415 baseModelSearchResult.getBaseModels();
416 int total = baseModelSearchResult.getLength();
417
418 if (addDefaultVocabulary && (total == 0)) {
419 total = assetVocabularyPersistence.countByGroupId(groupId);
420
421 if (total == 0) {
422 vocabularies = new ArrayList<>(1);
423
424 AssetVocabulary defaultVocabulary =
425 assetVocabularyLocalService.addDefaultVocabulary(groupId);
426
427 vocabularies.add(defaultVocabulary);
428
429 total = 1;
430 }
431 }
432
433 return new AssetVocabularyDisplay(vocabularies, total, start, end);
434 }
435
436 @Override
437 public AssetVocabulary updateVocabulary(
438 long vocabularyId, String title, Map<Locale, String> titleMap,
439 Map<Locale, String> descriptionMap, String settings,
440 ServiceContext serviceContext)
441 throws PortalException {
442
443 AssetVocabularyPermission.check(
444 getPermissionChecker(), vocabularyId, ActionKeys.UPDATE);
445
446 return assetVocabularyLocalService.updateVocabulary(
447 vocabularyId, title, titleMap, descriptionMap, settings,
448 serviceContext);
449 }
450
451
454 @Deprecated
455 protected List<AssetVocabulary> filterVocabularies(
456 List<AssetVocabulary> vocabularies)
457 throws PortalException {
458
459 PermissionChecker permissionChecker = getPermissionChecker();
460
461 vocabularies = ListUtil.copy(vocabularies);
462
463 Iterator<AssetVocabulary> itr = vocabularies.iterator();
464
465 while (itr.hasNext()) {
466 AssetVocabulary vocabulary = itr.next();
467
468 if (!AssetVocabularyPermission.contains(
469 permissionChecker, vocabulary, ActionKeys.VIEW)) {
470
471 itr.remove();
472 }
473 }
474
475 return vocabularies;
476 }
477
478 }