Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
propertyPath in update parameter response
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardW98 committed May 1, 2023
1 parent 8d5c27e commit 14216b3
Show file tree
Hide file tree
Showing 15 changed files with 163 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class WorkParameterValueResponse {

private String key;

private String propertyPath;
private String workName;

private List<String> options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public List<WorkParameterValueResponse> getValues(List<WorkParameterValueRequest
&& workParameterValueRequests.get(0).getValue().equalsIgnoreCase("option2"))
return List.of(
WorkParameterValueResponse.builder().key("WORKFLOW_MULTI_SELECT_SAMPLE")
.options(List.of("option5", "option4", "option3")).value("option5").build(),
WorkParameterValueResponse.builder().key("description").value("dynamic-description").build());
.options(List.of("option5", "option4", "option3")).value("option5")
.workName("complexWorkFlow").build(),
WorkParameterValueResponse.builder().key("dynamic-options")
.options(List.of("option15", "option14", "option13")).value("option13")
.workName("adGroupsWorkFlowTask").build());
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public List<WorkParameter> getWorkFlowTaskParameters() {
WorkParameter.builder().key("adGroups").description("The ad groups").type(WorkParameterType.TEXT)
.optional(false).build(),
WorkParameter.builder().key("userId").description("The user id").type(WorkParameterType.TEXT)
.optional(false).build());
.optional(false).build(),
WorkParameter.builder().key("dynamic-options").description("Dynamic options")
.type(WorkParameterType.SELECT).optional(true).build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
@Setter
public class WorkFlowWorkDefinition extends AbstractEntity {

@Column(unique = true)
private UUID workDefinitionId;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@

import com.redhat.parodos.workflow.definition.dto.WorkParameterValueRequestDTO;
import com.redhat.parodos.workflow.definition.dto.WorkParameterValueResponseDTO;
import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition;
import com.redhat.parodos.workflow.definition.service.WorkFlowDefinitionService;
import com.redhat.parodos.workflow.parameter.WorkParameterValueRequest;
import com.redhat.parodos.workflow.parameter.WorkParameter;
import com.redhat.parodos.workflow.parameter.WorkParameterValueProvider;
import com.redhat.parodos.workflow.parameter.WorkParameterValueRequest;
import com.redhat.parodos.workflow.parameter.WorkParameterValueResponse;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

@Service
public class WorkParameterServiceImpl implements WorkParameterService {

private final Map<String, WorkParameterValueProvider> workFlowValueProviderMap;

public WorkParameterServiceImpl(Map<String, WorkParameterValueProvider> workFlowValueProviderMap) {
private final WorkFlowDefinitionService workFlowDefinitionService;

public WorkParameterServiceImpl(Map<String, WorkParameterValueProvider> workFlowValueProviderMap,
WorkFlowDefinitionService workFlowDefinitionService) {
this.workFlowValueProviderMap = workFlowValueProviderMap;
this.workFlowDefinitionService = workFlowDefinitionService;
}

@Override
Expand All @@ -33,10 +40,30 @@ public List<WorkParameterValueResponseDTO> getValues(String workflowDefinitionNa
.map(workParameterValueRequestDTO -> new ModelMapper().map(
workParameterValueRequestDTO, WorkParameterValueRequest.class))
.toList())
.stream().map(parameterValue -> new ModelMapper().map(parameterValue,
WorkParameterValueResponseDTO.class))
.toList())
.stream()
.map(parameterValue -> mappingParameterResponse(workflowDefinitionName, parameterValue))
.filter(Objects::nonNull).toList())
.orElse(List.of());
}

private WorkParameterValueResponseDTO mappingParameterResponse(String workflowDefinitionName,
WorkParameterValueResponse workParameterValueResponse) {
String workName = Optional.ofNullable(workParameterValueResponse.getWorkName()).orElse(workflowDefinitionName);
Map<String, Object> parameters = workFlowDefinitionService.getWorkParametersByWorkName(workName);
if (parameters == null || !parameters.containsKey(workParameterValueResponse.getKey()))
return null;

String propertyPath = workName;
WorkFlowDefinition workFlowDefinition = workFlowDefinitionService.getParentWorkFlow(workName);

while (workFlowDefinition != null) {
propertyPath = String.join(".", workFlowDefinition.getName(), propertyPath);
workFlowDefinition = workFlowDefinitionService.getParentWorkFlow(workFlowDefinition.getName());
}

return WorkParameterValueResponseDTO.builder().key(workParameterValueResponse.getKey())
.options(workParameterValueResponse.getOptions()).value(workParameterValueResponse.getValue())
.propertyPath(propertyPath).build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public interface WorkFlowWorkRepository extends JpaRepository<WorkFlowWorkDefini

List<WorkFlowWorkDefinition> findByWorkFlowDefinitionIdOrderByCreateDateAsc(UUID workFlowDefinitionId);

List<WorkFlowWorkDefinition> findByWorkDefinitionId(UUID workDefinitionId);
WorkFlowWorkDefinition findFirstByWorkDefinitionId(UUID workDefinitionId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.redhat.parodos.workflow.definition.service;

import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition;
import com.redhat.parodos.workflow.enums.WorkFlowProcessingType;
import com.redhat.parodos.workflow.enums.WorkFlowType;
import com.redhat.parodos.workflow.definition.dto.WorkFlowCheckerDTO;
Expand All @@ -24,6 +25,7 @@
import com.redhat.parodos.workflows.workflow.WorkFlowPropertiesMetadata;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
Expand All @@ -48,6 +50,10 @@ WorkFlowDefinitionResponseDTO save(String workFlowName, WorkFlowType workFlowTyp
void saveWorkFlowChecker(String workFlowTaskName, String workFlowCheckerName,
WorkFlowCheckerDTO workFlowCheckerDTO);

Map<String, Object> getWorkParametersByWorkName(String workName);

WorkFlowDefinition getParentWorkFlow(String workName);

void cleanAllDefinitionMappings();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package com.redhat.parodos.workflow.definition.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.redhat.parodos.common.AbstractEntity;
import com.redhat.parodos.workflow.definition.dto.WorkDefinitionResponseDTO;
import com.redhat.parodos.workflow.definition.dto.WorkFlowCheckerDTO;
import com.redhat.parodos.workflow.definition.dto.WorkFlowDefinitionResponseDTO;
import com.redhat.parodos.workflow.definition.entity.WorkFlowCheckerMappingDefinition;
import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition;
import com.redhat.parodos.workflow.definition.entity.WorkFlowPropertiesDefinition;
import com.redhat.parodos.workflow.definition.entity.WorkFlowTaskDefinition;
import com.redhat.parodos.workflow.definition.entity.WorkFlowWorkDefinition;
import com.redhat.parodos.workflow.definition.repository.WorkFlowCheckerMappingDefinitionRepository;
Expand All @@ -39,7 +41,6 @@
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import com.redhat.parodos.workflow.definition.entity.WorkFlowPropertiesDefinition;

import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -50,7 +51,6 @@
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
* workflow definition service implementation
Expand Down Expand Up @@ -190,7 +190,7 @@ public WorkFlowDefinitionResponseDTO getWorkFlowDefinitionById(UUID id) {
.orElseThrow(() -> new RuntimeException(String.format("Workflow definition id %s not found", id)));
List<WorkFlowWorkDefinition> workFlowWorkDependencies = workFlowWorkRepository
.findByWorkFlowDefinitionIdOrderByCreateDateAsc(workFlowDefinition.getId()).stream()
.sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).collect(Collectors.toList());
.sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).toList();
return WorkFlowDefinitionResponseDTO.fromEntity(workFlowDefinition,
buildWorkFlowWorksDTOs(workFlowDefinition, workFlowWorkDependencies));
}
Expand All @@ -203,7 +203,7 @@ public WorkFlowDefinitionResponseDTO getWorkFlowDefinitionByName(String name) {
}
List<WorkFlowWorkDefinition> workFlowWorkDependencies = workFlowWorkRepository
.findByWorkFlowDefinitionIdOrderByCreateDateAsc(workFlowDefinition.getId()).stream()
.sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).collect(Collectors.toList());
.sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).toList();

return WorkFlowDefinitionResponseDTO.fromEntity(workFlowDefinition,
buildWorkFlowWorksDTOs(workFlowDefinition, workFlowWorkDependencies));
Expand All @@ -230,6 +230,29 @@ public void saveWorkFlowChecker(String workFlowTaskName, String workFlowCheckerN
}
}

@Override
public Map<String, Object> getWorkParametersByWorkName(String workName) {
return Optional.ofNullable(workFlowTaskDefinitionRepository.findFirstByName(workName))
.map(WorkFlowTaskDefinition::getParameters).map(stringifyParameters -> WorkFlowDTOUtil
.readStringAsObject(stringifyParameters, new TypeReference<Map<String, Object>>() {
}, null))
.orElse(Optional.ofNullable(workFlowDefinitionRepository.findFirstByName(workName))
.map(WorkFlowDefinition::getParameters).map(stringifyParameters -> WorkFlowDTOUtil
.readStringAsObject(stringifyParameters, new TypeReference<Map<String, Object>>() {
}, null))
.orElse(null));
}

@Override
public WorkFlowDefinition getParentWorkFlow(String workName) {
UUID workId = Optional.ofNullable(workFlowDefinitionRepository.findFirstByName(workName))
.map(AbstractEntity::getId)
.orElse(Optional.ofNullable(workFlowTaskDefinitionRepository.findFirstByName(workName))
.map(AbstractEntity::getId).orElse(null));
return workId == null ? null : Optional.ofNullable(workFlowWorkRepository.findFirstByWorkDefinitionId(workId))
.map(WorkFlowWorkDefinition::getWorkFlowDefinition).orElse(null);
}

private void getWorksFromWorkDefinition(List<WorkFlowWorkDefinition> workFlowWorkDefinitions,
CopyOnWriteArrayList<WorkDefinitionResponseDTO> responseDTOs) {
workFlowWorkDefinitions.forEach(workFlowWorkDefinition -> {
Expand Down Expand Up @@ -298,8 +321,7 @@ private List<WorkDefinitionResponseDTO> buildWorkFlowWorksDTOs(WorkFlowDefinitio
List<WorkFlowWorkDefinition> workFlowWorkUnits1Definition = workFlowWorkRepository
.findByWorkFlowDefinitionIdOrderByCreateDateAsc(
UUID.fromString(workDefinitionResponseDTOs.get(i).getId()))
.stream().sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate))
.collect(Collectors.toList());
.stream().sorted(Comparator.comparing(WorkFlowWorkDefinition::getCreateDate)).toList();
this.getWorksFromWorkDefinition(workFlowWorkUnits1Definition, workDefinitionResponseDTOs);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private String validateWorkflow(String workflowName, WorkFlow workFlow) {
return String.format("workflow '%s' is not registered!", workflowName);
}

if (!workFlowWorkRepository.findByWorkDefinitionId(workFlowDefinition.getId()).isEmpty()) {
if (workFlowWorkRepository.findFirstByWorkDefinitionId(workFlowDefinition.getId()) != null) {
log.error("workflow '{}' is not main workflow!", workflowName);
return String.format("workflow '%s' is not main workflow!", workflowName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.redhat.parodos.workflow.enums.WorkFlowProcessingType;
import com.redhat.parodos.workflow.enums.WorkFlowType;
import com.redhat.parodos.workflow.parameter.WorkParameter;
import com.redhat.parodos.workflow.parameter.WorkParameterType;
import com.redhat.parodos.workflow.task.WorkFlowTask;
import com.redhat.parodos.workflows.work.Work;
import com.redhat.parodos.workflows.workflow.WorkFlow;
Expand All @@ -41,12 +40,10 @@
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* An implementation of the WorkflowRegistry that loads all Bean definitions of type
Expand All @@ -59,7 +56,7 @@

@Slf4j
@Component
public class BeanWorkFlowRegistryImpl implements WorkFlowRegistry<String> {
public class BeanWorkFlowRegistryImpl implements WorkFlowRegistry {

// Spring will populate this through classpath scanning when the Context starts up
private final ConfigurableListableBeanFactory beanFactory;
Expand Down Expand Up @@ -109,27 +106,16 @@ private void saveWorkFlow(String workFlowBeanName, WorkFlow workFlowBean) {
// workflow type
WorkFlowType workFlowType = workFlowTypeDetailsPair.getFirst();
// workflow parameters from annotation attributes
AnnotationAttributes[] annotationAttributes = (AnnotationAttributes[]) workFlowTypeDetailsPair.getSecond()
.get("parameters");
List<WorkParameter> workParameters = new ArrayList<>();
if (annotationAttributes != null && annotationAttributes.length > 0) {
workParameters = Arrays.stream(annotationAttributes)
.map(annotationAttribute -> WorkParameter.builder().key(annotationAttribute.getString("key"))
.description(annotationAttribute.getString("description"))
.type((WorkParameterType) annotationAttribute.get("type"))
.optional(annotationAttribute.getBoolean("optional"))
.selectOptions(Arrays.stream(annotationAttribute.getStringArray("selectOptions")).toList())
.valueProviderName(annotationAttribute.getString("valueProviderName")).build())
.toList();
}
List<WorkParameter> workParameters = WorkFlowRegistryDelegate
.getWorkParameters((AnnotationAttributes[]) workFlowTypeDetailsPair.getSecond().get("parameters"));
workFlowDefinitionService.save(workFlowBeanName, workFlowType, workFlowBean.getProperties(), workParameters,
works, getWorkFlowProcessingType(workFlowBean));
}

private List<Work> getWorks(String workFlowName) {
return Arrays.stream(beanFactory.getDependenciesForBean(workFlowName))
.filter(dependency -> isBeanInstanceOf(beanFactory, dependency, WorkFlow.class, WorkFlowTask.class))
.map(dependency -> beanFactory.getBean(dependency, Work.class)).collect(Collectors.toList());
.map(dependency -> beanFactory.getBean(dependency, Work.class)).toList();
}

private void saveChecker(Map<String, WorkFlowTask> workFlowTasks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
/**
* Contract for Registering and Obtaining Workflows. Implementations could use the
* classpath, Spring Bean registry, a Database or some other custom means.
*
* <p>
* For the WorkFlowRegistry in-memory collection the Key: BeanId, Value: WorkFlow
*
* @author Luke Shannon (Github: lshannon)
* @author Annel Ketcha (Github: anludke)
* @author Richard Wang (Github: richardw98)
*/
public interface WorkFlowRegistry<T> {
public interface WorkFlowRegistry {

WorkFlow getWorkFlowByName(T workFlowName);
WorkFlow getWorkFlowByName(String workFlowName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.redhat.parodos.workflow.registry;

import com.redhat.parodos.workflow.parameter.WorkParameter;
import com.redhat.parodos.workflow.parameter.WorkParameterType;
import lombok.NonNull;
import org.springframework.core.annotation.AnnotationAttributes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WorkFlowRegistryDelegate {

private WorkFlowRegistryDelegate() {
}

@NonNull
static List<WorkParameter> getWorkParameters(AnnotationAttributes[] annotationAttributes) {
List<WorkParameter> workParameters = new ArrayList<>();
if (annotationAttributes != null && annotationAttributes.length > 0) {
workParameters = Arrays.stream(annotationAttributes)
.map(annotationAttribute -> WorkParameter.builder().key(annotationAttribute.getString("key"))
.description(annotationAttribute.getString("description"))
.type((WorkParameterType) annotationAttribute.get("type"))
.optional(annotationAttribute.getBoolean("optional"))
.selectOptions(Arrays.stream(annotationAttribute.getStringArray("selectOptions")).toList())
.valueProviderName(annotationAttribute.getString("valueProviderName")).build())
.toList();
}
return workParameters;
}

}
Loading

0 comments on commit 14216b3

Please sign in to comment.