public static void simpleExample() { // START SNIPPET: simple // We're connecting to a DSTU1 compliant server in this example FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://fhirtest.uhn.ca/baseDstu2"; IGenericClient client = ctx.newRestfulGenericClient(serverBase); // Perform a search Bundle results = client .search() .forResource(Patient.class) .where(Patient.FAMILY.matches().value("duck")) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .execute(); System.out.println("Found " + results.getEntry().size() + " patients named 'duck'"); // END SNIPPET: simple }
.where(Patient.BIRTHDATE.beforeOrEquals().day("2011-01-01")) .and(Patient.CAREPROVIDER.hasChainedProperty(Organization.NAME.matches().value("Health"))) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .execute(); .where(Patient.FAMILY.matches().values("Smith", "Smyth")) .returnBundle(Bundle.class) .execute(); .where(Patient.ADDRESS.matches().values("Toronto")) .and(Patient.ADDRESS.matches().values("Ontario")) .and(Patient.ADDRESS.matches().values("Canada")) .returnBundle(Bundle.class) .execute(); .withIdAndCompartment("123", "condition") .where(Patient.ADDRESS.matches().values("Toronto")) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .execute(); .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .execute(); .where(Patient.ADDRESS.matches().values("Toronto")) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .summaryMode(SummaryEnum.TRUE) .execute(); .where(Patient.ADDRESS.matches().values("Toronto"))
@SuppressWarnings("unused") public void cacheControl() { FhirContext ctx = FhirContext.forDstu3(); // Create the client IGenericClient client = ctx.newRestfulGenericClient("http://localhost:9999/fhir"); Bundle bundle = new Bundle(); // ..populate the bundle.. // START SNIPPET: cacheControl Bundle response = client .search() .forResource(Patient.class) .returnBundle(Bundle.class) .cacheControl(new CacheControlDirective().setNoCache(true)) // <-- add a directive .execute(); // END SNIPPET: cacheControl }
public static void main(String[] args) throws Exception { FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://fhirtest.uhn.ca/baseDstu2"; IGenericClient client = ctx.newRestfulGenericClient(serverBase); // use RelatedPerson because there aren't that many on the server Bundle bundle = client.search().forResource(RelatedPerson.class).returnBundle(Bundle.class).execute(); BundleFetcher.fetchRestOfBundle(client, bundle); if (bundle.getTotal() != bundle.getEntry().size()) { System.out.println("Counts didn't match! Expected " + bundle.getTotal() + " but bundle only had " + bundle.getEntry().size() + " entries!"); } // IParser parser = ctx.newXmlParser().setPrettyPrint(true); // System.out.println(parser.encodeResourceToString(bundle)); } }
protected Bundle sendManuallyWithCount(ICriterion<?> requestData, int count) { return client.search() .forResource(PdqPatient.class) .where(requestData) .count(count) .returnBundle(Bundle.class) .execute(); }
/** * Fetch multiple instances of the domain class from the data store. */ @Override public <T extends BaseResource> List<T> fetchObjects(Class<T> clazz, String[] ids) { if (ids == null || ids.length == 0) { return Collections.emptyList(); } StringClientParam param = new StringClientParam(BaseResource.SP_RES_ID); Bundle results = fhirClient.search().forResource(clazz).where(param.matches().values(ids)).execute(); return FhirUtil.getEntries(results, clazz); }
/** * General purpose find * * @param clazz Resource class to find * @param criterion Criterion to be applied to the query. Must include a patient matching criterion * @param <T> Resource * @return A list of resource matching the criterion */ public <T extends IResource> Collection<T> find(Class<T> clazz, ICriterion<?>... criterion) { String patientId = session.getContextDSTU2().getPatientResource().getIdElement().getIdPart(); IQuery<ca.uhn.fhir.model.api.Bundle> queryBuilder = session.search().forResource(clazz); // use reflection to match the getPatientContext criterion queryBuilder.where(findPatientReferenceOnResource(clazz).hasId(patientId)); for (ICriterion<?> theCriterion : criterion) { queryBuilder = queryBuilder.and(theCriterion); } IClientExecutable<IQuery<Bundle>, Bundle> results = queryBuilder.returnBundle(Bundle.class); return asCollection(results.execute()); }
/** * Fetch multiple instances of the domain class from the data store. */ @Override public <T extends BaseResource> List<T> fetchObjects(Class<T> clazz, String[] ids) { if (ids == null || ids.length == 0) { return Collections.emptyList(); } StringClientParam param = new StringClientParam(BaseResource.SP_RES_ID); Bundle results = fhirClient.search().forResource(clazz).where(param.matches().values(ids)).returnBundle(Bundle.class) .execute(); return FhirUtil.getEntries(results, clazz); }
@Override public void buildQuery(LocationSearchCriteria criteria, IQuery<?> query) { super.buildQuery(criteria, query); if (criteria.getType() != null) { query.where(Location.TYPE.exactly().code(criteria.getType())); } if (criteria.getStatus() != null) { query.where(Location.STATUS.exactly().code(criteria.getStatus().toCode())); } if (criteria.getName() != null) { query.where(Location.NAME.matches().value(criteria.getName())); } }
@Override public IClientExecutable<IQuery<Bundle>, ?> getClientExecutable(IGenericClient client, Object requestData, Map<String, Object> parameters) { IQuery<IBaseBundle> query; String queriedResourceType = (String)parameters.get(Constants.FHIR_RESOURCE_TYPE_HEADER); if (requestData instanceof ICriterion) { query = client.search() .forResource(queriedResourceType) .where((ICriterion<?>) requestData); } else { query = client.search() .byUrl(requestData.toString()); } if (parameters.containsKey(Constants.FHIR_COUNT)) { query.count(Integer.parseInt(parameters.get(Constants.FHIR_COUNT).toString())); } return query.returnBundle(Bundle.class); }
/** * General purpose fluent find * * @param clazz Resource class to find * @param criterion Criterion to be applied to the query. Must include a patient matching criterion * @param <T> Resource * @return A list of resource matching the criterion */ public <T extends IBaseResource> Collection<T> find(Class<T> clazz, ICriterion<?>... criterion) { String patientId = session.getContext().getPatientResource().getId().getIdPart(); IQuery<Bundle> queryBuilder = session.search().forResource(clazz); // use reflection to match the getPatientContext criterion queryBuilder.where(findPatientReferenceOnResource(clazz).hasId(patientId)); for (ICriterion<?> theCriterion : criterion) { queryBuilder = queryBuilder.and(theCriterion); } Bundle results = queryBuilder.execute(); return asCollection(results); }
@Override public IBundleProvider search(SearchParameterMap theParams) { FhirContext fhirContext = myClient.getFhirContext(); IBaseBundle bundle = myClient .search() .forResource(ResourceTypeEnum.SEARCHPARAMETER.getCode()) .cacheControl(new CacheControlDirective().setNoCache(true)) .execute(); return new SimpleBundleProvider(BundleUtil.toListOfResources(fhirContext, bundle)); }
@Override public IBaseResource getResource(IIdType payloadId) throws ResourceGoneException { RuntimeResourceDefinition resourceDef = myFhirContext.getResourceDefinition(payloadId.getResourceType()); return myClient.search().forResource(resourceDef.getName()).withIdAndCompartment(payloadId.getIdPart(), payloadId.getResourceType()).execute(); } }
/** * Alternative method for performing a search that allows for external configuration of the * query object. * * @param query The query object. * @return List of matching resources. May return null to indicate no matches. */ @Override public List<R> search(IQuery<Bundle> query) { return FhirUtil.getEntries(query.execute(), resourceClass); } }
/** * Alternative method for performing a search that allows for external configuration of the * query object. * * @param query The query object. * @return List of matching resources. May return null to indicate no matches. */ @Override public List<R> search(IQuery<?> query) { return FhirUtil.getEntries(query.returnBundle(Bundle.class).execute(), resourceClass); } }
@Test public void testClient() { FhirContext ctx = new FhirContext(FhirVersionEnum.DSTU3); GenericClient client = (GenericClient) ctx.newRestfulGenericClient(FHIR_EP); //client.registerInterceptor(new BasicAuthInterceptor("user123", "user321$")); client.setDontValidateConformance(true); Bundle bundle = client.search().forResource(Patient.class).count(1).returnBundle(Bundle.class).execute(); assertTrue("No patient resources returned.", bundle.getEntry().size() > 0); Patient patient = (Patient) bundle.getEntryFirstRep().getResource(); String patient_id = patient.getIdElement().getIdPart(); Patient patient1 = client.read(Patient.class, "Patient/" + patient_id); assertEquals(patient_id, patient1.getIdElement().getIdPart()); bundle = client.search().byUrl("Patient?_id=" + patient_id).returnBundle(Bundle.class).execute(); assertEquals(1, bundle.getEntry().size()); Patient patient2 = (Patient) bundle.getEntry().get(0).getResource(); assertEquals(patient_id, patient2.getIdElement().getIdPart()); }
/** * Validates the criteria settings and then transfers them to the query object. This method * transfers the base criteria only. Override to transfer additional criteria that are specific * to the resource type. * * @param criteria Research search criteria. * @param query The query object. */ protected void buildQuery(C criteria, IQuery<?> query) { criteria.validate(); if (criteria.getMaximum() > 0) { query.count(criteria.getMaximum()); } if (criteria.getId() != null) { query.where(new StringClientParam(BaseResource.SP_RES_ID).matches().value(criteria.getId())); } }
/** * Validates the criteria settings and then transfers them to the query object. This method * transfers the base criteria only. Override to transfer additional criteria that are specific * to the resource type. * * @param criteria Research search criteria. * @param query The query object. */ protected void buildQuery(C criteria, IQuery<Bundle> query) { criteria.validate(); if (criteria.getMaximum() > 0) { query.limitTo(criteria.getMaximum()); } if (criteria.getId() != null) { query.where(new StringClientParam(BaseResource.SP_RES_ID).matches().value(criteria.getId())); } }
protected Bundle sendManuallyWithCount(ICriterion<?> requestData, int count) { return client.search() .forResource(PdqPatient.class) .where(requestData) .count(count) .returnBundle(Bundle.class) .execute(); }
private static void copy(FhirContext theCtx, IGenericClient theSource, IGenericClient theTarget, String theResType, List<IBaseResource> theQueued, Set<String> theSent) { Bundle received = theSource .search() .forResource(theResType) .returnBundle(Bundle.class) .execute(); copy(theCtx, theTarget, theResType, theQueued, theSent, received); while (received.getLink("next") != null) { ourLog.info("Fetching next page..."); received = theSource.loadPage().next(received).execute(); copy(theCtx, theTarget, theResType, theQueued, theSent, received); } }