Search in sources :

Example 1 with IVersionSpecificBundleFactory

use of ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory in project cqf-ruler by DBCG.

the class ProcessMessageProvider method processMessageBundle.

@Operation(name = "$process-message-bundle", idempotent = false)
public Bundle processMessageBundle(HttpServletRequest theServletRequest, RequestDetails theRequestDetails, @OperationParam(name = "content", min = 1, max = 1) @Description(formalDefinition = "The message to process (or, if using asynchronous messaging, it may be a response message to accept)") Bundle theMessageToProcess) {
    logger.info("Validating the Bundle");
    Bundle bundle = theMessageToProcess;
    Boolean errorExists = false;
    OperationOutcome outcome = validateBundle(errorExists, bundle);
    if (!errorExists) {
        IVersionSpecificBundleFactory bundleFactory = this.getFhirContext().newBundleFactory();
        bundle.setId(getUUID());
        bundleFactory.initializeWithBundleResource(bundle);
        Bundle dafBundle = (Bundle) bundleFactory.getResourceBundle();
        dafBundle.setTimestamp(new Date());
        this.getDaoRegistry().getResourceDao(Bundle.class).create(dafBundle);
        MessageHeader messageHeader = null;
        String patientId = null;
        String commId = null;
        List<MessageHeader> headers = BundleUtil.toListOfResourcesOfType(this.getFhirContext(), bundle, MessageHeader.class);
        for (MessageHeader mh : headers) {
            messageHeader = mh;
            messageHeader.setId(getUUID());
            Meta meta = messageHeader.getMeta();
            meta.setLastUpdated(new Date());
            messageHeader.setMeta(meta);
        }
        List<IBaseResource> resources = new ArrayList<>();
        List<Patient> patients = BundleUtil.toListOfResourcesOfType(this.getFhirContext(), bundle, Patient.class);
        for (Patient p : patients) {
            patientId = p.getId();
            p.setId(p.getIdElement().toVersionless());
            resources.add(p);
        }
        List<Bundle> bundles = BundleUtil.toListOfResourcesOfType(this.getFhirContext(), bundle, Bundle.class);
        for (Bundle b : bundles) {
            patientId = this.processBundle(b, theRequestDetails);
            b.setId(b.getIdElement().toVersionless());
            resources.add(b);
        }
        for (BundleEntryComponent e : bundle.getEntry()) {
            Resource r = e.getResource();
            if (r == null) {
                continue;
            }
            if (r.fhirType().equals("Bundle") || r.fhirType().equals("MessageHeader") || r.fhirType().equals("Patient")) {
                continue;
            }
            r.setId(r.getIdElement().toVersionless());
            resources.add(r);
        }
        if (patientId != null) {
            commId = constructAndSaveCommunication(patientId);
        }
        if (messageHeader == null) {
            messageHeader = constructMessageHeaderResource();
            BundleEntryComponent entryComp = new BundleEntryComponent();
            entryComp.setResource(messageHeader);
            dafBundle.addEntry(entryComp);
        }
        if (commId != null) {
            List<Reference> referenceList = new ArrayList<>();
            Reference commRef = new Reference();
            commRef.setReference("Communication/" + commId);
            referenceList.add(commRef);
            messageHeader.setFocus(referenceList);
        }
        IVersionSpecificBundleFactory newBundleFactory = this.getFhirContext().newBundleFactory();
        newBundleFactory.addResourcesToBundle(resources, BundleTypeEnum.TRANSACTION, theRequestDetails.getFhirServerBase(), null, null);
        Bundle transactionBundle = (Bundle) newBundleFactory.getResourceBundle();
        for (BundleEntryComponent entry : transactionBundle.getEntry()) {
            UriType uri = new UriType(theRequestDetails.getFhirServerBase() + "/" + entry.getResource().fhirType() + "/" + entry.getResource().getIdElement().getIdPart());
            Enumeration<HTTPVerb> method = new Enumeration<>(new HTTPVerbEnumFactory());
            method.setValue(HTTPVerb.PUT);
            entry.setRequest(new BundleEntryRequestComponent(method, uri));
        }
        @SuppressWarnings("unchecked") IFhirSystemDao<Bundle, Meta> fhirSystemDao = this.getDaoRegistry().getSystemDao();
        fhirSystemDao.transaction(theRequestDetails, transactionBundle);
        return dafBundle;
    } else {
        BundleEntryComponent entryComp = new BundleEntryComponent();
        entryComp.setResource(outcome);
        bundle.addEntry(entryComp);
        return bundle;
    }
}
Also used : HTTPVerb(org.hl7.fhir.r4.model.Bundle.HTTPVerb) Meta(org.hl7.fhir.r4.model.Meta) ArrayList(java.util.ArrayList) UriType(org.hl7.fhir.r4.model.UriType) BundleEntryRequestComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent) OperationOutcome(org.hl7.fhir.r4.model.OperationOutcome) Enumeration(org.hl7.fhir.r4.model.Enumeration) Bundle(org.hl7.fhir.r4.model.Bundle) Reference(org.hl7.fhir.r4.model.Reference) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) IVersionSpecificBundleFactory(ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory) Patient(org.hl7.fhir.r4.model.Patient) HTTPVerbEnumFactory(org.hl7.fhir.r4.model.Bundle.HTTPVerbEnumFactory) Date(java.util.Date) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) MessageHeader(org.hl7.fhir.r4.model.MessageHeader) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 2 with IVersionSpecificBundleFactory

use of ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory in project cqf-ruler by DBCG.

the class MeasureDataProcessProvider method extractLineListData.

@Operation(name = "$extract-line-list-data", idempotent = true, type = MeasureReport.class)
public Bundle extractLineListData(RequestDetails details, @OperationParam(name = "measureReport", min = 0, max = 1, type = MeasureReport.class) MeasureReport measureReport, @OperationParam(name = "subjectList") List<String> subjectList) {
    IVersionSpecificBundleFactory bundleFactory = measureResourceProvider.getContext().newBundleFactory();
    Map<String, Reference> populationSubjectListReferenceMap = new HashMap<>();
    gatherSubjectList(measureReport, subjectList, populationSubjectListReferenceMap);
    gatherEicrs(bundleFactory, populationSubjectListReferenceMap);
    Bundle bundle = (Bundle) bundleFactory.getResourceBundle();
    if (bundle != null && bundle.hasEntry() && bundle.getEntryFirstRep().getResource() instanceof Bundle) {
        return (Bundle) bundle.getEntryFirstRep().getResource();
    }
    return bundle;
}
Also used : HashMap(java.util.HashMap) Reference(org.hl7.fhir.r4.model.Reference) Bundle(org.hl7.fhir.r4.model.Bundle) IVersionSpecificBundleFactory(ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory) Operation(ca.uhn.fhir.rest.annotation.Operation)

Example 3 with IVersionSpecificBundleFactory

use of ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory in project cqf-ruler by DBCG.

the class MeasureDataProcessProvider method gatherEicrs.

private void gatherEicrs(IVersionSpecificBundleFactory bundleFactory, Map<String, Reference> populationSubjectListReferenceMap) {
    Map<String, Bundle> eicrs = new HashMap<>();
    List<Bundle> documentBundles = search(Bundle.class, Searches.all()).getAllResourcesTyped().stream().filter(x -> x.hasEntry() && DOCUMENT.equals(x.getType())).collect(Collectors.toList());
    for (Bundle bundle : documentBundles) {
        IBaseResource firstResource = bundle.getEntryFirstRep().getResource();
        if (!(firstResource instanceof Composition)) {
            logger.debug("Any bundle of type document must have the first entry of type Composition, but found: {}", firstResource);
            continue;
        }
        Composition composition = (Composition) firstResource;
        Reference compositionSubject = composition.getSubject();
        String[] referenceSplit = compositionSubject.getReference().split("/");
        for (Map.Entry<String, Reference> entry : populationSubjectListReferenceMap.entrySet()) {
            if (compositionSubject.equals(entry.getValue()) || compositionSubject.getReference().equals(entry.getKey()) || (referenceSplit.length > 1 && referenceSplit[1].equals(entry.getKey()))) {
                eicrs.putIfAbsent(entry.getKey(), bundle);
            }
        }
    }
    bundleFactory.addResourcesToBundle(eicrs.values().stream().collect(Collectors.toList()), COLLECTION, null, null, null);
}
Also used : DaoRegistryOperationProvider(org.opencds.cqf.ruler.provider.DaoRegistryOperationProvider) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Operation(ca.uhn.fhir.rest.annotation.Operation) Reference(org.hl7.fhir.r4.model.Reference) ArrayList(java.util.ArrayList) RequestDetails(ca.uhn.fhir.rest.api.server.RequestDetails) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) MeasureResourceProvider(ca.uhn.fhir.jpa.rp.r4.MeasureResourceProvider) Map(java.util.Map) OperationParam(ca.uhn.fhir.rest.annotation.OperationParam) Group(org.hl7.fhir.r4.model.Group) Logger(org.slf4j.Logger) IVersionSpecificBundleFactory(ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory) COLLECTION(ca.uhn.fhir.model.valueset.BundleTypeEnum.COLLECTION) DOCUMENT(org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT) ListResource(org.hl7.fhir.r4.model.ListResource) Composition(org.hl7.fhir.r4.model.Composition) Collectors(java.util.stream.Collectors) IdType(org.hl7.fhir.r4.model.IdType) Searches(org.opencds.cqf.ruler.utility.Searches) ResourceType(org.hl7.fhir.r4.model.ResourceType) List(java.util.List) MeasureReport(org.hl7.fhir.r4.model.MeasureReport) Bundle(org.hl7.fhir.r4.model.Bundle) Composition(org.hl7.fhir.r4.model.Composition) HashMap(java.util.HashMap) Bundle(org.hl7.fhir.r4.model.Bundle) Reference(org.hl7.fhir.r4.model.Reference) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Operation (ca.uhn.fhir.rest.annotation.Operation)3 IVersionSpecificBundleFactory (ca.uhn.fhir.rest.api.IVersionSpecificBundleFactory)3 Bundle (org.hl7.fhir.r4.model.Bundle)3 Reference (org.hl7.fhir.r4.model.Reference)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)2 MeasureResourceProvider (ca.uhn.fhir.jpa.rp.r4.MeasureResourceProvider)1 COLLECTION (ca.uhn.fhir.model.valueset.BundleTypeEnum.COLLECTION)1 OperationParam (ca.uhn.fhir.rest.annotation.OperationParam)1 RequestDetails (ca.uhn.fhir.rest.api.server.RequestDetails)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)1 BundleEntryRequestComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryRequestComponent)1 DOCUMENT (org.hl7.fhir.r4.model.Bundle.BundleType.DOCUMENT)1 HTTPVerb (org.hl7.fhir.r4.model.Bundle.HTTPVerb)1 HTTPVerbEnumFactory (org.hl7.fhir.r4.model.Bundle.HTTPVerbEnumFactory)1