|
| 1 | +package de.netzwerk_universitaetsmedizin.codex.processes.data_transfer.listener; |
| 2 | + |
| 3 | +import static de.netzwerk_universitaetsmedizin.codex.processes.data_transfer.ConstantsDataTransfer.CODESYSTEM_NUM_CODEX_DATA_TRANSFER; |
| 4 | +import static de.netzwerk_universitaetsmedizin.codex.processes.data_transfer.ConstantsDataTransfer.CODESYSTEM_NUM_CODEX_DATA_TRANSFER_VALUE_LOCAL_VALIDATION_SUCCESSFUL; |
| 5 | + |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.text.SimpleDateFormat; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +import javax.activation.DataHandler; |
| 11 | +import javax.mail.internet.MimeBodyPart; |
| 12 | +import javax.mail.internet.MimeMultipart; |
| 13 | +import javax.mail.util.ByteArrayDataSource; |
| 14 | + |
| 15 | +import org.camunda.bpm.engine.delegate.DelegateExecution; |
| 16 | +import org.camunda.bpm.engine.delegate.ExecutionListener; |
| 17 | +import org.highmed.dsf.bpe.service.MailService; |
| 18 | +import org.highmed.dsf.fhir.client.FhirWebserviceClientProvider; |
| 19 | +import org.highmed.dsf.fhir.task.TaskHelper; |
| 20 | +import org.hl7.fhir.r4.model.BooleanType; |
| 21 | +import org.hl7.fhir.r4.model.Task; |
| 22 | +import org.hl7.fhir.r4.model.Task.TaskOutputComponent; |
| 23 | +import org.hl7.fhir.r4.model.Task.TaskStatus; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.beans.factory.InitializingBean; |
| 27 | + |
| 28 | +import ca.uhn.fhir.context.FhirContext; |
| 29 | +import de.netzwerk_universitaetsmedizin.codex.processes.data_transfer.DataTransferProcessPluginDefinition; |
| 30 | + |
| 31 | +public class AfterDryRunEndListener implements ExecutionListener, InitializingBean |
| 32 | +{ |
| 33 | + private static final Logger logger = LoggerFactory.getLogger(AfterDryRunEndListener.class); |
| 34 | + |
| 35 | + private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 36 | + |
| 37 | + private final TaskHelper taskHelper; |
| 38 | + private final FhirWebserviceClientProvider clientProvider; |
| 39 | + private final FhirContext fhirContext; |
| 40 | + private final MailService mailService; |
| 41 | + |
| 42 | + private final String localOrganizationIdentifierValue; |
| 43 | + private final boolean sendDryRunSuccessMail; |
| 44 | + |
| 45 | + public AfterDryRunEndListener(TaskHelper taskHelper, FhirWebserviceClientProvider clientProvider, |
| 46 | + FhirContext fhirContext, MailService mailService, String localOrganizationIdentifierValue, |
| 47 | + boolean sendDryRunSuccessMail) |
| 48 | + { |
| 49 | + this.taskHelper = taskHelper; |
| 50 | + this.clientProvider = clientProvider; |
| 51 | + this.fhirContext = fhirContext; |
| 52 | + this.mailService = mailService; |
| 53 | + |
| 54 | + this.localOrganizationIdentifierValue = localOrganizationIdentifierValue; |
| 55 | + this.sendDryRunSuccessMail = sendDryRunSuccessMail; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void afterPropertiesSet() throws Exception |
| 60 | + { |
| 61 | + Objects.requireNonNull(taskHelper, "taskHelper"); |
| 62 | + Objects.requireNonNull(clientProvider, "clientProvider"); |
| 63 | + Objects.requireNonNull(fhirContext, "fhirContext"); |
| 64 | + Objects.requireNonNull(mailService, "mailService"); |
| 65 | + |
| 66 | + Objects.requireNonNull(localOrganizationIdentifierValue, "localOrganizationIdentifierValue"); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void notify(DelegateExecution execution) throws Exception |
| 71 | + { |
| 72 | + if (!sendDryRunSuccessMail) |
| 73 | + return; |
| 74 | + |
| 75 | + Task task = taskHelper.getLeadingTaskFromExecutionVariables(execution); |
| 76 | + Task finalTask = clientProvider.getLocalWebserviceClient().read(Task.class, task.getIdElement().getIdPart()); |
| 77 | + |
| 78 | + if (!TaskStatus.COMPLETED.equals(finalTask.getStatus())) |
| 79 | + { |
| 80 | + logger.warn("Final Task from DSF FHIR server not in status {} but {}, not sending dry-run success mail", |
| 81 | + TaskStatus.COMPLETED, finalTask.getStatus()); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (!isLocalValidationSuccessful(finalTask)) |
| 86 | + { |
| 87 | + logger.warn( |
| 88 | + "Final Task from DSF FHIR server missing '{}' output parameter with value 'true', not sending dry-run success mail", |
| 89 | + CODESYSTEM_NUM_CODEX_DATA_TRANSFER_VALUE_LOCAL_VALIDATION_SUCCESSFUL); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + String finalTaskAsXml = fhirContext.newXmlParser().setPrettyPrint(true).encodeResourceToString(finalTask); |
| 94 | + String attachmentFilename = finalTask.getIdElement().getIdPart() + ".xml"; |
| 95 | + |
| 96 | + MimeBodyPart text = new MimeBodyPart(); |
| 97 | + text.setText(createMesssage(finalTask, attachmentFilename)); |
| 98 | + |
| 99 | + MimeBodyPart attachment = new MimeBodyPart(); |
| 100 | + attachment.setFileName(attachmentFilename); |
| 101 | + attachment.setDataHandler(new DataHandler( |
| 102 | + new ByteArrayDataSource(finalTaskAsXml.getBytes(StandardCharsets.UTF_8), "application/xml"))); |
| 103 | + |
| 104 | + MimeMultipart body = new MimeMultipart(); |
| 105 | + body.addBodyPart(text); |
| 106 | + body.addBodyPart(attachment); |
| 107 | + |
| 108 | + MimeBodyPart message = new MimeBodyPart(); |
| 109 | + message.setContent(body); |
| 110 | + |
| 111 | + mailService.send("Dry-Run Success: " + localOrganizationIdentifierValue, message); |
| 112 | + } |
| 113 | + |
| 114 | + private boolean isLocalValidationSuccessful(Task task) |
| 115 | + { |
| 116 | + return task.getOutput().stream().filter(TaskOutputComponent::hasType).filter(o -> o.getType().hasCoding()) |
| 117 | + .filter(o -> o.getType().getCoding().stream() |
| 118 | + .anyMatch(c -> CODESYSTEM_NUM_CODEX_DATA_TRANSFER.equals(c.getSystem()) |
| 119 | + && CODESYSTEM_NUM_CODEX_DATA_TRANSFER_VALUE_LOCAL_VALIDATION_SUCCESSFUL |
| 120 | + .equals(c.getCode()))) |
| 121 | + .filter(TaskOutputComponent::hasValue).filter(o -> o.getValue() instanceof BooleanType) |
| 122 | + .map(o -> (BooleanType) o.getValue()).anyMatch(b -> Boolean.TRUE.equals(b.getValue())); |
| 123 | + } |
| 124 | + |
| 125 | + private String createMesssage(Task finalTask, String attachmentFileName) |
| 126 | + { |
| 127 | + StringBuilder b = new StringBuilder(); |
| 128 | + |
| 129 | + b.append("Send process version "); |
| 130 | + b.append(DataTransferProcessPluginDefinition.VERSION); |
| 131 | + b.append(" dry-run at organization with identifier '"); |
| 132 | + b.append(localOrganizationIdentifierValue); |
| 133 | + b.append("' successfully completed on "); |
| 134 | + b.append(DATE_FORMAT.format(finalTask.getMeta().getLastUpdated())); |
| 135 | + b.append(".\n\nTask resource is attached as '"); |
| 136 | + b.append(attachmentFileName); |
| 137 | + b.append("'"); |
| 138 | + |
| 139 | + return b.toString(); |
| 140 | + } |
| 141 | +} |
0 commit comments