Work on custom params

This commit is contained in:
James Agnew 2017-02-04 14:41:11 -05:00
parent 12dc4d1c11
commit c85c2269a1
8 changed files with 145 additions and 21 deletions

View File

@ -458,7 +458,7 @@ class ModelScanner {
compositeOf.add(param);
}
RuntimeSearchParam param = new RuntimeSearchParam(searchParam.name(), searchParam.description(), searchParam.path(), RestSearchParameterTypeEnum.COMPOSITE, compositeOf, null, toTargetList(searchParam.target()), RuntimeSearchParamStatusEnum.ACTIVE);
RuntimeSearchParam param = new RuntimeSearchParam(null, null, searchParam.name(), searchParam.description(), searchParam.path(), RestSearchParameterTypeEnum.COMPOSITE, compositeOf, null, toTargetList(searchParam.target()), RuntimeSearchParamStatusEnum.ACTIVE);
theResourceDef.addSearchParam(param);
}
}

View File

@ -6,6 +6,8 @@ import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import org.hl7.fhir.instance.model.api.IIdType;
import ca.uhn.fhir.rest.method.RestSearchParameterTypeEnum;
/*
@ -29,7 +31,7 @@ import ca.uhn.fhir.rest.method.RestSearchParameterTypeEnum;
*/
public class RuntimeSearchParam {
private final IIdType myId;
private final List<RuntimeSearchParam> myCompositeOf;
private final String myDescription;
private final String myName;
@ -38,10 +40,21 @@ public class RuntimeSearchParam {
private final Set<String> myTargets;
private final Set<String> myProvidesMembershipInCompartments;
private final RuntimeSearchParamStatusEnum myStatus;
private final String myUri;
public RuntimeSearchParam(String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
public IIdType getId() {
return myId;
}
public String getUri() {
return myUri;
}
public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
super();
myId = theId;
myUri = theUri;
myName = theName;
myDescription = theDescription;
myPath = thePath;
@ -69,7 +82,7 @@ public class RuntimeSearchParam {
}
public RuntimeSearchParam(String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
this(theName, theDescription, thePath, theParamType, null, theProvidesMembershipInCompartments, theTargets, theStatus);
this(null, null, theName, theDescription, thePath, theParamType, null, theProvidesMembershipInCompartments, theTargets, theStatus);
}
public List<RuntimeSearchParam> getCompositeOf() {

View File

@ -7,6 +7,7 @@ import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
@ -16,6 +17,8 @@ import ca.uhn.fhir.context.RuntimeSearchParam;
public abstract class BaseSearchParamRegistry implements ISearchParamRegistry {
private static final Map<String, RuntimeSearchParam> EMPTY_SP_MAP = Collections.emptyMap();
private Map<String, Map<String, RuntimeSearchParam>> myBuiltInSearchParams;
@Autowired
@ -62,4 +65,13 @@ public abstract class BaseSearchParamRegistry implements ISearchParamRegistry {
myBuiltInSearchParams = Collections.unmodifiableMap(resourceNameToSearchParams);
}
@Override
public Collection<RuntimeSearchParam> getAllSearchParams(String theResourceName) {
Validate.notBlank(theResourceName, "theResourceName must not be null or blank");
Map<String, RuntimeSearchParam> map = myBuiltInSearchParams.get(theResourceName);
map = ObjectUtils.defaultIfNull(map, EMPTY_SP_MAP);
return Collections.unmodifiableCollection(map.values());
}
}

View File

@ -1,5 +1,6 @@
package ca.uhn.fhir.jpa.dao;
import java.util.Collection;
import java.util.Map;
import ca.uhn.fhir.context.RuntimeSearchParam;
@ -8,6 +9,8 @@ public interface ISearchParamRegistry {
Map<String,RuntimeSearchParam> getActiveSearchParams(String theResourceName);
Collection<RuntimeSearchParam> getAllSearchParams(String theResourceName);
void forceRefresh();
}

View File

@ -1,5 +1,7 @@
package ca.uhn.fhir.jpa.provider.dstu3;
import java.util.Collection;
/*
* #%L
* HAPI FHIR JPA Server
@ -27,10 +29,13 @@ import javax.servlet.http.HttpServletRequest;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.CapabilityStatement.*;
import org.hl7.fhir.dstu3.model.Enumerations.SearchParamType;
import org.springframework.beans.factory.annotation.Autowired;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeSearchParam;
import ca.uhn.fhir.jpa.dao.DaoConfig;
import ca.uhn.fhir.jpa.dao.IFhirSystemDao;
import ca.uhn.fhir.jpa.dao.ISearchParamRegistry;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.util.CoverageIgnore;
import ca.uhn.fhir.util.ExtensionConstants;
@ -63,6 +68,9 @@ public class JpaConformanceProviderDstu3 extends org.hl7.fhir.dstu3.hapi.rest.se
super.setCache(false);
}
@Autowired
private ISearchParamRegistry mySearchParamRegistry;
@Override
public CapabilityStatement getServerConformance(HttpServletRequest theRequest) {
CapabilityStatement retVal = myCachedValue;
@ -89,19 +97,46 @@ public class JpaConformanceProviderDstu3 extends org.hl7.fhir.dstu3.hapi.rest.se
nextResource.addExtension(new Extension(ExtensionConstants.CONF_RESOURCE_COUNT, new DecimalType(count)));
}
// Add chained params
for (CapabilityStatementRestResourceSearchParamComponent nextParam : nextResource.getSearchParam()) {
if (nextParam.getType() == SearchParamType.REFERENCE) {
// List<CodeType> targets = nextParam.getTarget();
// for (CodeType next : targets) {
// RuntimeResourceDefinition def = ctx.getResourceDefinition(next.getValue());
// for (RuntimeSearchParam nextChainedParam : def.getSearchParams()) {
// nextParam.addChain(nextChainedParam.getName());
// }
// }
}
}
nextResource.getSearchParam().clear();
Collection<RuntimeSearchParam> searchParams = mySearchParamRegistry.getAllSearchParams(nextResource.getType());
for (RuntimeSearchParam runtimeSp : searchParams) {
CapabilityStatementRestResourceSearchParamComponent confSp = nextResource.addSearchParam();
confSp.setName(runtimeSp.getName());
confSp.setDocumentation(runtimeSp.getDescription());
confSp.setDefinition(runtimeSp.getUri());
switch (runtimeSp.getParamType()) {
case COMPOSITE:
confSp.setType(SearchParamType.COMPOSITE);
break;
case DATE:
confSp.setType(SearchParamType.DATE);
break;
case NUMBER:
confSp.setType(SearchParamType.NUMBER);
break;
case QUANTITY:
confSp.setType(SearchParamType.QUANTITY);
break;
case REFERENCE:
confSp.setType(SearchParamType.REFERENCE);
break;
case STRING:
confSp.setType(SearchParamType.STRING);
break;
case TOKEN:
confSp.setType(SearchParamType.TOKEN);
break;
case URI:
confSp.setType(SearchParamType.URI);
break;
case HAS:
// Shouldn't happen
break;
}
}
}
}

View File

@ -2,6 +2,7 @@ package ca.uhn.fhir.jpa.dao.dstu3;
import static org.junit.Assert.assertEquals;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -57,6 +58,11 @@ public class SearchParamExtractorDstu3Test {
public void forceRefresh() {
// nothing
}
@Override
public Collection<RuntimeSearchParam> getAllSearchParams(String theResourceName) {
throw new UnsupportedOperationException();
}
};
SearchParamExtractorDstu3 extractor = new SearchParamExtractorDstu3(ourCtx, ourValidationSupport, searchParamRegistry);

View File

@ -6,14 +6,16 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.CapabilityStatement.CapabilityStatementRestComponent;
import org.hl7.fhir.dstu3.model.CapabilityStatement.CapabilityStatementRestResourceComponent;
import org.hl7.fhir.dstu3.model.CapabilityStatement.CapabilityStatementRestResourceSearchParamComponent;
import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender;
import org.hl7.fhir.dstu3.model.Observation;
import org.hl7.fhir.dstu3.model.Observation.ObservationStatus;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.SearchParameter;
import org.hl7.fhir.dstu3.model.SearchParameter.XPathUsageType;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.After;
@ -61,6 +63,56 @@ public class ResourceProviderCustomSearchParamDstu3Test extends BaseResourceProv
}
}
@SuppressWarnings("unused")
@Test
public void testConformance() {
// Add a custom search parameter
SearchParameter fooSp = new SearchParameter();
fooSp.setCode("foo");
fooSp.setType(org.hl7.fhir.dstu3.model.Enumerations.SearchParamType.TOKEN);
fooSp.setTitle("FOO SP");
fooSp.setXpath("Patient.gender");
fooSp.setXpathUsage(org.hl7.fhir.dstu3.model.SearchParameter.XPathUsageType.NORMAL);
fooSp.setStatus(org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus.ACTIVE);
mySearchParameterDao.create(fooSp, mySrd);
// Disable an existing parameter
fooSp = new SearchParameter();
fooSp.setCode("gender");
fooSp.setType(org.hl7.fhir.dstu3.model.Enumerations.SearchParamType.TOKEN);
fooSp.setTitle("Gender");
fooSp.setXpath("Patient.gender");
fooSp.setXpathUsage(org.hl7.fhir.dstu3.model.SearchParameter.XPathUsageType.NORMAL);
fooSp.setStatus(org.hl7.fhir.dstu3.model.Enumerations.PublicationStatus.RETIRED);
mySearchParameterDao.create(fooSp, mySrd);
CapabilityStatement conformance = ourClient
.fetchConformance()
.ofType(CapabilityStatement.class)
.execute();
Map<String, CapabilityStatementRestResourceSearchParamComponent> map = extractSearchParams(conformance, "Patient");
CapabilityStatementRestResourceSearchParamComponent param = map.get("foo");
assertEquals("foo", param.getName());
}
private Map<String, CapabilityStatementRestResourceSearchParamComponent> extractSearchParams(CapabilityStatement conformance, String resType) {
Map<String, CapabilityStatementRestResourceSearchParamComponent> map = new HashMap<String, CapabilityStatement.CapabilityStatementRestResourceSearchParamComponent>();
for (CapabilityStatementRestComponent nextRest : conformance.getRest()) {
for (CapabilityStatementRestResourceComponent nextResource : nextRest.getResource()) {
if (!resType.equals(nextResource.getType())) {
continue;
}
for (CapabilityStatementRestResourceSearchParamComponent nextParam : nextResource.getSearchParam()) {
map.put(nextParam.getName(), nextParam);
}
}
}
return map;
}
@SuppressWarnings("unused")
@Test
public void testSearchWithCustomParam() {

View File

@ -96,8 +96,11 @@ public class ValidateDstu2_1Test {
params.addParameter().setName("resource").setResource(patient);
params.addParameter().setName("mode").setValue(new CodeType(" "));
String encodedResource = ourCtx.newXmlParser().encodeResourceToString(params);
HttpPost httpPost = new HttpPost("http://localhost:" + ourPort + "/Patient/$validate");
httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(params), ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
httpPost.setEntity(new StringEntity(encodedResource, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));
HttpResponse status = ourClient.execute(httpPost);
String resp = IOUtils.toString(status.getEntity().getContent());