Commit 1bccdbf0 authored by Aalekh Jain's avatar Aalekh Jain
Browse files

Added `BaseWorkflowMetadataRepository.java`

Will serve as the base class for workflow system metadata repository and workflow metadata repository class
parent 603eb67c
package org.opengroup.osdu.workflow.provider.azure.repository;
import com.azure.cosmos.models.SqlParameter;
import com.azure.cosmos.models.SqlQuerySpec;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.workflow.exception.ResourceConflictException;
import org.opengroup.osdu.workflow.exception.WorkflowNotFoundException;
import org.opengroup.osdu.workflow.model.WorkflowMetadata;
import org.opengroup.osdu.workflow.provider.azure.config.AzureWorkflowEngineConfig;
import org.opengroup.osdu.workflow.provider.azure.model.WorkflowMetadataDoc;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BaseWorkflowMetadataRepository {
private static final String LOGGER_NAME = BaseWorkflowMetadataRepository.class.getName();
@Autowired
private JaxRsDpsLog logger;
@Autowired
private AzureWorkflowEngineConfig workflowEngineConfig;
protected static final String KEY_DAG_CONTENT = "dagContent";
protected WorkflowMetadataDoc buildWorkflowMetadataDoc(final WorkflowMetadata workflowMetadata) {
// If we need to save multiple versions of workflow, then choose id as guid and get becomes a query.
// This is to avoid conflicts. Only one combination of Id and partition key should exist.
Map<String, Object> registrationInstructionForMetadata =
new HashMap<>(workflowMetadata.getRegistrationInstructions());
String dagContent =
(String) registrationInstructionForMetadata.remove(KEY_DAG_CONTENT);
if (workflowEngineConfig.getIgnoreDagContent()) {
dagContent = "";
}
return WorkflowMetadataDoc.builder()
.id(workflowMetadata.getWorkflowName())
.partitionKey(workflowMetadata.getWorkflowName())
.workflowName(workflowMetadata.getWorkflowName())
.description(workflowMetadata.getDescription())
.createdBy(workflowMetadata.getCreatedBy())
.creationTimestamp(workflowMetadata.getCreationTimestamp())
.version(workflowMetadata.getVersion())
.isRegisteredByWorkflowService(
dagContent != null && !dagContent.isEmpty())
.registrationInstructions(registrationInstructionForMetadata).build();
}
protected WorkflowMetadata buildWorkflowMetadata(final WorkflowMetadataDoc workflowMetadataDoc) {
return WorkflowMetadata.builder()
.workflowId(workflowMetadataDoc.getId())
.workflowName(workflowMetadataDoc.getWorkflowName())
.description(workflowMetadataDoc.getDescription())
.createdBy(workflowMetadataDoc.getCreatedBy())
.creationTimestamp(workflowMetadataDoc.getCreationTimestamp())
.version(workflowMetadataDoc.getVersion())
.isDeployedThroughWorkflowService(workflowMetadataDoc.getIsRegisteredByWorkflowService())
.registrationInstructions(workflowMetadataDoc.getRegistrationInstructions()).build();
}
protected SqlQuerySpec buildSqlQuerySpecForGetAllWorkflow(String prefix) {
SqlQuerySpec sqlQuerySpec;
if(prefix != null && !(prefix.isEmpty())) {
SqlParameter prefixParameter = new SqlParameter("@prefix", prefix);
sqlQuerySpec = new SqlQuerySpec("SELECT * FROM c " +
"where STARTSWITH(c.workflowName, @prefix, true) " +
"ORDER BY c._ts DESC", prefixParameter);
}
else {
sqlQuerySpec = new SqlQuerySpec("SELECT * FROM c " +
"ORDER BY c._ts DESC");
}
return sqlQuerySpec;
}
protected List<WorkflowMetadata> convertWorkflowMetadataDocsToWorkflowMetadataList(List<WorkflowMetadataDoc> workflowMetadataDocs) {
List<WorkflowMetadata> workflowMetadataList = new ArrayList<>();
for (WorkflowMetadataDoc workflowMetadataDoc: workflowMetadataDocs) {
workflowMetadataList.add(buildWorkflowMetadata(workflowMetadataDoc));
}
return workflowMetadataList;
}
protected void handleGetWorkflowException(String workflowName) {
final String errorMessage = String.format("Workflow: %s doesn't exist", workflowName);
logger.error(LOGGER_NAME, errorMessage);
throw new WorkflowNotFoundException(errorMessage);
}
protected void handleCreateWorkflowException(AppException e, String workflowName) {
if(e.getError().getCode() == 409) {
final String errorMessage = String.format("Workflow with name %s already exists", workflowName);
logger.error(LOGGER_NAME, errorMessage);
throw new ResourceConflictException(workflowName, errorMessage);
} else {
throw e;
}
}
}
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