Skip to content

Commit cdd2d87

Browse files
committed
Extracted default method (#117)
1 parent 9b54f96 commit cdd2d87

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

lib/src/main/java/com/imsweb/staging/engine/DecisionEngine.java

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class DecisionEngine {
4646

4747
// string to use for blank or null in error strings
4848
public static final String _BLANK_OUTPUT = "<blank>";
49-
private static final Pattern _TEMPLATE_REFERENCE = Pattern.compile("\\{\\{(.*?)\\}\\}");
49+
private static final Pattern _TEMPLATE_REFERENCE = Pattern.compile("\\{\\{(.*?)}}");
5050
private DataProvider _provider;
5151

5252
private static final String _CONTEXT_MISSING_MESSAGE = "Context must not be missing";
@@ -583,6 +583,49 @@ public Set<String> getOutputs(Schema schema) {
583583
return outputs;
584584
}
585585

586+
/**
587+
* Calculates the default value for an Input using supplied context
588+
* @param input Input definition
589+
* @param context a Map containing the context
590+
* @param result a Result object to store errors
591+
* @return the default value for the input or blank if there is none
592+
*/
593+
public String getDefault(Input input, Map<String, String> context, Result result) {
594+
String value = "";
595+
596+
if (input.getDefault() != null)
597+
value = translateValue(input.getDefault(), context);
598+
else if (input.getDefaultTable() != null) {
599+
Table defaultTable = getProvider().getTable(input.getDefaultTable());
600+
if (defaultTable == null) {
601+
result.addError(new ErrorBuilder(Type.UNKNOWN_TABLE).message("Default table does not exist: " + input.getDefaultTable()).key(input.getKey()).build());
602+
return value;
603+
}
604+
605+
// look up default value from table
606+
List<? extends Endpoint> endpoints = matchTable(defaultTable, context);
607+
if (endpoints != null) {
608+
value = endpoints.stream()
609+
.filter(endpoint -> EndpointType.VALUE.equals(endpoint.getType()))
610+
.filter(endpoint -> endpoint.getResultKey().equals(input.getKey()))
611+
.map(endpoint -> translateValue(endpoint.getValue(), context))
612+
.findFirst()
613+
.orElse(null);
614+
}
615+
616+
// if no match found, report the error
617+
if (endpoints == null || value == null) {
618+
result.addError(new ErrorBuilder(Type.MATCH_NOT_FOUND)
619+
.message("Default table " + input.getDefaultTable() + " did not find a match")
620+
.key(input.getKey())
621+
.build());
622+
return "";
623+
}
624+
}
625+
626+
return value;
627+
}
628+
586629
/**
587630
* Using the supplied context, process an schema. The results will be added to the context.
588631
* @param schemaId an schema identifier
@@ -620,40 +663,8 @@ public Result process(Schema schema, Map<String, String> context) {
620663
String value = context.get(input.getKey());
621664

622665
// if value not supplied, use the default or defaultTable and set it back into the context; if not supplied and no default, set the input the blank
623-
if (value == null) {
624-
if (input.getDefault() != null)
625-
value = translateValue(input.getDefault(), context);
626-
else if (input.getDefaultTable() != null) {
627-
Table defaultTable = getProvider().getTable(input.getDefaultTable());
628-
if (defaultTable == null) {
629-
result.addError(new ErrorBuilder(Type.UNKNOWN_TABLE).message("Default table does not exist: " + input.getDefaultTable()).key(input.getKey()).build());
630-
continue;
631-
}
632-
633-
// look up default value from table
634-
List<? extends Endpoint> endpoints = matchTable(defaultTable, context);
635-
if (endpoints != null) {
636-
value = endpoints.stream()
637-
.filter(endpoint -> EndpointType.VALUE.equals(endpoint.getType()))
638-
.filter(endpoint -> endpoint.getResultKey().equals(input.getKey()))
639-
.map(endpoint -> translateValue(endpoint.getValue(), context))
640-
.findFirst()
641-
.orElse(null);
642-
}
643-
644-
if (value == null) {
645-
result.addError(new ErrorBuilder(Type.MATCH_NOT_FOUND)
646-
.message("Default table " + input.getDefaultTable() + " did not find a match")
647-
.key(input.getKey())
648-
.build());
649-
continue;
650-
}
651-
}
652-
else
653-
value = "";
654-
655-
context.put(input.getKey(), value == null ? "" : value);
656-
}
666+
if (value == null)
667+
context.put(input.getKey(), getDefault(input, context, result));
657668

658669
// validate value against associated table, if supplied; if a value is not supplied, or blank, there is no need to validate it against the table
659670
if (value != null && !value.isEmpty() && input.getTable() != null) {

0 commit comments

Comments
 (0)