001
014
015 package com.liferay.portlet.bookmarks.service.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.search.Indexable;
023 import com.liferay.portal.kernel.search.IndexableType;
024 import com.liferay.portal.kernel.search.Indexer;
025 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
026 import com.liferay.portal.kernel.systemevent.SystemEvent;
027 import com.liferay.portal.kernel.util.ContentTypes;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.kernel.workflow.WorkflowConstants;
030 import com.liferay.portal.model.ResourceConstants;
031 import com.liferay.portal.model.SystemEventConstants;
032 import com.liferay.portal.model.User;
033 import com.liferay.portal.service.ServiceContext;
034 import com.liferay.portlet.asset.model.AssetEntry;
035 import com.liferay.portlet.asset.model.AssetLinkConstants;
036 import com.liferay.portlet.bookmarks.FolderNameException;
037 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
038 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
039 import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
040 import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
041 import com.liferay.portlet.social.model.SocialActivityConstants;
042 import com.liferay.portlet.trash.model.TrashEntry;
043
044 import java.util.ArrayList;
045 import java.util.Date;
046 import java.util.List;
047
048
052 public class BookmarksFolderLocalServiceImpl
053 extends BookmarksFolderLocalServiceBaseImpl {
054
055 @Override
056 public BookmarksFolder addFolder(
057 long userId, long parentFolderId, String name, String description,
058 ServiceContext serviceContext)
059 throws PortalException, SystemException {
060
061
062
063 User user = userPersistence.findByPrimaryKey(userId);
064 long groupId = serviceContext.getScopeGroupId();
065 parentFolderId = getParentFolderId(groupId, parentFolderId);
066 Date now = new Date();
067
068 validate(name);
069
070 long folderId = counterLocalService.increment();
071
072 BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
073
074 folder.setUuid(serviceContext.getUuid());
075 folder.setGroupId(groupId);
076 folder.setCompanyId(user.getCompanyId());
077 folder.setUserId(user.getUserId());
078 folder.setUserName(user.getFullName());
079 folder.setCreateDate(serviceContext.getCreateDate(now));
080 folder.setModifiedDate(serviceContext.getModifiedDate(now));
081 folder.setParentFolderId(parentFolderId);
082 folder.setName(name);
083 folder.setDescription(description);
084 folder.setExpandoBridgeAttributes(serviceContext);
085
086 bookmarksFolderPersistence.update(folder);
087
088
089
090 resourceLocalService.addModelResources(folder, serviceContext);
091
092
093
094 updateAsset(
095 userId, folder, serviceContext.getAssetCategoryIds(),
096 serviceContext.getAssetTagNames(),
097 serviceContext.getAssetLinkEntryIds());
098
099 return folder;
100 }
101
102 @Indexable(type = IndexableType.DELETE)
103 @Override
104 @SystemEvent(
105 action = SystemEventConstants.ACTION_SKIP, send = false,
106 type = SystemEventConstants.TYPE_DELETE)
107 public BookmarksFolder deleteFolder(BookmarksFolder folder)
108 throws PortalException, SystemException {
109
110 return deleteFolder(folder, true);
111 }
112
113 @Indexable(type = IndexableType.DELETE)
114 @Override
115 @SystemEvent(
116 action = SystemEventConstants.ACTION_SKIP, send = false,
117 type = SystemEventConstants.TYPE_DELETE)
118 public BookmarksFolder deleteFolder(
119 BookmarksFolder folder, boolean includeTrashedEntries)
120 throws PortalException, SystemException {
121
122 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P_S(
123 folder.getGroupId(), folder.getFolderId(),
124 WorkflowConstants.STATUS_ANY);
125
126 for (BookmarksFolder curFolder : folders) {
127 if (includeTrashedEntries || !curFolder.isInTrash()) {
128 deleteFolder(curFolder);
129 }
130 }
131
132
133
134 bookmarksFolderPersistence.remove(folder);
135
136
137
138 resourceLocalService.deleteResource(
139 folder, ResourceConstants.SCOPE_INDIVIDUAL);
140
141
142
143 bookmarksEntryLocalService.deleteEntries(
144 folder.getGroupId(), folder.getFolderId(), includeTrashedEntries);
145
146
147
148 assetEntryLocalService.deleteEntry(
149 BookmarksFolder.class.getName(), folder.getFolderId());
150
151
152
153 expandoRowLocalService.deleteRows(folder.getFolderId());
154
155
156
157 subscriptionLocalService.deleteSubscriptions(
158 folder.getCompanyId(), BookmarksFolder.class.getName(),
159 folder.getFolderId());
160
161
162
163 trashEntryLocalService.deleteEntry(
164 BookmarksFolder.class.getName(), folder.getFolderId());
165
166 return folder;
167 }
168
169 @Indexable(type = IndexableType.DELETE)
170 @Override
171 public BookmarksFolder deleteFolder(long folderId)
172 throws PortalException, SystemException {
173
174 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
175 folderId);
176
177 return bookmarksFolderLocalService.deleteFolder(folder);
178 }
179
180 @Indexable(type = IndexableType.DELETE)
181 @Override
182 public BookmarksFolder deleteFolder(
183 long folderId, boolean includeTrashedEntries)
184 throws PortalException, SystemException {
185
186 BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
187 folderId);
188
189 return bookmarksFolderLocalService.deleteFolder(
190 folder, includeTrashedEntries);
191 }
192
193 @Override
194 public void deleteFolders(long groupId)
195 throws PortalException, SystemException {
196
197 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
198 groupId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
199
200 for (BookmarksFolder folder : folders) {
201 bookmarksFolderLocalService.deleteFolder(folder);
202 }
203 }
204
205 @Override
206 public List<BookmarksFolder> getCompanyFolders(
207 long companyId, int start, int end)
208 throws SystemException {
209
210 return bookmarksFolderPersistence.findByCompanyId(
211 companyId, start, end);
212 }
213
214 @Override
215 public int getCompanyFoldersCount(long companyId) throws SystemException {
216 return bookmarksFolderPersistence.countByCompanyId(companyId);
217 }
218
219 @Override
220 public BookmarksFolder getFolder(long folderId)
221 throws PortalException, SystemException {
222
223 return bookmarksFolderPersistence.findByPrimaryKey(folderId);
224 }
225
226 @Override
227 public List<BookmarksFolder> getFolders(long groupId)
228 throws SystemException {
229
230 return bookmarksFolderPersistence.findByGroupId(groupId);
231 }
232
233 @Override
234 public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
235 throws SystemException {
236
237 return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
238 }
239
240 @Override
241 public List<BookmarksFolder> getFolders(
242 long groupId, long parentFolderId, int start, int end)
243 throws SystemException {
244
245 return getFolders(
246 groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
247 end);
248 }
249
250 @Override
251 public List<BookmarksFolder> getFolders(
252 long groupId, long parentFolderId, int status, int start, int end)
253 throws SystemException {
254
255 return bookmarksFolderPersistence.findByG_P_S(
256 groupId, parentFolderId, status, start, end);
257 }
258
259 @Override
260 public List<Object> getFoldersAndEntries(long groupId, long folderId)
261 throws SystemException {
262
263 return getFoldersAndEntries(
264 groupId, folderId, WorkflowConstants.STATUS_ANY);
265 }
266
267 @Override
268 public List<Object> getFoldersAndEntries(
269 long groupId, long folderId, int status)
270 throws SystemException {
271
272 QueryDefinition queryDefinition = new QueryDefinition(status);
273
274 return bookmarksFolderFinder.findF_E_ByG_F(
275 groupId, folderId, queryDefinition);
276 }
277
278 @Override
279 public List<Object> getFoldersAndEntries(
280 long groupId, long folderId, int status, int start, int end)
281 throws SystemException {
282
283 QueryDefinition queryDefinition = new QueryDefinition(
284 status, start, end, null);
285
286 return bookmarksFolderFinder.findF_E_ByG_F(
287 groupId, folderId, queryDefinition);
288 }
289
290 @Override
291 public int getFoldersAndEntriesCount(
292 long groupId, long folderId, int status)
293 throws SystemException {
294
295 QueryDefinition queryDefinition = new QueryDefinition(status);
296
297 return bookmarksFolderFinder.countF_E_ByG_F(
298 groupId, folderId, queryDefinition);
299 }
300
301 @Override
302 public int getFoldersCount(long groupId, long parentFolderId)
303 throws SystemException {
304
305 return getFoldersCount(
306 groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
307 }
308
309 @Override
310 public int getFoldersCount(long groupId, long parentFolderId, int status)
311 throws SystemException {
312
313 return bookmarksFolderPersistence.countByG_P_S(
314 groupId, parentFolderId, status);
315 }
316
317 @Override
318 public List<BookmarksFolder> getNoAssetFolders() throws SystemException {
319 return bookmarksFolderFinder.findByNoAssets();
320 }
321
322 @Override
323 public void getSubfolderIds(
324 List<Long> folderIds, long groupId, long folderId)
325 throws SystemException {
326
327 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
328 groupId, folderId);
329
330 for (BookmarksFolder folder : folders) {
331 folderIds.add(folder.getFolderId());
332
333 getSubfolderIds(
334 folderIds, folder.getGroupId(), folder.getFolderId());
335 }
336 }
337
338 @Indexable(type = IndexableType.REINDEX)
339 @Override
340 public BookmarksFolder moveFolder(long folderId, long parentFolderId)
341 throws PortalException, SystemException {
342
343 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
344 folderId);
345
346 folder.setParentFolderId(parentFolderId);
347
348 bookmarksFolderPersistence.update(folder);
349
350 return folder;
351 }
352
353 @Override
354 public BookmarksFolder moveFolderFromTrash(
355 long userId, long folderId, long parentFolderId)
356 throws PortalException, SystemException {
357
358 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
359 folderId);
360
361 if (folder.isInTrash()) {
362 restoreFolderFromTrash(userId, folderId);
363 }
364 else {
365 updateStatus(userId, folder, WorkflowConstants.STATUS_APPROVED);
366 }
367
368 return bookmarksFolderLocalService.moveFolder(folderId, parentFolderId);
369 }
370
371 @Indexable(type = IndexableType.REINDEX)
372 @Override
373 public BookmarksFolder moveFolderToTrash(long userId, long folderId)
374 throws PortalException, SystemException {
375
376
377
378 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
379 folderId);
380
381 updateStatus(userId, folder, WorkflowConstants.STATUS_IN_TRASH);
382
383
384
385 socialActivityCounterLocalService.enableActivityCounters(
386 BookmarksFolder.class.getName(), folder.getFolderId());
387
388 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
389
390 extraDataJSONObject.put("title", folder.getName());
391
392 socialActivityLocalService.addActivity(
393 userId, folder.getGroupId(), BookmarksFolder.class.getName(),
394 folder.getFolderId(), SocialActivityConstants.TYPE_MOVE_TO_TRASH,
395 extraDataJSONObject.toString(), 0);
396
397 return folder;
398 }
399
400 @Indexable(type = IndexableType.REINDEX)
401 @Override
402 public void restoreFolderFromTrash(long userId, long folderId)
403 throws PortalException, SystemException {
404
405
406
407 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
408 folderId);
409
410 TrashEntry trashEntry = trashEntryLocalService.getEntry(
411 BookmarksFolder.class.getName(), folderId);
412
413 updateStatus(userId, folder, trashEntry.getStatus());
414
415
416
417 socialActivityCounterLocalService.enableActivityCounters(
418 BookmarksFolder.class.getName(), folder.getFolderId());
419
420 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
421
422 extraDataJSONObject.put("title", folder.getName());
423
424 socialActivityLocalService.addActivity(
425 userId, folder.getGroupId(), BookmarksFolder.class.getName(),
426 folder.getFolderId(),
427 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
428 extraDataJSONObject.toString(), 0);
429 }
430
431 @Override
432 public void subscribeFolder(long userId, long groupId, long folderId)
433 throws PortalException, SystemException {
434
435 if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
436 folderId = groupId;
437 }
438
439 subscriptionLocalService.addSubscription(
440 userId, groupId, BookmarksFolder.class.getName(), folderId);
441 }
442
443 @Override
444 public void unsubscribeFolder(long userId, long groupId, long folderId)
445 throws PortalException, SystemException {
446
447 if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
448 folderId = groupId;
449 }
450
451 subscriptionLocalService.deleteSubscription(
452 userId, BookmarksFolder.class.getName(), folderId);
453 }
454
455 @Override
456 public void updateAsset(
457 long userId, BookmarksFolder folder, long[] assetCategoryIds,
458 String[] assetTagNames, long[] assetLinkEntryIds)
459 throws PortalException, SystemException {
460
461 AssetEntry assetEntry = assetEntryLocalService.updateEntry(
462 userId, folder.getGroupId(), folder.getCreateDate(),
463 folder.getModifiedDate(), BookmarksFolder.class.getName(),
464 folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
465 assetTagNames, true, null, null, null, ContentTypes.TEXT_PLAIN,
466 folder.getName(), folder.getDescription(), null, null, null, 0, 0,
467 null, false);
468
469 assetLinkLocalService.updateLinks(
470 userId, assetEntry.getEntryId(), assetLinkEntryIds,
471 AssetLinkConstants.TYPE_RELATED);
472 }
473
474 @Indexable(type = IndexableType.REINDEX)
475 @Override
476 public BookmarksFolder updateFolder(
477 long userId, long folderId, long parentFolderId, String name,
478 String description, boolean mergeWithParentFolder,
479 ServiceContext serviceContext)
480 throws PortalException, SystemException {
481
482
483
484 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
485 folderId);
486
487 parentFolderId = getParentFolderId(folder, parentFolderId);
488
489 if (mergeWithParentFolder && (folderId != parentFolderId)) {
490 mergeFolders(folder, parentFolderId);
491
492 return folder;
493 }
494
495
496
497 validate(name);
498
499 folder.setModifiedDate(serviceContext.getModifiedDate(null));
500 folder.setParentFolderId(parentFolderId);
501 folder.setName(name);
502 folder.setDescription(description);
503 folder.setExpandoBridgeAttributes(serviceContext);
504
505 bookmarksFolderPersistence.update(folder);
506
507
508
509 updateAsset(
510 userId, folder, serviceContext.getAssetCategoryIds(),
511 serviceContext.getAssetTagNames(),
512 serviceContext.getAssetLinkEntryIds());
513
514 return folder;
515 }
516
517 @Override
518 public BookmarksFolder updateStatus(
519 long userId, BookmarksFolder folder, int status)
520 throws PortalException, SystemException {
521
522
523
524 User user = userPersistence.findByPrimaryKey(userId);
525
526 int oldStatus = folder.getStatus();
527
528 folder.setStatus(status);
529 folder.setStatusByUserId(userId);
530 folder.setStatusByUserName(user.getFullName());
531 folder.setStatusDate(new Date());
532
533 bookmarksFolderPersistence.update(folder);
534
535
536
537 List<Object> foldersAndEntries =
538 bookmarksFolderLocalService.getFoldersAndEntries(
539 folder.getGroupId(), folder.getFolderId());
540
541 updateDependentStatus(foldersAndEntries, status);
542
543 if (status == WorkflowConstants.STATUS_APPROVED) {
544
545
546
547 assetEntryLocalService.updateVisible(
548 BookmarksFolder.class.getName(), folder.getFolderId(), true);
549
550
551
552 socialActivityCounterLocalService.enableActivityCounters(
553 BookmarksFolder.class.getName(), folder.getFolderId());
554 }
555 else if (status == WorkflowConstants.STATUS_IN_TRASH) {
556
557
558
559 assetEntryLocalService.updateVisible(
560 BookmarksFolder.class.getName(), folder.getFolderId(), false);
561
562
563
564 socialActivityCounterLocalService.disableActivityCounters(
565 BookmarksFolder.class.getName(), folder.getFolderId());
566 }
567
568
569
570 if (oldStatus == WorkflowConstants.STATUS_IN_TRASH) {
571 trashEntryLocalService.deleteEntry(
572 BookmarksFolder.class.getName(), folder.getFolderId());
573 }
574 else if (status == WorkflowConstants.STATUS_IN_TRASH) {
575 trashEntryLocalService.addTrashEntry(
576 userId, folder.getGroupId(), BookmarksFolder.class.getName(),
577 folder.getFolderId(), oldStatus, null, null);
578 }
579
580
581
582 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
583 BookmarksFolder.class);
584
585 indexer.reindex(folder);
586
587 return folder;
588 }
589
590 protected long getParentFolderId(
591 BookmarksFolder folder, long parentFolderId)
592 throws SystemException {
593
594 if (parentFolderId ==
595 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
596
597 return parentFolderId;
598 }
599
600 if (folder.getFolderId() == parentFolderId) {
601 return folder.getParentFolderId();
602 }
603 else {
604 BookmarksFolder parentFolder =
605 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
606
607 if ((parentFolder == null) ||
608 (folder.getGroupId() != parentFolder.getGroupId())) {
609
610 return folder.getParentFolderId();
611 }
612
613 List<Long> subfolderIds = new ArrayList<Long>();
614
615 getSubfolderIds(
616 subfolderIds, folder.getGroupId(), folder.getFolderId());
617
618 if (subfolderIds.contains(parentFolderId)) {
619 return folder.getParentFolderId();
620 }
621
622 return parentFolderId;
623 }
624 }
625
626 protected long getParentFolderId(long groupId, long parentFolderId)
627 throws SystemException {
628
629 if (parentFolderId !=
630 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
631
632 BookmarksFolder parentFolder =
633 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
634
635 if ((parentFolder == null) ||
636 (groupId != parentFolder.getGroupId())) {
637
638 parentFolderId =
639 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
640 }
641 }
642
643 return parentFolderId;
644 }
645
646 protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
647 throws PortalException, SystemException {
648
649 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
650 fromFolder.getGroupId(), fromFolder.getFolderId());
651
652 for (BookmarksFolder folder : folders) {
653 mergeFolders(folder, toFolderId);
654 }
655
656 List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
657 fromFolder.getGroupId(), fromFolder.getFolderId());
658
659 for (BookmarksEntry entry : entries) {
660 entry.setFolderId(toFolderId);
661
662 bookmarksEntryPersistence.update(entry);
663
664 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
665 BookmarksEntry.class);
666
667 indexer.reindex(entry);
668 }
669
670 bookmarksFolderLocalService.deleteFolder(fromFolder);
671 }
672
673 protected void updateDependentStatus(
674 List<Object> foldersAndEntries, int status)
675 throws PortalException, SystemException {
676
677 for (Object object : foldersAndEntries) {
678 if (object instanceof BookmarksEntry) {
679 BookmarksEntry entry = (BookmarksEntry)object;
680
681 if (status == WorkflowConstants.STATUS_IN_TRASH) {
682
683
684
685 assetEntryLocalService.updateVisible(
686 BookmarksEntry.class.getName(), entry.getEntryId(),
687 false);
688
689
690
691 socialActivityCounterLocalService.disableActivityCounters(
692 BookmarksEntry.class.getName(), entry.getEntryId());
693
694 if (entry.getStatus() == WorkflowConstants.STATUS_PENDING) {
695 entry.setStatus(WorkflowConstants.STATUS_DRAFT);
696
697 bookmarksEntryPersistence.update(entry);
698 }
699 }
700 else {
701
702
703
704 if (entry.getStatus() ==
705 WorkflowConstants.STATUS_APPROVED) {
706
707 assetEntryLocalService.updateVisible(
708 BookmarksEntry.class.getName(), entry.getEntryId(),
709 true);
710 }
711
712
713
714 socialActivityCounterLocalService.enableActivityCounters(
715 BookmarksEntry.class.getName(), entry.getEntryId());
716 }
717
718
719
720 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
721 BookmarksEntry.class);
722
723 indexer.reindex(entry);
724 }
725 else if (object instanceof BookmarksFolder) {
726 BookmarksFolder folder = (BookmarksFolder)object;
727
728 if (folder.isInTrash()) {
729 continue;
730 }
731
732
733
734 List<Object> curFoldersAndEntries = getFoldersAndEntries(
735 folder.getGroupId(), folder.getFolderId());
736
737 updateDependentStatus(curFoldersAndEntries, status);
738
739 if (status == WorkflowConstants.STATUS_IN_TRASH) {
740
741
742
743 assetEntryLocalService.updateVisible(
744 BookmarksFolder.class.getName(), folder.getFolderId(),
745 false);
746
747
748
749 socialActivityCounterLocalService.disableActivityCounters(
750 BookmarksFolder.class.getName(), folder.getFolderId());
751 }
752 else {
753
754
755
756 assetEntryLocalService.updateVisible(
757 BookmarksFolder.class.getName(), folder.getFolderId(),
758 true);
759
760
761
762 socialActivityCounterLocalService.enableActivityCounters(
763 BookmarksFolder.class.getName(), folder.getFolderId());
764 }
765
766
767
768 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
769 BookmarksFolder.class);
770
771 indexer.reindex(folder);
772 }
773 }
774 }
775
776 protected void validate(String name) throws PortalException {
777 if (Validator.isNull(name) || name.contains("\\\\") ||
778 name.contains("
779
780 throw new FolderNameException();
781 }
782 }
783
784 }