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)); } }
@Override public IBundleProvider search(SearchParameterMap theMap) { FhirContext fhirContext = myClient.getFhirContext(); String searchURL = ResourceTypeEnum.SUBSCRIPTION.getCode() + theMap.toNormalizedQueryString(myFhirContext); IBaseBundle bundle = myClient .search() .byUrl(searchURL) .cacheControl(new CacheControlDirective().setNoCache(true)) .execute(); return new SimpleBundleProvider(BundleUtil.toListOfResources(fhirContext, bundle)); }
@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); }
.forResource(Patient.class) .where(Patient.BIRTHDATE.beforeOrEquals().day("2011-01-01")) .and(Patient.CAREPROVIDER.hasChainedProperty(Organization.NAME.matches().value("Health"))) .forResource(Patient.class) .where(Patient.FAMILY.matches().values("Smith", "Smyth")) .returnBundle(Bundle.class) .forResource(Patient.class) .where(Patient.ADDRESS.matches().values("Toronto")) .and(Patient.ADDRESS.matches().values("Ontario")) .forResource(Patient.class) .withIdAndCompartment("123", "condition") .where(Patient.ADDRESS.matches().values("Toronto")) .byUrl(searchUrl) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .execute(); .forResource(Patient.class) .where(Patient.ADDRESS.matches().values("Toronto")) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .forResource(Patient.class) .where(Patient.ADDRESS.matches().values("Toronto")) .returnBundle(ca.uhn.fhir.model.dstu2.resource.Bundle.class) .forResource(Patient.class)
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 }
@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()); }
/** * Perform a search directly by URL. * * @param url The URL to search for. Note that this URL may be complete (e.g. "http://example.com/base/Patient?name=foo") in which case the client's base URL will be ignored. Or it can be relative * (e.g. "Patient?name=foo") in which case the client's base URL will be used. * @param extraParameters see {@link ExtraParameters} for a full list of parameters that can be passed, may be NULL * @return the {@link IBaseBundle} */ public IBaseBundle searchByUrl(String url, Map<ExtraParameters, Object> extraParameters) { IQuery<IBaseBundle> query = client.search().byUrl(url); ExtraParameters.process(extraParameters, query); return query.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); } }
@Override public void run(ThreadEx thread) throws Exception { Bundle bundle = fhirService.getClient().search().byUrl(url).returnBundle(Bundle.class).execute(); thread.setAttribute("bundle", bundle); }
org.hl7.fhir.dstu3.model.Bundle response = client .search() .forResource(org.hl7.fhir.dstu3.model.ConceptMap.class) .where(org.hl7.fhir.dstu3.model.ConceptMap.URL.matches().value(conceptMapUrl)) .returnBundle(org.hl7.fhir.dstu3.model.Bundle.class) Bundle response = client .search() .forResource(ConceptMap.class) .where(ConceptMap.URL.matches().value(conceptMapUrl)) .returnBundle(Bundle.class)
@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(); } }
@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)); }
@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 }
private static void paging() { { // START SNIPPET: searchPaging FhirContext ctx = FhirContext.forDstu2(); IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2"); // Perform a search Bundle resultBundle = client.search() .forResource(Patient.class) .where(Patient.NAME.matches().value("Smith")) .returnBundle(Bundle.class) .execute(); if (resultBundle.getLink(Bundle.LINK_NEXT) != null) { // load next page Bundle nextPage = client.loadPage().next(resultBundle).execute(); } // END SNIPPET: searchPaging } }
/** * Creates an empty query object for this resource class. * * @return The newly created query object. */ @Override public IQuery<?> createQuery() { return fhirClient.search().forResource(resourceClass); }
/** * Creates an empty query object for this resource class. * * @return The newly created query object. */ @Override public IQuery<Bundle> createQuery() { return fhirClient.search().forResource(resourceClass); }
.forResource(MyPatient.class) .returnBundle(Bundle.class) .execute();
public DefaultNamingSystemServiceImpl addNamingSystemsFromFhirServer(String url) { addNamingSystems(fhirContext.newRestfulGenericClient(url) .search() .forResource(NamingSystem.class) .returnBundle(Bundle.class) .execute()); return this; }
public DefaultNamingSystemServiceImpl addNamingSystemsFromFhirServer(String url) { addNamingSystems(fhirContext.newRestfulGenericClient(url) .search() .forResource(NamingSystem.class) .returnBundle(Bundle.class) .execute()); return this; }
public Collection<String> getTypes() { TreeSet<String> results = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); try { Bundle bundle = ClientUtil.getFhirClient().search().forResource(ValueSet.class) .where(ValueSet.NAME.matchesExactly().value("DocumentType")).execute(); for (ValueSet vs : FhirUtil.getEntries(bundle, ValueSet.class)) { for (ValueSet.DefineConcept concept : vs.getDefine().getConcept()) { results.add(concept.getDisplay().toString()); } } } catch (Exception e) {} return results; } }