Hey ![]()
we are currently updating to 2024.06 and there something changed in DataServicesDocument. Before this we used the Builder of it like this:
DataServicesDocument.builder()
.kernelDocument(document)
.createdBy(DUMMY_USER)
.modifiedBy(DUMMY_USER)
.createdAt(now)
.modifiedAt(now)
.modelName(document.getDocumentModelId())
.docRef(new DocumentReference(ACCOUNTING_MODEL_NAME + "/" + DUMMY_MODEL_ID))
.build();
and now we do:
return documentFactory.newDataServicesDocument(document);
But now we have the problem that I get a Nullpointer when creating a new DocumentSpec because the docRef is null.
So first we create a DocumentSpec mit a String which was a json. To do this we create a IDocument with documentSupport.convertJSONToDocument(ACCOUNTING_MODEL_NAME, new StringReader(data)). Then we use the new function documentFactory.newDataServicesDocument(document); to create a DataServicesDocument and then we use this DataServiceDocument in documentSupport.convertToDocumentSpec(dsDocument) to create a DocumentSpec and that is the point where we get a nullpointer. Because convertToDocumentSpec is creating a new DocumentSpec(dataServicesDocument.getMetadata().getDocRef(), document) And the dataServicesDocument.getMetadata().getDocRef() is not there (null)
What do we need to do to get the docRef in the metaData?
Here the full code of that:
package com.mgmtp.cosmo.accounting.a12.document;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.mgmtp.a12.dataservices.document.DataServicesDocument;
import com.mgmtp.a12.dataservices.document.DataServicesDocumentFactory;
import com.mgmtp.a12.dataservices.document.DocumentSpec;
import com.mgmtp.a12.dataservices.document.graph.DocumentGraph;
import com.mgmtp.a12.dataservices.document.support.DocumentSupport;
import com.mgmtp.a12.kernel.md.document.api.IDocument;
import com.mgmtp.cosmo.accounting.a12.document.mapper.AccountingCalculationMapper;
import com.mgmtp.cosmo.accounting.a12.document.to.AccountingCalculationRoot;
import com.mgmtp.cosmo.accounting.core.api.to.MonetaryAmountTO;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zalando.jackson.datatype.money.MoneyModule;
import java.io.StringReader;
import java.util.Collections;
import java.util.List;
/**
* Factory to create a {@link DocumentGraph} out of a list of {@link MonetaryAmountTO}s.
* Can be used to transform the output of the accoutning service into an A12 data format.
*/
@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class DocumentGraphFactory {
private static final String ACCOUNTING_MODEL_NAME = "AccountingModel";
private final ObjectMapper jsonMapper;
private final DocumentSupport documentSupport;
private final AccountingCalculationMapper calculationMapper;
private final DataServicesDocumentFactory documentFactory;
@Autowired
public DocumentGraphFactory(DocumentSupport documentSupport, AccountingCalculationMapper calculationMapper, DataServicesDocumentFactory documentFactory) {
this(new ObjectMapper()
.registerModule(new MoneyModule())
.registerModule(new JavaTimeModule()),
documentSupport,
calculationMapper,
documentFactory);
}
/**
* Transform a list of {@link MonetaryAmountTO}s to an A12 {@link DocumentGraph}
*
* @param amounts to be converted
* @return {@link DocumentGraph} containing alle the data of the input {@param amounts}
*/
public DocumentGraph create(List<MonetaryAmountTO> amounts) {
try {
String data = serializeAmounts(amounts);
DocumentSpec documentSpec = toDocumentSpec(data);
return toDocumentGraph(documentSpec);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Could not serialize given amounts.", e);
}
}
private String serializeAmounts(List<MonetaryAmountTO> amounts) throws JsonProcessingException {
AccountingCalculationRoot wrapper = calculationMapper.map(amounts);
return jsonMapper.writeValueAsString(wrapper);
}
private DocumentSpec toDocumentSpec(String data) {
DataServicesDocument dsDocument = toDataServiceDocument(data);
return documentSupport.convertToDocumentSpec(dsDocument);
}
private DataServicesDocument toDataServiceDocument(String data) {
IDocument document = documentSupport.convertJSONToDocument(ACCOUNTING_MODEL_NAME, new StringReader(data));
return createDataServicesDocument(document);
}
private DataServicesDocument createDataServicesDocument(IDocument document) {
return documentFactory.newDataServicesDocument(document);
}
private DocumentGraph toDocumentGraph(DocumentSpec documentSpec) {
DocumentGraph graph = new DocumentGraph();
graph.setDocuments(Collections.singletonList(documentSpec));
return graph;
}
}