Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pem 96 support structured #273

Merged
merged 7 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Note: version releases in the 0.x.y range may introduce breaking changes.

- Added WebTemplate based RmSkeleton builder. (see https://github.com/ehrbase/openEHR_SDK/pull/241)
- validate that all flat parts are consumed. (see https://github.com/ehrbase/openEHR_SDK/pull/264 )
- support Structured format. (see https://github.com/ehrbase/openEHR_SDK/pull/273)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
import org.ehrbase.client.classgenerator.olddtoexamples.coronaanamnesecomposition.definition.VorhandenDefiningcode;
import org.ehrbase.client.classgenerator.olddtoexamples.testalltypesenv1composition.TestAllTypesEnV1Composition;
import org.ehrbase.client.flattener.Flattener;
import org.ehrbase.client.flattener.RoundTripTest;
import org.ehrbase.client.flattener.Unflattener;
import org.ehrbase.client.templateprovider.TestDataTemplateProvider;
import org.ehrbase.serialisation.RMDataFormat;
import org.ehrbase.serialisation.flatencoding.FlatFormat;
import org.ehrbase.serialisation.flatencoding.FlatJasonProvider;
import org.ehrbase.serialisation.flatencoding.FlatJson;
import org.ehrbase.serialisation.jsonencoding.CanonicalJson;
import org.ehrbase.serialisation.jsonencoding.JacksonUtil;
import org.ehrbase.test_data.composition.CompositionTestDataCanonicalJson;
Expand Down Expand Up @@ -161,7 +160,7 @@ public void testUnflattenCorona() throws IOException {
public void checkTestCase(TestCase testCase, SoftAssertions softly) throws IOException {

String value = IOUtils.toString(testCase.simSDTJson.getStream(), UTF_8);
FlatJson flatJson =
RMDataFormat flatJson =
new FlatJasonProvider(new TestDataTemplateProvider())
.buildFlatJson(FlatFormat.SIM_SDT, testCase.templateId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,31 @@

package org.ehrbase.client.flattener;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nedap.archie.rm.RMObject;
import com.nedap.archie.rm.composition.Composition;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.assertj.core.api.SoftAssertions;
import org.ehrbase.client.classgenerator.examples.alternativeeventscomposition.AlternativeEventsComposition;
import org.ehrbase.client.classgenerator.examples.coronaanamnesecomposition.CoronaAnamneseComposition;
import org.ehrbase.client.classgenerator.examples.ehrbasemultioccurrencedev1composition.EhrbaseMultiOccurrenceDeV1Composition;
import org.ehrbase.client.classgenerator.examples.testalltypesenv1composition.TestAllTypesEnV1Composition;
import org.ehrbase.client.templateprovider.TestDataTemplateProvider;
import org.ehrbase.serialisation.RMDataFormat;
import org.ehrbase.serialisation.flatencoding.FlatFormat;
import org.ehrbase.serialisation.flatencoding.FlatJasonProvider;
import org.ehrbase.serialisation.flatencoding.FlatJson;
import org.ehrbase.serialisation.jsonencoding.JacksonUtil;
import org.ehrbase.test_data.composition.CompositionTestDataSimSDTJson;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

public class RoundTripTest {

class TestCase {
Expand Down Expand Up @@ -170,7 +171,7 @@ public void testRoundTrip() throws IOException {
public void checkTestCase(TestCase testCase, SoftAssertions softly) throws IOException {

String value = IOUtils.toString(testCase.simSDTJson.getStream(), UTF_8);
FlatJson flatJson =
RMDataFormat flatJson =
new FlatJasonProvider(new TestDataTemplateProvider())
.buildFlatJson(FlatFormat.SIM_SDT, testCase.templateId);

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
4 changes: 4 additions & 0 deletions serialisation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
<groupId>com.github.ehrbase.openEHR_SDK</groupId>
<artifactId>util</artifactId>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
package org.ehrbase.serialisation;

import com.nedap.archie.rm.RMObject;
import com.nedap.archie.rm.composition.Composition;

public interface RMDataFormat {

String marshal(RMObject rmObject);

<T extends RMObject> T unmarshal(String value, Class<T> clazz);

default Composition unmarshal(String value){

return unmarshal(value,Composition.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
/**
* The supported flat jason formats
*
* @see <a href="https://specifications.openehr.org/releases/ITS-REST/latest/simplified_data_template.html">Simplified Data Template (SDT)</a>
* @see <a
* href="https://specifications.openehr.org/releases/ITS-REST/latest/simplified_data_template.html">Simplified
* Data Template (SDT)</a>
*/
public enum FlatFormat {
/**
* Simplified IM Simplified Data template.
*/
SIM_SDT;
/** Simplified IM Simplified Data template. */
SIM_SDT,
/** STRUCTURED */
STRUCTURED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.ehrbase.serialisation.flatencoding;


import org.ehrbase.serialisation.RMDataFormat;
import org.ehrbase.util.exception.SdkException;
import org.ehrbase.webtemplate.templateprovider.TemplateProvider;

Expand All @@ -28,32 +28,33 @@
*/
public class FlatJasonProvider {

private final TemplateProvider templateProvider;

public FlatJasonProvider(TemplateProvider templateProvider) {
this.templateProvider = templateProvider;
}

/**
* Builds a {@link FlatJson} for the template with {@code templateId} and {@link FlatFormat} {@code format}
*
* @param format
* @param templateId
* @return
*/
public FlatJson buildFlatJson(FlatFormat format, String templateId) {

switch (format) {
case SIM_SDT:
return new FlatJson(this, templateId);
default:
throw new SdkException(String.format("Format %s not supported", format));
}


private final TemplateProvider templateProvider;

public FlatJasonProvider(TemplateProvider templateProvider) {
this.templateProvider = templateProvider;
}

/**
* Builds a {@link FlatJson} for the template with {@code templateId} and {@link FlatFormat}
* {@code format}
*
* @param format
* @param templateId
* @return
*/
public RMDataFormat buildFlatJson(FlatFormat format, String templateId) {

switch (format) {
case SIM_SDT:
return new FlatJson(this, templateId);
case STRUCTURED:
return new StructuredJson(this, templateId);
default:
throw new SdkException(String.format("Format %s not supported", format));
}
}

TemplateProvider getTemplateProvider() {
return templateProvider;
}
TemplateProvider getTemplateProvider() {
return templateProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,14 @@
import org.ehrbase.serialisation.flatencoding.std.umarshal.FlatJsonUnmarshaller;
import org.ehrbase.util.exception.SdkException;
import org.ehrbase.webtemplate.model.WebTemplate;
import org.openehr.schemas.v1.OPERATIONALTEMPLATE;

public class FlatJson implements RMDataFormat {

private final OPERATIONALTEMPLATE operationaltemplate;
private final WebTemplate templateIntrospect;
private final FlatJsonMarshaller flatJsonMarshaller;

FlatJson(FlatJasonProvider flatJasonProvider, String templateId) {

operationaltemplate =
flatJasonProvider
.getTemplateProvider()
.find(templateId)
.orElseThrow(
() -> new SdkException(String.format("Template %s not found", templateId)));


templateIntrospect =
flatJasonProvider
.getTemplateProvider()
Expand Down Expand Up @@ -75,6 +65,7 @@ public <T extends RMObject> T unmarshal(String value, Class<T> clazz) {
}
}

@Override
public Composition unmarshal(String value) {
return new FlatJsonUnmarshaller().unmarshal(value, templateIntrospect);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2021 Stefan Spiska (Vitasystems GmbH) and Hannover Medical School
*
* This file is part of Project EHRbase
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.ehrbase.serialisation.flatencoding;

import com.nedap.archie.rm.RMObject;
import com.nedap.archie.rm.composition.Composition;
import org.ehrbase.serialisation.RMDataFormat;
import org.ehrbase.serialisation.exception.MarshalException;
import org.ehrbase.serialisation.flatencoding.std.marshal.FlatJsonMarshaller;
import org.ehrbase.serialisation.flatencoding.std.umarshal.FlatJsonUnmarshaller;
import org.ehrbase.serialisation.flatencoding.structured.StructuredHelper;
import org.ehrbase.util.exception.SdkException;
import org.ehrbase.webtemplate.model.WebTemplate;

public class StructuredJson implements RMDataFormat {

private final WebTemplate templateIntrospect;
private final FlatJsonMarshaller flatJsonMarshaller;

StructuredJson(FlatJasonProvider flatJasonProvider, String templateId) {

templateIntrospect =
flatJasonProvider
.getTemplateProvider()
.buildIntrospect(templateId)
.orElseThrow(
() -> new SdkException(String.format("Template %s not found", templateId)));
flatJsonMarshaller = new FlatJsonMarshaller();
}

@Override
public String marshal(RMObject rmObject) {
if (rmObject instanceof Composition) {
String flatJson = flatJsonMarshaller.toFlatJson((Composition) rmObject, templateIntrospect);
return StructuredHelper.convertFlatToStructured(flatJson);
} else {
throw new MarshalException(
String.format(
"Class %s not supported in Structured format", rmObject.getClass().getSimpleName()));
}
}

@Override
public <T extends RMObject> T unmarshal(String value, Class<T> clazz) {
if (clazz.isAssignableFrom(Composition.class)) {
return (T) unmarshal(value);
} else {
throw new SdkException(
String.format("Class %s not supported in Structured format", clazz.getSimpleName()));
}
}

@Override
public Composition unmarshal(String value) {

return new FlatJsonUnmarshaller()
.unmarshal(StructuredHelper.convertStructuredToFlat(value), templateIntrospect);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,24 @@

public class CodePhraseStdConfig extends AbstractsStdConfig<CodePhrase> {

@Override
/**
* {@inheritDoc}
*/
public Map<String, Object> buildChildValues(String currentTerm, CodePhrase rmObject, Context<Map<String, Object>> context) {
Map<String, Object> result = new HashMap<>();
addValue(result, currentTerm, "code", rmObject.getCodeString());
addValue(result, currentTerm, "terminology", Optional.of(rmObject.getTerminologyId()).map(ObjectId::getValue).orElse(null));

return result;
}

@Override
/**
* {@inheritDoc}
*/
public Class<CodePhrase> getAssociatedClass() {
return CodePhrase.class;
}


@Override
/** {@inheritDoc} */
public Map<String, Object> buildChildValues(
String currentTerm, CodePhrase rmObject, Context<Map<String, Object>> context) {
Map<String, Object> result = new HashMap<>();
addValue(result, currentTerm, "code", rmObject.getCodeString());
addValue(
result,
currentTerm,
"terminology",
Optional.of(rmObject.getTerminologyId()).map(ObjectId::getValue).orElse(null));

return result;
}

@Override
/** {@inheritDoc} */
public Class<CodePhrase> getAssociatedClass() {
return CodePhrase.class;
}
}
Loading