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.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.json.JSONFactoryUtil;
022 import com.liferay.portal.kernel.json.JSONObject;
023 import com.liferay.portal.kernel.search.Indexable;
024 import com.liferay.portal.kernel.search.IndexableType;
025 import com.liferay.portal.kernel.search.Indexer;
026 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027 import com.liferay.portal.kernel.systemevent.SystemEvent;
028 import com.liferay.portal.kernel.util.ContentTypes;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.TreeModelFinder;
031 import com.liferay.portal.kernel.util.TreePathUtil;
032 import com.liferay.portal.kernel.util.Validator;
033 import com.liferay.portal.kernel.workflow.WorkflowConstants;
034 import com.liferay.portal.model.ResourceConstants;
035 import com.liferay.portal.model.SystemEventConstants;
036 import com.liferay.portal.model.TreeModel;
037 import com.liferay.portal.model.User;
038 import com.liferay.portal.service.ServiceContext;
039 import com.liferay.portlet.asset.model.AssetEntry;
040 import com.liferay.portlet.asset.model.AssetLinkConstants;
041 import com.liferay.portlet.bookmarks.FolderNameException;
042 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
043 import com.liferay.portlet.bookmarks.model.BookmarksFolder;
044 import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
045 import com.liferay.portlet.bookmarks.service.base.BookmarksFolderLocalServiceBaseImpl;
046 import com.liferay.portlet.bookmarks.util.comparator.FolderIdComparator;
047 import com.liferay.portlet.social.model.SocialActivityConstants;
048 import com.liferay.portlet.trash.model.TrashEntry;
049 import com.liferay.portlet.trash.model.TrashVersion;
050
051 import java.util.ArrayList;
052 import java.util.Date;
053 import java.util.List;
054
055
059 public class BookmarksFolderLocalServiceImpl
060 extends BookmarksFolderLocalServiceBaseImpl {
061
062 @Override
063 public BookmarksFolder addFolder(
064 long userId, long parentFolderId, String name, String description,
065 ServiceContext serviceContext)
066 throws PortalException, SystemException {
067
068
069
070 User user = userPersistence.findByPrimaryKey(userId);
071 long groupId = serviceContext.getScopeGroupId();
072 parentFolderId = getParentFolderId(groupId, parentFolderId);
073 Date now = new Date();
074
075 validate(name);
076
077 long folderId = counterLocalService.increment();
078
079 BookmarksFolder folder = bookmarksFolderPersistence.create(folderId);
080
081 folder.setUuid(serviceContext.getUuid());
082 folder.setGroupId(groupId);
083 folder.setCompanyId(user.getCompanyId());
084 folder.setUserId(user.getUserId());
085 folder.setUserName(user.getFullName());
086 folder.setCreateDate(serviceContext.getCreateDate(now));
087 folder.setModifiedDate(serviceContext.getModifiedDate(now));
088 folder.setParentFolderId(parentFolderId);
089 folder.setTreePath(folder.buildTreePath());
090 folder.setName(name);
091 folder.setDescription(description);
092 folder.setExpandoBridgeAttributes(serviceContext);
093
094 bookmarksFolderPersistence.update(folder);
095
096
097
098 resourceLocalService.addModelResources(folder, serviceContext);
099
100
101
102 updateAsset(
103 userId, folder, serviceContext.getAssetCategoryIds(),
104 serviceContext.getAssetTagNames(),
105 serviceContext.getAssetLinkEntryIds());
106
107 return folder;
108 }
109
110 @Indexable(type = IndexableType.DELETE)
111 @Override
112 @SystemEvent(
113 action = SystemEventConstants.ACTION_SKIP, send = false,
114 type = SystemEventConstants.TYPE_DELETE)
115 public BookmarksFolder deleteFolder(BookmarksFolder folder)
116 throws PortalException, SystemException {
117
118 return deleteFolder(folder, true);
119 }
120
121 @Indexable(type = IndexableType.DELETE)
122 @Override
123 @SystemEvent(
124 action = SystemEventConstants.ACTION_SKIP, send = false,
125 type = SystemEventConstants.TYPE_DELETE)
126 public BookmarksFolder deleteFolder(
127 BookmarksFolder folder, boolean includeTrashedEntries)
128 throws PortalException, SystemException {
129
130 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
131 folder.getGroupId(), folder.getFolderId());
132
133 for (BookmarksFolder curFolder : folders) {
134 if (includeTrashedEntries || !curFolder.isInTrashExplicitly()) {
135 deleteFolder(curFolder);
136 }
137 }
138
139
140
141 bookmarksFolderPersistence.remove(folder);
142
143
144
145 resourceLocalService.deleteResource(
146 folder, ResourceConstants.SCOPE_INDIVIDUAL);
147
148
149
150 bookmarksEntryLocalService.deleteEntries(
151 folder.getGroupId(), folder.getFolderId(), includeTrashedEntries);
152
153
154
155 assetEntryLocalService.deleteEntry(
156 BookmarksFolder.class.getName(), folder.getFolderId());
157
158
159
160 expandoRowLocalService.deleteRows(folder.getFolderId());
161
162
163
164 subscriptionLocalService.deleteSubscriptions(
165 folder.getCompanyId(), BookmarksFolder.class.getName(),
166 folder.getFolderId());
167
168
169
170 trashEntryLocalService.deleteEntry(
171 BookmarksFolder.class.getName(), folder.getFolderId());
172
173 return folder;
174 }
175
176 @Indexable(type = IndexableType.DELETE)
177 @Override
178 public BookmarksFolder deleteFolder(long folderId)
179 throws PortalException, SystemException {
180
181 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
182 folderId);
183
184 return bookmarksFolderLocalService.deleteFolder(folder);
185 }
186
187 @Indexable(type = IndexableType.DELETE)
188 @Override
189 public BookmarksFolder deleteFolder(
190 long folderId, boolean includeTrashedEntries)
191 throws PortalException, SystemException {
192
193 BookmarksFolder folder = bookmarksFolderLocalService.getFolder(
194 folderId);
195
196 return bookmarksFolderLocalService.deleteFolder(
197 folder, includeTrashedEntries);
198 }
199
200 @Override
201 public void deleteFolders(long groupId)
202 throws PortalException, SystemException {
203
204 List<BookmarksFolder> folders =
205 bookmarksFolderPersistence.findByGroupId(groupId);
206
207 for (BookmarksFolder folder : folders) {
208 bookmarksFolderLocalService.deleteFolder(folder);
209 }
210 }
211
212 @Override
213 public List<BookmarksFolder> getCompanyFolders(
214 long companyId, int start, int end)
215 throws SystemException {
216
217 return bookmarksFolderPersistence.findByCompanyId(
218 companyId, start, end);
219 }
220
221 @Override
222 public int getCompanyFoldersCount(long companyId) throws SystemException {
223 return bookmarksFolderPersistence.countByCompanyId(companyId);
224 }
225
226 @Override
227 public BookmarksFolder getFolder(long folderId)
228 throws PortalException, SystemException {
229
230 return bookmarksFolderPersistence.findByPrimaryKey(folderId);
231 }
232
233 @Override
234 public List<BookmarksFolder> getFolders(long groupId)
235 throws SystemException {
236
237 return bookmarksFolderPersistence.findByGroupId(groupId);
238 }
239
240 @Override
241 public List<BookmarksFolder> getFolders(long groupId, long parentFolderId)
242 throws SystemException {
243
244 return bookmarksFolderPersistence.findByG_P(groupId, parentFolderId);
245 }
246
247 @Override
248 public List<BookmarksFolder> getFolders(
249 long groupId, long parentFolderId, int start, int end)
250 throws SystemException {
251
252 return getFolders(
253 groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
254 end);
255 }
256
257 @Override
258 public List<BookmarksFolder> getFolders(
259 long groupId, long parentFolderId, int status, int start, int end)
260 throws SystemException {
261
262 return bookmarksFolderPersistence.findByG_P_S(
263 groupId, parentFolderId, status, start, end);
264 }
265
266 @Override
267 public List<Object> getFoldersAndEntries(long groupId, long folderId)
268 throws SystemException {
269
270 return getFoldersAndEntries(
271 groupId, folderId, WorkflowConstants.STATUS_ANY);
272 }
273
274 @Override
275 public List<Object> getFoldersAndEntries(
276 long groupId, long folderId, int status)
277 throws SystemException {
278
279 QueryDefinition queryDefinition = new QueryDefinition(status);
280
281 return bookmarksFolderFinder.findF_E_ByG_F(
282 groupId, folderId, queryDefinition);
283 }
284
285 @Override
286 public List<Object> getFoldersAndEntries(
287 long groupId, long folderId, int status, int start, int end)
288 throws SystemException {
289
290 QueryDefinition queryDefinition = new QueryDefinition(
291 status, start, end, null);
292
293 return bookmarksFolderFinder.findF_E_ByG_F(
294 groupId, folderId, queryDefinition);
295 }
296
297 @Override
298 public int getFoldersAndEntriesCount(
299 long groupId, long folderId, int status)
300 throws SystemException {
301
302 QueryDefinition queryDefinition = new QueryDefinition(status);
303
304 return bookmarksFolderFinder.countF_E_ByG_F(
305 groupId, folderId, queryDefinition);
306 }
307
308 @Override
309 public int getFoldersCount(long groupId, long parentFolderId)
310 throws SystemException {
311
312 return getFoldersCount(
313 groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
314 }
315
316 @Override
317 public int getFoldersCount(long groupId, long parentFolderId, int status)
318 throws SystemException {
319
320 return bookmarksFolderPersistence.countByG_P_S(
321 groupId, parentFolderId, status);
322 }
323
324 @Override
325 public List<BookmarksFolder> getNoAssetFolders() throws SystemException {
326 return bookmarksFolderFinder.findByNoAssets();
327 }
328
329 @Override
330 public void getSubfolderIds(
331 List<Long> folderIds, long groupId, long folderId)
332 throws SystemException {
333
334 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
335 groupId, folderId);
336
337 for (BookmarksFolder folder : folders) {
338 folderIds.add(folder.getFolderId());
339
340 getSubfolderIds(
341 folderIds, folder.getGroupId(), folder.getFolderId());
342 }
343 }
344
345 @Indexable(type = IndexableType.REINDEX)
346 @Override
347 public BookmarksFolder moveFolder(long folderId, long parentFolderId)
348 throws PortalException, SystemException {
349
350 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
351 folderId);
352
353 if (folder.getParentFolderId() == parentFolderId) {
354 return folder;
355 }
356
357 folder.setParentFolderId(parentFolderId);
358 folder.setTreePath(folder.buildTreePath());
359
360 bookmarksFolderPersistence.update(folder);
361
362 rebuildTree(
363 folder.getCompanyId(), folderId, folder.getTreePath(), true);
364
365 return folder;
366 }
367
368 @Override
369 public BookmarksFolder moveFolderFromTrash(
370 long userId, long folderId, long parentFolderId)
371 throws PortalException, SystemException {
372
373 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
374 folderId);
375
376 if (folder.isInTrashExplicitly()) {
377 restoreFolderFromTrash(userId, folderId);
378 }
379 else {
380
381
382
383 TrashEntry trashEntry = folder.getTrashEntry();
384
385 TrashVersion trashVersion =
386 trashVersionLocalService.fetchVersion(
387 trashEntry.getEntryId(), BookmarksFolder.class.getName(),
388 folderId);
389
390 int status = WorkflowConstants.STATUS_APPROVED;
391
392 if (trashVersion != null) {
393 status = trashVersion.getStatus();
394 }
395
396 updateStatus(userId, folder, status);
397
398
399
400 if (trashVersion != null) {
401 trashVersionLocalService.deleteTrashVersion(trashVersion);
402 }
403
404
405
406 List<Object> foldersAndEntries =
407 bookmarksFolderLocalService.getFoldersAndEntries(
408 folder.getGroupId(), folder.getFolderId(),
409 WorkflowConstants.STATUS_IN_TRASH);
410
411 restoreDependentsFromTrash(
412 foldersAndEntries, trashEntry.getEntryId());
413 }
414
415 return bookmarksFolderLocalService.moveFolder(folderId, parentFolderId);
416 }
417
418 @Indexable(type = IndexableType.REINDEX)
419 @Override
420 public BookmarksFolder moveFolderToTrash(long userId, long folderId)
421 throws PortalException, SystemException {
422
423
424
425 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
426 folderId);
427
428 int oldStatus = folder.getStatus();
429
430 folder = updateStatus(
431 userId, folder, WorkflowConstants.STATUS_IN_TRASH);
432
433
434
435 TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
436 userId, folder.getGroupId(), BookmarksFolder.class.getName(),
437 folder.getFolderId(), folder.getUuid(), null, oldStatus, null,
438 null);
439
440
441
442 List<Object> foldersAndEntries =
443 bookmarksFolderLocalService.getFoldersAndEntries(
444 folder.getGroupId(), folder.getFolderId());
445
446 moveDependentsToTrash(foldersAndEntries, trashEntry.getEntryId());
447
448
449
450 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
451
452 extraDataJSONObject.put("title", folder.getName());
453
454 socialActivityLocalService.addActivity(
455 userId, folder.getGroupId(), BookmarksFolder.class.getName(),
456 folder.getFolderId(), SocialActivityConstants.TYPE_MOVE_TO_TRASH,
457 extraDataJSONObject.toString(), 0);
458
459 return folder;
460 }
461
462 @Override
463 public void rebuildTree(long companyId)
464 throws PortalException, SystemException {
465
466 rebuildTree(
467 companyId, BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID,
468 StringPool.SLASH, false);
469 }
470
471 @Override
472 public void rebuildTree(
473 long companyId, long parentFolderId, String parentTreePath,
474 final boolean reindex)
475 throws PortalException, SystemException {
476
477 TreePathUtil.rebuildTree(
478 companyId, parentFolderId, parentTreePath,
479 new TreeModelFinder<BookmarksFolder>() {
480
481 @Override
482 public List<BookmarksFolder> findTreeModels(
483 long previousId, long companyId, long parentPrimaryKey,
484 int size)
485 throws SystemException {
486
487 return bookmarksFolderPersistence.findByF_C_P_NotS(
488 previousId, companyId, parentPrimaryKey,
489 WorkflowConstants.STATUS_IN_TRASH, QueryUtil.ALL_POS,
490 size, new FolderIdComparator());
491 }
492
493 @Override
494 public void rebuildDependentModelsTreePaths(
495 long parentPrimaryKey, String treePath)
496 throws PortalException, SystemException {
497
498 bookmarksEntryLocalService.setTreePaths(
499 parentPrimaryKey, treePath, false);
500 }
501
502 @Override
503 public void reindexTreeModels(List<TreeModel> treeModels)
504 throws PortalException {
505
506 if (!reindex) {
507 return;
508 }
509
510 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
511 BookmarksFolder.class);
512
513 for (TreeModel treeModel : treeModels) {
514 indexer.reindex(treeModel);
515 }
516 }
517
518 }
519 );
520 }
521
522 @Indexable(type = IndexableType.REINDEX)
523 @Override
524 public void restoreFolderFromTrash(long userId, long folderId)
525 throws PortalException, SystemException {
526
527
528
529 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
530 folderId);
531
532 TrashEntry trashEntry = trashEntryLocalService.getEntry(
533 BookmarksFolder.class.getName(), folderId);
534
535 updateStatus(userId, folder, trashEntry.getStatus());
536
537
538
539 List<Object> foldersAndEntries =
540 bookmarksFolderLocalService.getFoldersAndEntries(
541 folder.getGroupId(), folder.getFolderId(),
542 WorkflowConstants.STATUS_IN_TRASH);
543
544 restoreDependentsFromTrash(foldersAndEntries, trashEntry.getEntryId());
545
546
547
548 trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
549
550
551
552 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
553
554 extraDataJSONObject.put("title", folder.getName());
555
556 socialActivityLocalService.addActivity(
557 userId, folder.getGroupId(), BookmarksFolder.class.getName(),
558 folder.getFolderId(),
559 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
560 extraDataJSONObject.toString(), 0);
561 }
562
563 @Override
564 public void subscribeFolder(long userId, long groupId, long folderId)
565 throws PortalException, SystemException {
566
567 if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
568 folderId = groupId;
569 }
570
571 subscriptionLocalService.addSubscription(
572 userId, groupId, BookmarksFolder.class.getName(), folderId);
573 }
574
575 @Override
576 public void unsubscribeFolder(long userId, long groupId, long folderId)
577 throws PortalException, SystemException {
578
579 if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
580 folderId = groupId;
581 }
582
583 subscriptionLocalService.deleteSubscription(
584 userId, BookmarksFolder.class.getName(), folderId);
585 }
586
587 @Override
588 public void updateAsset(
589 long userId, BookmarksFolder folder, long[] assetCategoryIds,
590 String[] assetTagNames, long[] assetLinkEntryIds)
591 throws PortalException, SystemException {
592
593 AssetEntry assetEntry = assetEntryLocalService.updateEntry(
594 userId, folder.getGroupId(), folder.getCreateDate(),
595 folder.getModifiedDate(), BookmarksFolder.class.getName(),
596 folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
597 assetTagNames, true, null, null, null, ContentTypes.TEXT_PLAIN,
598 folder.getName(), folder.getDescription(), null, null, null, 0, 0,
599 null, false);
600
601 assetLinkLocalService.updateLinks(
602 userId, assetEntry.getEntryId(), assetLinkEntryIds,
603 AssetLinkConstants.TYPE_RELATED);
604 }
605
606 @Indexable(type = IndexableType.REINDEX)
607 @Override
608 public BookmarksFolder updateFolder(
609 long userId, long folderId, long parentFolderId, String name,
610 String description, boolean mergeWithParentFolder,
611 ServiceContext serviceContext)
612 throws PortalException, SystemException {
613
614
615
616 BookmarksFolder folder = bookmarksFolderPersistence.findByPrimaryKey(
617 folderId);
618
619 parentFolderId = getParentFolderId(folder, parentFolderId);
620
621 if (mergeWithParentFolder && (folderId != parentFolderId)) {
622 mergeFolders(folder, parentFolderId);
623
624 return folder;
625 }
626
627
628
629 validate(name);
630
631 long oldParentFolderId = folder.getParentFolderId();
632
633 if (oldParentFolderId != parentFolderId) {
634 folder.setParentFolderId(parentFolderId);
635 folder.setTreePath(folder.buildTreePath());
636 }
637
638 folder.setModifiedDate(serviceContext.getModifiedDate(null));
639 folder.setName(name);
640 folder.setDescription(description);
641 folder.setExpandoBridgeAttributes(serviceContext);
642
643 bookmarksFolderPersistence.update(folder);
644
645
646
647 updateAsset(
648 userId, folder, serviceContext.getAssetCategoryIds(),
649 serviceContext.getAssetTagNames(),
650 serviceContext.getAssetLinkEntryIds());
651
652 if (oldParentFolderId != parentFolderId) {
653 rebuildTree(
654 folder.getCompanyId(), folderId, folder.getTreePath(), true);
655 }
656
657 return folder;
658 }
659
660 @Override
661 public BookmarksFolder updateStatus(
662 long userId, BookmarksFolder folder, int status)
663 throws PortalException, SystemException {
664
665
666
667 User user = userPersistence.findByPrimaryKey(userId);
668
669 folder.setStatus(status);
670 folder.setStatusByUserId(userId);
671 folder.setStatusByUserName(user.getFullName());
672 folder.setStatusDate(new Date());
673
674 bookmarksFolderPersistence.update(folder);
675
676
677
678 if (status == WorkflowConstants.STATUS_APPROVED) {
679 assetEntryLocalService.updateVisible(
680 BookmarksFolder.class.getName(), folder.getFolderId(), true);
681 }
682 else if (status == WorkflowConstants.STATUS_IN_TRASH) {
683 assetEntryLocalService.updateVisible(
684 BookmarksFolder.class.getName(), folder.getFolderId(), false);
685 }
686
687
688
689 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
690 BookmarksFolder.class);
691
692 indexer.reindex(folder);
693
694 return folder;
695 }
696
697 protected long getParentFolderId(
698 BookmarksFolder folder, long parentFolderId)
699 throws SystemException {
700
701 if (parentFolderId ==
702 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
703
704 return parentFolderId;
705 }
706
707 if (folder.getFolderId() == parentFolderId) {
708 return folder.getParentFolderId();
709 }
710
711 BookmarksFolder parentFolder =
712 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
713
714 if ((parentFolder == null) ||
715 (folder.getGroupId() != parentFolder.getGroupId())) {
716
717 return folder.getParentFolderId();
718 }
719
720 List<Long> subfolderIds = new ArrayList<Long>();
721
722 getSubfolderIds(
723 subfolderIds, folder.getGroupId(), folder.getFolderId());
724
725 if (subfolderIds.contains(parentFolderId)) {
726 return folder.getParentFolderId();
727 }
728
729 return parentFolderId;
730 }
731
732 protected long getParentFolderId(long groupId, long parentFolderId)
733 throws SystemException {
734
735 if (parentFolderId !=
736 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
737
738 BookmarksFolder parentFolder =
739 bookmarksFolderPersistence.fetchByPrimaryKey(parentFolderId);
740
741 if ((parentFolder == null) ||
742 (groupId != parentFolder.getGroupId())) {
743
744 parentFolderId =
745 BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;
746 }
747 }
748
749 return parentFolderId;
750 }
751
752 protected void mergeFolders(BookmarksFolder fromFolder, long toFolderId)
753 throws PortalException, SystemException {
754
755 List<BookmarksFolder> folders = bookmarksFolderPersistence.findByG_P(
756 fromFolder.getGroupId(), fromFolder.getFolderId());
757
758 for (BookmarksFolder folder : folders) {
759 mergeFolders(folder, toFolderId);
760 }
761
762 List<BookmarksEntry> entries = bookmarksEntryPersistence.findByG_F(
763 fromFolder.getGroupId(), fromFolder.getFolderId());
764
765 for (BookmarksEntry entry : entries) {
766 entry.setFolderId(toFolderId);
767 entry.setTreePath(entry.buildTreePath());
768
769 bookmarksEntryPersistence.update(entry);
770
771 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
772 BookmarksEntry.class);
773
774 indexer.reindex(entry);
775 }
776
777 bookmarksFolderLocalService.deleteFolder(fromFolder);
778 }
779
780 protected void moveDependentsToTrash(
781 List<Object> foldersAndEntries, long trashEntryId)
782 throws PortalException, SystemException {
783
784 for (Object object : foldersAndEntries) {
785 if (object instanceof BookmarksEntry) {
786
787
788
789 BookmarksEntry entry = (BookmarksEntry)object;
790
791 if (entry.isInTrash()) {
792 continue;
793 }
794
795 int oldStatus = entry.getStatus();
796
797 entry.setStatus(WorkflowConstants.STATUS_IN_TRASH);
798
799 bookmarksEntryPersistence.update(entry);
800
801
802
803 int status = oldStatus;
804
805 if (oldStatus == WorkflowConstants.STATUS_PENDING) {
806 status = WorkflowConstants.STATUS_DRAFT;
807 }
808
809 if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
810 trashVersionLocalService.addTrashVersion(
811 trashEntryId, BookmarksEntry.class.getName(),
812 entry.getEntryId(), status, null);
813 }
814
815
816
817 assetEntryLocalService.updateVisible(
818 BookmarksEntry.class.getName(), entry.getEntryId(), false);
819
820
821
822 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
823 BookmarksEntry.class);
824
825 indexer.reindex(entry);
826 }
827 else if (object instanceof BookmarksFolder) {
828
829
830
831 BookmarksFolder folder = (BookmarksFolder)object;
832
833 if (folder.isInTrash()) {
834 continue;
835 }
836
837 int oldStatus = folder.getStatus();
838
839 folder.setStatus(WorkflowConstants.STATUS_IN_TRASH);
840
841 bookmarksFolderPersistence.update(folder);
842
843
844
845 if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
846 trashVersionLocalService.addTrashVersion(
847 trashEntryId, BookmarksEntry.class.getName(),
848 folder.getFolderId(), oldStatus, null);
849 }
850
851
852
853 List<Object> curFoldersAndEntries = getFoldersAndEntries(
854 folder.getGroupId(), folder.getFolderId());
855
856 moveDependentsToTrash(curFoldersAndEntries, trashEntryId);
857
858
859
860 assetEntryLocalService.updateVisible(
861 BookmarksFolder.class.getName(), folder.getFolderId(),
862 false);
863
864
865
866 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
867 BookmarksFolder.class);
868
869 indexer.reindex(folder);
870 }
871 }
872 }
873
874 protected void restoreDependentsFromTrash(
875 List<Object> foldersAndEntries, long trashEntryId)
876 throws PortalException, SystemException {
877
878 for (Object object : foldersAndEntries) {
879 if (object instanceof BookmarksEntry) {
880
881
882
883 BookmarksEntry entry = (BookmarksEntry)object;
884
885 TrashEntry trashEntry = trashEntryLocalService.fetchEntry(
886 BookmarksEntry.class.getName(), entry.getEntryId());
887
888 if (trashEntry != null) {
889 continue;
890 }
891
892 TrashVersion trashVersion =
893 trashVersionLocalService.fetchVersion(
894 trashEntryId, BookmarksEntry.class.getName(),
895 entry.getEntryId());
896
897 int oldStatus = WorkflowConstants.STATUS_APPROVED;
898
899 if (trashVersion != null) {
900 oldStatus = trashVersion.getStatus();
901 }
902
903 entry.setStatus(oldStatus);
904
905 bookmarksEntryPersistence.update(entry);
906
907
908
909 if (trashVersion != null) {
910 trashVersionLocalService.deleteTrashVersion(trashVersion);
911 }
912
913
914
915 if (oldStatus == WorkflowConstants.STATUS_APPROVED) {
916 assetEntryLocalService.updateVisible(
917 BookmarksEntry.class.getName(), entry.getEntryId(),
918 true);
919 }
920
921
922
923 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
924 BookmarksEntry.class);
925
926 indexer.reindex(entry);
927 }
928 else if (object instanceof BookmarksFolder) {
929
930
931
932 BookmarksFolder folder = (BookmarksFolder)object;
933
934 TrashEntry trashEntry = trashEntryLocalService.fetchEntry(
935 BookmarksFolder.class.getName(), folder.getFolderId());
936
937 if (trashEntry != null) {
938 continue;
939 }
940
941 TrashVersion trashVersion =
942 trashVersionLocalService.fetchVersion(
943 trashEntryId, BookmarksFolder.class.getName(),
944 folder.getFolderId());
945
946 int oldStatus = WorkflowConstants.STATUS_APPROVED;
947
948 if (trashVersion != null) {
949 oldStatus = trashVersion.getStatus();
950 }
951
952 folder.setStatus(oldStatus);
953
954 bookmarksFolderPersistence.update(folder);
955
956
957
958 List<Object> curFoldersAndEntries = getFoldersAndEntries(
959 folder.getGroupId(), folder.getFolderId(),
960 WorkflowConstants.STATUS_IN_TRASH);
961
962 restoreDependentsFromTrash(curFoldersAndEntries, trashEntryId);
963
964
965
966 if (trashVersion != null) {
967 trashVersionLocalService.deleteTrashVersion(trashVersion);
968 }
969
970
971
972 assetEntryLocalService.updateVisible(
973 BookmarksFolder.class.getName(), folder.getFolderId(),
974 true);
975
976
977
978 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
979 BookmarksFolder.class);
980
981 indexer.reindex(folder);
982 }
983 }
984 }
985
986 protected void validate(String name) throws PortalException {
987 if (Validator.isNull(name) || name.contains("\\\\") ||
988 name.contains("
989
990 throw new FolderNameException();
991 }
992 }
993
994 }