How to programmatically create and persist a document on basis of a given Document Model?

IDocumentFactory ( IDocumentFactory (kernel-documentation-dev 29.3.0 API)) is said to provide creation functionality for IDocuments.
The .createDocument method requires the corresponding Document Model ID, which is known:

public IDocument createA12Document() {
        IDocumentModelResolver modelResolver = new DocumentModelResolver();
        DocumentServiceFactory docServFact = new DocumentServiceFactory(modelResolver);
        IDocumentFactory docFact = docServFact.createDocumentFactory();
        IDocument doc = docFact.createDocument("MinSt_input");
        IFieldInstance fieldInstance = docFact.createFieldInstance("/MinSt1/NameStpfl", new int[]{1, 1});
        fieldInstance.setValue("tester");
        doc.addEntityInstance(fieldInstance);
        return doc;
}

We attempt to create an A12 document programmatically, set values for its fieldInstances.
Now I wonder: how to persist that created document?

I cannot find any .save, .persist (or similar) method in the documentation.

I’d greatly appreciate a working minimal example for the programmatic creation of an A12 document and how it can be persisted. Even though my above approach seems to work for the creation itself, I’d also want learn the best practice approach of this.

Thanks,
Alex

Moin @alexander-digital-glen,
you can use the Data Services Document API for that matter. The documentation also provides descripted explanations and UML class diagrams with the usable interfaces and services.

Based on your code the following should be working for you, if you then call the method persistA12Document, to save the document to the database:

@Component
public class CreateDocumentProgrammatically {

    private final DocumentService documentService;

    public CreateDocumentProgrammatically(DocumentService documentService) {
        this.documentService = documentService;
    }

    public IDocument createA12Document() {
        IDocumentModelResolver modelResolver = new DocumentModelResolver();
        DocumentServiceFactory docServFact = new DocumentServiceFactory(modelResolver);
        IDocumentFactory docFact = docServFact.createDocumentFactory();
        IDocument doc = docFact.createDocument("MinSt_input");
        IFieldInstance fieldInstance = docFact.createFieldInstance("/MinSt1/NameStpfl", new int[]{1, 1});
        fieldInstance.setValue("tester");
        doc.addEntityInstance(fieldInstance);
        return doc;
    }

    public DataServicesDocument persistA12Document(final IDocument doc) {
        return documentService.create(doc, Locale.ENGLISH);
    }
}

Kind regards,
Jan :slight_smile:

Good morning Jan,
thanks for your swift reply and effort. It helped a lot.