Search in sources :

Example 1 with UnmarshalException

use of org.ehrbase.serialisation.exception.UnmarshalException in project openEHR_SDK by ehrbase.

the class FlatJsonUnmarshaller method unmarshal.

/**
 * Unmarshal flat Json to Composition
 *
 * @param flat the flat Json
 * @param introspect the introspect belonging to the template
 * @return
 */
public Composition unmarshal(String flat, WebTemplate introspect) {
    Set<String> consumedPath;
    Map<String, String> currentValues;
    consumedPath = new HashSet<>();
    try {
        currentValues = new HashMap<>();
        for (Iterator<Map.Entry<String, JsonNode>> it = OBJECT_MAPPER.readTree(flat).fields(); it.hasNext(); ) {
            Map.Entry<String, JsonNode> e = it.next();
            currentValues.put(e.getKey(), e.getValue().toString());
        }
        Composition generate = WebTemplateSkeletonBuilder.build(introspect, false);
        StdToCompositionWalker walker = new StdToCompositionWalker();
        DefaultValues defaultValues = new DefaultValues(currentValues);
        // put default for the defaults
        if (!defaultValues.containsDefaultValue(DefaultValuePath.TIME)) {
            defaultValues.addDefaultValue(DefaultValuePath.TIME, OffsetDateTime.now());
        }
        if (!defaultValues.containsDefaultValue(DefaultValuePath.SETTING)) {
            defaultValues.addDefaultValue(DefaultValuePath.SETTING, Setting.OTHER_CARE);
        }
        String templateId = generate.getArchetypeDetails().getTemplateId().getValue();
        walker.walk(generate, currentValues.entrySet().stream().collect(Collectors.toMap(e1 -> new FlatPathDto(e1.getKey()), Map.Entry::getValue)), introspect, defaultValues, templateId);
        consumedPath = walker.getConsumedPaths();
        if (!CollectionUtils.isEmpty(getUnconsumed(consumedPath, currentValues))) {
            throw new UnmarshalException(String.format("Could not consume Parts %s", getUnconsumed(consumedPath, currentValues)));
        }
        return generate;
    } catch (JsonProcessingException e) {
        throw new UnmarshalException(e.getMessage(), e);
    }
}
Also used : Composition(com.nedap.archie.rm.composition.Composition) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) JsonNode(com.fasterxml.jackson.databind.JsonNode) UnmarshalException(org.ehrbase.serialisation.exception.UnmarshalException) DefaultValues(org.ehrbase.serialisation.walker.defaultvalues.DefaultValues) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with UnmarshalException

use of org.ehrbase.serialisation.exception.UnmarshalException in project openEHR_SDK by ehrbase.

the class CanonicalXML method unmarshal.

@Override
public <T extends RMObject> T unmarshal(String value, Class<T> clazz) {
    T composition;
    try {
        Unmarshaller unmarshaller = JAXBUtil.getArchieJAXBContext().createUnmarshaller();
        // Set the parent XMLReader on the XMLFilter
        SAXParserFactory spf = SAXParserFactory.newInstance();
        // disable external entities
        spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
        spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        spf.setNamespaceAware(true);
        spf.setValidating(false);
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        XMLFilter filter = new NamespaceFilter();
        filter.setParent(xr);
        UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
        filter.setContentHandler(unmarshallerHandler);
        filter.parse(new InputSource(IOUtils.toInputStream(value, UTF_8)));
        composition = (T) unmarshallerHandler.getResult();
    } catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
        throw new UnmarshalException(e.getMessage(), e);
    }
    return composition;
}
Also used : IOException(java.io.IOException) UnmarshalException(org.ehrbase.serialisation.exception.UnmarshalException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 3 with UnmarshalException

use of org.ehrbase.serialisation.exception.UnmarshalException in project openEHR_SDK by ehrbase.

the class StdToCompositionWalker method handleRaw.

private void handleRaw(Context<Map<FlatPathDto, String>> context) {
    ObjectMapper om = JacksonUtil.getObjectMapper();
    try {
        Map.Entry<FlatPathDto, String> current = context.getObjectDeque().peek().entrySet().stream().findAny().orElseThrow();
        RMObject newRmObject = new CanonicalJson().unmarshal(om.readValue(current.getValue(), String.class).replace("\"@class\"", "\"_type\""), RMObject.class);
        // Replace old skeleton
        replaceRmObject(context, newRmObject);
        consumedPaths.add(current.getKey().format());
    } catch (JsonProcessingException e) {
        throw new UnmarshalException(e.getMessage());
    }
}
Also used : CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) UnmarshalException(org.ehrbase.serialisation.exception.UnmarshalException) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RMObject(com.nedap.archie.rm.RMObject)

Aggregations

UnmarshalException (org.ehrbase.serialisation.exception.UnmarshalException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 FlatPathDto (org.ehrbase.webtemplate.path.flat.FlatPathDto)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RMObject (com.nedap.archie.rm.RMObject)1 Composition (com.nedap.archie.rm.composition.Composition)1 IOException (java.io.IOException)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)1 DefaultValues (org.ehrbase.serialisation.walker.defaultvalues.DefaultValues)1