use of org.hl7.fhir.r4.model.Bundle.HTTPVerbEnumFactory 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;
}
}
Aggregations