diff --git a/client/src/main/java/org/ehrbase/client/aql/containment/Containment.java b/client/src/main/java/org/ehrbase/client/aql/containment/Containment.java index b25b989c7..30f8d9282 100644 --- a/client/src/main/java/org/ehrbase/client/aql/containment/Containment.java +++ b/client/src/main/java/org/ehrbase/client/aql/containment/Containment.java @@ -43,11 +43,14 @@ public class Containment implements ContainmentExpression { * @param archetype The Archetype HRID. Sie Identification and the Virtual Archetype Space */ public Containment(String archetype) { - this.archetype = archetype; + typeName = StringUtils.substringBetween(archetype, "openEHR-EHR-", "."); if (StringUtils.isBlank(typeName)) { typeName = archetype; + archetype = null; } + this.archetype = archetype; + if ("EHR".equals(typeName)) { type = null; } else { @@ -61,8 +64,10 @@ public String buildAQL() { sb .append(typeName.toUpperCase()) .append(" ") - .append(getVariableName()) - .append("[").append(archetype).append("]"); + .append(getVariableName()); + if (archetype != null) { + sb.append("[").append(archetype).append("]"); + } if (contains != null) { sb .append(" contains ") diff --git a/client/src/main/java/org/ehrbase/client/aql/field/NativeSelectAqlField.java b/client/src/main/java/org/ehrbase/client/aql/field/NativeSelectAqlField.java new file mode 100644 index 000000000..36d9eeae4 --- /dev/null +++ b/client/src/main/java/org/ehrbase/client/aql/field/NativeSelectAqlField.java @@ -0,0 +1,29 @@ +/* + * + * * Copyright (c) 2020 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.client.aql.field; + +import org.ehrbase.client.aql.containment.Containment; + +public class NativeSelectAqlField extends AqlFieldImp { + + public NativeSelectAqlField(Containment containment, String path, Class valueClass) { + super(null, path, null, valueClass, false, containment); + } +} diff --git a/client/src/test/java/org/ehrbase/client/aql/query/EntityQueryTest.java b/client/src/test/java/org/ehrbase/client/aql/query/EntityQueryTest.java index 6a0700cf6..97c9bb909 100644 --- a/client/src/test/java/org/ehrbase/client/aql/query/EntityQueryTest.java +++ b/client/src/test/java/org/ehrbase/client/aql/query/EntityQueryTest.java @@ -20,8 +20,11 @@ package org.ehrbase.client.aql.query; import org.ehrbase.client.aql.condition.Condition; +import org.ehrbase.client.aql.containment.Containment; +import org.ehrbase.client.aql.containment.ContainmentExpression; import org.ehrbase.client.aql.containment.ContainmentPath; import org.ehrbase.client.aql.field.EhrFields; +import org.ehrbase.client.aql.field.NativeSelectAqlField; import org.ehrbase.client.aql.orderby.OrderByExpression; import org.ehrbase.client.aql.parameter.Parameter; import org.ehrbase.client.aql.record.Record3; @@ -29,6 +32,9 @@ import org.ehrbase.client.classgenerator.examples.ehrbasebloodpressuresimpledev0composition.definition.BloodPressureTrainingSampleObservation; import org.ehrbase.client.classgenerator.examples.ehrbasebloodpressuresimpledev0composition.definition.BloodPressureTrainingSampleObservationContainment; import org.ehrbase.client.classgenerator.examples.ehrbasebloodpressuresimpledev0composition.definition.CuffSizeDefiningcode; +import org.ehrbase.client.classgenerator.examples.virologischerbefundcomposition.definition.BefundObservationContainment; +import org.ehrbase.client.classgenerator.examples.virologischerbefundcomposition.definition.ProVirusClusterContainment; +import org.ehrbase.client.classgenerator.examples.virologischerbefundcomposition.definition.ProbeClusterContainment; import org.junit.Test; import java.time.temporal.TemporalAccessor; @@ -66,4 +72,33 @@ public void buildAql() { "order by c0/context/start_time/value ASCENDING, o1/data[at0001]/events[at0002]/time/value DESCENDING"); } + + @Test + public void buildAqlWithNativePath() { + BefundObservationContainment befundObservationContainment = BefundObservationContainment.getInstance(); + ProVirusClusterContainment proVirusClusterContainment = ProVirusClusterContainment.getInstance(); + ProbeClusterContainment probeClusterContainment = ProbeClusterContainment.getInstance(); + + Containment compositionContainment = new Containment("COMPOSITION"); + ContainmentExpression containment = + compositionContainment.contains(befundObservationContainment).contains(proVirusClusterContainment.and(probeClusterContainment)); + EntityQuery> entityQuery = Query.buildEntityQuery( + containment, + proVirusClusterContainment.VIRUS_VALUE, + probeClusterContainment.ZEITPUNKT_DER_PROBENENTNAHME_VALUE, + new NativeSelectAqlField<>(compositionContainment, "/uid/value", UUID.class) + ); + + entityQuery + .where(Condition.equal(new NativeSelectAqlField<>(compositionContainment, "/name/value", String.class), "Mikrobiologischer Befund")); + + assertThat(entityQuery.buildAql()).isEqualTo( + "Select c0/items[at0024]/value/value as F0, c1/items[at0015]/value/value as F1, c2/uid/value as F2 " + + "from EHR e " + + "contains COMPOSITION c2 " + + "contains OBSERVATION o3[openEHR-EHR-OBSERVATION.laboratory_test_result.v1] " + + "contains (CLUSTER c0[openEHR-EHR-CLUSTER.laboratory_test_analyte.v1] and CLUSTER c1[openEHR-EHR-CLUSTER.specimen.v1]) " + + "where c2/name/value = 'Mikrobiologischer Befund'" + ); + } } \ No newline at end of file diff --git a/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/AqlTestIT.java b/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/AqlTestIT.java index 154cc8f97..68bcc81d6 100644 --- a/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/AqlTestIT.java +++ b/client/src/test/java/org/ehrbase/client/openehrclient/defaultrestclient/AqlTestIT.java @@ -30,7 +30,13 @@ import org.assertj.core.groups.Tuple; import org.ehrbase.client.Integration; import org.ehrbase.client.TestData; +import org.ehrbase.client.aql.condition.Condition; +import org.ehrbase.client.aql.containment.Containment; +import org.ehrbase.client.aql.field.EhrFields; +import org.ehrbase.client.aql.field.NativeSelectAqlField; +import org.ehrbase.client.aql.parameter.Parameter; import org.ehrbase.client.aql.parameter.ParameterValue; +import org.ehrbase.client.aql.query.EntityQuery; import org.ehrbase.client.aql.query.Query; import org.ehrbase.client.aql.record.Record2; import org.ehrbase.client.classgenerator.examples.coronaanamnesecomposition.CoronaAnamneseComposition; @@ -394,4 +400,41 @@ public void testExecute11() throws IOException { } + + @Test + public void testExecute12() { + + UUID ehr = openEhrClient.ehrEndpoint().createEhr(); + + openEhrClient.compositionEndpoint(ehr).mergeCompositionEntity(TestData.buildEhrbaseBloodPressureSimpleDeV0()); + + EhrbaseBloodPressureSimpleDeV0Composition pressureSimple1 = TestData.buildEhrbaseBloodPressureSimpleDeV0(); + pressureSimple1.getBloodPressureTrainingSample().get(0).setSystolicMagnitude(1.1); + openEhrClient.compositionEndpoint(ehr).mergeCompositionEntity(pressureSimple1); + + EhrbaseBloodPressureSimpleDeV0Composition pressureSimple2 = TestData.buildEhrbaseBloodPressureSimpleDeV0(); + pressureSimple2.getBloodPressureTrainingSample().get(0).setSystolicMagnitude(1.1); + openEhrClient.compositionEndpoint(ehr).mergeCompositionEntity(pressureSimple2); + + Containment observationContainment = new Containment("OBSERVATION"); + + NativeSelectAqlField magnitudeField = new NativeSelectAqlField<>(observationContainment, "/data[at0001]/events[at0002]/data[at0003]/items[at0004]/value/magnitude", Double.class); + EntityQuery> entityQuery = Query.buildEntityQuery( + observationContainment, + EhrFields.EHR_ID(), + magnitudeField + ); + + Parameter ehrIdParameter = entityQuery.buildParameter(); + entityQuery.where( + Condition.equal(EhrFields.EHR_ID(), ehrIdParameter) + .and(Condition.equal(magnitudeField, 1.1d)) + ); + + List> result = openEhrClient.aqlEndpoint().execute(entityQuery, ehrIdParameter.setValue(ehr)); + assertThat(result) + .isNotNull() + .size().isEqualTo(2); + + } }