Search in sources :

Example 1 with Flattener

use of com.nedap.archie.flattener.Flattener in project archetype-languageserver by nedap.

the class ConvertToOptCommand method apply.

public void apply() {
    String documentUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString();
    String format = ((JsonPrimitive) params.getArguments().get(1)).getAsString();
    String serializedOpt = null;
    Flattener flattener = new Flattener(storage, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
    DocumentInformation documentInformation = storage.getDocumentInformation(documentUri);
    Archetype archetype = storage.getArchetype(documentInformation.getArchetypeId());
    OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype);
    String extension;
    switch(format) {
        case "adl":
            extension = ".opt2";
            serializedOpt = ADLArchetypeSerializer.serialize(opt);
            break;
        case "json":
            extension = "_opt.json";
            try {
                serializedOpt = JacksonUtil.getObjectMapper().writeValueAsString(opt);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            break;
        case "xml":
            {
                extension = "_opt.xml";
                StringWriter sw = new StringWriter();
                try {
                    Marshaller marshaller = JAXBUtil.getArchieJAXBContext().createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.marshal(opt, sw);
                } catch (JAXBException e) {
                    throw new RuntimeException(e);
                }
                serializedOpt = sw.toString();
                break;
            }
        // 
        default:
            throw new UnsupportedOperationException("unsupported format: " + format);
    }
    String uriToWrite = documentUri.substring(0, documentUri.lastIndexOf("/")) + "/opt/" + opt.getArchetypeId() + extension;
    textDocumentService.writeFile(uriToWrite, "opt in " + format, serializedOpt);
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JsonPrimitive(com.google.gson.JsonPrimitive) Archetype(com.nedap.archie.aom.Archetype) Flattener(com.nedap.archie.flattener.Flattener) DocumentInformation(com.nedap.openehr.lsp.document.DocumentInformation) JAXBException(javax.xml.bind.JAXBException) OperationalTemplate(com.nedap.archie.aom.OperationalTemplate) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with Flattener

use of com.nedap.archie.flattener.Flattener in project archetype-languageserver by nedap.

the class GenerateExampleCommand method apply.

public void apply() {
    String documentUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString();
    String format = ((JsonPrimitive) params.getArguments().get(1)).getAsString();
    String serializedExample = null;
    Flattener flattener = new Flattener(storage, BuiltinReferenceModels.getMetaModels(), FlattenerConfiguration.forOperationalTemplate());
    DocumentInformation documentInformation = storage.getDocumentInformation(documentUri);
    Archetype archetype = storage.getArchetype(documentInformation.getArchetypeId());
    OperationalTemplate opt = (OperationalTemplate) flattener.flatten(archetype);
    ExampleJsonInstanceGenerator exampleJsonInstanceGenerator = new ExampleJsonInstanceGenerator(BuiltinReferenceModels.getMetaModels(), archetype.getOriginalLanguage().getCodeString());
    Map<String, Object> exampleMap = exampleJsonInstanceGenerator.generate(opt);
    String extension;
    ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
    String jsonRmObject = null;
    OpenEHRBase example;
    try {
        jsonRmObject = objectMapper.writeValueAsString(exampleMap);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    switch(format) {
        case "json":
            extension = "_example.json";
            serializedExample = jsonRmObject;
            break;
        case "flat_json":
            extension = "_flat_example.json";
            FlatJsonGenerator flatJsonGenerator = new FlatJsonGenerator(ArchieRMInfoLookup.getInstance(), FlatJsonFormatConfiguration.nedapInternalFormat());
            try {
                example = objectMapper.readValue(jsonRmObject, OpenEHRBase.class);
                Map<String, Object> flatExample = flatJsonGenerator.buildPathsAndValues(example);
                serializedExample = JacksonUtil.getObjectMapper().writeValueAsString(flatExample);
            } catch (JsonProcessingException | DuplicateKeyException e) {
                throw new RuntimeException(e);
            }
            break;
        case "xml":
            {
                extension = "_example.xml";
                try {
                    example = objectMapper.readValue(jsonRmObject, OpenEHRBase.class);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                StringWriter sw = new StringWriter();
                try {
                    Marshaller marshaller = JAXBUtil.getArchieJAXBContext().createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                    marshaller.marshal(example, sw);
                } catch (JAXBException e) {
                    throw new RuntimeException(e);
                }
                serializedExample = sw.toString();
                break;
            }
        // 
        default:
            throw new UnsupportedOperationException("unsupported format: " + format);
    }
    String uriToWrite = documentUri.substring(0, documentUri.lastIndexOf("/")) + "/example/" + opt.getArchetypeId() + extension;
    textDocumentService.writeFile(uriToWrite, "opt in " + format, serializedExample);
}
Also used : FlatJsonGenerator(com.nedap.archie.json.flat.FlatJsonGenerator) Marshaller(javax.xml.bind.Marshaller) JsonPrimitive(com.google.gson.JsonPrimitive) Archetype(com.nedap.archie.aom.Archetype) Flattener(com.nedap.archie.flattener.Flattener) DocumentInformation(com.nedap.openehr.lsp.document.DocumentInformation) JAXBException(javax.xml.bind.JAXBException) DuplicateKeyException(com.nedap.archie.json.flat.DuplicateKeyException) StringWriter(java.io.StringWriter) OpenEHRBase(com.nedap.archie.base.OpenEHRBase) OperationalTemplate(com.nedap.archie.aom.OperationalTemplate) ExampleJsonInstanceGenerator(com.nedap.archie.creation.ExampleJsonInstanceGenerator) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 Archetype (com.nedap.archie.aom.Archetype)2 OperationalTemplate (com.nedap.archie.aom.OperationalTemplate)2 Flattener (com.nedap.archie.flattener.Flattener)2 DocumentInformation (com.nedap.openehr.lsp.document.DocumentInformation)2 StringWriter (java.io.StringWriter)2 JAXBException (javax.xml.bind.JAXBException)2 Marshaller (javax.xml.bind.Marshaller)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 OpenEHRBase (com.nedap.archie.base.OpenEHRBase)1 ExampleJsonInstanceGenerator (com.nedap.archie.creation.ExampleJsonInstanceGenerator)1 DuplicateKeyException (com.nedap.archie.json.flat.DuplicateKeyException)1 FlatJsonGenerator (com.nedap.archie.json.flat.FlatJsonGenerator)1