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;
}
}
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;
}
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);
}
Aggregations