Search in sources :

Example 1 with UnexpectedSwitchCaseException

use of org.ehrbase.api.exception.UnexpectedSwitchCaseException in project ehrbase by ehrbase.

the class ContributionServiceHelper method extractVersionObjects.

/**
 * returns a list of RM VERSIONs extracted from given serialization
 * @param listVersions List of still serialized version objects
 * @param format Format of the serialization
 * @return List of deserialized version objects
 * @throws IllegalArgumentException when processing of given input fails
 */
public static List<Version> extractVersionObjects(ArrayList listVersions, CompositionFormat format) {
    List<Version> versionsList = new LinkedList<>();
    switch(format) {
        case JSON:
            for (Object version : listVersions) {
                try {
                    // TODO CONTRIBUTION: round trip ((string->)object->string->object) really necessary?
                    String json = JacksonUtil.getObjectMapper().writeValueAsString(version);
                    RMObject versionRmObject = new CanonicalJson().unmarshal(json, RMObject.class);
                    if (versionRmObject instanceof Version) {
                        versionsList.add((Version) versionRmObject);
                    } else {
                        throw new IllegalArgumentException("Wrong input. At least one VERSION in this contribution is invalid.");
                    }
                } catch (JsonProcessingException e) {
                    throw new IllegalArgumentException("Error while processing given json input: " + e.getMessage());
                }
            }
            break;
        case XML:
        default:
            throw new UnexpectedSwitchCaseException(format);
    }
    return versionsList;
}
Also used : CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) Version(com.nedap.archie.rm.changecontrol.Version) RMObject(com.nedap.archie.rm.RMObject) UnexpectedSwitchCaseException(org.ehrbase.api.exception.UnexpectedSwitchCaseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RMObject(com.nedap.archie.rm.RMObject)

Example 2 with UnexpectedSwitchCaseException

use of org.ehrbase.api.exception.UnexpectedSwitchCaseException in project ehrbase by ehrbase.

the class ContributionServiceHelper method parseAuditDetails.

/**
 * Helper that parses the AuditDetails from the contribution input.
 * @param content Plain string content
 * @param format Format of content
 * @return AuditDetails object
 */
public static AuditDetails parseAuditDetails(String content, CompositionFormat format) {
    // extract both per standard parts of the content: data block containing versions & audit
    Map<String, Object> splitContent = splitContent(content, format);
    Object auditContent = splitContent.get("audit");
    AuditDetails auditResult;
    switch(format) {
        case JSON:
            try {
                String json = JacksonUtil.getObjectMapper().writeValueAsString(auditContent);
                auditResult = new CanonicalJson().unmarshal(json, AuditDetails.class);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException("Error while processing given json input: " + e.getMessage());
            }
            break;
        case XML:
        default:
            throw new UnexpectedSwitchCaseException(format);
    }
    return auditResult;
}
Also used : CanonicalJson(org.ehrbase.serialisation.jsonencoding.CanonicalJson) RMObject(com.nedap.archie.rm.RMObject) AuditDetails(com.nedap.archie.rm.generic.AuditDetails) UnexpectedSwitchCaseException(org.ehrbase.api.exception.UnexpectedSwitchCaseException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 RMObject (com.nedap.archie.rm.RMObject)2 UnexpectedSwitchCaseException (org.ehrbase.api.exception.UnexpectedSwitchCaseException)2 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)2 Version (com.nedap.archie.rm.changecontrol.Version)1 AuditDetails (com.nedap.archie.rm.generic.AuditDetails)1