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