Skip to content

Commit

Permalink
Work on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Sep 16, 2018
1 parent 6cc413c commit 23f7517
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ public enum RestSearchParameterTypeEnum {
*/
HAS("string", "http://hl7.org/fhir/search-param-type"),

/**
* Code Value: <b>number</b>
*
* Search parameter SHALL be a number (a whole number, or a decimal).
*/
SPECIAL("special", "http://hl7.org/fhir/search-param-type"),

;


/**
* Identifier for this Value Set:
Expand Down
32 changes: 30 additions & 2 deletions hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/*
* #%L
Expand All @@ -24,6 +25,8 @@
*/
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.api.*;
Expand All @@ -40,6 +43,7 @@

public class FhirTerser {

public static final Pattern COMPARTMENT_MATCHER_PATH = Pattern.compile("([a-zA-Z.]+)\\.where\\(resolve\\(\\) is ([a-zA-Z]+)\\)");
private FhirContext myContext;

public FhirTerser(FhirContext theContext) {
Expand Down Expand Up @@ -364,8 +368,26 @@ public boolean isSourceInCompartmentForTarget(String theCompartmentName, IBaseRe
List<RuntimeSearchParam> params = sourceDef.getSearchParamsForCompartmentName(theCompartmentName);
for (RuntimeSearchParam nextParam : params) {
for (String nextPath : nextParam.getPathsSplit()) {

/*
* DSTU3 and before just defined compartments as being (e.g.) named
* Patient with a path like CarePlan.subject
*
* R4 uses a fancier format like CarePlan.subject.where(resolve() is Patient)
*
* The following Regex is a hack to make that efficient at runtime.
*/
String wantType = null;
Pattern pattern = COMPARTMENT_MATCHER_PATH;
Matcher matcher = pattern.matcher(nextPath);
if (matcher.matches()) {
nextPath = matcher.group(1);
wantType = matcher.group(2);
}

for (IBaseReference nextValue : getValues(theSource, nextPath, IBaseReference.class)) {
String nextRef = nextValue.getReferenceElement().toUnqualifiedVersionless().getValue();
IIdType nextTargetId = nextValue.getReferenceElement();
String nextRef = nextTargetId.toUnqualifiedVersionless().getValue();

/*
* If the reference isn't an explicit resource ID, but instead is just
Expand All @@ -374,14 +396,20 @@ public boolean isSourceInCompartmentForTarget(String theCompartmentName, IBaseRe
*/
if (isBlank(nextRef) && nextValue.getResource() != null) {
IBaseResource nextTarget = nextValue.getResource();
IIdType nextTargetId = nextTarget.getIdElement().toUnqualifiedVersionless();
nextTargetId = nextTarget.getIdElement().toUnqualifiedVersionless();
if (!nextTargetId.hasResourceType()) {
String resourceType = myContext.getResourceDefinition(nextTarget).getName();
nextTargetId.setParts(null, resourceType, nextTargetId.getIdPart(), null);
}
nextRef = nextTargetId.getValue();
}

if (isNotBlank(wantType)) {
if (!nextTargetId.getResourceType().equals(wantType)) {
continue;
}
}

if (wantRef.equals(nextRef)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.hl7.fhir.instance.model.api.IIdType;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

Expand Down Expand Up @@ -378,6 +375,20 @@ public Verdict applyRule(RestOperationTypeEnum theOperation, RequestDetails theR
List<RuntimeSearchParam> params = sourceDef.getSearchParamsForCompartmentName(compartmentOwnerResourceType);
if (params.isEmpty() == false) {

List<RuntimeSearchParam> oldParams = params;
params = new ArrayList<>(params);
for (RuntimeSearchParam nextParam : oldParams) {
for (String nextParamPath : nextParam.getPathsSplit()) {
for (RuntimeSearchParam nextCandidateParam : sourceDef.getSearchParams()) {
if (nextCandidateParam != nextParam) {
for (String nextCandidatePath : nextCandidateParam.getPathsSplit()) {
nextCandidatePath.toLowerCase();
}
}
}
}
}

/*
* If this is a search, we can at least check whether
* the client has requested a search parameter that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,31 @@ public List<IAuthRule> buildRuleList(RequestDetails theRequestDetails) {
extractResponseAndClose(status);
assertEquals(403, status.getStatusLine().getStatusCode());
assertFalse(ourHitMethod);

ourReturn = Collections.singletonList(createDiagnosticReport(1, "Patient/1"));
ourHitMethod = false;
httpGet = new HttpGet("http://localhost:" + ourPort + "/DiagnosticReport?subject=subject/1");
status = ourClient.execute(httpGet);
extractResponseAndClose(status);
assertEquals(200, status.getStatusLine().getStatusCode());
assertTrue(ourHitMethod);

ourReturn = Collections.singletonList(createDiagnosticReport(1, "Patient/1"));
ourHitMethod = false;
httpGet = new HttpGet("http://localhost:" + ourPort + "/DiagnosticReport?subject=1");
status = ourClient.execute(httpGet);
extractResponseAndClose(status);
assertEquals(200, status.getStatusLine().getStatusCode());
assertTrue(ourHitMethod);

ourReturn = Collections.singletonList(createDiagnosticReport(1, "Patient/1"));
ourHitMethod = false;
httpGet = new HttpGet("http://localhost:" + ourPort + "/DiagnosticReport?subject=Patient/2");
status = ourClient.execute(httpGet);
extractResponseAndClose(status);
assertEquals(403, status.getStatusLine().getStatusCode());
assertFalse(ourHitMethod);

}

@Test
Expand Down

0 comments on commit 23f7517

Please sign in to comment.