1818import java .net .http .HttpClient ;
1919import java .net .http .HttpRequest ;
2020import java .net .http .HttpResponse ;
21+ import java .time .LocalDate ;
22+ import java .util .ArrayList ;
2123import java .util .HashMap ;
2224import java .util .List ;
2325import java .util .Map ;
2729@ ApplicationScoped
2830public class LibraryApiService {
2931 private static final String DEFAULT_LIBRARY_API_URL = "http://localhost:8083" ;
32+ private static final String AS_OF_DATE_PARAMETER = "asOfDate" ;
33+
34+ public record LibraryCheckEvaluation (
35+ EvaluationResult result ,
36+ Map <String , Object > effectiveParameters ,
37+ List <String > defaultedParameters
38+ ) {}
39+
40+ record EffectiveParameters (
41+ Map <String , Object > parameters ,
42+ List <String > defaultedParameters
43+ ) {}
3044
3145 @ Inject
3246 private StorageService storageService ;
@@ -100,12 +114,13 @@ public Optional<EligibilityCheck> getById(String id) {
100114 return Optional .of (matches .getFirst ());
101115 }
102116
103- public EvaluationResult evaluateCheck (CheckConfig checkConfig , Map <String , Object > inputs ) throws JsonProcessingException {
117+ public LibraryCheckEvaluation evaluateCheck (CheckConfig checkConfig , Map <String , Object > inputs ) throws JsonProcessingException {
104118
105119 // TODO: Check that checkConfig has required attributes and handle null values
120+ EffectiveParameters effectiveParameters = buildEffectiveParameters (checkConfig );
106121
107122 Map <String , Object > data = new HashMap <>();
108- data .put ("parameters" , checkConfig . getParameters ());
123+ data .put ("parameters" , effectiveParameters . parameters ());
109124 data .put ("situation" , inputs );
110125 ObjectMapper mapper = new ObjectMapper ();
111126 String bodyJson = mapper .writeValueAsString (data );
@@ -139,7 +154,11 @@ public EvaluationResult evaluateCheck(CheckConfig checkConfig, Map<String, Objec
139154 if (statusCode != 200 ){
140155 Log .error ("Error evaluating library check " + checkConfig .getCheckId ());
141156 Log .error ("Inputs and parameters that caused error:" + bodyJson );
142- return EvaluationResult .UNABLE_TO_DETERMINE ;
157+ return new LibraryCheckEvaluation (
158+ EvaluationResult .UNABLE_TO_DETERMINE ,
159+ effectiveParameters .parameters (),
160+ effectiveParameters .defaultedParameters ()
161+ );
143162 }
144163 String body = response .body ();
145164 Map <String , Object > responseBody = mapper .readValue (
@@ -150,13 +169,52 @@ public EvaluationResult evaluateCheck(CheckConfig checkConfig, Map<String, Objec
150169 // TODO: Need a safer way to validate the returned data is in the right format
151170 Object result = responseBody .get ("checkResult" );
152171 if (result == null ) {
153- return EvaluationResult .UNABLE_TO_DETERMINE ;
172+ return new LibraryCheckEvaluation (
173+ EvaluationResult .UNABLE_TO_DETERMINE ,
174+ effectiveParameters .parameters (),
175+ effectiveParameters .defaultedParameters ()
176+ );
154177 }
155- return EvaluationResult .fromStringIgnoreCase (result .toString ());
178+ return new LibraryCheckEvaluation (
179+ EvaluationResult .fromStringIgnoreCase (result .toString ()),
180+ effectiveParameters .parameters (),
181+ effectiveParameters .defaultedParameters ()
182+ );
156183 }
157184 catch (Exception e ){
158185 Log .error (e );
159- return EvaluationResult .UNABLE_TO_DETERMINE ;
186+ return new LibraryCheckEvaluation (
187+ EvaluationResult .UNABLE_TO_DETERMINE ,
188+ effectiveParameters .parameters (),
189+ effectiveParameters .defaultedParameters ()
190+ );
191+ }
192+ }
193+
194+ EffectiveParameters buildEffectiveParameters (CheckConfig checkConfig ) {
195+ Map <String , Object > configuredParameters = checkConfig .getParameters () != null
196+ ? checkConfig .getParameters ()
197+ : Map .of ();
198+ Map <String , Object > parameters = new HashMap <>(configuredParameters );
199+ List <String > defaultedParameters = new ArrayList <>();
200+
201+ if (declaresAsOfDateParameter (checkConfig ) && isMissingParameter (parameters .get (AS_OF_DATE_PARAMETER ))) {
202+ parameters .put (AS_OF_DATE_PARAMETER , LocalDate .now ().toString ());
203+ defaultedParameters .add (AS_OF_DATE_PARAMETER );
160204 }
205+
206+ return new EffectiveParameters (parameters , defaultedParameters );
207+ }
208+
209+ private boolean declaresAsOfDateParameter (CheckConfig checkConfig ) {
210+ if (checkConfig .getParameterDefinitions () == null ) {
211+ return false ;
212+ }
213+ return checkConfig .getParameterDefinitions ().stream ()
214+ .anyMatch (parameterDefinition -> AS_OF_DATE_PARAMETER .equals (parameterDefinition .getKey ()));
215+ }
216+
217+ private boolean isMissingParameter (Object value ) {
218+ return value == null || (value instanceof String && ((String ) value ).isBlank ());
161219 }
162220}
0 commit comments