Get the field order from a specific context in a document model

I want to write a function that does the following

List fieldPathsInModelOrder = getTheOrderFromDocModel (iDocumentModel, kontextPath);

where using kontextPath /FB/AT/BGrl/Bank for the screenshot below should return

“/FB/AT/BGrl/Bank/Bank_SPIF_ErtraegePV”,
“/FB/AT/BGrl/Bank/Bank_InlBetEinOhneAnspruch_Dazu”,
“/FB/AT/BGrl/Bank/Bank_InlBetEinOhneAnspruch_Ab”,
“/FB/AT/BGrl/Bank/Bank_DBA”,
“/FB/AT/BGrl/Bank/Bank_SPIF_Ertr”

Since the PrintEngine prints fields in a complex listing according to the field order in the document model, I assume there is way to get there.

Hi @jasper-silent-root,
you can use the method com.mgmtp.a12.kernel.md.model.api.services.IDocumentModelSearchService#getByPath, once you check that the element exists, you can cast to com.mgmtp.a12.kernel.md.model.api.IGroup and there you have access to the child elements with the method List<IElement> getElements(), you go through the children and extract the fields (com.mgmtp.a12.kernel.md.model.api.IField) and finally with the method com.mgmtp.a12.kernel.md.model.api.services.IDocumentModelService#getPath you get the correspoding paths in the order defined in the DM.

Thanks for your quick reply. My efforts have resulted in

image
which fails with

java.lang.ClassCastException: class com.mgmtp.a12.kernel.md.model.internal.service.DocumentModelSearchServiceImpl cannot be cast to class com.mgmtp.a12.kernel.md.model.a12internal.services.DocumentModelSearchService (com.mgmtp.a12.kernel.md.model.internal.service.DocumentModelSearchServiceImpl and com.mgmtp.a12.kernel.md.model.a12internal.services.DocumentModelSearchService are in unnamed module of loader ‘app’)

Not casting it is not an option either:

image

The right interface you should use is com.mgmtp.a12.kernel.md.model.api.services.IDocumentModelSearchService and you don’t need to cast for the service.

I just updated my first answer to have the complete qualified names of the interfaces.

adding the code snippets so the solution can be copy-pasted :wink:

import com.mgmtp.a12.kernel.md.facade.DocumentModelServiceFactory;
import com.mgmtp.a12.kernel.md.model.api.services.IDocumentModelSearchService;
import com.mgmtp.a12.kernel.md.model.api.services.IDocumentModelService;
import com.mgmtp.a12.kernel.md.model.api.IDocumentModel;
import com.mgmtp.a12.kernel.md.model.api.IElement;
import com.mgmtp.a12.kernel.md.model.api.IField;
import com.mgmtp.a12.kernel.md.model.api.IGroup;

// in your class define the private fields
private DocumentModelServiceFactory documentModelServiceFactory;
 
// instantiate in the Constructor as there is no bean
 this.documentModelServiceFactory = new DocumentModelServiceFactory();  

// then the Method will look as follow

private List<String> getTheOrderFromDocModel(IDocumentModel model, String kontextPath){
    IDocumentModelSearchService documentModelSearchService = documentModelServiceFactory.createDocumentModelSearchService(model);
    IDocumentModelService documentModelService = documentModelServiceFactory.createDocumentModelService();
    IElement iElement = documentModelSearchService.getByPath(kontextPath)
            .orElseThrow(() -> new IllegalStateException("kontextPath [%s] not found".formatted(kontextPath)));
    IGroup iGroup = (IGroup) iElement;
    return iGroup.getElements().stream()
            .filter(IField.class::isInstance)
            .map(documentModelService::getPath)
            .toList();
}

@nicolas-cool-mesh: Thank you very much for the code snippet :blush:, the only thing I would remark is that you “only” get the field names, to get the paths, you would have to use the method com.mgmtp.a12.kernel.md.model.api.services.IDocumentModelService#getPath.

I have adjusted the Snippet accordingly :+1: