Commit 63e574e6 authored by Aalekh Jain's avatar Aalekh Jain
Browse files

Refactoring in `WorkflowManagerServiceImpl.java`

parent 5406d69f
package org.opengroup.osdu.workflow.service;
import lombok.RequiredArgsConstructor;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.opengroup.osdu.core.common.exception.BadRequestException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
......@@ -17,9 +14,11 @@ import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowManagerService;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowMetadataRepository;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowRunService;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowSystemMetadataRepository;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
@Service
@RequiredArgsConstructor
public class WorkflowManagerServiceImpl implements IWorkflowManagerService {
......@@ -40,25 +39,12 @@ public class WorkflowManagerServiceImpl implements IWorkflowManagerService {
@Override
public WorkflowMetadata createWorkflow(final CreateWorkflowRequest request) {
validateWorkflowName(request.getWorkflowName());
final WorkflowMetadata workflowMetadata = getWorkflowMetadata(request, dpsHeaders.getUserEmail());
final WorkflowMetadata savedMetadata = workflowMetadataRepository.createWorkflow(workflowMetadata);
final WorkflowEngineRequest rq = new WorkflowEngineRequest(workflowMetadata.getWorkflowName());
workflowEngineService.createWorkflow(rq, request.getRegistrationInstructions());
auditLogger.workflowCreateEvent(Collections.singletonList(savedMetadata.toString()));
return savedMetadata;
return createWorkflowCommon(request, false);
}
@Override
public WorkflowMetadata createSystemWorkflow(final CreateWorkflowRequest request) {
validateWorkflowName(request.getWorkflowName());
final WorkflowMetadata workflowMetadata = getWorkflowMetadata(request, dpsHeaders.getUserEmail());
final WorkflowMetadata savedMetadata = workflowSystemMetadataRepository.createSystemWorkflow(workflowMetadata);
final WorkflowEngineRequest rq = new WorkflowEngineRequest(workflowMetadata.getWorkflowName(), false, true);
workflowEngineService.createWorkflow(rq, request.getRegistrationInstructions());
auditLogger.workflowCreateEvent(Collections.singletonList(savedMetadata.toString()));
return savedMetadata;
return createWorkflowCommon(request, true);
}
@Override
......@@ -72,21 +58,12 @@ public class WorkflowManagerServiceImpl implements IWorkflowManagerService {
@Override
public void deleteWorkflow(String workflowName) {
final WorkflowMetadata workflowMetadata = workflowMetadataRepository.getWorkflow(workflowName);
workflowRunService.deleteWorkflowRunsByWorkflowName(workflowName);
WorkflowEngineRequest rq = new WorkflowEngineRequest(workflowName, workflowMetadata.isDeployedThroughWorkflowService());
workflowEngineService.deleteWorkflow(rq);
workflowMetadataRepository.deleteWorkflow(workflowName);
auditLogger.workflowDeleteEvent(Collections.singletonList(workflowName));
deleteWorkflowCommon(workflowName, false);
}
@Override
public void deleteSystemWorkflow(String workflowName) {
final WorkflowMetadata workflowMetadata = workflowSystemMetadataRepository.getSystemWorkflow(workflowName);
WorkflowEngineRequest rq = new WorkflowEngineRequest(workflowName, workflowMetadata.isDeployedThroughWorkflowService(), true);
workflowEngineService.deleteWorkflow(rq);
workflowSystemMetadataRepository.deleteSystemWorkflow(workflowName);
auditLogger.workflowDeleteEvent(Collections.singletonList(workflowName));
deleteWorkflowCommon(workflowName, true);
}
@Override
......@@ -106,7 +83,8 @@ public class WorkflowManagerServiceImpl implements IWorkflowManagerService {
}
private WorkflowMetadata getWorkflowMetadata(final CreateWorkflowRequest request,
final String createdBy) {
final String createdBy,
final boolean isSystemWorkflow) {
return WorkflowMetadata.builder()
.description(request.getDescription())
.createdBy(createdBy)
......@@ -116,4 +94,37 @@ public class WorkflowManagerServiceImpl implements IWorkflowManagerService {
.workflowName(request.getWorkflowName())
.build();
}
private WorkflowMetadata createWorkflowCommon(final CreateWorkflowRequest request, final boolean isSystemWorkflow) {
validateWorkflowName(request.getWorkflowName());
final WorkflowMetadata workflowMetadata = getWorkflowMetadata(request, dpsHeaders.getUserEmail(), isSystemWorkflow);
WorkflowMetadata savedMetadata;
if (!isSystemWorkflow) {
savedMetadata = workflowMetadataRepository.createWorkflow(workflowMetadata);
} else {
savedMetadata = workflowSystemMetadataRepository.createSystemWorkflow(workflowMetadata);
}
final WorkflowEngineRequest rq = new WorkflowEngineRequest(workflowMetadata.getWorkflowName(), false, isSystemWorkflow);
workflowEngineService.createWorkflow(rq, request.getRegistrationInstructions());
auditLogger.workflowCreateEvent(Collections.singletonList(savedMetadata.toString()));
return savedMetadata;
}
private void deleteWorkflowCommon(final String workflowName, final boolean isSystemWorkflow) {
WorkflowMetadata workflowMetadata;
if (!isSystemWorkflow) {
workflowMetadata = workflowMetadataRepository.getWorkflow(workflowName);
workflowRunService.deleteWorkflowRunsByWorkflowName(workflowName);
} else {
workflowMetadata = workflowSystemMetadataRepository.getSystemWorkflow(workflowName);
}
WorkflowEngineRequest rq = new WorkflowEngineRequest(workflowName, workflowMetadata.isDeployedThroughWorkflowService(), isSystemWorkflow);
workflowEngineService.deleteWorkflow(rq);
if (!isSystemWorkflow) {
workflowMetadataRepository.deleteWorkflow(workflowName);
} else {
workflowSystemMetadataRepository.deleteSystemWorkflow(workflowName);
}
auditLogger.workflowDeleteEvent(Collections.singletonList(workflowName));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment