diff --git a/processes-api/src/main/java/org/exoplatform/processes/model/Work.java b/processes-api/src/main/java/org/exoplatform/processes/model/Work.java index fddb853bc..43f09cdff 100644 --- a/processes-api/src/main/java/org/exoplatform/processes/model/Work.java +++ b/processes-api/src/main/java/org/exoplatform/processes/model/Work.java @@ -19,8 +19,10 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.exoplatform.services.attachments.model.Attachment; import java.util.Date; +import java.util.List; @Data @AllArgsConstructor @@ -61,6 +63,8 @@ public class Work { private WorkFlow workFlow; + private List attachments; + /** * constructor for Work task object * diff --git a/processes-api/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentService.java b/processes-api/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentService.java index 807cd239b..514d88d51 100644 --- a/processes-api/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentService.java +++ b/processes-api/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentService.java @@ -2,6 +2,8 @@ import org.exoplatform.services.attachments.model.Attachment; +import java.util.List; + public interface ProcessesAttachmentService { /** @@ -27,6 +29,26 @@ public interface ProcessesAttachmentService { */ void moveAttachmentsToEntity(Long userId, Long sourceEntityId, String sourceEntityType, Long destEntityId, String destEntityType, Long projectId); + + /** + * Move attachments from source entity to a dest entity + * + * @param attachments list of attachment + * @param userId user identity id + * @param sourceEntityId source entity of attachments + * @param sourceEntityType target entity type to attach files from source entity + * @param destEntityId target entity id + * @param destEntityType target entity type + * @param projectId task project id + */ + void moveAttachmentsToEntity(List attachments, + Long userId, + Long sourceEntityId, + String sourceEntityType, + Long destEntityId, + String destEntityType, + Long projectId); + /** * Copy attachments from source entity to a dest entity * diff --git a/processes-services/src/main/java/org/exoplatform/processes/rest/model/WorkEntity.java b/processes-services/src/main/java/org/exoplatform/processes/rest/model/WorkEntity.java index 8c02bf67d..e10842f1d 100644 --- a/processes-services/src/main/java/org/exoplatform/processes/rest/model/WorkEntity.java +++ b/processes-services/src/main/java/org/exoplatform/processes/rest/model/WorkEntity.java @@ -18,10 +18,12 @@ package org.exoplatform.processes.rest.model; import java.util.Date; +import java.util.List; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.exoplatform.services.attachments.model.Attachment; @Data @AllArgsConstructor @@ -62,6 +64,8 @@ public class WorkEntity { private Boolean isDraft; + private List attachments; + public WorkEntity(long id, String title, String description, diff --git a/processes-services/src/main/java/org/exoplatform/processes/rest/util/EntityBuilder.java b/processes-services/src/main/java/org/exoplatform/processes/rest/util/EntityBuilder.java index 21bbdd900..9a6e1ce01 100644 --- a/processes-services/src/main/java/org/exoplatform/processes/rest/util/EntityBuilder.java +++ b/processes-services/src/main/java/org/exoplatform/processes/rest/util/EntityBuilder.java @@ -176,6 +176,7 @@ public static Work toWork(ProcessesService processesService, WorkEntity workEnti workEntity.getIsDraft(), workEntity.getDraftId(), workEntity.getProjectId()); + work.setAttachments(workEntity.getAttachments()); if (workEntity.getWorkFlow() != null) { try { WorkFlow workFlow = processesService.getWorkFlow(workEntity.getWorkFlow().getId()); @@ -208,6 +209,7 @@ public static WorkEntity toWorkEntity(ProcessesService processesService, Work wo if (expandProperties.contains("comments")) { // TODO: Add comments } + workEntity.setAttachments(work.getAttachments()); try { workEntity.setDescription(HTMLSanitizer.sanitize(work.getDescription())); diff --git a/processes-services/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentServiceImpl.java b/processes-services/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentServiceImpl.java index 33b1c57d4..12e84fd44 100644 --- a/processes-services/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentServiceImpl.java +++ b/processes-services/src/main/java/org/exoplatform/processes/service/ProcessesAttachmentServiceImpl.java @@ -167,7 +167,8 @@ private void moveOrCopyAttachmentsJcrNodes(List attachments, final Session session = jcrSession; final ProjectDto project = projectDto; IntStream.range(0, attachments.size()).forEach(index -> { - String attachmentId = attachments.get(index).getId(); + Attachment attachment = attachments.get(index); + String attachmentId = attachment.getId(); try { DriveData driveData; Node rootNode; @@ -204,7 +205,15 @@ private void moveOrCopyAttachmentsJcrNodes(List attachments, Map unmodifiablePermissions = Collections.unmodifiableMap(permissions); ((ExtendedNode) destNode).setPermissions(unmodifiablePermissions); String destPath = destNode.getPath().concat("/").concat(attachmentNode.getName()); - if (copy) { + if (!copy && !attachment.isEXoDrive()) { + Node sourceEntityIdNode = attachmentNode.getParent(); + session.move(attachmentNode.getPath(), destPath); + if (attachments.size() - 1 == index && sourceEntityIdNode != null + && sourceEntityIdNode.getPrimaryNodeType().isNodeType(NodetypeConstant.NT_FOLDER)) { + sourceEntityIdNode.remove(); + } + session.save(); + } else { session.save(); Workspace workspace = session.getWorkspace(); workspace.copy(attachmentNode.getPath(), destPath); @@ -212,14 +221,6 @@ private void moveOrCopyAttachmentsJcrNodes(List attachments, processDocument(copyNode, currentUser); Attachment copyAttachment = attachmentService.getAttachmentById(copyNode.getUUID()); updatedAttachments.put(index, copyAttachment); - } else { - Node sourceEntityIdNode = attachmentNode.getParent(); - session.move(attachmentNode.getPath(), destPath); - if (attachments.size() - 1 == index && sourceEntityIdNode != null - && sourceEntityIdNode.getPrimaryNodeType().isNodeType(NodetypeConstant.NT_FOLDER)) { - sourceEntityIdNode.remove(); - } - session.save(); } } catch (Exception e) { LOG.error("Error while moving or copying attachments", e); @@ -251,6 +252,21 @@ public void moveAttachmentsToEntity(Long userId, createWorkflowTaskFolder(userId, projectId, destEntityType, destEntityId); } } + @Override + public void moveAttachmentsToEntity(List attachments, + Long userId, + Long sourceEntityId, + String sourceEntityType, + Long destEntityId, + String destEntityType, + Long projectId) { + if (attachments!= null && !attachments.isEmpty()) { + moveOrCopyAttachmentsJcrNodes(attachments, destEntityId, destEntityType, false, projectId); + linkFromEntityToEntity(userId, attachments, sourceEntityId, sourceEntityType, destEntityId, destEntityType, true); + } else { + createWorkflowTaskFolder(userId, projectId, destEntityType, destEntityId); + } + } @Override public void copyAttachmentsToEntity(Long userId, diff --git a/processes-services/src/main/java/org/exoplatform/processes/storage/ProcessesStorageImpl.java b/processes-services/src/main/java/org/exoplatform/processes/storage/ProcessesStorageImpl.java index f0ebfd82b..1845e77b9 100644 --- a/processes-services/src/main/java/org/exoplatform/processes/storage/ProcessesStorageImpl.java +++ b/processes-services/src/main/java/org/exoplatform/processes/storage/ProcessesStorageImpl.java @@ -397,7 +397,8 @@ public Work saveWork(Work work, long userId) throws IllegalArgumentException { TaskDto taskDto = createWorkTask(work, identity); ProjectDto projectDto = taskDto.getStatus().getProject(); if (work.getDraftId() != null) { - processesAttachmentService.moveAttachmentsToEntity(userId, + processesAttachmentService.moveAttachmentsToEntity(work.getAttachments(), + userId, work.getDraftId(), WORK_DRAFT_ENTITY_TYPE, taskDto.getId(), diff --git a/processes-services/src/test/java/org/exoplatform/processes/storage/ProcessesStorageImplTest.java b/processes-services/src/test/java/org/exoplatform/processes/storage/ProcessesStorageImplTest.java index 8af747641..0b2b4cf84 100644 --- a/processes-services/src/test/java/org/exoplatform/processes/storage/ProcessesStorageImplTest.java +++ b/processes-services/src/test/java/org/exoplatform/processes/storage/ProcessesStorageImplTest.java @@ -336,12 +336,13 @@ public void saveWork() throws EntityNotFoundException, IllegalAccessException, O work.setIsDraft(true); work.setId(0); work.setDraftId(1L); + work.setAttachments(new ArrayList()); WorkFlow workFlow = new WorkFlow(); workFlow.setProjectId(1L); when(taskDto.getId()).thenReturn(1L); when(workDraftDAO.find(1L)).thenReturn(WorkEntity); processesStorage.saveWork(work, 1L); - verify(processesAttachmentService, times(1)).moveAttachmentsToEntity(1L, 1L, "workdraft", 1L, "task", 1L); + verify(processesAttachmentService, times(1)).moveAttachmentsToEntity(new ArrayList(), 1L, 1L, "workdraft", 1L, "task", 1L); verify(workDraftDAO, times(1)).delete(WorkEntity); when(projectService.getProject(work.getProjectId())).thenThrow(EntityNotFoundException.class); diff --git a/processes-webapp/src/main/webapp/vue-app/processes/components/AddWorkDrawer.vue b/processes-webapp/src/main/webapp/vue-app/processes/components/AddWorkDrawer.vue index 511ad3622..cf6874230 100644 --- a/processes-webapp/src/main/webapp/vue-app/processes/components/AddWorkDrawer.vue +++ b/processes-webapp/src/main/webapp/vue-app/processes/components/AddWorkDrawer.vue @@ -358,6 +358,7 @@ export default { this.work.draftId = this.work.id; this.work.id = 0; } + this.work.attachments=this.attachments; this.$root.$emit('add-work', this.work); }, toWorkDraft(work) { diff --git a/processes-webapp/src/main/webapp/vue-app/processes/components/attachments-integration/ProcessesAttachments.vue b/processes-webapp/src/main/webapp/vue-app/processes/components/attachments-integration/ProcessesAttachments.vue index f8bd2f075..bc20db359 100644 --- a/processes-webapp/src/main/webapp/vue-app/processes/components/attachments-integration/ProcessesAttachments.vue +++ b/processes-webapp/src/main/webapp/vue-app/processes/components/attachments-integration/ProcessesAttachments.vue @@ -145,13 +145,9 @@ export default { }); } document.addEventListener('attachment-added', event => { - if (this.editMode) { - this.initEntityAttachmentsList(); - } else { - this.attachments.push(event.detail.attachment); - } + this.attachments.push(event.detail.attachment); this.subscribeDocument(event.detail.attachment.id); - this.$root.$emit('attachments-updated'); + this.$root.$emit('attachments-updated',this.attachments); }); this.$root.$on('add-new-created-form-document', (doc) => { this.attachments.push(doc);