001
014
015 package com.liferay.portlet.documentlibrary.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.repository.model.FileEntry;
023 import com.liferay.portal.kernel.repository.model.FileVersion;
024 import com.liferay.portal.kernel.repository.model.Folder;
025 import com.liferay.portal.kernel.search.Indexer;
026 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027 import com.liferay.portal.kernel.transaction.TransactionCommitCallbackRegistryUtil;
028 import com.liferay.portal.kernel.util.ListUtil;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.workflow.WorkflowConstants;
033 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
034 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
035 import com.liferay.portal.model.Group;
036 import com.liferay.portal.model.User;
037 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
038 import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
039 import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
040 import com.liferay.portal.service.ServiceContext;
041 import com.liferay.portal.util.PropsValues;
042 import com.liferay.portlet.asset.NoSuchEntryException;
043 import com.liferay.portlet.asset.model.AssetEntry;
044 import com.liferay.portlet.asset.model.AssetLink;
045 import com.liferay.portlet.asset.model.AssetLinkConstants;
046 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
047 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
048 import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
049 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
050 import com.liferay.portlet.documentlibrary.model.DLFolder;
051 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
052 import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
053 import com.liferay.portlet.documentlibrary.service.base.DLAppHelperLocalServiceBaseImpl;
054 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
055 import com.liferay.portlet.documentlibrary.util.DLAppHelperThreadLocal;
056 import com.liferay.portlet.documentlibrary.util.DLProcessorRegistryUtil;
057 import com.liferay.portlet.documentlibrary.util.comparator.FileVersionVersionComparator;
058 import com.liferay.portlet.social.model.SocialActivityConstants;
059 import com.liferay.portlet.trash.model.TrashEntry;
060 import com.liferay.portlet.trash.model.TrashVersion;
061 import com.liferay.portlet.trash.util.TrashUtil;
062
063 import java.io.Serializable;
064
065 import java.util.Collections;
066 import java.util.HashMap;
067 import java.util.List;
068 import java.util.Map;
069 import java.util.concurrent.Callable;
070
071
074 public class DLAppHelperLocalServiceImpl
075 extends DLAppHelperLocalServiceBaseImpl {
076
077 public void addFileEntry(
078 long userId, FileEntry fileEntry, FileVersion fileVersion,
079 ServiceContext serviceContext)
080 throws PortalException, SystemException {
081
082 if (DLAppHelperThreadLocal.isEnabled()) {
083 updateAsset(
084 userId, fileEntry, fileVersion,
085 serviceContext.getAssetCategoryIds(),
086 serviceContext.getAssetTagNames(),
087 serviceContext.getAssetLinkEntryIds());
088
089 if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
090 mbMessageLocalService.addDiscussionMessage(
091 fileEntry.getUserId(), fileEntry.getUserName(),
092 fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
093 fileEntry.getFileEntryId(),
094 WorkflowConstants.ACTION_PUBLISH);
095 }
096 }
097
098 boolean previousEnabled = WorkflowThreadLocal.isEnabled();
099
100 if (!DLAppHelperThreadLocal.isEnabled()) {
101 WorkflowThreadLocal.setEnabled(false);
102 }
103
104 try {
105 if (fileVersion instanceof LiferayFileVersion) {
106 DLFileVersion dlFileVersion =
107 (DLFileVersion)fileVersion.getModel();
108
109 Map<String, Serializable> workflowContext =
110 new HashMap<String, Serializable>();
111
112 workflowContext.put("event", DLSyncConstants.EVENT_ADD);
113
114 WorkflowHandlerRegistryUtil.startWorkflowInstance(
115 dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(),
116 userId, DLFileEntryConstants.getClassName(),
117 dlFileVersion.getFileVersionId(), dlFileVersion,
118 serviceContext, workflowContext);
119 }
120 }
121 finally {
122 if (!DLAppHelperThreadLocal.isEnabled()) {
123 WorkflowThreadLocal.setEnabled(previousEnabled);
124 }
125 }
126
127 if (DLAppHelperThreadLocal.isEnabled()) {
128 registerDLProcessorCallback(fileEntry, null);
129 }
130 }
131
132 public void addFolder(Folder folder, ServiceContext serviceContext)
133 throws PortalException, SystemException {
134
135 if (!isStagingGroup(folder.getGroupId())) {
136 dlSyncLocalService.addSync(
137 folder.getFolderId(), folder.getUuid(), folder.getCompanyId(),
138 folder.getRepositoryId(), folder.getParentFolderId(),
139 folder.getName(), folder.getDescription(),
140 DLSyncConstants.TYPE_FOLDER, "-1");
141 }
142 }
143
144 public void cancelCheckOut(
145 long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
146 FileVersion destinationFileVersion, FileVersion draftFileVersion,
147 ServiceContext serviceContext)
148 throws PortalException, SystemException {
149
150 updateFileEntry(
151 userId, fileEntry, sourceFileVersion, destinationFileVersion,
152 serviceContext);
153
154 if (draftFileVersion == null) {
155 return;
156 }
157
158 AssetEntry draftAssetEntry = null;
159
160 try {
161 draftAssetEntry = assetEntryLocalService.getEntry(
162 DLFileEntryConstants.getClassName(),
163 draftFileVersion.getPrimaryKey());
164
165 assetEntryLocalService.deleteEntry(draftAssetEntry.getEntryId());
166 }
167 catch (NoSuchEntryException nsee) {
168 }
169 }
170
171 public void checkAssetEntry(
172 long userId, FileEntry fileEntry, FileVersion fileVersion)
173 throws PortalException, SystemException {
174
175 AssetEntry fileEntryAssetEntry = assetEntryLocalService.fetchEntry(
176 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
177
178 long[] assetCategoryIds = new long[0];
179 String[] assetTagNames = new String[0];
180
181 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
182
183 if (fileEntryAssetEntry == null) {
184 fileEntryAssetEntry = assetEntryLocalService.updateEntry(
185 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
186 fileEntry.getModifiedDate(),
187 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
188 fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
189 assetTagNames, false, null, null, null, fileEntry.getMimeType(),
190 fileEntry.getTitle(), fileEntry.getDescription(), null, null,
191 null, 0, 0, null, false);
192 }
193
194 AssetEntry fileVersionAssetEntry = assetEntryLocalService.fetchEntry(
195 DLFileEntryConstants.getClassName(),
196 fileVersion.getFileVersionId());
197
198 if ((fileVersionAssetEntry == null) && !fileVersion.isApproved() &&
199 !fileVersion.getVersion().equals(
200 DLFileEntryConstants.VERSION_DEFAULT)) {
201
202 assetCategoryIds = assetCategoryLocalService.getCategoryIds(
203 DLFileEntryConstants.getClassName(),
204 fileEntry.getFileEntryId());
205 assetTagNames = assetTagLocalService.getTagNames(
206 DLFileEntryConstants.getClassName(),
207 fileEntry.getFileEntryId());
208
209 fileVersionAssetEntry = assetEntryLocalService.updateEntry(
210 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
211 fileEntry.getModifiedDate(),
212 DLFileEntryConstants.getClassName(),
213 fileVersion.getFileVersionId(), fileEntry.getUuid(),
214 fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
215 null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
216 fileEntry.getDescription(), null, null, null, 0, 0, null,
217 false);
218
219 List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
220 fileEntryAssetEntry.getEntryId());
221
222 long[] assetLinkIds = StringUtil.split(
223 ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR),
224 0L);
225
226 assetLinkLocalService.updateLinks(
227 userId, fileVersionAssetEntry.getEntryId(), assetLinkIds,
228 AssetLinkConstants.TYPE_RELATED);
229 }
230 }
231
232 public void deleteFileEntry(FileEntry fileEntry)
233 throws PortalException, SystemException {
234
235 if (DLAppHelperThreadLocal.isEnabled()) {
236
237
238
239 subscriptionLocalService.deleteSubscriptions(
240 fileEntry.getCompanyId(), DLFileEntryConstants.getClassName(),
241 fileEntry.getFileEntryId());
242
243
244
245 DLProcessorRegistryUtil.cleanUp(fileEntry);
246
247
248
249 dlFileRankLocalService.deleteFileRanksByFileEntryId(
250 fileEntry.getFileEntryId());
251
252
253
254 dlFileShortcutLocalService.deleteFileShortcuts(
255 fileEntry.getFileEntryId());
256
257
258
259 if (!isStagingGroup(fileEntry.getGroupId())) {
260 dlSyncLocalService.updateSync(
261 fileEntry.getFileEntryId(), fileEntry.getFolderId(),
262 fileEntry.getTitle(), fileEntry.getDescription(),
263 DLSyncConstants.EVENT_DELETE, fileEntry.getVersion());
264 }
265
266
267
268 assetEntryLocalService.deleteEntry(
269 DLFileEntryConstants.getClassName(),
270 fileEntry.getFileEntryId());
271
272
273
274 mbMessageLocalService.deleteDiscussionMessages(
275 DLFileEntryConstants.getClassName(),
276 fileEntry.getFileEntryId());
277
278
279
280 ratingsStatsLocalService.deleteStats(
281 DLFileEntryConstants.getClassName(),
282 fileEntry.getFileEntryId());
283 }
284
285
286
287 if (fileEntry.getModel() instanceof DLFileEntry) {
288 trashEntryLocalService.deleteEntry(
289 DLFileEntryConstants.getClassName(),
290 fileEntry.getFileEntryId());
291 }
292 }
293
294 public void deleteFolder(Folder folder)
295 throws PortalException, SystemException {
296
297
298
299 if (!isStagingGroup(folder.getGroupId())) {
300 dlSyncLocalService.updateSync(
301 folder.getFolderId(), folder.getParentFolderId(),
302 folder.getName(), folder.getDescription(),
303 DLSyncConstants.EVENT_DELETE, "-1");
304 }
305
306
307
308 if (folder.getModel() instanceof DLFolder) {
309 trashEntryLocalService.deleteEntry(
310 DLFolderConstants.getClassName(), folder.getFolderId());
311 }
312 }
313
314 public void getFileAsStream(
315 long userId, FileEntry fileEntry, boolean incrementCounter)
316 throws SystemException {
317
318 if (!incrementCounter) {
319 return;
320 }
321
322
323
324 if (userId > 0) {
325 dlFileRankLocalService.updateFileRank(
326 fileEntry.getGroupId(), fileEntry.getCompanyId(), userId,
327 fileEntry.getFileEntryId(), new ServiceContext());
328 }
329
330
331
332 assetEntryLocalService.incrementViewCounter(
333 userId, DLFileEntryConstants.getClassName(),
334 fileEntry.getFileEntryId(), 1);
335
336 List<DLFileShortcut> fileShortcuts =
337 dlFileShortcutPersistence.findByToFileEntryId(
338 fileEntry.getFileEntryId());
339
340 for (DLFileShortcut fileShortcut : fileShortcuts) {
341 assetEntryLocalService.incrementViewCounter(
342 userId, DLFileShortcut.class.getName(),
343 fileShortcut.getFileShortcutId(), 1);
344 }
345 }
346
347 public List<DLFileShortcut> getFileShortcuts(
348 long groupId, long folderId, boolean active, int status)
349 throws SystemException {
350
351 return dlFileShortcutPersistence.findByG_F_A_S(
352 groupId, folderId, active, status);
353 }
354
355
358 public List<DLFileShortcut> getFileShortcuts(
359 long groupId, long folderId, int status)
360 throws SystemException {
361
362 return getFileShortcuts(groupId, folderId, true, status);
363 }
364
365 public int getFileShortcutsCount(
366 long groupId, long folderId, boolean active, int status)
367 throws SystemException {
368
369 return dlFileShortcutPersistence.countByG_F_A_S(
370 groupId, folderId, active, status);
371 }
372
373
376 public int getFileShortcutsCount(long groupId, long folderId, int status)
377 throws SystemException {
378
379 return getFileShortcutsCount(groupId, folderId, true, status);
380 }
381
382 public List<FileEntry> getNoAssetFileEntries() {
383 return null;
384 }
385
386 public void moveFileEntry(FileEntry fileEntry)
387 throws PortalException, SystemException {
388
389 if (!isStagingGroup(fileEntry.getGroupId())) {
390 dlSyncLocalService.updateSync(
391 fileEntry.getFileEntryId(), fileEntry.getFolderId(),
392 fileEntry.getTitle(), fileEntry.getDescription(),
393 DLSyncConstants.EVENT_UPDATE, fileEntry.getVersion());
394 }
395 }
396
397 public FileEntry moveFileEntryFromTrash(
398 long userId, FileEntry fileEntry, long newFolderId,
399 ServiceContext serviceContext)
400 throws PortalException, SystemException {
401
402
403
404 List<DLFileVersion> dlFileVersions =
405 dlFileVersionLocalService.getFileVersions(
406 fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
407
408 dlFileVersions = ListUtil.sort(
409 dlFileVersions, new FileVersionVersionComparator());
410
411 FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
412
413 if (fileVersion.isInTrash()) {
414 restoreFileEntryFromTrash(userId, fileEntry);
415
416 DLFileEntry dlFileEntry = dlFileEntryLocalService.moveFileEntry(
417 userId, fileEntry.getFileEntryId(), newFolderId,
418 serviceContext);
419
420 if (DLAppHelperThreadLocal.isEnabled()) {
421 dlFileRankLocalService.enableFileRanks(
422 fileEntry.getFileEntryId());
423 }
424
425 return new LiferayFileEntry(dlFileEntry);
426 }
427 else {
428 dlFileEntryLocalService.updateStatus(
429 userId, fileVersion.getFileVersionId(), fileVersion.getStatus(),
430 new HashMap<String, Serializable>(), serviceContext);
431
432 if (DLAppHelperThreadLocal.isEnabled()) {
433
434
435
436 dlFileRankLocalService.enableFileRanks(
437 fileEntry.getFileEntryId());
438
439
440
441 dlFileShortcutLocalService.enableFileShortcuts(
442 fileEntry.getFileEntryId());
443 }
444
445
446
447 return dlAppService.moveFileEntry(
448 fileEntry.getFileEntryId(), newFolderId, serviceContext);
449 }
450 }
451
452 public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
453 throws PortalException, SystemException {
454
455
456
457 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
458
459 dlFileEntry.setTitle(
460 TrashUtil.appendTrashNamespace(dlFileEntry.getTitle()));
461
462 dlFileEntryPersistence.update(dlFileEntry);
463
464 List<DLFileVersion> dlFileVersions =
465 dlFileVersionLocalService.getFileVersions(
466 fileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
467
468 dlFileVersions = ListUtil.sort(
469 dlFileVersions, new FileVersionVersionComparator());
470
471 FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));
472
473 Map<String, Serializable> workflowContext =
474 new HashMap<String, Serializable>();
475
476 workflowContext.put("dlFileVersions", (Serializable)dlFileVersions);
477
478 int oldStatus = fileVersion.getStatus();
479
480
481
482 dlFileEntryLocalService.updateStatus(
483 userId, fileVersion.getFileVersionId(),
484 WorkflowConstants.STATUS_IN_TRASH, workflowContext,
485 new ServiceContext());
486
487 if (!DLAppHelperThreadLocal.isEnabled()) {
488 return fileEntry;
489 }
490
491
492
493 dlFileShortcutLocalService.disableFileShortcuts(
494 fileEntry.getFileEntryId());
495
496
497
498 dlFileRankLocalService.disableFileRanks(fileEntry.getFileEntryId());
499
500
501
502 socialActivityCounterLocalService.disableActivityCounters(
503 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
504
505 socialActivityLocalService.addActivity(
506 userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
507 fileEntry.getFileEntryId(),
508 SocialActivityConstants.TYPE_MOVE_TO_TRASH, StringPool.BLANK, 0);
509
510
511
512 if (oldStatus == WorkflowConstants.STATUS_PENDING) {
513 workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
514 fileVersion.getCompanyId(), fileVersion.getGroupId(),
515 DLFileEntryConstants.getClassName(),
516 fileVersion.getFileVersionId());
517 }
518
519 return fileEntry;
520 }
521
522 public DLFileShortcut moveFileShortcutFromTrash(
523 long userId, DLFileShortcut dlFileShortcut, long newFolderId,
524 ServiceContext serviceContext)
525 throws PortalException, SystemException {
526
527 if (dlFileShortcut.isInTrash()) {
528 restoreFileShortcutFromTrash(userId, dlFileShortcut);
529 }
530
531 return dlAppService.updateFileShortcut(
532 dlFileShortcut.getFileShortcutId(), newFolderId,
533 dlFileShortcut.getToFileEntryId(), serviceContext);
534 }
535
536 public DLFileShortcut moveFileShortcutToTrash(
537 long userId, DLFileShortcut dlFileShortcut)
538 throws PortalException, SystemException {
539
540
541
542 int oldStatus = dlFileShortcut.getStatus();
543
544 dlFileShortcutLocalService.updateStatus(
545 userId, dlFileShortcut.getFileShortcutId(),
546 WorkflowConstants.STATUS_IN_TRASH, new ServiceContext());
547
548
549
550 socialActivityLocalService.addActivity(
551 userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
552 dlFileShortcut.getFileShortcutId(),
553 SocialActivityConstants.TYPE_MOVE_TO_TRASH, StringPool.BLANK, 0);
554
555
556
557 trashEntryLocalService.addTrashEntry(
558 userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
559 dlFileShortcut.getFileShortcutId(), oldStatus, null, null);
560
561 return dlFileShortcut;
562 }
563
564 public void moveFolder(Folder folder)
565 throws PortalException, SystemException {
566
567 if (!isStagingGroup(folder.getGroupId())) {
568 dlSyncLocalService.updateSync(
569 folder.getFolderId(), folder.getParentFolderId(),
570 folder.getName(), folder.getDescription(),
571 DLSyncConstants.EVENT_UPDATE, "-1");
572 }
573 }
574
575 public Folder moveFolderFromTrash(
576 long userId, Folder folder, long parentFolderId,
577 ServiceContext serviceContext)
578 throws PortalException, SystemException {
579
580 DLFolder dlFolder = (DLFolder)folder.getModel();
581
582 if (dlFolder.isInTrash()) {
583 restoreFolderFromTrash(userId, folder);
584 }
585 else {
586
587
588
589 dlFolderLocalService.updateStatus(
590 userId, folder.getFolderId(), WorkflowConstants.STATUS_APPROVED,
591 new HashMap<String, Serializable>(), new ServiceContext());
592
593
594
595 dlFileRankLocalService.enableFileRanksByFolderId(
596 folder.getFolderId());
597 }
598
599 return dlAppService.moveFolder(
600 folder.getFolderId(), parentFolderId, serviceContext);
601 }
602
603 public Folder moveFolderToTrash(long userId, Folder folder)
604 throws PortalException, SystemException {
605
606
607
608 DLFolder dlFolder = dlFolderLocalService.updateStatus(
609 userId, folder.getFolderId(), WorkflowConstants.STATUS_IN_TRASH,
610 new HashMap<String, Serializable>(), new ServiceContext());
611
612 dlFolder.setName(TrashUtil.appendTrashNamespace(dlFolder.getName()));
613
614 dlFolderPersistence.update(dlFolder);
615
616
617
618 dlFileRankLocalService.disableFileRanksByFolderId(folder.getFolderId());
619
620 return new LiferayFolder(dlFolder);
621 }
622
623 public void restoreFileEntryFromTrash(long userId, FileEntry fileEntry)
624 throws PortalException, SystemException {
625
626
627
628 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
629
630 dlFileEntry.setTitle(
631 TrashUtil.stripTrashNamespace(dlFileEntry.getTitle()));
632
633 dlFileEntryPersistence.update(dlFileEntry);
634
635 FileVersion fileVersion = new LiferayFileVersion(
636 dlFileEntry.getLatestFileVersion(true));
637
638 TrashEntry trashEntry = trashEntryLocalService.getEntry(
639 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
640
641
642
643 Map<String, Serializable> workflowContext =
644 new HashMap<String, Serializable>();
645
646 List<TrashVersion> trashVersions = trashEntryLocalService.getVersions(
647 trashEntry.getEntryId());
648
649 workflowContext.put("trashVersions", (Serializable)trashVersions);
650
651 dlFileEntryLocalService.updateStatus(
652 userId, fileVersion.getFileVersionId(), trashEntry.getStatus(),
653 workflowContext, new ServiceContext());
654
655 if (!DLAppHelperThreadLocal.isEnabled()) {
656 return;
657 }
658
659
660
661 dlFileShortcutLocalService.enableFileShortcuts(
662 fileEntry.getFileEntryId());
663
664
665
666 dlFileRankLocalService.enableFileRanks(fileEntry.getFileEntryId());
667
668
669
670 socialActivityCounterLocalService.enableActivityCounters(
671 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId());
672
673 socialActivityLocalService.addActivity(
674 userId, fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
675 fileEntry.getFileEntryId(),
676 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH, StringPool.BLANK,
677 0);
678 }
679
680 public void restoreFileShortcutFromTrash(
681 long userId, DLFileShortcut dlFileShortcut)
682 throws PortalException, SystemException {
683
684
685
686 TrashEntry trashEntry = trashEntryLocalService.getEntry(
687 DLFileShortcut.class.getName(), dlFileShortcut.getFileShortcutId());
688
689 dlFileShortcutLocalService.updateStatus(
690 userId, dlFileShortcut.getFileShortcutId(), trashEntry.getStatus(),
691 new ServiceContext());
692
693
694
695 socialActivityCounterLocalService.enableActivityCounters(
696 DLFileShortcut.class.getName(), dlFileShortcut.getFileShortcutId());
697
698 socialActivityLocalService.addActivity(
699 userId, dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
700 dlFileShortcut.getFileShortcutId(),
701 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH, StringPool.BLANK,
702 0);
703
704
705
706 trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
707 }
708
709 public void restoreFolderFromTrash(long userId, Folder folder)
710 throws PortalException, SystemException {
711
712
713
714 TrashEntry trashEntry = trashEntryLocalService.getEntry(
715 DLFolderConstants.getClassName(), folder.getFolderId());
716
717 dlFolderLocalService.updateStatus(
718 userId, folder.getFolderId(), WorkflowConstants.STATUS_APPROVED,
719 new HashMap<String, Serializable>(), new ServiceContext());
720
721
722
723 dlFileRankLocalService.enableFileRanksByFolderId(folder.getFolderId());
724
725
726
727 trashEntryLocalService.deleteEntry(trashEntry.getEntryId());
728 }
729
730 public AssetEntry updateAsset(
731 long userId, FileEntry fileEntry, FileVersion fileVersion,
732 long assetClassPk)
733 throws PortalException, SystemException {
734
735 long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(
736 DLFileEntryConstants.getClassName(), assetClassPk);
737 String[] assetTagNames = assetTagLocalService.getTagNames(
738 DLFileEntryConstants.getClassName(), assetClassPk);
739
740 AssetEntry assetEntry = assetEntryLocalService.getEntry(
741 DLFileEntryConstants.getClassName(), assetClassPk);
742
743 List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(
744 assetEntry.getEntryId());
745
746 long[] assetLinkIds = StringUtil.split(
747 ListUtil.toString(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
748
749 return updateAsset(
750 userId, fileEntry, fileVersion, assetCategoryIds, assetTagNames,
751 assetLinkIds);
752 }
753
754 public AssetEntry updateAsset(
755 long userId, FileEntry fileEntry, FileVersion fileVersion,
756 long[] assetCategoryIds, String[] assetTagNames,
757 long[] assetLinkEntryIds)
758 throws PortalException, SystemException {
759
760 AssetEntry assetEntry = null;
761
762 boolean visible = false;
763
764 boolean addDraftAssetEntry = false;
765
766 if (fileEntry instanceof LiferayFileEntry) {
767 DLFileVersion dlFileVersion = (DLFileVersion)fileVersion.getModel();
768
769 if (dlFileVersion.isApproved()) {
770 visible = true;
771 }
772 else {
773 String version = dlFileVersion.getVersion();
774
775 if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
776 addDraftAssetEntry = true;
777 }
778 }
779 }
780 else {
781 visible = true;
782 }
783
784 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
785
786 if (addDraftAssetEntry) {
787 assetEntry = assetEntryLocalService.updateEntry(
788 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
789 fileEntry.getModifiedDate(),
790 DLFileEntryConstants.getClassName(),
791 fileVersion.getFileVersionId(), fileEntry.getUuid(),
792 fileEntryTypeId, assetCategoryIds, assetTagNames, false, null,
793 null, null, fileEntry.getMimeType(), fileEntry.getTitle(),
794 fileEntry.getDescription(), null, null, null, 0, 0, null,
795 false);
796 }
797 else {
798 assetEntry = assetEntryLocalService.updateEntry(
799 userId, fileEntry.getGroupId(), fileEntry.getCreateDate(),
800 fileEntry.getModifiedDate(),
801 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
802 fileEntry.getUuid(), fileEntryTypeId, assetCategoryIds,
803 assetTagNames, visible, null, null, null,
804 fileEntry.getMimeType(), fileEntry.getTitle(),
805 fileEntry.getDescription(), null, null, null, 0, 0, null,
806 false);
807
808 List<DLFileShortcut> dlFileShortcuts =
809 dlFileShortcutPersistence.findByToFileEntryId(
810 fileEntry.getFileEntryId());
811
812 for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
813 assetEntryLocalService.updateEntry(
814 userId, dlFileShortcut.getGroupId(),
815 dlFileShortcut.getCreateDate(),
816 dlFileShortcut.getModifiedDate(),
817 DLFileShortcut.class.getName(),
818 dlFileShortcut.getFileShortcutId(),
819 dlFileShortcut.getUuid(), fileEntryTypeId, assetCategoryIds,
820 assetTagNames, true, null, null, null,
821 fileEntry.getMimeType(), fileEntry.getTitle(),
822 fileEntry.getDescription(), null, null, null, 0, 0, null,
823 false);
824 }
825 }
826
827 assetLinkLocalService.updateLinks(
828 userId, assetEntry.getEntryId(), assetLinkEntryIds,
829 AssetLinkConstants.TYPE_RELATED);
830
831 return assetEntry;
832 }
833
834 public void updateFileEntry(
835 long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
836 FileVersion destinationFileVersion, long assetClassPk)
837 throws PortalException, SystemException {
838
839 if (!DLAppHelperThreadLocal.isEnabled()) {
840 return;
841 }
842
843 boolean updateAsset = true;
844
845 if (fileEntry instanceof LiferayFileEntry &&
846 fileEntry.getVersion().equals(
847 destinationFileVersion.getVersion())) {
848
849 updateAsset = false;
850 }
851
852 if (updateAsset) {
853 updateAsset(
854 userId, fileEntry, destinationFileVersion, assetClassPk);
855 }
856
857 registerDLProcessorCallback(fileEntry, sourceFileVersion);
858 }
859
860 public void updateFileEntry(
861 long userId, FileEntry fileEntry, FileVersion sourceFileVersion,
862 FileVersion destinationFileVersion, ServiceContext serviceContext)
863 throws PortalException, SystemException {
864
865 if (!DLAppHelperThreadLocal.isEnabled()) {
866 return;
867 }
868
869 updateAsset(
870 userId, fileEntry, destinationFileVersion,
871 serviceContext.getAssetCategoryIds(),
872 serviceContext.getAssetTagNames(),
873 serviceContext.getAssetLinkEntryIds());
874
875 registerDLProcessorCallback(fileEntry, sourceFileVersion);
876 }
877
878 public void updateFolder(Folder folder, ServiceContext serviceContext)
879 throws PortalException, SystemException {
880
881 if (!isStagingGroup(folder.getGroupId())) {
882 dlSyncLocalService.updateSync(
883 folder.getFolderId(), folder.getParentFolderId(),
884 folder.getName(), folder.getDescription(),
885 DLSyncConstants.EVENT_UPDATE, "-1");
886 }
887 }
888
889 public void updateStatus(
890 long userId, FileEntry fileEntry, FileVersion latestFileVersion,
891 int oldStatus, int newStatus,
892 Map<String, Serializable> workflowContext)
893 throws PortalException, SystemException {
894
895 if (!DLAppHelperThreadLocal.isEnabled()) {
896 return;
897 }
898
899 if (newStatus == WorkflowConstants.STATUS_APPROVED) {
900
901
902
903 String latestFileVersionVersion = latestFileVersion.getVersion();
904
905 if (latestFileVersionVersion.equals(fileEntry.getVersion())) {
906 if (!latestFileVersionVersion.equals(
907 DLFileEntryConstants.VERSION_DEFAULT)) {
908
909 AssetEntry draftAssetEntry = null;
910
911 try {
912 long fileEntryTypeId = getFileEntryTypeId(fileEntry);
913
914 draftAssetEntry = assetEntryLocalService.getEntry(
915 DLFileEntryConstants.getClassName(),
916 latestFileVersion.getPrimaryKey());
917
918 long[] assetCategoryIds =
919 draftAssetEntry.getCategoryIds();
920 String[] assetTagNames = draftAssetEntry.getTagNames();
921
922 List<AssetLink> assetLinks =
923 assetLinkLocalService.getDirectLinks(
924 draftAssetEntry.getEntryId(),
925 AssetLinkConstants.TYPE_RELATED);
926
927 long[] assetLinkEntryIds = StringUtil.split(
928 ListUtil.toString(
929 assetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L);
930
931 AssetEntry assetEntry =
932 assetEntryLocalService.updateEntry(
933 userId, fileEntry.getGroupId(),
934 fileEntry.getCreateDate(),
935 fileEntry.getModifiedDate(),
936 DLFileEntryConstants.getClassName(),
937 fileEntry.getFileEntryId(), fileEntry.getUuid(),
938 fileEntryTypeId, assetCategoryIds,
939 assetTagNames, true, null, null, null,
940 draftAssetEntry.getMimeType(),
941 fileEntry.getTitle(),
942 fileEntry.getDescription(), null, null, null, 0,
943 0, null, false);
944
945 assetLinkLocalService.updateLinks(
946 userId, assetEntry.getEntryId(), assetLinkEntryIds,
947 AssetLinkConstants.TYPE_RELATED);
948
949 assetEntryLocalService.deleteEntry(
950 draftAssetEntry.getEntryId());
951 }
952 catch (NoSuchEntryException nsee) {
953 }
954 }
955
956 assetEntryLocalService.updateVisible(
957 DLFileEntryConstants.getClassName(),
958 fileEntry.getFileEntryId(), true);
959 }
960
961
962
963 String event = (String)workflowContext.get("event");
964
965 if (!isStagingGroup(fileEntry.getGroupId()) &&
966 Validator.isNotNull(event)) {
967
968 if (event.equals(DLSyncConstants.EVENT_ADD)) {
969 dlSyncLocalService.addSync(
970 fileEntry.getFileEntryId(), fileEntry.getUuid(),
971 fileEntry.getCompanyId(), fileEntry.getRepositoryId(),
972 fileEntry.getFolderId(), fileEntry.getTitle(),
973 fileEntry.getDescription(), DLSyncConstants.TYPE_FILE,
974 fileEntry.getVersion());
975 }
976 else if (event.equals(DLSyncConstants.EVENT_UPDATE)) {
977 dlSyncLocalService.updateSync(
978 fileEntry.getFileEntryId(), fileEntry.getFolderId(),
979 fileEntry.getTitle(), fileEntry.getDescription(),
980 DLSyncConstants.EVENT_UPDATE, fileEntry.getVersion());
981 }
982 }
983
984
985
986 int activityType = DLActivityKeys.UPDATE_FILE_ENTRY;
987
988 if (latestFileVersionVersion.equals(
989 DLFileEntryConstants.VERSION_DEFAULT)) {
990
991 activityType = DLActivityKeys.ADD_FILE_ENTRY;
992 }
993
994 if (oldStatus != WorkflowConstants.STATUS_IN_TRASH) {
995 socialActivityLocalService.addUniqueActivity(
996 latestFileVersion.getStatusByUserId(),
997 fileEntry.getGroupId(), latestFileVersion.getCreateDate(),
998 DLFileEntryConstants.getClassName(),
999 fileEntry.getFileEntryId(), activityType, StringPool.BLANK,
1000 0);
1001 }
1002 }
1003 else {
1004
1005
1006
1007 boolean visible = false;
1008
1009 if (newStatus != WorkflowConstants.STATUS_IN_TRASH) {
1010 List<DLFileVersion> approvedFileVersions =
1011 dlFileVersionPersistence.findByF_S(
1012 fileEntry.getFileEntryId(),
1013 WorkflowConstants.STATUS_APPROVED);
1014
1015 if (!approvedFileVersions.isEmpty()) {
1016 visible = true;
1017 }
1018 }
1019
1020 assetEntryLocalService.updateVisible(
1021 DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId(),
1022 visible);
1023 }
1024 }
1025
1026 public void updateStatuses(
1027 User user, List<Object> dlFileEntriesAndDLFolders, int status)
1028 throws PortalException, SystemException {
1029
1030 for (Object object : dlFileEntriesAndDLFolders) {
1031 if (object instanceof DLFileEntry) {
1032 DLFileEntry dlFileEntry = (DLFileEntry)object;
1033
1034 List<DLFileVersion> dlFileVersions =
1035 dlFileVersionLocalService.getFileVersions(
1036 dlFileEntry.getFileEntryId(),
1037 WorkflowConstants.STATUS_ANY);
1038
1039 dlFileVersions = ListUtil.copy(dlFileVersions);
1040
1041 Collections.sort(
1042 dlFileVersions, new FileVersionVersionComparator());
1043
1044 DLFileVersion latestDlFileVersion = dlFileVersions.get(0);
1045
1046 if ((status == WorkflowConstants.STATUS_APPROVED) &&
1047 (latestDlFileVersion.getStatus() ==
1048 WorkflowConstants.STATUS_IN_TRASH)) {
1049
1050 continue;
1051 }
1052
1053
1054
1055 if (status == WorkflowConstants.STATUS_APPROVED) {
1056 dlFileShortcutLocalService.enableFileShortcuts(
1057 dlFileEntry.getFileEntryId());
1058 }
1059 else {
1060 dlFileShortcutLocalService.disableFileShortcuts(
1061 dlFileEntry.getFileEntryId());
1062 }
1063
1064
1065
1066 if (status == WorkflowConstants.STATUS_APPROVED) {
1067 if (latestDlFileVersion.isApproved()) {
1068 assetEntryLocalService.updateVisible(
1069 DLFileEntryConstants.getClassName(),
1070 dlFileEntry.getFileEntryId(), true);
1071 }
1072 }
1073 else {
1074 assetEntryLocalService.updateVisible(
1075 DLFileEntryConstants.getClassName(),
1076 dlFileEntry.getFileEntryId(), false);
1077 }
1078
1079
1080
1081 JSONObject extraDataJSONObject =
1082 JSONFactoryUtil.createJSONObject();
1083
1084 extraDataJSONObject.put("title", dlFileEntry.getTitle());
1085
1086 if (status == WorkflowConstants.STATUS_APPROVED) {
1087 socialActivityCounterLocalService.enableActivityCounters(
1088 DLFileEntryConstants.getClassName(),
1089 dlFileEntry.getFileEntryId());
1090
1091 socialActivityLocalService.addActivity(
1092 user.getUserId(), dlFileEntry.getGroupId(),
1093 DLFileEntryConstants.getClassName(),
1094 dlFileEntry.getFileEntryId(),
1095 SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
1096 extraDataJSONObject.toString(), 0);
1097 }
1098 else if (latestDlFileVersion.getStatus() ==
1099 WorkflowConstants.STATUS_APPROVED) {
1100
1101 socialActivityCounterLocalService.disableActivityCounters(
1102 DLFileEntryConstants.getClassName(),
1103 dlFileEntry.getFileEntryId());
1104
1105 socialActivityLocalService.addActivity(
1106 user.getUserId(), dlFileEntry.getGroupId(),
1107 DLFileEntryConstants.getClassName(),
1108 dlFileEntry.getFileEntryId(),
1109 SocialActivityConstants.TYPE_MOVE_TO_TRASH,
1110 extraDataJSONObject.toString(), 0);
1111 }
1112
1113
1114
1115 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1116 DLFileEntry.class);
1117
1118 indexer.reindex(dlFileEntry);
1119
1120
1121
1122 if (status != WorkflowConstants.STATUS_APPROVED) {
1123 for (DLFileVersion dlFileVersion : dlFileVersions) {
1124 if (!dlFileVersion.isPending()) {
1125 continue;
1126 }
1127
1128 dlFileVersion.setStatus(WorkflowConstants.STATUS_DRAFT);
1129
1130 dlFileVersionPersistence.update(dlFileVersion);
1131
1132 workflowInstanceLinkLocalService.
1133 deleteWorkflowInstanceLink(
1134 dlFileVersion.getCompanyId(),
1135 dlFileVersion.getGroupId(),
1136 DLFileEntryConstants.getClassName(),
1137 dlFileVersion.getFileVersionId());
1138 }
1139 }
1140 }
1141 else if (object instanceof DLFolder) {
1142 DLFolder dlFolder = (DLFolder)object;
1143
1144 if (dlFolder.isInTrash()) {
1145 continue;
1146 }
1147
1148 QueryDefinition queryDefinition = new QueryDefinition(
1149 WorkflowConstants.STATUS_ANY);
1150
1151 List<Object> foldersAndFileEntriesAndFileShortcuts =
1152 dlFolderLocalService.
1153 getFoldersAndFileEntriesAndFileShortcuts(
1154 dlFolder.getGroupId(), dlFolder.getFolderId(), null,
1155 false, queryDefinition);
1156
1157 updateStatuses(
1158 user, foldersAndFileEntriesAndFileShortcuts, status);
1159 }
1160 }
1161 }
1162
1163 protected long getFileEntryTypeId(FileEntry fileEntry) {
1164 if (fileEntry instanceof LiferayFileEntry) {
1165 DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
1166
1167 return dlFileEntry.getFileEntryTypeId();
1168 }
1169 else {
1170 return 0;
1171 }
1172 }
1173
1174 protected boolean isStagingGroup(long groupId) {
1175 try {
1176 Group group = groupLocalService.getGroup(groupId);
1177
1178 return group.isStagingGroup();
1179 }
1180 catch (Exception e) {
1181 return false;
1182 }
1183 }
1184
1185 protected void registerDLProcessorCallback(
1186 final FileEntry fileEntry, final FileVersion fileVersion) {
1187
1188 TransactionCommitCallbackRegistryUtil.registerCallback(
1189 new Callable<Void>() {
1190
1191 public Void call() throws Exception {
1192 DLProcessorRegistryUtil.trigger(fileEntry, fileVersion);
1193
1194 return null;
1195 }
1196
1197 });
1198 }
1199
1200 }