In my using a custom implementation of IDocumentRepository is storing documents as json. In my code I have to create a document-row in document-table for a specific data-model. Procedure is triggered from outside a12. Is there some java code in a12 to create an empty document’s json programmatically in intension to store it in json document table ? “empty” in here means minimal data, so only non repeatable groups are present. Arrays of repeatables are empty.
Hi,
I assume you’re with version 2022.06 (not 2020.06).
I think you can use the DS DocumentPersisterService, something like
var defaultEmptyDoc = """
{
"Person": {
"PersonalData": {
"FirstName": "Johny",
"LastName": "Smith",
"Email": "johny.smith@mgm-tp.com",
"DateOfBirth": "1987-10-11",
"Nationality": "British",
"PlaceOfBirth": "London"
},
"Phones": [
{
"PhoneNumber": "123456",
"Type": "WORK"
}
]
}
}
""";
Reader content = new InputStreamReader(new ByteArrayInputStream(defaultEmptyDoc.getBytes()));
documentPersisterService.create(documentConvertor.convertJSONToDocument("Person-dm", content), null);
Maybe defaultEmptyDoc can be read from the resource file.
Hi @bernhard-keen-stream,
in Kernel, we have the method EmptyDocumentFactory#createEmptyDocument with which you can create a document with all non-repeatable stuff based on a specific document model. The method is internal, you could try it out, but really just to try (since it is internal) and if it is what you are looking for, you can create a requirement to make it public (it won’t have the same way to call it, but you will be able to call it
).
OK. Thanks. I already got it and form my purpose the following code was sufficient:
@Component
class A12EmptyDocumentFactory {
private final IDocumentModelResolver documentModelResolver;
private final ObjectMapper objectMapper;
A12DocumentFactory(IDocumentModelResolver documentModelResolver, @Qualifier("a12ObjectMapper") ObjectMapper objectMapper) {
this.documentModelResolver = documentModelResolver;
this.objectMapper = objectMapper;
}
@Logged
String emptyDocument(String modelName) {
try {
return objectMapper.writeValueAsString(emptyDocumentNode(modelName));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
@Logged
ObjectNode emptyDocumentNode(String modelName) {
var documentModel = documentModelResolver.getDocumentModelById(modelName);
var documentModelRoot = documentModel.getContent().getDocumentModelRoot();
var jsonRoot = objectMapper.createObjectNode();
evaluate(documentModelRoot, jsonRoot);
return jsonRoot;
}
private void evaluate(IGroup a12Group, ObjectNode jsonObjectNode) {
a12Group.getElements().forEach(element -> {
if (element instanceof IGroup a12ChildGroup && isMandatoryGroup(a12ChildGroup)) {
var jsonChildGroup = objectMapper.createObjectNode();
jsonObjectNode.set(a12ChildGroup.getName(), jsonChildGroup);
evaluate(a12ChildGroup, jsonChildGroup);
} else if (element instanceof IField a12Field) {
jsonObjectNode.set(a12Field.getName(), null);
}
});
}
private boolean isMandatoryGroup(IGroup group) {
return group.getRepeatability() == 1;
}
}
Hi @gildardo-cached-canyon , In Cosmo projects, we also need to create empty documents, it would be great if you can tell me when it can be public ? Could i just call it to use now first ?
From my check at kernel 29.1.0 of 2024.06 release line, the API is still marked as internal.
As @gildardo-cached-canyon already pointed out, you can try it out. Please request to publish it via a ticket if you feel it fits your task.