Skip to content

Commit

Permalink
fix: [BUG] Process :Document+parent Folder lost in case of Upload fro…
Browse files Browse the repository at this point in the history
…m existing document (new process/new request) - EXO-74485 (#395) (#396)

Prior to this fix, when user uploaded attachments to a workflow/request  from existing documents, the documents were lost from the source location, this is due to that the service moves the documents from the source to the entity folder (this is okay if documents are uploaded from external location), this change fix this by adding the information to the attachment entity and allowing the move only if the documents is not added from exo drives, otherwise a copy will be performed
  • Loading branch information
mkrout authored Oct 10, 2024
1 parent b126107 commit 3623a2b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +63,8 @@ public class Work {

private WorkFlow workFlow;

private List<Attachment> attachments;

/**
* constructor for Work task object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.exoplatform.services.attachments.model.Attachment;

import java.util.List;

public interface ProcessesAttachmentService {

/**
Expand All @@ -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<Attachment> attachments,
Long userId,
Long sourceEntityId,
String sourceEntityType,
Long destEntityId,
String destEntityType,
Long projectId);

/**
* Copy attachments from source entity to a dest entity
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,6 +64,8 @@ public class WorkEntity {

private Boolean isDraft;

private List<Attachment> attachments;

public WorkEntity(long id,
String title,
String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ private void moveOrCopyAttachmentsJcrNodes(List<Attachment> 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;
Expand Down Expand Up @@ -204,22 +205,22 @@ private void moveOrCopyAttachmentsJcrNodes(List<Attachment> attachments,
Map<String, String[]> 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);
Node copyNode = (Node) session.getItem(destPath);
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);
Expand Down Expand Up @@ -251,6 +252,21 @@ public void moveAttachmentsToEntity(Long userId,
createWorkflowTaskFolder(userId, projectId, destEntityType, destEntityId);
}
}
@Override
public void moveAttachmentsToEntity(List<Attachment> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,13 @@ public void saveWork() throws EntityNotFoundException, IllegalAccessException, O
work.setIsDraft(true);
work.setId(0);
work.setDraftId(1L);
work.setAttachments(new ArrayList<Attachment>());
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<Attachment>(), 1L, 1L, "workdraft", 1L, "task", 1L);
verify(workDraftDAO, times(1)).delete(WorkEntity);
when(projectService.getProject(work.getProjectId())).thenThrow(EntityNotFoundException.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3623a2b

Please sign in to comment.