|
2 | 2 |
|
3 | 3 | import java.util.List; |
4 | 4 | import java.util.Map; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.stream.Collectors; |
5 | 8 |
|
6 | 9 | import javax.ejb.EJB; |
7 | 10 | import javax.enterprise.context.ApplicationScoped; |
8 | 11 | import javax.inject.Inject; |
9 | 12 | import javax.validation.constraints.NotNull; |
10 | 13 |
|
11 | 14 | import org.apache.commons.collections4.CollectionUtils; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
12 | 17 |
|
13 | 18 | import de.symeda.sormas.api.caze.CaseDataDto; |
14 | 19 | import de.symeda.sormas.api.customizablefield.CustomizableFieldContext; |
15 | 20 | import de.symeda.sormas.api.customizablefield.CustomizableFieldMetadataDto; |
16 | 21 | import de.symeda.sormas.api.customizablefield.CustomizableFieldValueDto; |
| 22 | +import de.symeda.sormas.api.externalmessage.survey.PatchField; |
17 | 23 | import de.symeda.sormas.api.patch.CaseDataPatchRequest; |
| 24 | +import de.symeda.sormas.api.patch.DataPatchFailureCause; |
18 | 25 | import de.symeda.sormas.api.patch.SinglePatchResult; |
| 26 | +import de.symeda.sormas.api.patch.mapping.ValueMappingResult; |
19 | 27 | import de.symeda.sormas.api.utils.Tuple; |
20 | 28 | import de.symeda.sormas.backend.customizablefield.CustomizableFieldValueFacadeEjb; |
21 | 29 | import de.symeda.sormas.backend.patch.DataPatcherImpl; |
22 | 30 | import de.symeda.sormas.backend.patch.customizablefield.mappers.CustomizableFieldValuePatchMapperRegistry; |
| 31 | +import de.symeda.sormas.backend.util.CollectorUtils; |
23 | 32 |
|
24 | 33 | /** |
25 | 34 | * Wrapper arround {@link de.symeda.sormas.api.customizablefield.CustomizableFieldValueFacade} to be able to patch customizable field |
|
28 | 37 | @ApplicationScoped |
29 | 38 | public class CustomizableFieldDataPatcher { |
30 | 39 |
|
| 40 | + private final static Logger logger = LoggerFactory.getLogger(CustomizableFieldDataPatcher.class); |
| 41 | + |
31 | 42 | @EJB |
32 | 43 | private CustomizableFieldValueFacadeEjb.CustomizableFieldValueFacadeEjbLocal facade; |
33 | 44 |
|
34 | 45 | @Inject |
35 | 46 | private CustomizableFieldValuePatchMapperRegistry registry; |
36 | 47 |
|
37 | | - public List<Tuple<SinglePatchResult, CustomizableFieldValueDto>> patch(Request request) { |
| 48 | + @Inject |
| 49 | + private CustomizableFieldHelper customizableFieldHelper; |
| 50 | + |
| 51 | + public List<Tuple<SinglePatchResult, ValueMappingResult<CustomizableFieldValueDto>>> patch(Request request) { |
38 | 52 | List<DataPatcherImpl.SingleFieldPatchResult> patchingTuples = request.getPatchingTuples(); |
39 | 53 |
|
40 | 54 | if (CollectionUtils.isEmpty(patchingTuples)) { |
41 | 55 | return List.of(); |
42 | 56 | } |
43 | 57 |
|
44 | | - CaseDataPatchRequest caseDataPatchRequest = request.getCaseDataPatchRequest(); |
| 58 | + List<HelperClass> tempCollect = patchingTuples.stream() |
| 59 | + .map( |
| 60 | + singleFieldResult -> customizableFieldHelper.from(singleFieldResult.getField()) |
| 61 | + .map(context -> new HelperClass().setPatchField(context)) |
| 62 | + .orElseGet( |
| 63 | + () -> new HelperClass().setSingleFieldPatchResult( |
| 64 | + new DataPatcherImpl.SingleFieldPatchResult().setField(singleFieldResult.getField()) |
| 65 | + .setFailureCause(DataPatchFailureCause.INVALID_CUSTOM_CONTEXT)))) |
| 66 | + .collect(Collectors.toList()); |
| 67 | + |
| 68 | + Set<CustomizableFieldContext> contexts = |
| 69 | + tempCollect.stream().map(HelperClass::getPatchField).map(CustomizablePatchField::getContext).collect(Collectors.toSet()); |
| 70 | + |
| 71 | + Map<CustomizableFieldContext, Map<CustomizableFieldMetadataDto, CustomizableFieldValueDto>> customizableByContextDictionary = |
| 72 | + contexts.stream() |
| 73 | + .map(context -> Tuple.of(context, facade.getValuesForEntity(extractEntityId(request.getCaseDataDto(), context), context))) |
| 74 | + .collect(Collectors.toMap(Tuple::getFirst, Tuple::getSecond)); |
| 75 | + |
| 76 | + List<ValueMappingResult<CustomizableFieldValueDto>> results = tempCollect.stream().peek(helperClass -> { |
| 77 | + CustomizablePatchField patchField = helperClass.getPatchField(); |
| 78 | + |
| 79 | + Map<CustomizableFieldMetadataDto, CustomizableFieldValueDto> map = customizableByContextDictionary.get(patchField); |
| 80 | + |
| 81 | + Optional<Map.Entry<CustomizableFieldMetadataDto, CustomizableFieldValueDto>> singleOpt = map.entrySet() |
| 82 | + .stream() |
| 83 | + .filter(entry -> entry.getKey().getName().equals(patchField.getLeafFieldName())) |
| 84 | + .collect(CollectorUtils.toOptionalSingle()); |
| 85 | + |
| 86 | + if (singleOpt.isEmpty()) { |
| 87 | + logger.error("For context. [{}] the leaf field does not exist: [{}]", patchField.getContext(), patchField.getLeafFieldName()); |
| 88 | + helperClass.setFailureCause(DataPatchFailureCause.FIELD_DOES_NOT_EXIST); |
| 89 | + } else { |
| 90 | + helperClass.setCustomizableFieldValue(singleOpt.get().getValue()); |
| 91 | + helperClass.setMetadata(singleOpt.get().getKey()); |
| 92 | + } |
| 93 | + // TODO: handle errors: |
| 94 | + // - leafFieldName not found |
| 95 | + // - other error during mapping. |
| 96 | + }) |
| 97 | + .map( |
| 98 | + helperClass -> new CustomizableFieldValuePatchRequest().setValue(helperClass.getSingleFieldPatchResult().getValue()) |
| 99 | + .setTargetType(helperClass.getMetadata().getFieldType()) |
| 100 | + .setCustomizableFieldValueDto(helperClass.getCustomizableFieldValue())) |
| 101 | + .map(a -> registry.map(a)) |
| 102 | + .collect(Collectors.toList()); |
| 103 | + |
| 104 | + return results; |
| 105 | + |
| 106 | +// contextsToLoad.stream().map(a -> facade.getValuesForEntity("", a)) |
| 107 | +// .flatMap(a-> a.entrySet().stream() |
| 108 | +// .map(b ->Tuple.of(b.getKey(), b.getValue()))) |
| 109 | +// .filter(a -> ); |
| 110 | +// |
| 111 | +// |
| 112 | +// |
| 113 | +// return valuesForEntity.entrySet().stream() |
| 114 | +// .filter(a -> patchingTuples.stream().anyMatch(b -> b.)) |
| 115 | +// .map(a -> new CustomizableFieldValuePatchRequest()) |
| 116 | +// .map(request1 -> Tuple.of(registry.map(request1))) |
| 117 | +// .tempCollect(Collectors.toList()); |
| 118 | + } |
45 | 119 |
|
46 | | - Map<CustomizableFieldMetadataDto, CustomizableFieldValueDto> valuesForEntity = |
47 | | - facade.getValuesForEntity(caseDataPatchRequest.getCaseUuid(), CustomizableFieldContext.CASE); |
| 120 | + public String extractEntityId(CaseDataDto caseDataDto, CustomizableFieldContext context) { |
| 121 | + if (context == CustomizableFieldContext.CASE) { |
| 122 | + return caseDataDto.getUuid(); |
| 123 | + } else if (context == CustomizableFieldContext.EPIDATA) { |
| 124 | + return caseDataDto.getEpiData().getUuid(); |
| 125 | + } else { |
| 126 | + logger.error("Unsupported for now."); |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
48 | 130 |
|
49 | | - return List.of(); |
| 131 | + public boolean match(PatchField patchField, CustomizableFieldMetadataDto metadata) { |
| 132 | + return false; |
50 | 133 | } |
51 | 134 |
|
52 | 135 | public void save(List<CustomizableFieldValueDto> values) { |
@@ -91,4 +174,59 @@ public Request setCaseDataDto(CaseDataDto caseDataDto) { |
91 | 174 | return this; |
92 | 175 | } |
93 | 176 | } |
| 177 | + |
| 178 | + public static final class HelperClass { |
| 179 | + |
| 180 | + private CustomizablePatchField patchField; |
| 181 | + private DataPatcherImpl.SingleFieldPatchResult singleFieldPatchResult; |
| 182 | + private DataPatchFailureCause failureCause; |
| 183 | + |
| 184 | + private CustomizableFieldMetadataDto metadata; |
| 185 | + private CustomizableFieldValueDto customizableFieldValue; |
| 186 | + |
| 187 | + public CustomizablePatchField getPatchField() { |
| 188 | + return patchField; |
| 189 | + } |
| 190 | + |
| 191 | + public HelperClass setPatchField(CustomizablePatchField patchField) { |
| 192 | + this.patchField = patchField; |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + public DataPatcherImpl.SingleFieldPatchResult getSingleFieldPatchResult() { |
| 197 | + return singleFieldPatchResult; |
| 198 | + } |
| 199 | + |
| 200 | + public HelperClass setSingleFieldPatchResult(DataPatcherImpl.SingleFieldPatchResult singleFieldPatchResult) { |
| 201 | + this.singleFieldPatchResult = singleFieldPatchResult; |
| 202 | + return this; |
| 203 | + } |
| 204 | + |
| 205 | + public CustomizableFieldValueDto getCustomizableFieldValue() { |
| 206 | + return customizableFieldValue; |
| 207 | + } |
| 208 | + |
| 209 | + public HelperClass setCustomizableFieldValue(CustomizableFieldValueDto customizableFieldValue) { |
| 210 | + this.customizableFieldValue = customizableFieldValue; |
| 211 | + return this; |
| 212 | + } |
| 213 | + |
| 214 | + public DataPatchFailureCause getFailureCause() { |
| 215 | + return failureCause; |
| 216 | + } |
| 217 | + |
| 218 | + public HelperClass setFailureCause(DataPatchFailureCause failureCause) { |
| 219 | + this.failureCause = failureCause; |
| 220 | + return this; |
| 221 | + } |
| 222 | + |
| 223 | + public CustomizableFieldMetadataDto getMetadata() { |
| 224 | + return metadata; |
| 225 | + } |
| 226 | + |
| 227 | + public HelperClass setMetadata(CustomizableFieldMetadataDto metadata) { |
| 228 | + this.metadata = metadata; |
| 229 | + return this; |
| 230 | + } |
| 231 | + } |
94 | 232 | } |
0 commit comments