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

The following examples show how to use org.hl7.fhir.r4.model.CanonicalType. 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: QuestionnaireBuilder.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
private void addReferenceQuestions(QuestionnaireItemComponent group, ElementDefinition element, String path, List<CanonicalType> profileURL, List<QuestionnaireResponse.QuestionnaireResponseItemComponent> answerGroups) throws FHIRException {
  //  var
  //    rn : String;
  //    i : integer;
  //    q : TFhirQuestionnaireGroupQuestion;
  ToolingExtensions.addFhirType(group, "Reference");

  QuestionnaireItemComponent q = addQuestion(group, QuestionnaireItemType.REFERENCE, path, "value", group.getText(), answerGroups);
  group.setText(null);
  CommaSeparatedStringBuilder rn = new CommaSeparatedStringBuilder();
  for (UriType u : profileURL)
  if (u.getValue().startsWith("http://hl7.org/fhir/StructureDefinition/"))
    rn.append(u.getValue().substring(40));
  if (rn.length() == 0)
    ToolingExtensions.addReferenceFilter(q, "subject=$subj&patient=$subj&encounter=$encounter");
  else {
    ToolingExtensions.addAllowedResource(q, rn.toString());
    ToolingExtensions.addReferenceFilter(q, "subject=$subj&patient=$subj&encounter=$encounter");
  }
  for (QuestionnaireResponse.QuestionnaireResponseItemComponent ag : answerGroups)
    ag.setText(null);
}
 
Example #2
Source File: ToolingExtensions.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public static String readStringExtension(Element c, String uri) {
  Extension ex = ExtensionHelper.getExtension(c, uri);
  if (ex == null)
    return null;
  if (ex.getValue() instanceof UriType)
    return ((UriType) ex.getValue()).getValue();
  if (ex.getValue() instanceof CanonicalType)
    return ((CanonicalType) 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();
  if (!(ex.getValue() instanceof StringType))
    return null;
  return ((StringType) ex.getValue()).getValue();
}
 
Example #3
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 #4
Source File: ImplementationGuide14_40.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
public static org.hl7.fhir.r4.model.ImplementationGuide.ImplementationGuideDefinitionResourceComponent convertImplementationGuidePackageResourceComponent(org.hl7.fhir.dstu2016may.model.ImplementationGuide.ImplementationGuidePackageResourceComponent src) throws FHIRException {
    if (src == null || src.isEmpty())
        return null;
    org.hl7.fhir.r4.model.ImplementationGuide.ImplementationGuideDefinitionResourceComponent tgt = new org.hl7.fhir.r4.model.ImplementationGuide.ImplementationGuideDefinitionResourceComponent();
    VersionConvertor_14_40.copyElement(src, tgt);
    if (src.hasExampleFor()) {
        Type t = VersionConvertor_14_40.convertType(src.getExampleFor());
        tgt.setExample(t instanceof org.hl7.fhir.r4.model.Reference ? new CanonicalType(((org.hl7.fhir.r4.model.Reference) t).getReference()) : t);
    } else if (src.hasExample())
        tgt.setExample(new org.hl7.fhir.r4.model.BooleanType(src.getExample()));
    if (src.hasName())
        tgt.setNameElement(VersionConvertor_14_40.convertString(src.getNameElement()));
    if (src.hasDescription())
        tgt.setDescriptionElement(VersionConvertor_14_40.convertString(src.getDescriptionElement()));
    if (src.hasSourceReference())
        tgt.setReference(VersionConvertor_14_40.convertReference(src.getSourceReference()));
    else if (src.hasSourceUriType())
        tgt.setReference(new org.hl7.fhir.r4.model.Reference(src.getSourceUriType().getValue()));
    return tgt;
}
 
Example #5
Source File: ValueSetUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static ValueSet makeShareable(ValueSet vs) {
  if (!vs.hasMeta())
    vs.setMeta(new Meta());
  for (UriType t : vs.getMeta().getProfile()) 
    if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablevalueset"))
      return vs;
  vs.getMeta().getProfile().add(new CanonicalType("http://hl7.org/fhir/StructureDefinition/shareablevalueset"));
  return vs;
}
 
Example #6
Source File: CodeSystemUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static CodeSystem makeShareable(CodeSystem cs) {
  if (!cs.hasMeta())
    cs.setMeta(new Meta());
  for (UriType t : cs.getMeta().getProfile()) 
    if (t.getValue().equals("http://hl7.org/fhir/StructureDefinition/shareablecodesystem"))
      return cs;
  cs.getMeta().getProfile().add(new CanonicalType("http://hl7.org/fhir/StructureDefinition/shareablecodesystem"));
  return cs;
}
 
Example #7
Source File: XLSXWriter.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private String canonicalList(List<CanonicalType> list) {
  CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder("|");
  for (CanonicalType c : list) {
    String v = c.getValue();
    if (v.startsWith("http://hl7.org/fhir/StructureDefinition/"))
      v = v.substring(40);
    b.append(v);
  }
  return b.toString();
}
 
Example #8
Source File: VersionConvertor_14_40.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static org.hl7.fhir.dstu2016may.model.Extension convertExtension(org.hl7.fhir.r4.model.Extension src) throws FHIRException {
    if (src == null || src.isEmpty())
        return null;
    org.hl7.fhir.dstu2016may.model.Extension tgt = new org.hl7.fhir.dstu2016may.model.Extension();
    copyElement(src, tgt);
    if (src.hasUrlElement())
        tgt.setUrlElement(convertUri(src.getUrlElement()));
    if (src.hasValue())
        if (CANONICAL_URLS.contains(src.getUrl()) && src.getValue() instanceof org.hl7.fhir.r4.model.CanonicalType)
            tgt.setValue(convertCanonicalToReference((CanonicalType) src.getValue()));
        else
            tgt.setValue(convertType(src.getValue()));
    return tgt;
}
 
Example #9
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 #10
Source File: CanonicalHelper.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public static String getId(CanonicalType canonical) {
    if (canonical.hasValue()) {
        String id = canonical.getValue();
        String temp = id.contains("/") ? id.substring(id.lastIndexOf("/") + 1) : id;
        return temp.split("\\|")[0];
    }

    throw new RuntimeException("CanonicalType must have a value for id extraction");
}
 
Example #11
Source File: CanonicalHelper.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
public static String getResourceName(CanonicalType canonical) {
    if (canonical.hasValue()) {
        String id = canonical.getValue();
        if (id.contains("/")) {
            id = id.replace(id.substring(id.lastIndexOf("/")), "");
            return id.contains("/") ? id.substring(id.lastIndexOf("/") + 1) : id;
        }
        return null;
    }

    throw new RuntimeException("CanonicalType must have a value for resource name extraction");
}
 
Example #12
Source File: CqlExecutionProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
private String buildIncludes(Iterable<CanonicalType> references) {
    StringBuilder builder = new StringBuilder();
    for (CanonicalType reference : references) {

        if (builder.length() > 0) {
            builder.append(" ");
        }

        builder.append("include ");

        // TODO: This assumes the libraries resource id is the same as the library name,
        // need to work this out better
        Library lib =this.libraryResourceProvider.resolveLibraryById(CanonicalHelper.getId(reference));
        if (lib.hasName()) {
            builder.append(lib.getName());
        }
        else {
            throw new RuntimeException("Library name unknown");
        }

        if (reference.hasValue() && reference.getValue().split("\\|").length > 1) {
            builder.append(" version '");
            builder.append(reference.getValue().split("\\|")[1]);
            builder.append("'");
        }

        builder.append(" called ");
        builder.append(lib.getName());
    }

    return builder.toString();
}
 
Example #13
Source File: FhirChCrlDocumentBundle.java    From elexis-3-core with Eclipse Public License 1.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void createBundle(){
	try {
		Date now = new Date();
		
		this.bundle = new Bundle();
		bundle.setId("BundleFromPractitioner");
		bundle.setMeta(new Meta().setLastUpdated(now).setProfile(Collections.singletonList(
			new CanonicalType("http://fhir.ch/ig/ch-crl/StructureDefinition/ch-crl-bundle"))));
		bundle.setType(BundleType.DOCUMENT);
		
		BundleEntryComponent compositionEntry = bundle.addEntry();
		Composition composition = new Composition();
		compositionEntry.setResource(composition);
		composition.setId("CompFromPractitioner");
		composition.setMeta(new Meta().setLastUpdated(now)
			.setProfile(Collections.singletonList(new CanonicalType(
				"http://fhir.ch/ig/ch-crl/StructureDefinition/ch-crl-composition"))));
		composition.setStatus(CompositionStatus.FINAL);
		composition.setType(new CodeableConcept(
			new Coding("http://loinc.org", "72134-0", "Cancer event report")));
		composition.setDate(now);
		composition.setTitle("Report to the Cancer Registry");
		
		BundleEntryComponent subjectEntry = bundle.addEntry();
		IFhirTransformer<Patient, IPatient> patientTransformer =
			(IFhirTransformer<Patient, IPatient>) FhirTransformersHolder
				.getTransformerFor(Patient.class, IPatient.class);
		Patient subject = patientTransformer.getFhirObject(patient)
			.orElseThrow(() -> new IllegalStateException("Could not create subject"));
		subject.getExtension().clear();
		fixAhvIdentifier(subject);
		
		subjectEntry.setResource(subject);
		
		BundleEntryComponent practitionerEntry = bundle.addEntry();
		IFhirTransformer<Practitioner, IMandator> practitionerTransformer =
			(IFhirTransformer<Practitioner, IMandator>) FhirTransformersHolder
				.getTransformerFor(Practitioner.class, IMandator.class);
		Practitioner practitioner = practitionerTransformer.getFhirObject(author)
			.orElseThrow(() -> new IllegalStateException("Could not create autor"));
		practitioner.getExtension().clear();
		practitioner.getIdentifier().clear();
		practitionerEntry.setResource(practitioner);
		
		BundleEntryComponent documentReferenceEntry = bundle.addEntry();
		DocumentReference documentReference = new DocumentReference();
		documentReferenceEntry.setResource(documentReference);
		documentReference.setId(document.getId());
		DocumentReferenceContentComponent content = documentReference.addContent();
		content.setAttachment(new Attachment().setContentType("application/pdf")
			.setData(IOUtils.toByteArray(document.getContent())));
		
		composition.setSubject(new Reference(subject));
		composition.setAuthor(Collections.singletonList(new Reference(practitioner)));
		SectionComponent section = composition.addSection();
		section.addEntry(new Reference(documentReference));
	} catch (IOException e) {
		LoggerFactory.getLogger(getClass()).error("Error creating FHIR bundle", e);
		throw new IllegalStateException("Error creating FHIR bundle", e);
	}
}
 
Example #14
Source File: CqlExecutionProvider.java    From cqf-ruler with Apache License 2.0 4 votes vote down vote up
private Iterable<CanonicalType> getLibraryReferences(DomainResource instance) {
    List<CanonicalType> references = new ArrayList<>();

    if (instance.hasContained()) {
        for (Resource resource : instance.getContained()) {
            if (resource instanceof Library) {
                resource.setId(resource.getIdElement().getIdPart().replace("#", ""));
                getLibraryResourceProvider().update((Library) resource);
                // getLibraryLoader().putLibrary(resource.getIdElement().getIdPart(),
                // getLibraryLoader().toElmLibrary((Library) resource));
            }
        }
    }

    if (instance instanceof ActivityDefinition) {
        references.addAll(((ActivityDefinition) instance).getLibrary());
    }

    else if (instance instanceof PlanDefinition) {
        references.addAll(((PlanDefinition) instance).getLibrary());
    }

    else if (instance instanceof Measure) {
        references.addAll(((Measure) instance).getLibrary());
    }

    for (Extension extension : instance
            .getExtensionsByUrl("http://hl7.org/fhir/StructureDefinition/cqif-library")) {
        Type value = extension.getValue();

        if (value instanceof CanonicalType) {
            references.add((CanonicalType) value);
        }

        else {
            throw new RuntimeException("Library extension does not have a value of type reference");
        }
    }

    return cleanReferences(references);
}
 
Example #15
Source File: VersionConvertor_14_40.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
static public Reference convertCanonicalToReference(CanonicalType src) throws FHIRException {
    Reference dst = new Reference(src.getValue());
    copyElement(src, dst);
    return dst;
}
 
Example #16
Source File: VersionConvertor_14_40.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
static public CanonicalType convertReferenceToCanonical(Reference src) throws FHIRException {
    CanonicalType dst = new CanonicalType(src.getReference());
    copyElement(src, dst);
    return dst;
}
 
Example #17
Source File: StructureMapUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private boolean profilesMatch(List<String> profiles, List<CanonicalType> profile) {
  return profiles == null || profiles.size() == 0 || profile.size() == 0 || (profiles.size() == 1 && profiles.get(0).equals(profile.get(0).getValue()));
}