Skip to content

Commit

Permalink
allow native parts in EntityQuery
Browse files Browse the repository at this point in the history
see #91
  • Loading branch information
stefanspiska committed Oct 13, 2020
1 parent 7214973 commit 0c8e259
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ public class Containment implements ContainmentExpression {
* @param archetype The Archetype HRID. Sie <a href="https://specifications.openehr.org/releases/AM/latest/Overview.html#_identification_and_the_virtual_archetype_space">Identification and the Virtual Archetype Space</a>
*/
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 {
Expand All @@ -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 ")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends AqlFieldImp<T> {

public NativeSelectAqlField(Containment containment, String path, Class<T> valueClass) {
super(null, path, null, valueClass, false, containment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@
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;
import org.ehrbase.client.classgenerator.examples.ehrbasebloodpressuresimpledev0composition.EhrbaseBloodPressureSimpleDeV0CompositionContainment;
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;
Expand Down Expand Up @@ -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<Record3<String, TemporalAccessor, UUID>> 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'"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Double> magnitudeField = new NativeSelectAqlField<>(observationContainment, "/data[at0001]/events[at0002]/data[at0003]/items[at0004]/value/magnitude", Double.class);
EntityQuery<Record2<UUID, Double>> entityQuery = Query.buildEntityQuery(
observationContainment,
EhrFields.EHR_ID(),
magnitudeField
);

Parameter<UUID> ehrIdParameter = entityQuery.buildParameter();
entityQuery.where(
Condition.equal(EhrFields.EHR_ID(), ehrIdParameter)
.and(Condition.equal(magnitudeField, 1.1d))
);

List<Record2<UUID, Double>> result = openEhrClient.aqlEndpoint().execute(entityQuery, ehrIdParameter.setValue(ehr));
assertThat(result)
.isNotNull()
.size().isEqualTo(2);

}
}

0 comments on commit 0c8e259

Please sign in to comment.