org.hl7.fhir.r4.model.DomainResource Java Examples

The following examples show how to use org.hl7.fhir.r4.model.DomainResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CqlExecutionProvider.java    From cqf-ruler with Apache License 2.0 6 votes vote down vote up
public Object evaluateInContext(DomainResource instance, String cql, String patientId) {
    Iterable<CanonicalType> libraries = getLibraryReferences(instance);

    String source = String.format(
            "library LocalLibrary using FHIR version '4.0.0' include FHIRHelpers version '4.0.0' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s",
            buildIncludes(libraries), instance.fhirType(), instance.fhirType(), instance.fhirType(), cql);
    
    LibraryLoader libraryLoader = LibraryHelper.createLibraryLoader(this.getLibraryResourceProvider());
    
    org.cqframework.cql.elm.execution.Library library = TranslatorHelper.translateLibrary(source,
            libraryLoader.getLibraryManager(), libraryLoader.getModelManager());
    
    // resolve execution context
    Context context = setupContext(instance, patientId, libraryLoader, library);
    return context.resolveExpressionRef("Expression").evaluate(context);
}
 
Example #2
Source File: GraphQLEngineTests.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
@Override
public ReferenceResolution lookup(Object appInfo, IBaseResource context, IBaseReference reference) throws FHIRException {
  try {
    if (reference.getReferenceElement().isLocal()) {
      if (!(context instanceof DomainResource))
        return null;
      for (Resource r : ((DomainResource) context).getContained()) {
        if (('#' + r.getId()).equals(reference.getReferenceElement().getValue())) {
          return new ReferenceResolution(context, r);
        }
      }
    } else {
      String[] parts = reference.getReferenceElement().getValue().split("/");
      String filename = TestingUtilities.resourceNameToFile(parts[0].toLowerCase() + '-' + parts[1].toLowerCase() + ".xml");
      if (new File(filename).exists())
        return new ReferenceResolution(null, new XmlParser().parse(new FileInputStream(filename)));
    }
    return null;
  } catch (Exception e) {
    throw new FHIRException(e);
  }
}
 
Example #3
Source File: NarrativeRemover.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private static void execute(File folder) {
  for (File f : folder.listFiles()) {
    if (f.isDirectory())
      execute(f);
    else {
      System.out.println(f.getAbsolutePath());
      try {
        Resource r = new JsonParser().parse(new FileInputStream(f));
        if (r instanceof DomainResource) {
          DomainResource d = (DomainResource) r;
          d.setText(null);
          new JsonParser().compose(new FileOutputStream(f), d);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  
}
 
Example #4
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public static String readStringExtension(DomainResource c, String uri) {
  Extension ex = getExtension(c, uri);
  if (ex == null)
    return null;
  if ((ex.getValue() instanceof StringType))
    return ((StringType) ex.getValue()).getValue();
  if ((ex.getValue() instanceof UriType))
    return ((UriType) ex.getValue()).getValue();
  if (ex.getValue() instanceof CodeType)
    return ((CodeType) ex.getValue()).getValue();
  if (ex.getValue() instanceof IntegerType)
    return ((IntegerType) ex.getValue()).asStringValue();
  if ((ex.getValue() instanceof MarkdownType))
    return ((MarkdownType) ex.getValue()).getValue();
  return null;
}
 
Example #5
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * @param name the identity of the extension of interest
 * @return The extension, if on this element, else null
 */
public static Extension getExtension(DomainResource resource, String name) {
  if (name == null)
    return null;
  if (!resource.hasExtension())
    return null;
  for (Extension e : resource.getExtension()) {
    if (name.equals(e.getUrl()))
      return e;
  }
  return null;
}
 
Example #6
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static boolean readBoolExtension(DomainResource c, String uri) {
  Extension ex = ExtensionHelper.getExtension(c, uri);
  if (ex == null)
    return false;
  if (!(ex.getValue() instanceof BooleanType))
    return false;
  return ((BooleanType) ex.getValue()).getValue();
}
 
Example #7
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void setStringExtension(DomainResource resource, String uri, String value) {
  if (Utilities.noString(value))
    return;
      Extension ext = getExtension(resource, uri);
  if (ext != null)
    ext.setValue(new StringType(value));
  else
    resource.getExtension().add(new Extension(new UriType(uri)).setValue(new StringType(value)));
}
 
Example #8
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void setCodeExtension(DomainResource resource, String uri, String value) {
  if (Utilities.noString(value))
    return;
  
  Extension ext = getExtension(resource, uri);
  if (ext != null)
    ext.setValue(new CodeType(value));
  else
    resource.getExtension().add(new Extension(new UriType(uri)).setValue(new CodeType(value)));
}
 
Example #9
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void setIntegerExtension(DomainResource resource, String uri, int value) {
  Extension ext = getExtension(resource, uri);
  if (ext != null)
    ext.setValue(new IntegerType(value));
  else
    resource.getExtension().add(new Extension(new UriType(uri)).setValue(new IntegerType(value)));
}
 
Example #10
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static List<Extension> getExtensions(DomainResource resource, String url) {
  List<Extension> results = new ArrayList<Extension>();
  for (Extension ex : resource.getExtension())
    if (ex.getUrl().equals(url))
      results.add(ex);
  return results;
}
 
Example #11
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void removeExtension(DomainResource focus, String url) {
  Iterator<Extension> i = focus.getExtension().iterator();
  while (i.hasNext()) {
    Extension e = i.next(); // must be called before you can call i.remove()
    if (e.getUrl().equals(url)) {
      i.remove();
    }
  }
}
 
Example #12
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static int readIntegerExtension(DomainResource dr, String uri, int defaultValue) {
  Extension ex = ExtensionHelper.getExtension(dr, uri);
  if (ex == null)
    return defaultValue;
  if (ex.getValue() instanceof IntegerType)
    return ((IntegerType) ex.getValue()).getValue();
  throw new Error("Unable to read extension "+uri+" as an integer");
}
 
Example #13
Source File: CqlExecutionProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public Object evaluateInContext(DomainResource instance, String cql, String patientId, Boolean aliasedExpression) {
    Iterable<CanonicalType> libraries = getLibraryReferences(instance);
    if (aliasedExpression) {
        Object result = null;
        for (CanonicalType reference : libraries) {
            Library lib =this.libraryResourceProvider.resolveLibraryById(CanonicalHelper.getId(reference));
            if (lib == null)
            {
                throw new RuntimeException("Library with id " + reference.getIdBase() + "not found");
            }
            LibraryLoader libraryLoader = LibraryHelper.createLibraryLoader(this.getLibraryResourceProvider());
            // resolve primary library
            org.cqframework.cql.elm.execution.Library library = LibraryHelper.resolveLibraryById(lib.getId(), libraryLoader, this.libraryResourceProvider);

            // resolve execution context
            Context context = setupContext(instance, patientId, libraryLoader, library);
            result = context.resolveExpressionRef(cql).evaluate(context);
            if (result != null) {
                return result;
            }
        }
        throw new RuntimeException("Could not find Expression in Referenced Libraries");
    }
    else {
        return evaluateInContext(instance, cql, patientId);
    }
}
 
Example #14
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void setStandardsStatus(DomainResource dr, StandardsStatus status, String normativeVersion) {
  if (status == null)
    ToolingExtensions.removeExtension(dr, ToolingExtensions.EXT_STANDARDS_STATUS);
  else
    ToolingExtensions.setCodeExtension(dr, ToolingExtensions.EXT_STANDARDS_STATUS, status.toCode());
  if (normativeVersion == null)
    ToolingExtensions.removeExtension(dr, ToolingExtensions.EXT_NORMATIVE_VERSION);
  else
    ToolingExtensions.setCodeExtension(dr, ToolingExtensions.EXT_NORMATIVE_VERSION, normativeVersion);
}
 
Example #15
Source File: ResourceRoundTripTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@Test
@Disabled
public void test() throws FileNotFoundException, IOException, FHIRException, EOperationOutcome, UcumException {
  Resource res = new XmlParser().parse(new FileInputStream(TestingUtilities.resourceNameToFile("unicode.xml")));
  new NarrativeGenerator("", "", TestingUtilities.context()).generate((DomainResource) res, null);
  new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.resourceNameToFile("gen", "unicode.out.xml")), res);
}
 
Example #16
Source File: NarrativeGeneratorTests.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private void process(String path) throws FileNotFoundException, IOException, XmlPullParserException, EOperationOutcome, FHIRException {
  XmlParser p = new XmlParser();
  DomainResource r = (DomainResource) p.parse(new FileInputStream(path));
  gen.generate(r, null);
  FileOutputStream s = new FileOutputStream(TestingUtilities.resourceNameToFile("gen", "gen.xml"));
  new XmlParser().compose(s, r, true);
  s.close();

}
 
Example #17
Source File: CareConnectProfileDbValidationSupportR4.java    From careconnect-reference-implementation with Apache License 2.0 5 votes vote down vote up
private DomainResource fetchCodeSystemOrValueSet(FhirContext theContext, String theSystem, boolean codeSystem) {
  synchronized (this) {
    logW("******* CareConnect fetchCodeSystemOrValueSet: system="+theSystem);

    Map<String, IBaseResource> codeSystems = cachedResource;

    return null;
  }
}
 
Example #18
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static boolean findBooleanExtension(DomainResource c, String uri) {
  Extension ex = ExtensionHelper.getExtension(c, uri);
  if (ex == null)
    return false;
  if (!(ex.getValue() instanceof BooleanType))
    return false;
  return true;
}
 
Example #19
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static Boolean readBooleanExtension(DomainResource c, String uri) {
  Extension ex = ExtensionHelper.getExtension(c, uri);
  if (ex == null)
    return null;
  if (!(ex.getValue() instanceof BooleanType))
    return null;
  return ((BooleanType) ex.getValue()).getValue();
}
 
Example #20
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static PrimitiveType<Type> readPrimitiveExtension(DomainResource c, String uri) {
  Extension ex = getExtension(c, uri);
  if (ex == null)
    return null;
  return (PrimitiveType<Type>) ex.getValue();
}
 
Example #21
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void addCodeExtension(DomainResource dr, String url, String value) {
  Extension ex = getExtension(dr, url);
  if (ex != null)
    ex.setValue(new CodeType(value));
  else
    dr.getExtension().add(Factory.newExtension(url, new CodeType(value), true));   
}
 
Example #22
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void addIntegerExtension(DomainResource dr, String url, int value) {
  Extension ex = getExtension(dr, url);
  if (ex != null)
    ex.setValue(new IntegerType(value));
  else
    dr.getExtension().add(Factory.newExtension(url, new IntegerType(value), true));   
}
 
Example #23
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void addBooleanExtension(DomainResource e, String url, boolean content) {
  Extension ex = getExtension(e, url);
  if (ex != null)
    ex.setValue(new BooleanType(content));
  else
    e.getExtension().add(Factory.newExtension(url, new BooleanType(content), true));   
}
 
Example #24
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void addStringExtension(DomainResource e, String url, String content) {
  if (!StringUtils.isBlank(content)) {
    Extension ex = getExtension(e, url);
    if (ex != null)
      ex.setValue(new StringType(content));
    else
      e.getExtension().add(Factory.newExtension(url, new StringType(content), true));   
  }
}
 
Example #25
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static void addMarkdownExtension(DomainResource dr, String url, String content) {
  if (!StringUtils.isBlank(content)) {
    Extension ex = getExtension(dr, url);
    if (ex != null)
      ex.setValue(new StringType(content));
    else
      dr.getExtension().add(Factory.newExtension(url, new MarkdownType(content), true));   
  }
}
 
Example #26
Source File: CqlExecutionProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
private Context setupContext(DomainResource instance, String patientId, LibraryLoader libraryLoader,
        org.cqframework.cql.elm.execution.Library library) {
    // Provide the instance as the value of the '%context' parameter, as well as the
    // value of a parameter named the same as the resource
    // This enables expressions to access the resource by root, as well as through
    // the %context attribute
    Context context = new Context(library);
    context.setParameter(null, instance.fhirType(), instance);
    context.setParameter(null, "%context", instance);
    context.setExpressionCaching(true);
    context.registerLibraryLoader(libraryLoader);
    context.setContextValue("Patient", patientId);
    context.registerDataProvider("http://hl7.org/fhir", this.providerFactory.createDataProvider("FHIR", "4.0.0"));
    return context;
}
 
Example #27
Source File: ShExGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private ValueSet resolveBindingReference(DomainResource ctxt, String reference) {
  try {
    return context.fetchResource(ValueSet.class, reference);
  } catch (Throwable e) {
    return null;
  }
}
 
Example #28
Source File: PlanDefinitionApplyProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public Resource resolveContained(DomainResource resource, String id) {
    for (Resource res : resource.getContained()) {
        if (res.hasIdElement()) {
            if (res.getIdElement().getIdPart().equals(id)) {
                return res;
            }
        }
    }

    throw new RuntimeException(String.format("Resource %s does not contain resource with id %s", resource.fhirType(), id));
}
 
Example #29
Source File: XmlParserBase.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
protected DomainResource parseDomainResourceContained(XmlPullParser xpp)  throws IOException, FHIRFormatError, XmlPullParserException {
	next(xpp);
	int eventType = nextNoWhitespace(xpp);
	if (eventType == XmlPullParser.START_TAG) { 
		DomainResource dr = (DomainResource) parseResource(xpp);
		nextNoWhitespace(xpp);
		next(xpp);
		return dr;
	} else {
		unknownContent(xpp);
		return null;
	}
}
 
Example #30
Source File: IdentifiableDomainResourceAttributeMapper.java    From elexis-3-core with Eclipse Public License 1.0 4 votes vote down vote up
default void mapNarrative(Identifiable source, DomainResource target) {
	Narrative narrative = new Narrative();
	narrative.setStatus(NarrativeStatus.GENERATED);
	narrative.setDivAsString(source.getLabel());
	target.setText(narrative);
}