Search in sources :

Example 1 with Version

use of com.nedap.archie.rm.changecontrol.Version in project ehrbase by ehrbase.

the class ContributionServiceHelperTest method getVersions.

@Test
public void getVersions() throws IOException {
    // comparable test method that creates list of version manually. should be the same as the one created with getVersions
    InputStream stream = ContributionTestDataCanonicalJson.ONE_ENTRY_COMPOSITION.getStream();
    assertNotNull(stream);
    String streamString = IOUtils.toString(stream, UTF_8);
    List<Version> versionsA = unmarshalMapContentToRmObjectPreStep(ContributionTestDataCanonicalJson.ONE_ENTRY_COMPOSITION.getStream(), CompositionFormat.JSON);
    List<Version> versionsB = ContributionServiceHelper.parseVersions(streamString, CompositionFormat.JSON);
    assertEquals(versionsA, versionsB);
    // second test input
    InputStream stream2 = ContributionTestDataCanonicalJson.TWO_ENTRIES_COMPOSITION.getStream();
    assertNotNull(stream2);
    String streamString2 = IOUtils.toString(stream2, UTF_8);
    List<Version> versionsA2 = unmarshalMapContentToRmObjectPreStep(ContributionTestDataCanonicalJson.TWO_ENTRIES_COMPOSITION.getStream(), CompositionFormat.JSON);
    List<Version> versionsB2 = ContributionServiceHelper.parseVersions(streamString2, CompositionFormat.JSON);
    assertEquals(versionsA2, versionsB2);
// TODO add more test data and XML when ready
}
Also used : Version(com.nedap.archie.rm.changecontrol.Version) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 2 with Version

use of com.nedap.archie.rm.changecontrol.Version in project ehrbase by ehrbase.

the class ContributionServiceHelperTest method unmarshalMapContentToRmObjectCheck.

// helper method with the actual logical tests
private void unmarshalMapContentToRmObjectCheck(List<Version> versions, CompositionFormat format, ContributionServiceImp.SupportedClasses classType) {
    for (Version version : versions) {
        RMObject versionRmObject = ContributionServiceHelper.unmarshalMapContentToRmObject((LinkedHashMap) version.getData(), format);
        assertNotNull(ContributionServiceImp.SupportedClasses.valueOf(versionRmObject.getClass().getSimpleName().toUpperCase()));
        assertEquals(classType, ContributionServiceImp.SupportedClasses.valueOf(versionRmObject.getClass().getSimpleName().toUpperCase()));
    }
}
Also used : Version(com.nedap.archie.rm.changecontrol.Version) RMObject(com.nedap.archie.rm.RMObject)

Example 3 with Version

use of com.nedap.archie.rm.changecontrol.Version in project ehrbase by ehrbase.

the class ContributionServiceHelper method parseVersions.

/**
 * Convenience helper that combines some methods from above and prepares direct access to the list of contained versions
 * @param content Plain string content
 * @param format Format of content
 * @return List of version objects extracted from content
 */
public static List<Version> parseVersions(String content, CompositionFormat format) {
    // extract both per standard parts of the content: data block containing versions & audit
    Map<String, Object> splitContent = splitContent(content, format);
    // process versions: unmarshal to some iterable object & create RM objects out of input
    List<Version> versions = null;
    Object versionsContent = splitContent.get("versions");
    if (versionsContent instanceof List) {
        versions = extractVersionObjects((ArrayList) versionsContent, format);
    } else {
        throw new IllegalArgumentException("Can't process input, possible malformed version payload");
    }
    return versions;
}
Also used : Version(com.nedap.archie.rm.changecontrol.Version) RMObject(com.nedap.archie.rm.RMObject)

Example 4 with Version

use of com.nedap.archie.rm.changecontrol.Version 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 5 with Version

use of com.nedap.archie.rm.changecontrol.Version in project ehrbase by ehrbase.

the class ContributionServiceImp method commitContribution.

@Override
public UUID commitContribution(UUID ehrId, String content, CompositionFormat format) {
    // pre-step: check for valid ehrId
    if (!ehrService.hasEhr(ehrId)) {
        throw new ObjectNotFoundException("ehr", "No EHR found with given ID: " + ehrId.toString());
    }
    // create new empty/standard-value contribution - will be updated later with full details
    I_ContributionAccess contributionAccess = I_ContributionAccess.getInstance(this.getDataAccess(), ehrId);
    // parse and set audit information from input
    AuditDetails audit = ContributionServiceHelper.parseAuditDetails(content, format);
    contributionAccess.setAuditDetailsValues(audit);
    // commits with all default values (but without audit handling as it is done above)
    UUID contributionId = contributionAccess.commit(null, null, null);
    List<Version> versions = ContributionServiceHelper.parseVersions(content, format);
    if (versions.isEmpty())
        throw new InvalidApiParameterException("Invalid Contribution, must have at least one Version object.");
    // go through those RM objects and execute the action of it (as listed in its audit) and connect it to new contribution
    for (Version version : versions) {
        Object versionData = version.getData();
        if (versionData != null) {
            // the version contains the optional "data" attribute (i.e. payload), therefore has specific object type (composition, folder,...)
            RMObject versionRmObject;
            if (versionData instanceof LinkedHashMap) {
                versionRmObject = ContributionServiceHelper.unmarshalMapContentToRmObject((LinkedHashMap) versionData, format);
            } else {
                throw new IllegalArgumentException("Contribution input can't be processed");
            }
            // switch to allow acting depending on exact type
            SupportedClasses versionClass;
            try {
                versionClass = SupportedClasses.valueOf(versionRmObject.getClass().getSimpleName().toUpperCase());
            } catch (Exception e) {
                throw new InvalidApiParameterException("Invalid version object in contribution. " + versionRmObject.getClass().getSimpleName().toUpperCase() + " not supported.");
            }
            switch(versionClass) {
                case COMPOSITION:
                    try {
                        processCompositionVersion(ehrId, contributionId, version, (Composition) versionRmObject);
                    } catch (UnprocessableEntityException e) {
                        throw new ValidationException(e.getMessage());
                    }
                    break;
                case EHRSTATUS:
                    processEhrStatusVersion(ehrId, contributionId, version, (EhrStatus) versionRmObject);
                    break;
                case FOLDER:
                    processFolderVersion(ehrId, contributionId, version, (Folder) versionRmObject);
                    break;
                default:
                    throw new UnexpectedSwitchCaseException(versionClass);
            }
        } else {
            // version doesn't contain "data", so it is only a metadata one to, for instance, delete a specific object via ID regardless of type
            processMetadataVersion(ehrId, contributionId, version);
        }
    }
    return contributionId;
}
Also used : Version(com.nedap.archie.rm.changecontrol.Version) RMObject(com.nedap.archie.rm.RMObject) AuditDetails(com.nedap.archie.rm.generic.AuditDetails) RMObject(com.nedap.archie.rm.RMObject)

Aggregations

Version (com.nedap.archie.rm.changecontrol.Version)8 RMObject (com.nedap.archie.rm.RMObject)6 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TemplateId (com.nedap.archie.rm.archetyped.TemplateId)1 AuditDetails (com.nedap.archie.rm.generic.AuditDetails)1 InputStream (java.io.InputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 UnexpectedSwitchCaseException (org.ehrbase.api.exception.UnexpectedSwitchCaseException)1 CanonicalJson (org.ehrbase.serialisation.jsonencoding.CanonicalJson)1