I have some rather complex code that is currently retrieving field instances from one document and writing those to another document. This code takes as parameter the input document, the output document and some IField of the input document model and output document model. Now given a new use case I would like to reuse this code but this time only with groups instead of fields or a mixture thereof. How can I do this given that there is no common interface between FieldInstanceV2 and GroupInstanceV2? Based on the model api I would have expected there to be an ElementInstanceV2 interface but there unfortunately isn’t.
In it’s simplest form the question is how to neatly implement something like this:
public DocumentV2 copy(DocumentV2 source, DocumentPointer sourcePointer, DocumentV2 target, DocumentPointer targetPointer) {
// now what?
}
Hi @andreas-fresh-mesa,
in the DocumentV2-API, FieldInstanceV2 and GroupInstanceV2 represent what they can contain in a document:
FieldInstanceV2 contains the corresponding field value.
GroupInstanceV2 contains a collection of field instances + a collection of sub-groups (which each of them can be seen as a collection of group instances).
An interface encompassing both interfaces mentioned above did not seem necessary at the time of design. Nevertheless, the implementation of the method that you want to implement does not look all that far-fetched (at least for me):
DocumentV2 copy(DocumentV2 source, DocumentPointer sourcePointer,
DocumentV2 target, DocumentPointer targetPointer) {
// Try field first
FieldInstanceV2 field = source.field(sourcePointer);
if (field != null) {
return target.withField(targetPointer, field);
}
// Otherwise try group
GroupInstanceV2 group = source.group(sourcePointer);
if (group != null) {
return target.withGroup(targetPointer, group);
}
throw new IllegalArgumentException(
"No field or group found at source pointer: " + sourcePointer);
}
Another option would be to use the A12 mapping and in this case, I would assume that you would have less custom code.