I’m trying to do calculations on a document that has computed fields. However the json from which I deserialize the document does not contain all of the calculated fields. While running the computation I get the following exception:
Caused by: com.mgmtp.a12.md.rt.api.ComputationException: Field instance '/Root[1]/PaymentWithInvoice[1]' not found!
at com.mgmtp.a12.md.internal.rt.ComputationResultImpl.createComputedValue(ComputationResultImpl.java:79)
at com.mgmtp.a12.md.internal.rt.ComputationResultImpl.<init>(ComputationResultImpl.java:57)
at com.mgmtp.a12.md.internal.rt.DocumentDynamicRtService.compute(DocumentDynamicRtService.java:96)
How can I add such missing fields to the document before the computation such that I can avoid this exception?
I use a document visitor to add those fields:
@RequiredArgsConstructor
public class MissingFieldsAddingDocumentVisitor extends DefaultDocumentVisitor {
private final DocumentFactory documentFactory;
@Override
public VisitProcess visitGroupInstance(GroupInstance groupInstance) {
Group group = groupInstance.getModelElement();
Set<Element> existingFields = groupInstance.getChildren().stream().filter(FieldInstance.class::isInstance)
.map(ElementInstance::getModelElement).collect(Collectors.toSet());
List<Field> missingFields = group.getElements().stream().filter(Field.class::isInstance).map(Field.class::cast)
.filter(e -> !existingFields.contains(e)).collect(Collectors.toList());
if (!missingFields.isEmpty()) {
missingFields.forEach((field -> documentFactory.createFieldInstance(field, groupInstance)));
}
return super.visitGroupInstance(groupInstance);
}
}
This visitor is used to walk the document and will add missing fields to all previously existing groups of the document.