From ede3af3f46a08322d94afcf8284d8d9921c93b5f Mon Sep 17 00:00:00 2001 From: Annel Ketcha Date: Fri, 16 Jun 2023 09:03:37 -0400 Subject: [PATCH 1/2] provide the ability to get optional and/or nullable param --- .../workflow/task/BaseWorkFlowTask.java | 18 ++++++++++++++++++ .../ProjectAccessRequestWorkFlowTask.java | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java b/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java index 127317556..d063eabd4 100644 --- a/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java +++ b/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java @@ -32,6 +32,8 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.annotation.Autowired; +import static java.util.Objects.isNull; + /** * Base Class for a WorkFlowTask. *

@@ -123,4 +125,20 @@ public String getOptionalParameterValue(String parameterName, String defaultValu .map(Map.Entry::getValue).findFirst().orElse(defaultValue); } + /** + * Gets non-null and non-empty optional parameter. Returns the defaultValue if not + * found + * @param parameterName parameter name + * @param defaultValue default value + * @param isNullable is nullable + * @return parameter value + * @throws MissingParameterException exception + */ + public String getOptionalParameterValue(String parameterName, String defaultValue, Boolean isNullable) { + Map parameters = getAllParameters(workContext); + return parameters.entrySet().stream().filter(entry -> !isNullable && !isNull(entry.getValue()) && !entry.getValue().equalsIgnoreCase("null")) + .filter(entry -> parameterName.equals(entry.getKey())) + .map(Map.Entry::getValue).findFirst().orElse(defaultValue); + } + } diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java index 773eedf77..d6ff06df7 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTask.java @@ -54,7 +54,7 @@ public WorkReport execute(WorkContext workContext) { String role; try { username = getRequiredParameterValue(PARAMETER_USERNAME); - role = getOptionalParameterValue(PARAMETER_ROLE, PARAMETER_ROLE_DEFAULT); + role = getOptionalParameterValue(PARAMETER_ROLE, PARAMETER_ROLE_DEFAULT, false); log.info("Project access request with the following - username: {}, role: {}", username, role); } catch (MissingParameterException e) { From 2e220415d5b053c25fa3df7b36ec8ae388053967 Mon Sep 17 00:00:00 2001 From: Annel Ketcha Date: Fri, 16 Jun 2023 09:40:05 -0400 Subject: [PATCH 2/2] update comment --- .../redhat/parodos/workflow/task/BaseWorkFlowTask.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java b/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java index d063eabd4..a10055052 100644 --- a/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java +++ b/parodos-model-api/src/main/java/com/redhat/parodos/workflow/task/BaseWorkFlowTask.java @@ -126,7 +126,7 @@ public String getOptionalParameterValue(String parameterName, String defaultValu } /** - * Gets non-null and non-empty optional parameter. Returns the defaultValue if not + * Gets an optional parameter and nullable or not. Returns the defaultValue if not * found * @param parameterName parameter name * @param defaultValue default value @@ -136,9 +136,10 @@ public String getOptionalParameterValue(String parameterName, String defaultValu */ public String getOptionalParameterValue(String parameterName, String defaultValue, Boolean isNullable) { Map parameters = getAllParameters(workContext); - return parameters.entrySet().stream().filter(entry -> !isNullable && !isNull(entry.getValue()) && !entry.getValue().equalsIgnoreCase("null")) - .filter(entry -> parameterName.equals(entry.getKey())) - .map(Map.Entry::getValue).findFirst().orElse(defaultValue); + return parameters.entrySet().stream() + .filter(entry -> !isNullable && !isNull(entry.getValue()) && !entry.getValue().equalsIgnoreCase("null")) + .filter(entry -> parameterName.equals(entry.getKey())).map(Map.Entry::getValue).findFirst() + .orElse(defaultValue); } }