4545import de .symeda .sormas .backend .feature .FeatureConfigurationFacadeEjb ;
4646import de .symeda .sormas .backend .json .ObjectMapperProvider ;
4747import de .symeda .sormas .backend .patch .mapping .FieldCustomMapperRegistry ;
48+ import de .symeda .sormas .backend .patch .mapping .GroupedFieldMapperRegistry ;
4849import de .symeda .sormas .backend .patch .mapping .ValueMapperRegistry ;
4950import de .symeda .sormas .backend .person .PersonFacadeEjb ;
5051import de .symeda .sormas .backend .util .CollectorUtils ;
@@ -89,6 +90,9 @@ public class DataPatcherImpl implements DataPatcher {
8990 @ EJB
9091 private ConfigFacadeEjb .ConfigFacadeEjbLocal configFacade ;
9192
93+ @ Inject
94+ private GroupedFieldMapperRegistry groupedFieldMapperRegistry ;
95+
9296 public DataPatcherImpl () {
9397 }
9498
@@ -120,14 +124,16 @@ public DataPatchResponse patch(CaseDataPatchRequest request) {
120124 // make this generic for additional "root"-types
121125 Supplier <PersonDto > personSupplier = Suppliers .memoize (() -> getPersonDto (caseData ));
122126
123- Stream <Tuple <String , Tuple <DataPatchFailureCause , Object >>> tupleStream = computeActualDictionary (request );
127+ List <Tuple <String , Tuple <DataPatchFailureCause , Object >>> patchingTuples = computePatchingTuples (request );
124128
125- // TODO: tupleStream (transform to map ?) must be passed to the group field handler so that it can be handled.
129+ groupedFieldMapperRegistry .aggregatedPatch ()
130+
131+ // TODO: patchingTuples (transform to map ?) must be passed to the group field handler so that it can be handled.
126132 // TODO: provide only the valid one as simple object ? Once this is done the actual dictionary must be not contain does anymore
127133
128- List <SinglePatchResult > results = tupleStream .map (entry -> {
134+ List <de . symeda . sormas . api . patch . SinglePatchResult > results = patchingTuples . stream () .map (entry -> {
129135 String fullFieldName = entry .getFirst ();
130- SinglePatchResult singlePatchResult = new SinglePatchResult ().setFieldName (fullFieldName );
136+ de . symeda . sormas . api . patch . SinglePatchResult singlePatchResult = new de . symeda . sormas . api . patch . SinglePatchResult ().setFieldName (fullFieldName );
131137
132138 Supplier <Object > target = () -> findAppropriateTarget (fullFieldName , caseData , personSupplier );
133139
@@ -142,9 +148,9 @@ public DataPatchResponse patch(CaseDataPatchRequest request) {
142148
143149 }).collect (Collectors .toList ());
144150
145- Map <String , Object > validPatchDictionary = buildDictionaryFor (results , SinglePatchResult ::getValue , true );
151+ Map <String , Object > validPatchDictionary = buildDictionaryFor (results , de . symeda . sormas . api . patch . SinglePatchResult ::getValue , true );
146152 DataPatchResponse response = new DataPatchResponse ().setApplied (false )
147- .setFailures (buildDictionaryFor (results , SinglePatchResult ::getFailure , false ))
153+ .setFailures (buildDictionaryFor (results , de . symeda . sormas . api . patch . SinglePatchResult ::getFailure , false ))
148154 .setValidPatchDictionary (validPatchDictionary );
149155
150156 if (validPatchDictionary .isEmpty () || (!request .isPatchedInCaseOfFailures () && response .hasFailures ())) {
@@ -190,27 +196,28 @@ private boolean anyFieldPatchedWithPrefix(Map<String, Object> validPatchDictiona
190196 }
191197
192198 private @ NotNull <R > Map <String , R > buildDictionaryFor (
193- List <SinglePatchResult > results ,
194- Function <SinglePatchResult , R > fct ,
199+ List <de . symeda . sormas . api . patch . SinglePatchResult > results ,
200+ Function <de . symeda . sormas . api . patch . SinglePatchResult , R > fct ,
195201 boolean valueContext ) {
196202 return results .stream ()
197203 // edge case were target value is null: this is allowed, which makes both fields null.
198204 .filter (
199205 singlePatchResult -> fct .apply (singlePatchResult ) != null
200206 || (valueContext && singlePatchResult .getFailure () == null && singlePatchResult .getValue () == null ))
201- .collect (CollectorUtils .toNullSafeMap (SinglePatchResult ::getFieldName , fct ));
207+ .collect (CollectorUtils .toNullSafeMap (de . symeda . sormas . api . patch . SinglePatchResult ::getFieldName , fct ));
202208 }
203209
204210 // TODO: make usable for external use
205- private @ NotNull SinglePatchResult valueMappingResult (
211+ private @ NotNull de . symeda . sormas . api . patch . SinglePatchResult valueMappingResult (
206212 Tuple <String , Tuple <DataPatchFailureCause , Object >> entry ,
207213 Disease disease ,
208214 CaseDataPatchRequest request ,
209215 Supplier <Object > targetOpt ) {
210216
211217 String fullFieldName = entry .getFirst ();
212218
213- SinglePatchResult singlePatchResult = new SinglePatchResult ().setFieldName (fullFieldName );
219+ de .symeda .sormas .api .patch .SinglePatchResult singlePatchResult =
220+ new de .symeda .sormas .api .patch .SinglePatchResult ().setFieldName (fullFieldName );
214221
215222 Object target = targetOpt .get ();
216223 String relativeFieldName = fullFieldName .substring (fullFieldName .indexOf ('.' ) + 1 );
@@ -271,7 +278,8 @@ private DataPatchFailure buildFailure(DataPatchFailureCause fieldDoesNotExist, O
271278 return new DataPatchFailure ().setDataPatchFailureCause (fieldDoesNotExist ).setProvidedFieldValue (untypedTargetValue );
272279 }
273280
274- private @ NotNull Optional <SinglePatchResult > invalidFieldResult (Tuple <String , Tuple <DataPatchFailureCause , Object >> entry ) {
281+ private @ NotNull Optional <de .symeda .sormas .api .patch .SinglePatchResult > invalidFieldResult (
282+ Tuple <String , Tuple <DataPatchFailureCause , Object >> entry ) {
275283 return Optional .ofNullable (extractFailureCause (entry )).map (invalidFieldFailureCause -> buildFailureFor (entry , invalidFieldFailureCause ));
276284 }
277285
@@ -280,7 +288,7 @@ private DataPatchFailureCause extractFailureCause(Tuple<String, Tuple<DataPatchF
280288 }
281289
282290 // TODO: make usable for external use
283- public Optional <SinglePatchResult > fieldMappingResult (
291+ public Optional <de . symeda . sormas . api . patch . SinglePatchResult > fieldMappingResult (
284292 Tuple <String , Tuple <DataPatchFailureCause , Object >> entry ,
285293 Disease disease ,
286294 CaseDataPatchRequest request ,
@@ -292,7 +300,8 @@ public Optional<SinglePatchResult> fieldMappingResult(
292300
293301 Object untypedTargetValue = extractValue (entry );
294302 if (mapper .isPresent ()) {
295- SinglePatchResult singlePatchResult = new SinglePatchResult ().setFieldName (fullFieldName );
303+ de .symeda .sormas .api .patch .SinglePatchResult singlePatchResult =
304+ new de .symeda .sormas .api .patch .SinglePatchResult ().setFieldName (fullFieldName );
296305
297306 Optional <DataPatchFailure > dataPatchFailureOpt = mapper .orElseThrow ()
298307 .map (
@@ -308,9 +317,12 @@ public Optional<SinglePatchResult> fieldMappingResult(
308317 return Optional .empty ();
309318 }
310319
311- private SinglePatchResult buildFailureFor (Tuple <String , Tuple <DataPatchFailureCause , Object >> entry , DataPatchFailureCause fieldFailureCause ) {
320+ private de .symeda .sormas .api .patch .SinglePatchResult buildFailureFor (
321+ Tuple <String , Tuple <DataPatchFailureCause , Object >> entry ,
322+ DataPatchFailureCause fieldFailureCause ) {
312323
313- return new SinglePatchResult ().setFieldName (entry .getFirst ()).setFailure (buildFailure (fieldFailureCause , extractValue (entry )));
324+ return new de .symeda .sormas .api .patch .SinglePatchResult ().setFieldName (entry .getFirst ())
325+ .setFailure (buildFailure (fieldFailureCause , extractValue (entry )));
314326 }
315327
316328 private Object extractValue (Tuple <String , Tuple <DataPatchFailureCause , Object >> entry ) {
@@ -323,7 +335,7 @@ private FieldVisibilityCheckers getFieldVisibilityCheckers(Disease disease) {
323335 .andWithFeatureType (featureConfigurationFacade .getActiveServerFeatureConfigurations ());
324336 }
325337
326- private Stream <Tuple <String , Tuple <DataPatchFailureCause , Object >>> computeActualDictionary (CaseDataPatchRequest request ) {
338+ private List <Tuple <String , Tuple <DataPatchFailureCause , Object >>> computePatchingTuples (CaseDataPatchRequest request ) {
327339 Predicate <Map .Entry <String , Object >> filterPredicate = buildAdequateDictionaryValuePredicate (request );
328340
329341 return request .getPatchDictionary ()
@@ -351,7 +363,8 @@ private Stream<Tuple<String, Tuple<DataPatchFailureCause, Object>>> computeActua
351363
352364 return splitMultipleFieldsPath (entry );
353365 })
354- .map (tuple -> Tuple .of (tuple .getFirst (), tuple .getSecond ()));
366+ .map (tuple -> Tuple .of (tuple .getFirst (), tuple .getSecond ()))
367+ .collect (Collectors .toList ());
355368 }
356369
357370 private AbstractMap .@ NotNull SimpleEntry <String , Object > toMapEntry (String first , Object value ) {
0 commit comments