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