1
14
15 package com.liferay.portlet.asset.service.impl;
16
17 import com.liferay.portal.NoSuchGroupException;
18 import com.liferay.portal.kernel.dao.orm.QueryUtil;
19 import com.liferay.portal.kernel.exception.PortalException;
20 import com.liferay.portal.kernel.exception.SystemException;
21 import com.liferay.portal.kernel.log.Log;
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
23 import com.liferay.portal.kernel.search.BooleanClauseOccur;
24 import com.liferay.portal.kernel.search.BooleanQuery;
25 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
26 import com.liferay.portal.kernel.search.Document;
27 import com.liferay.portal.kernel.search.Field;
28 import com.liferay.portal.kernel.search.Hits;
29 import com.liferay.portal.kernel.search.SearchEngineUtil;
30 import com.liferay.portal.kernel.search.TermQuery;
31 import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
32 import com.liferay.portal.kernel.util.GetterUtil;
33 import com.liferay.portal.kernel.util.InstancePool;
34 import com.liferay.portal.kernel.util.ListUtil;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.kernel.util.Validator;
38 import com.liferay.portal.model.User;
39 import com.liferay.portal.service.ServiceContext;
40 import com.liferay.portal.util.PortalUtil;
41 import com.liferay.portal.util.PortletKeys;
42 import com.liferay.portal.util.PropsValues;
43 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
44 import com.liferay.portlet.asset.NoSuchTagException;
45 import com.liferay.portlet.asset.model.AssetCategory;
46 import com.liferay.portlet.asset.model.AssetEntry;
47 import com.liferay.portlet.asset.model.AssetEntryDisplay;
48 import com.liferay.portlet.asset.model.AssetRendererFactory;
49 import com.liferay.portlet.asset.model.AssetTag;
50 import com.liferay.portlet.asset.service.base.AssetEntryLocalServiceBaseImpl;
51 import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
52 import com.liferay.portlet.asset.util.AssetEntryValidator;
53 import com.liferay.portlet.blogs.model.BlogsEntry;
54 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
55 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
56 import com.liferay.portlet.documentlibrary.model.DLFolder;
57 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
58 import com.liferay.portlet.imagegallery.model.IGImage;
59 import com.liferay.portlet.journal.model.JournalArticle;
60 import com.liferay.portlet.messageboards.model.MBMessage;
61 import com.liferay.portlet.wiki.model.WikiPage;
62
63 import java.util.ArrayList;
64 import java.util.Date;
65 import java.util.List;
66
67
73 public class AssetEntryLocalServiceImpl extends AssetEntryLocalServiceBaseImpl {
74
75 public void deleteEntry(AssetEntry entry) throws SystemException {
76 assetEntryPersistence.remove(entry);
77 }
78
79 public void deleteEntry(long entryId)
80 throws PortalException, SystemException {
81
82 AssetEntry entry = assetEntryPersistence.findByPrimaryKey(entryId);
83
84 deleteEntry(entry);
85 }
86
87 public void deleteEntry(String className, long classPK)
88 throws SystemException {
89
90 long classNameId = PortalUtil.getClassNameId(className);
91
92 AssetEntry entry = assetEntryPersistence.fetchByC_C(
93 classNameId, classPK);
94
95 if (entry != null) {
96 deleteEntry(entry);
97 }
98 }
99
100 public List<AssetEntry> getCompanyEntries(
101 long companyId, int start, int end)
102 throws SystemException {
103
104 return assetEntryPersistence.findByCompanyId(companyId, start, end);
105 }
106
107 public int getCompanyEntriesCount(long companyId) throws SystemException {
108 return assetEntryPersistence.countByCompanyId(companyId);
109 }
110
111 public AssetEntryDisplay[] getCompanyEntryDisplays(
112 long companyId, int start, int end, String languageId)
113 throws SystemException {
114
115 return getEntryDisplays(
116 getCompanyEntries(companyId, start, end), languageId);
117 }
118
119 public List<AssetEntry> getEntries(AssetEntryQuery entryQuery)
120 throws SystemException {
121
122 return assetEntryFinder.findEntries(entryQuery);
123 }
124
125 public int getEntriesCount(AssetEntryQuery entryQuery)
126 throws SystemException {
127
128 return assetEntryFinder.countEntries(entryQuery);
129 }
130
131 public AssetEntry getEntry(long entryId)
132 throws PortalException, SystemException {
133
134 return assetEntryPersistence.findByPrimaryKey(entryId);
135 }
136
137 public AssetEntry getEntry(String className, long classPK)
138 throws PortalException, SystemException {
139
140 long classNameId = PortalUtil.getClassNameId(className);
141
142 return assetEntryPersistence.findByC_C(classNameId, classPK);
143 }
144
145 public List<AssetEntry> getTopViewedEntries(
146 String className, boolean asc, int start, int end)
147 throws SystemException {
148
149 return getTopViewedEntries(new String[] {className}, asc, start, end);
150 }
151
152 public List<AssetEntry> getTopViewedEntries(
153 String[] className, boolean asc, int start, int end)
154 throws SystemException {
155
156 long[] classNameIds = new long[className.length];
157
158 for (int i = 0; i < className.length; i++) {
159 classNameIds[i] = PortalUtil.getClassNameId(className[i]);
160 }
161
162 AssetEntryQuery entryQuery = new AssetEntryQuery();
163
164 entryQuery.setClassNameIds(classNameIds);
165 entryQuery.setEnd(end);
166 entryQuery.setExcludeZeroViewCount(true);
167 entryQuery.setOrderByCol1("viewCount");
168 entryQuery.setOrderByType1(asc ? "ASC" : "DESC");
169 entryQuery.setStart(start);
170
171 return assetEntryFinder.findEntries(entryQuery);
172 }
173
174 public AssetEntry incrementViewCounter(String className, long classPK)
175 throws SystemException {
176
177 if (classPK <= 0) {
178 return null;
179 }
180
181 long classNameId = PortalUtil.getClassNameId(className);
182
183 AssetEntry entry = assetEntryPersistence.fetchByC_C(
184 classNameId, classPK);
185
186 if (entry != null) {
187 entry.setViewCount(entry.getViewCount() + 1);
188
189 assetEntryPersistence.update(entry, false);
190 }
191
192 return entry;
193 }
194
195 public Hits search(
196 long companyId, String portletId, String keywords, int start,
197 int end)
198 throws SystemException {
199
200 try {
201 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
202
203 if (Validator.isNotNull(portletId)) {
204 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
205 }
206 else {
207 BooleanQuery portletIdsQuery = BooleanQueryFactoryUtil.create();
208
209 List<AssetRendererFactory> rendererFactories =
210 AssetRendererFactoryRegistryUtil.
211 getAssetRendererFactories();
212
213 for (AssetRendererFactory rendererFactory : rendererFactories) {
214 TermQuery termQuery = TermQueryFactoryUtil.create(
215 Field.PORTLET_ID, rendererFactory.getPortletId());
216
217 portletIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
218 }
219
220 contextQuery.add(portletIdsQuery, BooleanClauseOccur.MUST);
221 }
222
223 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
224
225 if (Validator.isNotNull(keywords)) {
226 searchQuery.addTerm(Field.TITLE, keywords);
227 searchQuery.addTerm(Field.CONTENT, keywords);
228 searchQuery.addTerm(Field.DESCRIPTION, keywords);
229 searchQuery.addTerm(Field.PROPERTIES, keywords);
230 searchQuery.addTerm(Field.ASSET_TAG_NAMES, keywords, true);
231 }
232
233 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
234
235 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
236
237 if (searchQuery.clauses().size() > 0) {
238 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
239 }
240
241 return SearchEngineUtil.search(companyId, fullQuery, start, end);
242 }
243 catch (Exception e) {
244 throw new SystemException(e);
245 }
246 }
247
248 public AssetEntryDisplay[] searchEntryDisplays(
249 long companyId, String portletId, String keywords,
250 String languageId, int start, int end)
251 throws SystemException {
252
253 List<AssetEntry> entries = new ArrayList<AssetEntry>();
254
255 Hits hits = search(companyId, portletId, keywords, start, end);
256
257 List<Document> hitsList = hits.toList();
258
259 for (Document doc : hitsList) {
260 try {
261 AssetEntry entry = getEntry(doc);
262
263 if (entry != null) {
264 entries.add(entry);
265 }
266 }
267 catch (Exception e) {
268 if (_log.isWarnEnabled()) {
269 _log.warn(e);
270 }
271 }
272 }
273
274 return getEntryDisplays(entries, languageId);
275 }
276
277 public int searchEntryDisplaysCount(
278 long companyId, String portletId, String keywords,
279 String languageId)
280 throws SystemException {
281
282 Hits hits = search(
283 companyId, portletId, keywords, QueryUtil.ALL_POS,
284 QueryUtil.ALL_POS);
285
286 return hits.getLength();
287 }
288
289 public AssetEntry updateEntry(
290 long userId, long groupId, String className, long classPK,
291 long[] categoryIds, String[] tagNames)
292 throws PortalException, SystemException {
293
294 return updateEntry(
295 userId, groupId, className, classPK, categoryIds, tagNames,
296 true, null, null, null, null, null, null, null, null, null, 0, 0,
297 null, false);
298 }
299
300 public AssetEntry updateEntry(
301 long userId, long groupId, String className, long classPK,
302 long[] categoryIds, String[] tagNames, boolean visible,
303 Date startDate, Date endDate, Date publishDate, Date expirationDate,
304 String mimeType, String title, String description, String summary,
305 String url, int height, int width, Integer priority, boolean sync)
306 throws PortalException, SystemException {
307
308
310 User user = userPersistence.findByPrimaryKey(userId);
311 long classNameId = PortalUtil.getClassNameId(className);
312
313 title = StringUtil.shorten(title, 300, StringPool.BLANK);
314 Date now = new Date();
315
316 validate(className, categoryIds, tagNames);
317
318 AssetEntry entry = assetEntryPersistence.fetchByC_C(
319 classNameId, classPK);
320
321 if (entry == null) {
322 long entryId = counterLocalService.increment();
323
324 entry = assetEntryPersistence.create(entryId);
325
326 entry.setCompanyId(user.getCompanyId());
327 entry.setUserId(user.getUserId());
328 entry.setUserName(user.getFullName());
329 entry.setCreateDate(now);
330 entry.setClassNameId(classNameId);
331 entry.setClassPK(classPK);
332 entry.setVisible(visible);
333 entry.setPublishDate(publishDate);
334 entry.setExpirationDate(expirationDate);
335
336 if (priority == null) {
337 entry.setPriority(0);
338 }
339
340 entry.setViewCount(0);
341 }
342
343 entry.setGroupId(groupId);
344 entry.setModifiedDate(now);
345 entry.setVisible(visible);
346 entry.setStartDate(startDate);
347 entry.setEndDate(endDate);
348 entry.setPublishDate(publishDate);
349 entry.setExpirationDate(expirationDate);
350 entry.setMimeType(mimeType);
351 entry.setTitle(title);
352 entry.setDescription(description);
353 entry.setSummary(summary);
354 entry.setUrl(url);
355 entry.setHeight(height);
356 entry.setWidth(width);
357
358 if (priority != null) {
359 entry.setPriority(priority.intValue());
360 }
361
362
364 if (categoryIds != null) {
365 assetEntryPersistence.setAssetCategories(
366 entry.getEntryId(), categoryIds);
367 }
368
369
371 if (tagNames != null) {
372 long parentGroupId = PortalUtil.getParentGroupId(groupId);
373
374 List<AssetTag> tags = new ArrayList<AssetTag>(tagNames.length);
375
376 for (String tagName : tagNames) {
377 AssetTag tag = null;
378
379 try {
380 tag = assetTagLocalService.getTag(parentGroupId, tagName);
381 }
382 catch (NoSuchTagException nste) {
383 ServiceContext serviceContext = new ServiceContext();
384
385 serviceContext.setAddCommunityPermissions(true);
386 serviceContext.setAddGuestPermissions(true);
387 serviceContext.setScopeGroupId(parentGroupId);
388
389 tag = assetTagLocalService.addTag(
390 user.getUserId(), tagName,
391 PropsValues.ASSET_TAG_PROPERTIES_DEFAULT,
392 serviceContext);
393 }
394
395 if (tag != null) {
396 tags.add(tag);
397 }
398 }
399
400 List<AssetTag> oldTags = assetEntryPersistence.getAssetTags(
401 entry.getEntryId());
402
403 assetEntryPersistence.setAssetTags(entry.getEntryId(), tags);
404
405 if (entry.isNew()) {
406 for (AssetTag tag : tags) {
407 assetTagLocalService.incrementAssetCount(
408 tag.getTagId(), classNameId);
409 }
410 }
411 else {
412 for (AssetTag oldTag : oldTags) {
413 if (!tags.contains(oldTag)) {
414 assetTagLocalService.decrementAssetCount(
415 oldTag.getTagId(), classNameId);
416 }
417 }
418
419 for (AssetTag tag : tags) {
420 if (!oldTags.contains(tag)) {
421 assetTagLocalService.incrementAssetCount(
422 tag.getTagId(), classNameId);
423 }
424 }
425 }
426 }
427
428
431 assetEntryPersistence.update(entry, false);
432
433
435 if (!sync) {
436 return entry;
437 }
438
439 if (className.equals(BlogsEntry.class.getName())) {
440 BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
441 classPK);
442
443 blogsEntry.setTitle(title);
444
445 blogsEntryPersistence.update(blogsEntry, false);
446 }
447 else if (className.equals(BookmarksEntry.class.getName())) {
448 BookmarksEntry bookmarksEntry =
449 bookmarksEntryPersistence.findByPrimaryKey(classPK);
450
451 bookmarksEntry.setName(title);
452 bookmarksEntry.setComments(description);
453 bookmarksEntry.setUrl(url);
454
455 bookmarksEntryPersistence.update(bookmarksEntry, false);
456 }
457 else if (className.equals(DLFileEntry.class.getName())) {
458 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
459 classPK);
460
461 dlFileEntry.setTitle(title);
462 dlFileEntry.setDescription(description);
463
464 dlFileEntryPersistence.update(dlFileEntry, false);
465 }
466 else if (className.equals(JournalArticle.class.getName())) {
467 JournalArticle journalArticle =
468 journalArticlePersistence.findByPrimaryKey(classPK);
469
470 journalArticle.setTitle(title);
471 journalArticle.setDescription(description);
472
473 journalArticlePersistence.update(journalArticle, false);
474 }
475 else if (className.equals(MBMessage.class.getName())) {
476 MBMessage mbMessage = mbMessagePersistence.findByPrimaryKey(
477 classPK);
478
479 mbMessage.setSubject(title);
480
481 mbMessagePersistence.update(mbMessage, false);
482 }
483 else if (className.equals(WikiPage.class.getName())) {
484 WikiPage wikiPage = wikiPagePersistence.findByPrimaryKey(classPK);
485
486 wikiPage.setTitle(title);
487
488 wikiPagePersistence.update(wikiPage, false);
489 }
490
491 return entry;
492 }
493
494 public AssetEntry updateVisible(
495 String className, long classPK, boolean visible)
496 throws PortalException, SystemException {
497
498 long classNameId = PortalUtil.getClassNameId(className);
499
500 AssetEntry entry = assetEntryPersistence.findByC_C(
501 classNameId, classPK);
502
503 entry.setVisible(visible);
504
505 assetEntryPersistence.update(entry, false);
506
507 return entry;
508 }
509
510 public void validate(
511 String className, long[] categoryIds, String[] tagNames)
512 throws PortalException {
513
514 AssetEntryValidator validator = (AssetEntryValidator)InstancePool.get(
515 PropsValues.ASSET_ENTRY_VALIDATOR);
516
517 validator.validate(className, categoryIds, tagNames);
518 }
519
520 protected AssetEntry getEntry(Document doc)
521 throws PortalException, SystemException {
522
523 String portletId = GetterUtil.getString(doc.get(Field.PORTLET_ID));
524
525 if (portletId.equals(PortletKeys.BLOGS)) {
526 long entryId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
527
528 long classNameId = PortalUtil.getClassNameId(
529 BlogsEntry.class.getName());
530 long classPK = entryId;
531
532 return assetEntryPersistence.findByC_C(classNameId, classPK);
533 }
534 else if (portletId.equals(PortletKeys.BOOKMARKS)) {
535 long entryId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
536
537 long classNameId = PortalUtil.getClassNameId(
538 BookmarksEntry.class.getName());
539 long classPK = entryId;
540
541 return assetEntryPersistence.findByC_C(classNameId, classPK);
542 }
543 else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)) {
544 long repositoryId = GetterUtil.getLong(doc.get("repositoryId"));
545 String name = doc.get("path");
546
547 long groupId = 0;
548 long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
549
550 try {
551 groupPersistence.findByPrimaryKey(repositoryId);
552
553 groupId = repositoryId;
554 }
555 catch (NoSuchGroupException nsge) {
556 DLFolder folder = dlFolderPersistence.findByPrimaryKey(
557 repositoryId);
558
559 groupId = folder.getGroupId();
560 folderId = folder.getFolderId();
561 }
562
563 DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
564 groupId, folderId, name);
565
566 long classNameId = PortalUtil.getClassNameId(
567 DLFileEntry.class.getName());
568 long classPK = fileEntry.getFileEntryId();
569
570 return assetEntryPersistence.findByC_C(classNameId, classPK);
571 }
572 else if (portletId.equals(PortletKeys.IMAGE_GALLERY)) {
573 long imageId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
574
575 long classNameId = PortalUtil.getClassNameId(
576 IGImage.class.getName());
577 long classPK = imageId;
578
579 return assetEntryPersistence.findByC_C(classNameId, classPK);
580 }
581 else if (portletId.equals(PortletKeys.JOURNAL)) {
582 long groupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));
583 String articleId = doc.get(Field.ENTRY_CLASS_PK);
584
586 long articleResourcePrimKey =
587 journalArticleResourceLocalService.getArticleResourcePrimKey(
588 groupId, articleId);
589
590 long classNameId = PortalUtil.getClassNameId(
591 JournalArticle.class.getName());
592 long classPK = articleResourcePrimKey;
593
594 return assetEntryPersistence.findByC_C(classNameId, classPK);
595 }
596 else if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
597 long messageId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
598
599 long classNameId = PortalUtil.getClassNameId(
600 MBMessage.class.getName());
601 long classPK = messageId;
602
603 return assetEntryPersistence.findByC_C(classNameId, classPK);
604 }
605 else if (portletId.equals(PortletKeys.WIKI)) {
606 long nodeId = GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK));
607 String title = doc.get(Field.TITLE);
608
609 long pageResourcePrimKey =
610 wikiPageResourceLocalService.getPageResourcePrimKey(
611 nodeId, title);
612
613 long classNameId = PortalUtil.getClassNameId(
614 WikiPage.class.getName());
615 long classPK = pageResourcePrimKey;
616
617 return assetEntryPersistence.findByC_C(classNameId, classPK);
618 }
619
620 return null;
621 }
622
623 protected AssetEntryDisplay[] getEntryDisplays(
624 List<AssetEntry> entries, String languageId)
625 throws SystemException {
626
627 AssetEntryDisplay[] entryDisplays =
628 new AssetEntryDisplay[entries.size()];
629
630 for (int i = 0; i < entries.size(); i++) {
631 AssetEntry entry = entries.get(i);
632
633 String className = PortalUtil.getClassName(entry.getClassNameId());
634 String portletId = PortalUtil.getClassNamePortletId(className);
635 String portletTitle = PortalUtil.getPortletTitle(
636 portletId, languageId);
637
638 List<AssetCategory> categories =
639 assetEntryPersistence.getAssetCategories(entry.getEntryId());
640
641 String categoryIdsString = ListUtil.toString(
642 categories, "assetCategoryId", StringPool.COMMA);
643 long[] categoryIds = StringUtil.split(
644 categoryIdsString, StringPool.COMMA, 0L);
645
646 List<AssetTag> tags = assetEntryPersistence.getAssetTags(
647 entry.getEntryId());
648
649 String tagNames = ListUtil.toString(tags, "name", ", ");
650
651 AssetEntryDisplay entryDisplay = new AssetEntryDisplay();
652
653 entryDisplay.setEntryId(entry.getEntryId());
654 entryDisplay.setCompanyId(entry.getCompanyId());
655 entryDisplay.setUserId(entry.getUserId());
656 entryDisplay.setUserName(entry.getUserName());
657 entryDisplay.setCreateDate(entry.getCreateDate());
658 entryDisplay.setModifiedDate(entry.getModifiedDate());
659 entryDisplay.setClassNameId(entry.getClassNameId());
660 entryDisplay.setClassName(className);
661 entryDisplay.setClassPK(entry.getClassPK());
662 entryDisplay.setPortletId(portletId);
663 entryDisplay.setPortletTitle(portletTitle);
664 entryDisplay.setStartDate(entry.getStartDate());
665 entryDisplay.setEndDate(entry.getEndDate());
666 entryDisplay.setPublishDate(entry.getPublishDate());
667 entryDisplay.setExpirationDate(entry.getExpirationDate());
668 entryDisplay.setMimeType(entry.getMimeType());
669 entryDisplay.setTitle(entry.getTitle());
670 entryDisplay.setDescription(entry.getDescription());
671 entryDisplay.setSummary(entry.getSummary());
672 entryDisplay.setUrl(entry.getUrl());
673 entryDisplay.setHeight(entry.getHeight());
674 entryDisplay.setWidth(entry.getWidth());
675 entryDisplay.setPriority(entry.getPriority());
676 entryDisplay.setViewCount(entry.getViewCount());
677 entryDisplay.setCategoryIds(categoryIds);
678 entryDisplay.setTagNames(tagNames);
679
680 entryDisplays[i] = entryDisplay;
681 }
682
683 return entryDisplays;
684 }
685
686 private static Log _log = LogFactoryUtil.getLog(
687 AssetEntryLocalServiceImpl.class);
688
689 }