001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.portal.kernel.bean.BeanReference;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.WildcardMode;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.json.JSONFactoryUtil;
022 import com.liferay.portal.kernel.json.JSONObject;
023 import com.liferay.portal.kernel.language.LanguageUtil;
024 import com.liferay.portal.kernel.lock.Lock;
025 import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
026 import com.liferay.portal.kernel.portlet.PortletProvider;
027 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
028 import com.liferay.portal.kernel.repository.LocalRepository;
029 import com.liferay.portal.kernel.repository.Repository;
030 import com.liferay.portal.kernel.repository.RepositoryProviderUtil;
031 import com.liferay.portal.kernel.repository.capabilities.RepositoryEventTriggerCapability;
032 import com.liferay.portal.kernel.repository.event.RepositoryEventType;
033 import com.liferay.portal.kernel.repository.event.TrashRepositoryEventType;
034 import com.liferay.portal.kernel.repository.event.WorkflowRepositoryEventType;
035 import com.liferay.portal.kernel.repository.model.FileEntry;
036 import com.liferay.portal.kernel.repository.model.FileShortcut;
037 import com.liferay.portal.kernel.repository.model.FileVersion;
038 import com.liferay.portal.kernel.repository.model.Folder;
039 import com.liferay.portal.kernel.repository.model.RepositoryModel;
040 import com.liferay.portal.kernel.search.Indexer;
041 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
042 import com.liferay.portal.kernel.settings.LocalizedValuesMap;
043 import com.liferay.portal.kernel.social.SocialActivityManagerUtil;
044 import com.liferay.portal.kernel.util.Constants;
045 import com.liferay.portal.kernel.util.GetterUtil;
046 import com.liferay.portal.kernel.util.ListUtil;
047 import com.liferay.portal.kernel.util.LocalizationUtil;
048 import com.liferay.portal.kernel.util.ObjectValuePair;
049 import com.liferay.portal.kernel.util.UnicodeProperties;
050 import com.liferay.portal.kernel.util.Validator;
051 import com.liferay.portal.kernel.workflow.WorkflowConstants;
052 import com.liferay.portal.model.UserConstants;
053 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
054 import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
055 import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
056 import com.liferay.portal.service.ServiceContext;
057 import com.liferay.portal.util.GroupSubscriptionCheckSubscriptionSender;
058 import com.liferay.portal.util.SubscriptionSender;
059 import com.liferay.portlet.asset.model.AssetEntry;
060 import com.liferay.portlet.asset.model.AssetLink;
061 import com.liferay.portlet.asset.model.AssetLinkConstants;
062 import com.liferay.portlet.documentlibrary.DLGroupServiceSettings;
063 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
064 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
065 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
066 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
067 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
068 import com.liferay.portlet.documentlibrary.model.DLFileShortcutConstants;
069 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
070 import com.liferay.portlet.documentlibrary.model.DLFolder;
071 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
072 import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
073 import com.liferay.portlet.documentlibrary.service.DLAppService;
074 import com.liferay.portlet.documentlibrary.service.base.DLAppHelperLocalServiceBaseImpl;
075 import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
076 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
077 import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
078 import com.liferay.portlet.documentlibrary.util.comparator.DLFileVersionVersionComparator;
079 import com.liferay.portlet.social.model.SocialActivityConstants;
080 import com.liferay.portlet.trash.model.TrashEntry;
081 import com.liferay.portlet.trash.model.TrashVersion;
082 import com.liferay.portlet.trash.util.TrashUtil;
083 import com.liferay.util.dao.orm.CustomSQLUtil;
084
085 import java.io.Serializable;
086
087 import java.util.ArrayList;
088 import java.util.Date;
089 import java.util.HashMap;
090 import java.util.List;
091 import java.util.Map;
092
093
098 public class DLAppHelperLocalServiceImpl
099 extends DLAppHelperLocalServiceBaseImpl {
100
101 @Override
102 public void addFolder(
103 long userId, Folder folder, ServiceContext serviceContext)
104 throws PortalException {
105
106 if (!DLAppHelperThreadLocal.isEnabled()) {
107 return;
108 }
109
110 updateAsset(
111 userId, folder, serviceContext.getAssetCategoryIds(),
112 serviceContext.getAssetTagNames(),
113 serviceContext.getAssetLinkEntryIds());
114 }
115
116 @Override
117 public void cancelCheckOut(
118 long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
119 FileVersion destinationFileVersion, FileVersion draftFileVersion,
120 ServiceContext serviceContext)
121 throws PortalException {
122
123 if (draftFileVersion == null) {
124 return;
125 }
126
127 AssetEntry draftAssetEntry = assetEntryLocalService.fetchEntry(
128 DLFileEntryConstants.getClassName(),
129 draftFileVersion.getPrimaryKey());
130
131 if (draftAssetEntry != null) {
132 assetEntryLocalService.deleteEntry(draftAssetEntry);
133 }
134 }
135
136 @Override
137 public void checkAssetEntry(
138 long userId, FileEntry fileEntry, FileVersion fileVersion)
139 throws PortalException {
140
141 AssetEntry fileEntryAssetEntry = assetEntryLocalService.fetchEntry(
142 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
143
144 long[] assetCategoryIds = new long[0];
145 String[] assetTagNames = new String[0];
146
147 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
148
149 if (fileEntryAssetEntry == null) {
150 fileEntryAssetEntry = assetEntryLocalService.updateEntry(
151 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
152 fileEntry.getModifiedDate(),
153 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
154 fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
155 assetTagNames, false, null, null, null, fileEntry.getMimeType(),
156 fileEntry.getTitle(), fileEntry.getDescription(), null, null,
157 null, 0, 0, null);
158 }
159
160 AssetEntry fileVersionAssetEntry = assetEntryLocalService.fetchEntry(
161 DLFileEntryConstants.getClassName(),
162 fileVersion.getFileVersionId());
163
164 if ((fileVersionAssetEntry == null) && !fileVersion.isApproved() &&
165 !fileVersion.getVersion().equals(
166 DLFileEntryConstants.VERSION_DEFAULT)) {
167
168 assetCategoryIds = assetCategoryLocalService.getCategoryIds(
169 DLFileEntryConstants.getClassName(),
170 fileEntry.getFileEntryId());
171 assetTagNames = assetTagLocalService.getTagNames(
172 DLFileEntryConstants.getClassName(),
173 fileEntry.getFileEntryId());
174
175 fileVersionAssetEntry = assetEntryLocalService.updateEntry(
176 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
177 fileEntry.getModifiedDate(),
178 DLFileEntryConstants.getClassName(),
179 fileVersion.getFileVersionId(), fileEntry.getUuid(),
180 fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
181 null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
182 fileEntry.getDescription(), null, null, null, 0, 0, null);
183
184 List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
185 fileEntryAssetEntry.getEntryId());
186
187 long[] assetLinkIds = ListUtil.toLongArray(
188 assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
189
190 assetLinkLocalService.updateLinks(
191 userId, fileVersionAssetEntry.getEntryId(), assetLinkIds,
192 AssetLinkConstants.TYPE_RELATED);
193 }
194 }
195
196 @Override
197 public void deleteFileEntry(FileEntry fileEntry) throws PortalException {
198 if (!DLAppHelperThreadLocal.isEnabled()) {
199 return;
200 }
201
202
203
204 subscriptionLocalService.deleteSubscriptions(
205 fileEntry.getCompanyId(), DLFileEntryConstants.getClassName(),
206 fileEntry.getFileEntryId());
207
208
209
210 dlFileRankLocalService.deleteFileRanksByFileEntryId(
211 fileEntry.getFileEntryId());
212
213
214
215 dlFileShortcutLocalService.deleteFileShortcuts(
216 fileEntry.getFileEntryId());
217
218
219
220 assetEntryLocalService.deleteEntry(
221 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
222
223
224
225 ratingsStatsLocalService.deleteStats(
226 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
227 }
228
229 @Override
230 public void deleteFolder(Folder folder) throws PortalException {
231 if (!DLAppHelperThreadLocal.isEnabled()) {
232 return;
233 }
234
235
236
237 assetEntryLocalService.deleteEntry(
238 DLFolderConstants.getClassName(), folder.getFolderId());
239 }
240
241 @Override
242 public void deleteRepositoryFileEntries(long repositoryId)
243 throws PortalException {
244
245 LocalRepository localRepository =
246 RepositoryProviderUtil.getLocalRepository(repositoryId);
247
248 List<FileEntry> fileEntries = localRepository.getRepositoryFileEntries(
249 UserConstants.USER_ID_DEFAULT,
250 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, QueryUtil.ALL_POS,
251 QueryUtil.ALL_POS, null);
252
253 for (FileEntry fileEntry : fileEntries) {
254 deleteFileEntry(fileEntry);
255 }
256 }
257
258 @Override
259 public void getFileAsStream(
260 long userId, FileEntry fileEntry, boolean incrementCounter) {
261
262 if (!incrementCounter) {
263 return;
264 }
265
266
267
268 if (userId > 0) {
269 dlFileRankLocalService.updateFileRank(
270 fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
271 fileEntry.getFileEntryId(), new ServiceContext());
272 }
273
274
275
276 assetEntryLocalService.incrementViewCounter(
277 userId, DLFileEntryConstants.getClassName(),
278 fileEntry.getFileEntryId(), 1);
279
280 List<DLFileShortcut> fileShortcuts =
281 dlFileShortcutPersistence.findByToFileEntryId(
282 fileEntry.getFileEntryId());
283
284 for (DLFileShortcut fileShortcut : fileShortcuts) {
285 assetEntryLocalService.incrementViewCounter(
286 userId, DLFileShortcutConstants.getClassName(),
287 fileShortcut.getFileShortcutId(), 1);
288 }
289 }
290
291 @Override
292 public List<DLFileShortcut> getFileShortcuts(
293 long groupId, long folderId, boolean active, int status) {
294
295 return dlFileShortcutPersistence.findByG_F_A_S(
296 groupId, folderId, active, status);
297 }
298
299
303 @Deprecated
304 @Override
305 public List<DLFileShortcut> getFileShortcuts(
306 long groupId, long folderId, int status) {
307
308 return getFileShortcuts(groupId, folderId, true, status);
309 }
310
311 @Override
312 public int getFileShortcutsCount(
313 long groupId, long folderId, boolean active, int status) {
314
315 return dlFileShortcutPersistence.countByG_F_A_S(
316 groupId, folderId, active, status);
317 }
318
319
323 @Deprecated
324 @Override
325 public int getFileShortcutsCount(long groupId, long folderId, int status) {
326 return getFileShortcutsCount(groupId, folderId, true, status);
327 }
328
329 @Override
330 public List<FileEntry> getNoAssetFileEntries() {
331 return null;
332 }
333
334 @Override
335 public void moveDependentsToTrash(DLFolder dlFolder)
336 throws PortalException {
337
338 trashOrRestoreFolder(dlFolder, true);
339 }
340
341
345 @Deprecated
346 @Override
347 public void moveDependentsToTrash(
348 List<Object> dlFileEntriesAndDLFolders, long trashEntryId)
349 throws PortalException {
350
351 if (dlFileEntriesAndDLFolders.isEmpty()) {
352 return;
353 }
354
355 Object object = dlFileEntriesAndDLFolders.get(0);
356
357 long folderId = 0;
358
359 if (object instanceof DLFileEntry) {
360 DLFileEntry dlFileEntry = (DLFileEntry)object;
361
362 folderId = dlFileEntry.getFolderId();
363 }
364 else if (object instanceof DLFileShortcut) {
365 DLFileShortcut dlFileShortcut = (DLFileShortcut)object;
366
367 folderId = dlFileShortcut.getFolderId();
368 }
369 else if (object instanceof DLFolder) {
370 DLFolder dlFolder = (DLFolder)object;
371
372 folderId = dlFolder.getFolderId();
373 }
374
375 DLFolder dlFolder = dlFolderLocalService.getDLFolder(folderId);
376
377 moveDependentsToTrash(dlFolder);
378 }
379
380 @Override
381 public FileEntry moveFileEntryFromTrash(
382 long userId, FileEntry fileEntry, long newFolderId,
383 ServiceContext serviceContext)
384 throws PortalException {
385
386 boolean hasLock = dlFileEntryLocalService.hasFileEntryLock(
387 userId, fileEntry.getFileEntryId());
388
389 if (!hasLock) {
390 dlFileEntryLocalService.lockFileEntry(
391 userId, fileEntry.getFileEntryId());
392 }
393
394 try {
395 return doMoveFileEntryFromTrash(
396 userId, fileEntry, newFolderId, serviceContext);
397 }
398 finally {
399 if (!hasLock) {
400 dlFileEntryLocalService.unlockFileEntry(
401 fileEntry.getFileEntryId());
402 }
403 }
404 }
405
406
413 @Override
414 public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
415 throws PortalException {
416
417 boolean hasLock = dlFileEntryLocalService.hasFileEntryLock(
418 userId, fileEntry.getFileEntryId());
419
420 if (!hasLock) {
421 dlFileEntryLocalService.lockFileEntry(
422 userId, fileEntry.getFileEntryId());
423 }
424
425 try {
426 if (fileEntry.isCheckedOut()) {
427 dlFileEntryLocalService.cancelCheckOut(
428 userId, fileEntry.getFileEntryId());
429 }
430
431 return doMoveFileEntryToTrash(userId, fileEntry);
432 }
433 finally {
434 if (!hasLock) {
435 dlFileEntryLocalService.unlockFileEntry(
436 fileEntry.getFileEntryId());
437 }
438 }
439 }
440
441 @Override
442 public FileShortcut moveFileShortcutFromTrash(
443 long userId, FileShortcut fileShortcut, long newFolderId,
444 ServiceContext serviceContext)
445 throws PortalException {
446
447 DLFileShortcut dlFileShortcut = (DLFileShortcut)fileShortcut.getModel();
448
449 if (dlFileShortcut.isInTrashExplicitly()) {
450 restoreFileShortcutFromTrash(userId, fileShortcut);
451 }
452 else {
453
454
455
456 TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
457 DLFileShortcutConstants.getClassName(),
458 fileShortcut.getFileShortcutId());
459
460 int status = WorkflowConstants.STATUS_APPROVED;
461
462 if (trashVersion != null) {
463 status = trashVersion.getStatus();
464 }
465
466 dlFileShortcutLocalService.updateStatus(
467 userId, fileShortcut.getFileShortcutId(), status,
468 new ServiceContext());
469
470
471
472 if (trashVersion != null) {
473 trashVersionLocalService.deleteTrashVersion(trashVersion);
474 }
475
476
477
478 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
479
480 extraDataJSONObject.put("title", fileShortcut.getToTitle());
481
482 SocialActivityManagerUtil.addActivity(
483 userId, fileShortcut,
484 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
485 extraDataJSONObject.toString(), 0);
486 }
487
488 return dlAppService.updateFileShortcut(
489 fileShortcut.getFileShortcutId(), newFolderId,
490 fileShortcut.getToFileEntryId(), serviceContext);
491 }
492
493
500 @Override
501 public FileShortcut moveFileShortcutToTrash(
502 long userId, FileShortcut fileShortcut)
503 throws PortalException {
504
505
506
507 DLFileShortcut dlFileShortcut = (DLFileShortcut)fileShortcut.getModel();
508
509 int oldStatus = dlFileShortcut.getStatus();
510
511 dlFileShortcutLocalService.updateStatus(
512 userId, fileShortcut.getFileShortcutId(),
513 WorkflowConstants.STATUS_IN_TRASH, new ServiceContext());
514
515
516
517 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
518
519 extraDataJSONObject.put(
520 "title", TrashUtil.getOriginalTitle(fileShortcut.getToTitle()));
521
522 SocialActivityManagerUtil.addActivity(
523 userId, fileShortcut, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
524 extraDataJSONObject.toString(), 0);
525
526
527
528 trashEntryLocalService.addTrashEntry(
529 userId, fileShortcut.getGroupId(),
530 DLFileShortcutConstants.getClassName(),
531 fileShortcut.getFileShortcutId(), fileShortcut.getUuid(), null,
532 oldStatus, null, null);
533
534 return fileShortcut;
535 }
536
537 @Override
538 public Folder moveFolderFromTrash(
539 long userId, Folder folder, long parentFolderId,
540 ServiceContext serviceContext)
541 throws PortalException {
542
543 boolean hasLock = dlFolderLocalService.hasFolderLock(
544 userId, folder.getFolderId());
545
546 Lock lock = null;
547
548 if (!hasLock) {
549 lock = dlFolderLocalService.lockFolder(
550 userId, folder.getFolderId());
551 }
552
553 try {
554 return doMoveFolderFromTrash(
555 userId, folder, parentFolderId, serviceContext);
556 }
557 finally {
558 if (!hasLock) {
559 dlFolderLocalService.unlockFolder(
560 folder.getFolderId(), lock.getUuid());
561 }
562 }
563 }
564
565
572 @Override
573 public Folder moveFolderToTrash(long userId, Folder folder)
574 throws PortalException {
575
576 boolean hasLock = dlFolderLocalService.hasFolderLock(
577 userId, folder.getFolderId());
578
579 Lock lock = null;
580
581 if (!hasLock) {
582 lock = dlFolderLocalService.lockFolder(
583 userId, folder.getFolderId());
584 }
585
586 try {
587 return doMoveFolderToTrash(userId, folder);
588 }
589 finally {
590 if (!hasLock) {
591 dlFolderLocalService.unlockFolder(
592 folder.getFolderId(), lock.getUuid());
593 }
594 }
595 }
596
597 @Override
598 public void restoreDependentsFromTrash(DLFolder dlFolder)
599 throws PortalException {
600
601 trashOrRestoreFolder(dlFolder, false);
602 }
603
604
608 @Deprecated
609 @Override
610 public void restoreDependentsFromTrash(
611 List<Object> dlFileEntriesAndDLFolders)
612 throws PortalException {
613
614 if (dlFileEntriesAndDLFolders.isEmpty()) {
615 return;
616 }
617
618 Object object = dlFileEntriesAndDLFolders.get(0);
619
620 long folderId = 0;
621
622 if (object instanceof DLFileEntry) {
623 DLFileEntry dlFileEntry = (DLFileEntry)object;
624
625 folderId = dlFileEntry.getFolderId();
626 }
627 else if (object instanceof DLFileShortcut) {
628 DLFileShortcut dlFileShortcut = (DLFileShortcut)object;
629
630 folderId = dlFileShortcut.getFolderId();
631 }
632 else if (object instanceof DLFolder) {
633 DLFolder dlFolder = (DLFolder)object;
634
635 folderId = dlFolder.getFolderId();
636 }
637
638 DLFolder dlFolder = dlFolderLocalService.getDLFolder(folderId);
639
640 restoreDependentsFromTrash(dlFolder);
641 }
642
643
647 @Deprecated
648 @Override
649 public void restoreDependentsFromTrash(
650 List<Object> dlFileEntriesAndDLFolders, long trashEntryId)
651 throws PortalException {
652
653 restoreDependentsFromTrash(dlFileEntriesAndDLFolders);
654 }
655
656 @Override
657 public void restoreFileEntryFromTrash(long userId, FileEntry fileEntry)
658 throws PortalException {
659
660
661
662 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
663
664 dlFileEntry.setFileName(
665 TrashUtil.getOriginalTitle(dlFileEntry.getTitle(), "fileName"));
666 dlFileEntry.setTitle(
667 TrashUtil.getOriginalTitle(dlFileEntry.getTitle()));
668
669 dlFileEntryPersistence.update(dlFileEntry);
670
671 FileVersion fileVersion = fileEntry.getFileVersion();
672
673 TrashEntry trashEntry = trashEntryLocalService.getEntry(
674 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
675
676 dlFileEntryLocalService.updateStatus(
677 userId, fileVersion.getFileVersionId(), trashEntry.getStatus(),
678 new ServiceContext(), new HashMap<String, Serializable>());
679
680 if (DLAppHelperThreadLocal.isEnabled()) {
681
682
683
684 dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
685
686
687
688 dlFileShortcutLocalService.enableFileShortcuts(
689 fileEntry.getFileEntryId());
690
691
692
693 triggerRepositoryEvent(
694 fileEntry.getRepositoryId(),
695 TrashRepositoryEventType.EntryRestored.class, FileEntry.class,
696 fileEntry);
697 }
698
699
700
701 List<TrashVersion> trashVersions = trashVersionLocalService.getVersions(
702 trashEntry.getEntryId());
703
704 for (TrashVersion trashVersion : trashVersions) {
705 DLFileVersion trashDLFileVersion =
706 dlFileVersionPersistence.findByPrimaryKey(
707 trashVersion.getClassPK());
708
709 trashDLFileVersion.setStatus(trashVersion.getStatus());
710
711 dlFileVersionPersistence.update(trashDLFileVersion);
712 }
713
714 trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
715
716 if (!DLAppHelperThreadLocal.isEnabled()) {
717 return;
718 }
719
720
721
722 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
723
724 extraDataJSONObject.put("title", fileEntry.getTitle());
725
726 SocialActivityManagerUtil.addActivity(
727 userId, fileEntry, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
728 extraDataJSONObject.toString(), 0);
729 }
730
731 @Override
732 public void restoreFileShortcutFromTrash(
733 long userId, FileShortcut fileShortcut)
734 throws PortalException {
735
736
737
738 TrashEntry trashEntry = trashEntryLocalService.getEntry(
739 DLFileShortcutConstants.getClassName(),
740 fileShortcut.getFileShortcutId());
741
742 dlFileShortcutLocalService.updateStatus(
743 userId, fileShortcut.getFileShortcutId(), trashEntry.getStatus(),
744 new ServiceContext());
745
746
747
748 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
749
750 extraDataJSONObject.put("title", fileShortcut.getToTitle());
751
752 SocialActivityManagerUtil.addActivity(
753 userId, fileShortcut,
754 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
755 extraDataJSONObject.toString(), 0);
756
757
758
759 trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
760 }
761
762 @Override
763 public void restoreFolderFromTrash(long userId, Folder folder)
764 throws PortalException {
765
766
767
768 DLFolder dlFolder = (DLFolder)folder.getModel();
769
770 dlFolder.setName(TrashUtil.getOriginalTitle(dlFolder.getName()));
771
772 dlFolderPersistence.update(dlFolder);
773
774 TrashEntry trashEntry = trashEntryLocalService.getEntry(
775 DLFolder.class.getName(), dlFolder.getFolderId());
776
777 dlFolderLocalService.updateStatus(
778 userId, folder.getFolderId(), trashEntry.getStatus(),
779 new HashMap<String, Serializable>(), new ServiceContext());
780
781
782
783 dlFileRankLocalService.enableFileRanksByFolderId(folder.getFolderId());
784
785
786
787 restoreDependentsFromTrash(dlFolder);
788
789
790
791 triggerRepositoryEvent(
792 folder.getRepositoryId(),
793 TrashRepositoryEventType.EntryRestored.class, Folder.class, folder);
794
795
796
797 trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
798
799
800
801 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
802
803 extraDataJSONObject.put("title", folder.getName());
804
805 SocialActivityManagerUtil.addActivity(
806 userId, folder, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
807 extraDataJSONObject.toString(), 0);
808 }
809
810 @Override
811 public AssetEntry updateAsset(
812 long userId, FileEntry fileEntry, FileVersion fileVersion,
813 long assetClassPk)
814 throws PortalException {
815
816 long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
817 DLFileEntryConstants.getClassName(), assetClassPk);
818 String[] assetTagNames = assetTagLocalService.getTagNames(
819 DLFileEntryConstants.getClassName(), assetClassPk);
820
821 AssetEntry assetEntry = assetEntryLocalService.getEntry(
822 DLFileEntryConstants.getClassName(), assetClassPk);
823
824 List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
825 assetEntry.getEntryId());
826
827 long[] assetLinkIds = ListUtil.toLongArray(
828 assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
829
830 return updateAsset(
831 userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
832 assetLinkIds);
833 }
834
835 @Override
836 public AssetEntry updateAsset(
837 long userId, FileEntry fileEntry, FileVersion fileVersion,
838 long[] assetCategoryIds, String[] assetTagNames,
839 long[] assetLinkEntryIds)
840 throws PortalException {
841
842 AssetEntry assetEntry = null;
843
844 boolean visible = false;
845
846 boolean addDraftAssetEntry = false;
847
848 if (fileEntry instanceof LiferayFileEntry) {
849 DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
850
851 if (dlFileVersion.isApproved()) {
852 visible = true;
853 }
854 else {
855 String version = dlFileVersion.getVersion();
856
857 if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
858 addDraftAssetEntry = true;
859 }
860 }
861 }
862 else {
863 visible = true;
864 }
865
866 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
867
868 if (addDraftAssetEntry) {
869 if (assetCategoryIds == null) {
870 assetCategoryIds = assetCategoryLocalService.getCategoryIds(
871 DLFileEntryConstants.getClassName(),
872 fileEntry.getFileEntryId());
873 }
874
875 if (assetTagNames == null) {
876 assetTagNames = assetTagLocalService.getTagNames(
877 DLFileEntryConstants.getClassName(),
878 fileEntry.getFileEntryId());
879 }
880
881 if (assetLinkEntryIds == null) {
882 AssetEntry previousAssetEntry = assetEntryLocalService.getEntry(
883 DLFileEntryConstants.getClassName(),
884 fileEntry.getFileEntryId());
885
886 List<AssetLink> assetLinks =
887 assetLinkLocalService.getDirectLinks(
888 previousAssetEntry.getEntryId(),
889 AssetLinkConstants.TYPE_RELATED);
890
891 assetLinkEntryIds = ListUtil.toLongArray(
892 assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
893 }
894
895 assetEntry = assetEntryLocalService.updateEntry(
896 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
897 fileEntry.getModifiedDate(),
898 DLFileEntryConstants.getClassName(),
899 fileVersion.getFileVersionId(), fileEntry.getUuid(),
900 fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
901 null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
902 fileEntry.getDescription(), null, null, null, 0, 0, null);
903 }
904 else {
905 assetEntry = assetEntryLocalService.updateEntry(
906 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
907 fileEntry.getModifiedDate(),
908 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
909 fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
910 assetTagNames, visible, null, null, null,
911 fileEntry.getMimeType(), fileEntry.getTitle(),
912 fileEntry.getDescription(), null, null, null, 0, 0, null);
913
914 List<DLFileShortcut> dlFileShortcuts =
915 dlFileShortcutPersistence.findByToFileEntryId(
916 fileEntry.getFileEntryId());
917
918 for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
919 assetEntryLocalService.updateEntry(
920 userId, dlFileShortcut.getGroupId(),
921 dlFileShortcut.getCreateDate(),
922 dlFileShortcut.getModifiedDate(),
923 DLFileShortcutConstants.getClassName(),
924 dlFileShortcut.getFileShortcutId(),
925 dlFileShortcut.getUuid(), fileEntryTypeId, assetCategoryIds,
926 assetTagNames, true, null, null, null,
927 fileEntry.getMimeType(), fileEntry.getTitle(),
928 fileEntry.getDescription(), null, null, null, 0, 0, null);
929 }
930 }
931
932 assetLinkLocalService.updateLinks(
933 userId, assetEntry.getEntryId(), assetLinkEntryIds,
934 AssetLinkConstants.TYPE_RELATED);
935
936 return assetEntry;
937 }
938
939 @Override
940 public AssetEntry updateAsset(
941 long userId, Folder folder, long[] assetCategoryIds,
942 String[] assetTagNames, long[] assetLinkEntryIds)
943 throws PortalException {
944
945 AssetEntry assetEntry = null;
946
947 boolean visible = false;
948
949 if (folder instanceof LiferayFolder) {
950 DLFolder dlFolder = (DLFolder)folder.getModel();
951
952 if (dlFolder.isApproved() && !dlFolder.isHidden() &&
953 !dlFolder.isInHiddenFolder()) {
954
955 visible = true;
956 }
957 }
958 else {
959 visible = true;
960 }
961
962 assetEntry = assetEntryLocalService.updateEntry(
963 userId, folder.getGroupId(), folder.getCreateDate(),
964 folder.getModifiedDate(), DLFolderConstants.getClassName(),
965 folder.getFolderId(), folder.getUuid(), 0, assetCategoryIds,
966 assetTagNames, visible, null, null, null, null, folder.getName(),
967 folder.getDescription(), null, null, null, 0, 0, null);
968
969 assetLinkLocalService.updateLinks(
970 userId, assetEntry.getEntryId(), assetLinkEntryIds,
971 AssetLinkConstants.TYPE_RELATED);
972
973 return assetEntry;
974 }
975
976 @Override
977 public void updateFileEntry(
978 long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
979 FileVersion destinationFileVersion, long assetClassPk)
980 throws PortalException {
981
982 if (!DLAppHelperThreadLocal.isEnabled()) {
983 return;
984 }
985
986 boolean updateAsset = true;
987
988 if (fileEntry instanceof LiferayFileEntry &&
989 fileEntry.getVersion().equals(
990 destinationFileVersion.getVersion())) {
991
992 updateAsset = false;
993 }
994
995 if (updateAsset) {
996 updateAsset(
997 userId, fileEntry, destinationFileVersion, assetClassPk);
998 }
999 }
1000
1001 @Override
1002 public void updateFileEntry(
1003 long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
1004 FileVersion destinationFileVersion, ServiceContext serviceContext)
1005 throws PortalException {
1006
1007 if (!DLAppHelperThreadLocal.isEnabled()) {
1008 return;
1009 }
1010
1011 updateAsset(
1012 userId, fileEntry, destinationFileVersion,
1013 serviceContext.getAssetCategoryIds(),
1014 serviceContext.getAssetTagNames(),
1015 serviceContext.getAssetLinkEntryIds());
1016 }
1017
1018 @Override
1019 public void updateFolder(
1020 long userId, Folder folder, ServiceContext serviceContext)
1021 throws PortalException {
1022
1023 updateAsset(
1024 userId, folder, serviceContext.getAssetCategoryIds(),
1025 serviceContext.getAssetTagNames(),
1026 serviceContext.getAssetLinkEntryIds());
1027 }
1028
1029 @Override
1030 public void updateStatus(
1031 long userId, FileEntry fileEntry, FileVersion latestFileVersion,
1032 int oldStatus, int newStatus, ServiceContext serviceContext,
1033 Map<String, Serializable> workflowContext)
1034 throws PortalException {
1035
1036 if (!DLAppHelperThreadLocal.isEnabled()) {
1037 return;
1038 }
1039
1040 if (newStatus == WorkflowConstants.STATUS_APPROVED) {
1041
1042
1043
1044 String latestFileVersionVersion = latestFileVersion.getVersion();
1045
1046 if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
1047 if (!latestFileVersionVersion.equals(
1048 DLFileEntryConstants.VERSION_DEFAULT)) {
1049
1050 AssetEntry draftAssetEntry =
1051 assetEntryLocalService.fetchEntry(
1052 DLFileEntryConstants.getClassName(),
1053 latestFileVersion.getPrimaryKey());
1054
1055 if (draftAssetEntry != null) {
1056 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
1057
1058 long[] assetCategoryIds =
1059 draftAssetEntry.getCategoryIds();
1060 String[] assetTagNames = draftAssetEntry.getTagNames();
1061
1062 List<AssetLink> assetLinks =
1063 assetLinkLocalService.getDirectLinks(
1064 draftAssetEntry.getEntryId(),
1065 AssetLinkConstants.TYPE_RELATED);
1066
1067 long[] assetLinkEntryIds = ListUtil.toLongArray(
1068 assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);
1069
1070 AssetEntry assetEntry =
1071 assetEntryLocalService.updateEntry(
1072 userId, fileEntry.getGroupId(),
1073 fileEntry.getCreateDate(),
1074 fileEntry.getModifiedDate(),
1075 DLFileEntryConstants.getClassName(),
1076 fileEntry.getFileEntryId(), fileEntry.getUuid(),
1077 fileEntryTypeId, assetCategoryIds,
1078 assetTagNames, true, null, null, null,
1079 draftAssetEntry.getMimeType(),
1080 fileEntry.getTitle(),
1081 fileEntry.getDescription(), null, null, null, 0,
1082 0, null);
1083
1084 assetLinkLocalService.updateLinks(
1085 userId, assetEntry.getEntryId(), assetLinkEntryIds,
1086 AssetLinkConstants.TYPE_RELATED);
1087
1088 assetEntryLocalService.deleteEntry(draftAssetEntry);
1089 }
1090 }
1091
1092 AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
1093 DLFileEntryConstants.getClassName(),
1094 fileEntry.getFileEntryId());
1095
1096 if (assetEntry != null) {
1097 assetEntryLocalService.updateVisible(
1098 DLFileEntryConstants.getClassName(),
1099 fileEntry.getFileEntryId(), true);
1100 }
1101 }
1102
1103
1104
1105 String event = GetterUtil.getString(workflowContext.get("event"));
1106
1107 if (Validator.isNotNull(event)) {
1108 triggerRepositoryEvent(
1109 fileEntry.getRepositoryId(),
1110 getWorkflowRepositoryEventTypeClass(event), FileEntry.class,
1111 fileEntry);
1112 }
1113
1114 if ((oldStatus != WorkflowConstants.STATUS_IN_TRASH) &&
1115 !fileEntry.isInTrash()) {
1116
1117
1118
1119 Date activityCreateDate = latestFileVersion.getModifiedDate();
1120 int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
1121
1122 if (event.equals(DLSyncConstants.EVENT_ADD)) {
1123 activityCreateDate = latestFileVersion.getCreateDate();
1124 activityType = DLActivityKeys.ADD_FILE_ENTRY;
1125 }
1126
1127 JSONObject extraDataJSONObject =
1128 JSONFactoryUtil.createJSONObject();
1129
1130 extraDataJSONObject.put("title", fileEntry.getTitle());
1131
1132 SocialActivityManagerUtil.addUniqueActivity(
1133 latestFileVersion.getStatusByUserId(), activityCreateDate,
1134 fileEntry, activityType, extraDataJSONObject.toString(), 0);
1135
1136
1137
1138 notifySubscribers(
1139 userId, latestFileVersion,
1140 (String)workflowContext.get(WorkflowConstants.CONTEXT_URL),
1141 serviceContext);
1142 }
1143 }
1144 else {
1145
1146
1147
1148 boolean visible = false;
1149
1150 if (newStatus != WorkflowConstants.STATUS_IN_TRASH) {
1151 List<DLFileVersion> approvedFileVersions =
1152 dlFileVersionPersistence.findByF_S(
1153 fileEntry.getFileEntryId(),
1154 WorkflowConstants.STATUS_APPROVED);
1155
1156 if (!approvedFileVersions.isEmpty()) {
1157 visible = true;
1158 }
1159 }
1160
1161 assetEntryLocalService.updateVisible(
1162 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
1163 visible);
1164 }
1165 }
1166
1167 protected FileEntry doMoveFileEntryFromTrash(
1168 long userId, FileEntry fileEntry, long newFolderId,
1169 ServiceContext serviceContext)
1170 throws PortalException {
1171
1172
1173
1174 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1175
1176 if (dlFileEntry.isInTrashExplicitly()) {
1177 restoreFileEntryFromTrash(userId, fileEntry);
1178
1179 fileEntry = dlAppLocalService.moveFileEntry(
1180 userId, fileEntry.getFileEntryId(), newFolderId,
1181 serviceContext);
1182
1183 if (DLAppHelperThreadLocal.isEnabled()) {
1184 dlFileRankLocalService.enableFileRanks(
1185 fileEntry.getFileEntryId());
1186 }
1187
1188 return fileEntry;
1189 }
1190
1191 List<DLFileVersion> dlFileVersions =
1192 dlFileVersionLocalService.getFileVersions(
1193 fileEntry.getFileEntryId(), WorkflowConstants.STATUS_IN_TRASH);
1194
1195 dlFileVersions = ListUtil.sort(
1196 dlFileVersions, new DLFileVersionVersionComparator());
1197
1198 FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
1199
1200 TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1201 DLFileVersion.class.getName(), fileVersion.getFileVersionId());
1202
1203 int oldStatus = WorkflowConstants.STATUS_APPROVED;
1204
1205 if (trashVersion != null) {
1206 oldStatus = trashVersion.getStatus();
1207 }
1208
1209 dlFileEntryLocalService.updateStatus(
1210 userId, fileVersion.getFileVersionId(), oldStatus, serviceContext,
1211 new HashMap<String, Serializable>());
1212
1213
1214
1215 for (DLFileVersion dlFileVersion : dlFileVersions) {
1216
1217
1218
1219 trashVersion = trashVersionLocalService.fetchVersion(
1220 DLFileVersion.class.getName(),
1221 dlFileVersion.getFileVersionId());
1222
1223 oldStatus = WorkflowConstants.STATUS_APPROVED;
1224
1225 if (trashVersion != null) {
1226 oldStatus = trashVersion.getStatus();
1227 }
1228
1229 dlFileVersion.setStatus(oldStatus);
1230
1231 dlFileVersionPersistence.update(dlFileVersion);
1232
1233
1234
1235 if (trashVersion != null) {
1236 trashVersionLocalService.deleteTrashVersion(trashVersion);
1237 }
1238 }
1239
1240 if (DLAppHelperThreadLocal.isEnabled()) {
1241
1242
1243
1244 dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
1245
1246
1247
1248 dlFileShortcutLocalService.enableFileShortcuts(
1249 fileEntry.getFileEntryId());
1250 }
1251
1252
1253
1254 fileEntry = dlAppService.moveFileEntry(
1255 fileEntry.getFileEntryId(), newFolderId, serviceContext);
1256
1257
1258
1259 triggerRepositoryEvent(
1260 fileEntry.getRepositoryId(),
1261 TrashRepositoryEventType.EntryRestored.class, FileEntry.class,
1262 fileEntry);
1263
1264
1265
1266 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1267
1268 extraDataJSONObject.put("title", fileEntry.getTitle());
1269
1270 SocialActivityManagerUtil.addActivity(
1271 userId, fileEntry, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1272 extraDataJSONObject.toString(), 0);
1273
1274 return fileEntry;
1275 }
1276
1277 protected FileEntry doMoveFileEntryToTrash(long userId, FileEntry fileEntry)
1278 throws PortalException {
1279
1280
1281
1282 List<DLFileVersion> dlFileVersions =
1283 dlFileVersionLocalService.getFileVersions(
1284 fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1285
1286 dlFileVersions = ListUtil.sort(
1287 dlFileVersions, new DLFileVersionVersionComparator());
1288
1289 List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1290 new ArrayList<>();
1291
1292 if ((dlFileVersions != null) && !dlFileVersions.isEmpty()) {
1293 dlFileVersionStatusOVPs = getDlFileVersionStatuses(dlFileVersions);
1294 }
1295
1296 FileVersion fileVersion = fileEntry.getFileVersion();
1297
1298 int oldStatus = fileVersion.getStatus();
1299
1300 dlFileEntryLocalService.updateStatus(
1301 userId, fileVersion.getFileVersionId(),
1302 WorkflowConstants.STATUS_IN_TRASH, new ServiceContext(),
1303 new HashMap<String, Serializable>());
1304
1305 if (DLAppHelperThreadLocal.isEnabled()) {
1306
1307
1308
1309 dlFileShortcutLocalService.disableFileShortcuts(
1310 fileEntry.getFileEntryId());
1311
1312
1313
1314 dlFileRankLocalService.disableFileRanks(fileEntry.getFileEntryId());
1315
1316
1317
1318 triggerRepositoryEvent(
1319 fileEntry.getRepositoryId(),
1320 TrashRepositoryEventType.EntryTrashed.class, FileEntry.class,
1321 fileEntry);
1322 }
1323
1324
1325
1326 DLFileVersion oldDLFileVersion = (DLFileVersion)fileVersion.getModel();
1327
1328 int oldDLFileVersionStatus = oldDLFileVersion.getStatus();
1329
1330 for (DLFileVersion curDLFileVersion : dlFileVersions) {
1331 curDLFileVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1332
1333 dlFileVersionPersistence.update(curDLFileVersion);
1334 }
1335
1336 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1337
1338 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1339
1340 typeSettingsProperties.put("fileName", dlFileEntry.getFileName());
1341 typeSettingsProperties.put("title", dlFileEntry.getTitle());
1342
1343 TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
1344 userId, dlFileEntry.getGroupId(),
1345 DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId(),
1346 dlFileEntry.getUuid(), dlFileEntry.getClassName(),
1347 oldDLFileVersionStatus, dlFileVersionStatusOVPs,
1348 typeSettingsProperties);
1349
1350 String trashTitle = TrashUtil.getTrashTitle(trashEntry.getEntryId());
1351
1352 dlFileEntry.setFileName(trashTitle);
1353 dlFileEntry.setTitle(trashTitle);
1354
1355 dlFileEntryPersistence.update(dlFileEntry);
1356
1357 if (!DLAppHelperThreadLocal.isEnabled()) {
1358 return fileEntry;
1359 }
1360
1361
1362
1363 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1364
1365 extraDataJSONObject.put(
1366 "title", TrashUtil.getOriginalTitle(fileEntry.getTitle()));
1367
1368 SocialActivityManagerUtil.addActivity(
1369 userId, fileEntry, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1370 extraDataJSONObject.toString(), 0);
1371
1372
1373
1374 if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1375 workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
1376 fileVersion.getCompanyId(), fileVersion.getGroupId(),
1377 DLFileEntryConstants.getClassName(),
1378 fileVersion.getFileVersionId());
1379 }
1380
1381 return fileEntry;
1382 }
1383
1384 protected Folder doMoveFolderFromTrash(
1385 long userId, Folder folder, long parentFolderId,
1386 ServiceContext serviceContext)
1387 throws PortalException {
1388
1389 DLFolder dlFolder = (DLFolder)folder.getModel();
1390
1391 if (dlFolder.isInTrashExplicitly()) {
1392 restoreFolderFromTrash(userId, folder);
1393 }
1394 else {
1395
1396
1397
1398 TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1399 DLFolder.class.getName(), dlFolder.getFolderId());
1400
1401 int status = WorkflowConstants.STATUS_APPROVED;
1402
1403 if (trashVersion != null) {
1404 status = trashVersion.getStatus();
1405 }
1406
1407 dlFolderLocalService.updateStatus(
1408 userId, folder.getFolderId(), status,
1409 new HashMap<String, Serializable>(), new ServiceContext());
1410
1411
1412
1413 dlFileRankLocalService.enableFileRanksByFolderId(
1414 folder.getFolderId());
1415
1416
1417
1418 if (trashVersion != null) {
1419 trashVersionLocalService.deleteTrashVersion(trashVersion);
1420 }
1421
1422
1423
1424 restoreDependentsFromTrash(dlFolder);
1425
1426
1427
1428 triggerRepositoryEvent(
1429 folder.getRepositoryId(),
1430 TrashRepositoryEventType.EntryRestored.class, Folder.class,
1431 folder);
1432
1433
1434
1435 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1436
1437 extraDataJSONObject.put("title", folder.getName());
1438
1439 SocialActivityManagerUtil.addActivity(
1440 userId, folder, SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1441 extraDataJSONObject.toString(), 0);
1442 }
1443
1444 return dlAppLocalService.moveFolder(
1445 userId, folder.getFolderId(), parentFolderId, serviceContext);
1446 }
1447
1448 protected Folder doMoveFolderToTrash(long userId, Folder folder)
1449 throws PortalException {
1450
1451
1452
1453 DLFolder dlFolder = dlFolderLocalService.updateStatus(
1454 userId, folder.getFolderId(), WorkflowConstants.STATUS_IN_TRASH,
1455 new HashMap<String, Serializable>(), new ServiceContext());
1456
1457
1458
1459 dlFileRankLocalService.disableFileRanksByFolderId(folder.getFolderId());
1460
1461
1462
1463 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1464
1465 typeSettingsProperties.put("title", dlFolder.getName());
1466
1467 TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(
1468 userId, dlFolder.getGroupId(), DLFolderConstants.getClassName(),
1469 dlFolder.getFolderId(), dlFolder.getUuid(), null,
1470 WorkflowConstants.STATUS_APPROVED, null, typeSettingsProperties);
1471
1472 dlFolder.setName(TrashUtil.getTrashTitle(trashEntry.getEntryId()));
1473
1474 dlFolderPersistence.update(dlFolder);
1475
1476
1477
1478 moveDependentsToTrash(dlFolder);
1479
1480
1481
1482 triggerRepositoryEvent(
1483 folder.getRepositoryId(),
1484 TrashRepositoryEventType.EntryTrashed.class, Folder.class, folder);
1485
1486
1487
1488 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
1489
1490 extraDataJSONObject.put("title", folder.getName());
1491
1492 SocialActivityManagerUtil.addActivity(
1493 userId, folder, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1494 extraDataJSONObject.toString(), 0);
1495
1496 return new LiferayFolder(dlFolder);
1497 }
1498
1499 protected List<ObjectValuePair<Long, Integer>> getDlFileVersionStatuses(
1500 List<DLFileVersion> dlFileVersions) {
1501
1502 List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1503 new ArrayList<>(dlFileVersions.size());
1504
1505 for (DLFileVersion dlFileVersion : dlFileVersions) {
1506 int status = dlFileVersion.getStatus();
1507
1508 if (status == WorkflowConstants.STATUS_PENDING) {
1509 status = WorkflowConstants.STATUS_DRAFT;
1510 }
1511
1512 ObjectValuePair<Long, Integer> dlFileVersionStatusOVP =
1513 new ObjectValuePair<>(dlFileVersion.getFileVersionId(), status);
1514
1515 dlFileVersionStatusOVPs.add(dlFileVersionStatusOVP);
1516 }
1517
1518 return dlFileVersionStatusOVPs;
1519 }
1520
1521 protected long getFileEntryTypeId(FileEntry fileEntry) {
1522 if (fileEntry instanceof LiferayFileEntry) {
1523 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1524
1525 return dlFileEntry.getFileEntryTypeId();
1526 }
1527
1528 return 0;
1529 }
1530
1531 protected Class<? extends WorkflowRepositoryEventType>
1532 getWorkflowRepositoryEventTypeClass(
1533 String syncEvent) {
1534
1535 if (syncEvent.equals(DLSyncConstants.EVENT_ADD)) {
1536 return WorkflowRepositoryEventType.Add.class;
1537 }
1538 else if (syncEvent.equals(DLSyncConstants.EVENT_UPDATE)) {
1539 return WorkflowRepositoryEventType.Update.class;
1540 }
1541 else {
1542 throw new IllegalArgumentException(
1543 String.format("Unsupported sync event %s", syncEvent));
1544 }
1545 }
1546
1547 protected void notifySubscribers(
1548 long userId, FileVersion fileVersion, String entryURL,
1549 ServiceContext serviceContext)
1550 throws PortalException {
1551
1552 if (!fileVersion.isApproved() || Validator.isNull(entryURL)) {
1553 return;
1554 }
1555
1556 DLGroupServiceSettings dlGroupServiceSettings =
1557 DLGroupServiceSettings.getInstance(fileVersion.getGroupId());
1558
1559 boolean commandUpdate = false;
1560
1561 if (serviceContext.isCommandUpdate() ||
1562 Constants.CHECKIN.equals(serviceContext.getCommand())) {
1563
1564 commandUpdate = true;
1565 }
1566
1567 if (serviceContext.isCommandAdd() &&
1568 dlGroupServiceSettings.isEmailFileEntryAddedEnabled()) {
1569 }
1570 else if (commandUpdate &&
1571 dlGroupServiceSettings.isEmailFileEntryUpdatedEnabled()) {
1572 }
1573 else {
1574 return;
1575 }
1576
1577 String entryTitle = fileVersion.getTitle();
1578
1579 String fromName = dlGroupServiceSettings.getEmailFromName();
1580 String fromAddress = dlGroupServiceSettings.getEmailFromAddress();
1581
1582 LocalizedValuesMap subjectLocalizedValuesMap = null;
1583 LocalizedValuesMap bodyLocalizedValuesMap = null;
1584
1585 if (commandUpdate) {
1586 subjectLocalizedValuesMap =
1587 dlGroupServiceSettings.getEmailFileEntryUpdatedSubject();
1588 bodyLocalizedValuesMap =
1589 dlGroupServiceSettings.getEmailFileEntryUpdatedBody();
1590 }
1591 else {
1592 subjectLocalizedValuesMap =
1593 dlGroupServiceSettings.getEmailFileEntryAddedSubject();
1594 bodyLocalizedValuesMap =
1595 dlGroupServiceSettings.getEmailFileEntryAddedBody();
1596 }
1597
1598 FileEntry fileEntry = fileVersion.getFileEntry();
1599
1600 Folder folder = null;
1601
1602 long folderId = fileEntry.getFolderId();
1603
1604 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
1605 folder = dlAppLocalService.getFolder(folderId);
1606 }
1607
1608 String folderName = LanguageUtil.get(
1609 serviceContext.getLocale(), "home");
1610
1611 if (folder != null) {
1612 folderName = folder.getName();
1613 }
1614
1615 SubscriptionSender subscriptionSender =
1616 new GroupSubscriptionCheckSubscriptionSender(
1617 DLPermission.RESOURCE_NAME);
1618
1619 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1620
1621 DLFileEntryType dlFileEntryType =
1622 dlFileEntryTypeLocalService.getDLFileEntryType(
1623 dlFileEntry.getFileEntryTypeId());
1624
1625 subscriptionSender.setClassPK(fileVersion.getFileEntryId());
1626 subscriptionSender.setClassName(DLFileEntryConstants.getClassName());
1627 subscriptionSender.setCompanyId(fileVersion.getCompanyId());
1628 subscriptionSender.setContextAttributes(
1629 "[$DOCUMENT_STATUS_BY_USER_NAME$]",
1630 fileVersion.getStatusByUserName(), "[$DOCUMENT_TITLE$]", entryTitle,
1631 "[$DOCUMENT_TYPE$]",
1632 dlFileEntryType.getName(serviceContext.getLocale()),
1633 "[$DOCUMENT_URL$]", entryURL, "[$FOLDER_NAME$]", folderName);
1634 subscriptionSender.setContextCreatorUserPrefix("DOCUMENT");
1635 subscriptionSender.setCreatorUserId(fileVersion.getUserId());
1636 subscriptionSender.setCurrentUserId(userId);
1637 subscriptionSender.setEntryTitle(entryTitle);
1638 subscriptionSender.setEntryURL(entryURL);
1639 subscriptionSender.setFrom(fromAddress, fromName);
1640 subscriptionSender.setHtmlFormat(true);
1641 subscriptionSender.setLocalizedBodyMap(
1642 LocalizationUtil.getMap(bodyLocalizedValuesMap));
1643 subscriptionSender.setLocalizedSubjectMap(
1644 LocalizationUtil.getMap(subjectLocalizedValuesMap));
1645 subscriptionSender.setMailId(
1646 "file_entry", fileVersion.getFileEntryId());
1647
1648 int notificationType =
1649 UserNotificationDefinition.NOTIFICATION_TYPE_ADD_ENTRY;
1650
1651 if (commandUpdate) {
1652 notificationType =
1653 UserNotificationDefinition.NOTIFICATION_TYPE_UPDATE_ENTRY;
1654 }
1655
1656 subscriptionSender.setNotificationType(notificationType);
1657
1658 String portletId = PortletProviderUtil.getPortletId(
1659 FileEntry.class.getName(), PortletProvider.Action.VIEW);
1660
1661 subscriptionSender.setPortletId(portletId);
1662
1663 subscriptionSender.setReplyToAddress(fromAddress);
1664 subscriptionSender.setScopeGroupId(fileVersion.getGroupId());
1665 subscriptionSender.setServiceContext(serviceContext);
1666
1667 subscriptionSender.addPersistedSubscribers(
1668 DLFolder.class.getName(), fileVersion.getGroupId());
1669
1670 if (folder != null) {
1671 subscriptionSender.addPersistedSubscribers(
1672 DLFolder.class.getName(), folder.getFolderId());
1673
1674 for (Long ancestorFolderId : folder.getAncestorFolderIds()) {
1675 subscriptionSender.addPersistedSubscribers(
1676 DLFolder.class.getName(), ancestorFolderId);
1677 }
1678 }
1679
1680 if (dlFileEntryType.getFileEntryTypeId() ==
1681 DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
1682
1683 subscriptionSender.addPersistedSubscribers(
1684 DLFileEntryType.class.getName(), fileVersion.getGroupId());
1685 }
1686 else {
1687 subscriptionSender.addPersistedSubscribers(
1688 DLFileEntryType.class.getName(),
1689 dlFileEntryType.getFileEntryTypeId());
1690 }
1691
1692 subscriptionSender.addPersistedSubscribers(
1693 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
1694
1695 subscriptionSender.flushNotificationsAsync();
1696 }
1697
1698 protected void trashOrRestoreFolder(DLFolder dlFolder, boolean moveToTrash)
1699 throws PortalException {
1700
1701 TrashEntry trashEntry = null;
1702
1703 if (moveToTrash) {
1704 trashEntry = trashEntryLocalService.getEntry(
1705 DLFolderConstants.getClassName(), dlFolder.getFolderId());
1706 }
1707
1708 long dlFileEntryClassNameId = classNameLocalService.getClassNameId(
1709 DLFileEntry.class);
1710
1711 List<AssetEntry> dlFileEntryAssetEntries =
1712 assetEntryFinder.findByDLFileEntryC_T(
1713 dlFileEntryClassNameId, dlFolder.getTreePath());
1714
1715 for (AssetEntry dlFileEntryAssetEntry : dlFileEntryAssetEntries) {
1716 assetEntryLocalService.updateVisible(
1717 dlFileEntryAssetEntry, !moveToTrash);
1718 }
1719
1720 long dlFolderClassNameId = classNameLocalService.getClassNameId(
1721 DLFolder.class);
1722
1723 List<AssetEntry> dlFolderAssetEntries =
1724 assetEntryFinder.findByDLFolderC_T(
1725 dlFolderClassNameId, dlFolder.getTreePath());
1726
1727 for (AssetEntry dlFolderAssetEntry : dlFolderAssetEntries) {
1728 assetEntryLocalService.updateVisible(
1729 dlFolderAssetEntry, !moveToTrash);
1730 }
1731
1732 List<DLFolder> dlFolders = dlFolderPersistence.findByG_M_T_H(
1733 dlFolder.getGroupId(), false,
1734 CustomSQLUtil.keywords(
1735 dlFolder.getTreePath(), WildcardMode.TRAILING)[0],
1736 false);
1737
1738 for (DLFolder childDLFolder : dlFolders) {
1739 trashOrRestoreFolder(
1740 dlFolder, childDLFolder, moveToTrash, trashEntry);
1741 }
1742 }
1743
1744 protected void trashOrRestoreFolder(
1745 DLFolder dlFolder, DLFolder childDLFolder, boolean moveToTrash,
1746 TrashEntry trashEntry)
1747 throws PortalException {
1748
1749 List<DLFileEntry> dlFileEntries =
1750 dlFileEntryLocalService.getFileEntries(
1751 childDLFolder.getGroupId(), childDLFolder.getFolderId());
1752
1753 for (DLFileEntry dlFileEntry : dlFileEntries) {
1754 if (moveToTrash) {
1755 if (dlFileEntry.isInTrashExplicitly()) {
1756 continue;
1757 }
1758 }
1759 else if (!dlFileEntry.isInTrashImplicitly()) {
1760 continue;
1761 }
1762
1763
1764
1765 dlFileShortcutLocalService.updateFileShortcutsActive(
1766 dlFileEntry.getFileEntryId(), !moveToTrash);
1767
1768
1769
1770 List<DLFileVersion> dlFileVersions = null;
1771
1772 if (moveToTrash) {
1773 dlFileVersions = dlFileVersionLocalService.getFileVersions(
1774 dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1775 }
1776 else {
1777 dlFileVersions = dlFileVersionLocalService.getFileVersions(
1778 dlFileEntry.getFileEntryId(),
1779 WorkflowConstants.STATUS_IN_TRASH);
1780 }
1781
1782 for (DLFileVersion dlFileVersion : dlFileVersions) {
1783
1784
1785
1786 if (moveToTrash) {
1787 int oldStatus = dlFileVersion.getStatus();
1788
1789 dlFileVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1790
1791 dlFileVersionPersistence.update(dlFileVersion);
1792
1793
1794
1795 if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
1796 int newStatus = oldStatus;
1797
1798 if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1799 newStatus = WorkflowConstants.STATUS_DRAFT;
1800 }
1801
1802 trashVersionLocalService.addTrashVersion(
1803 trashEntry.getEntryId(),
1804 DLFileVersion.class.getName(),
1805 dlFileVersion.getFileVersionId(), newStatus, null);
1806 }
1807
1808
1809
1810 if (oldStatus == WorkflowConstants.STATUS_PENDING) {
1811 workflowInstanceLinkLocalService.
1812 deleteWorkflowInstanceLink(
1813 dlFileVersion.getCompanyId(),
1814 dlFileVersion.getGroupId(),
1815 DLFileEntryConstants.getClassName(),
1816 dlFileVersion.getFileVersionId());
1817 }
1818 }
1819 else {
1820 TrashVersion trashVersion =
1821 trashVersionLocalService.fetchVersion(
1822 DLFileVersion.class.getName(),
1823 dlFileVersion.getFileVersionId());
1824
1825 int oldStatus = WorkflowConstants.STATUS_APPROVED;
1826
1827 if (trashVersion != null) {
1828 oldStatus = trashVersion.getStatus();
1829 }
1830
1831 dlFileVersion.setStatus(oldStatus);
1832
1833 dlFileVersionPersistence.update(dlFileVersion);
1834
1835
1836
1837 if (trashVersion != null) {
1838 trashVersionLocalService.deleteTrashVersion(
1839 trashVersion);
1840 }
1841 }
1842 }
1843
1844
1845
1846 Indexer<DLFileEntry> indexer =
1847 IndexerRegistryUtil.nullSafeGetIndexer(DLFileEntry.class);
1848
1849 indexer.reindex(dlFileEntry);
1850 }
1851
1852 List<DLFileShortcut> dlFileShortcuts =
1853 dlFileShortcutPersistence.findByG_F(
1854 childDLFolder.getGroupId(), childDLFolder.getFolderId());
1855
1856 for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
1857 if (moveToTrash) {
1858 if (dlFileShortcut.isInTrashExplicitly()) {
1859 continue;
1860 }
1861
1862 int oldStatus = dlFileShortcut.getStatus();
1863
1864 dlFileShortcut.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1865
1866 dlFileShortcutPersistence.update(dlFileShortcut);
1867
1868
1869
1870 if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
1871 trashVersionLocalService.addTrashVersion(
1872 trashEntry.getEntryId(),
1873 DLFileShortcutConstants.getClassName(),
1874 dlFileShortcut.getFileShortcutId(), oldStatus, null);
1875 }
1876 }
1877 else {
1878 if (!dlFileShortcut.isInTrashImplicitly()) {
1879 continue;
1880 }
1881
1882 TrashVersion trashVersion =
1883 trashVersionLocalService.fetchVersion(
1884 DLFileShortcutConstants.getClassName(),
1885 dlFileShortcut.getFileShortcutId());
1886
1887 int oldStatus = WorkflowConstants.STATUS_APPROVED;
1888
1889 if (trashVersion != null) {
1890 oldStatus = trashVersion.getStatus();
1891 }
1892
1893 dlFileShortcut.setStatus(oldStatus);
1894
1895 dlFileShortcutPersistence.update(dlFileShortcut);
1896
1897 if (trashVersion != null) {
1898 trashVersionLocalService.deleteTrashVersion(trashVersion);
1899 }
1900 }
1901 }
1902
1903 if (childDLFolder.equals(dlFolder)) {
1904 return;
1905 }
1906
1907 if (moveToTrash) {
1908 if (childDLFolder.isInTrashExplicitly()) {
1909 return;
1910 }
1911
1912 int oldStatus = childDLFolder.getStatus();
1913
1914 childDLFolder.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1915
1916 dlFolderPersistence.update(childDLFolder);
1917
1918
1919
1920 if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
1921 trashVersionLocalService.addTrashVersion(
1922 trashEntry.getEntryId(), DLFolder.class.getName(),
1923 childDLFolder.getFolderId(), oldStatus, null);
1924 }
1925 }
1926 else {
1927 if (!childDLFolder.isInTrashImplicitly()) {
1928 return;
1929 }
1930
1931 TrashVersion trashVersion = trashVersionLocalService.fetchVersion(
1932 DLFolder.class.getName(), childDLFolder.getFolderId());
1933
1934 int oldStatus = WorkflowConstants.STATUS_APPROVED;
1935
1936 if (trashVersion != null) {
1937 oldStatus = trashVersion.getStatus();
1938 }
1939
1940 childDLFolder.setStatus(oldStatus);
1941
1942 dlFolderPersistence.update(childDLFolder);
1943
1944
1945
1946 if (trashVersion != null) {
1947 trashVersionLocalService.deleteTrashVersion(trashVersion);
1948 }
1949 }
1950
1951
1952
1953 Indexer<DLFolder> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1954 DLFolder.class);
1955
1956 indexer.reindex(childDLFolder);
1957 }
1958
1959 protected <T extends RepositoryModel<T>> void triggerRepositoryEvent(
1960 long repositoryId,
1961 Class<? extends RepositoryEventType> repositoryEventType,
1962 Class<T> modelClass, T target)
1963 throws PortalException {
1964
1965 Repository repository = RepositoryProviderUtil.getRepository(
1966 repositoryId);
1967
1968 if (repository.isCapabilityProvided(
1969 RepositoryEventTriggerCapability.class)) {
1970
1971 RepositoryEventTriggerCapability repositoryEventTriggerCapability =
1972 repository.getCapability(
1973 RepositoryEventTriggerCapability.class);
1974
1975 repositoryEventTriggerCapability.trigger(
1976 repositoryEventType, modelClass, target);
1977 }
1978 }
1979
1980 @BeanReference(type = DLAppService.class)
1981 protected DLAppService dlAppService;
1982
1983 }