Skip to content

Commit

Permalink
ERE-728 fixed Bug: PrescriptionId mismatch between Task and QES-Bundl…
Browse files Browse the repository at this point in the history
…e by using request id for order

ERE-730 do not create prescription when abort is pressed by checking throwables in document service
  • Loading branch information
ManuelB committed Sep 24, 2024
1 parent c9bd34a commit f611cb6
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class ERezeptWorkflowService extends BearerTokenManageService {
@Inject
Event<GetCardsResponseEvent> getCardsResponseEvent;

private Client client;
Client client;
private String userIdForComfortSignature;


Expand Down Expand Up @@ -201,7 +201,8 @@ public void onSignAndUploadBundlesEvent(@ObservesAsync SignAndUploadBundlesEvent
for(int i = 0;i<listOfListOfBundles.size();i++) {
List<BundleWithAccessCodeOrThrowable> unflattenBundles = new ArrayList<>();
for(int j=0;j<listOfListOfBundles.get(i).size(); j++) {
unflattenBundles.add(it.next());
BundleWithAccessCodeOrThrowable next = it.next();
unflattenBundles.add(next);
}
bundleWithAccessCodeOrThrowable
.add(unflattenBundles);
Expand Down Expand Up @@ -283,11 +284,42 @@ public List<BundleWithAccessCodeOrThrowable> createMultipleERezeptsOnPrescriptio
}
i++;
}
uploadSignedBundle(bundles, runtimeConfig, replyTo, replyToMessageId, bundleWithAccessCodes, tasks);

return bundleWithAccessCodes;
}

void uploadSignedBundle(List<Bundle> bundles, RuntimeConfig runtimeConfig, Session replyTo, String replyToMessageId,
List<BundleWithAccessCodeOrThrowable> bundleWithAccessCodes, List<Task> tasks) {
int i;
try {
List<SignResponse> signedDocuments = signBundleWithIdentifiers(bundles, false, runtimeConfig, replyTo, replyToMessageId);
i = 0;
for(SignResponse signedDocument : signedDocuments) {
BundleWithAccessCodeOrThrowable bundleWithAccessCode = bundleWithAccessCodes.get(i);
for(BundleWithAccessCodeOrThrowable bundleWithAccessCode : bundleWithAccessCodes) {
// find correct signed document
SignResponse signedDocument = signedDocuments.stream().filter((sd) -> {
if(bundleWithAccessCode.getBundle() != null) {
if(bundleWithAccessCode.getBundle().getIdentifier() != null) {
return sd.getRequestID().equals(bundleWithAccessCode.getBundle().getIdentifier().getValue());
} else if(bundleWithAccessCode.getBundle().getId() != null) {
return sd.getRequestID().equals(bundleWithAccessCode.getBundle().getId());
} else {
return false;
}
} else {
return false;
}
}).findFirst().orElse(null);
if(signedDocument == null) {
log.warning("Was not able to find a signedDocument for: "+(bundleWithAccessCode.getBundle() != null ? bundleWithAccessCode.getBundle().getId() : "null"));
if(signedDocuments.size() > i) {
log.warning("Using first one.");
signedDocument = signedDocuments.get(i);
} else {
i++;
continue;
}
}
try {
Task task = tasks.get(i);
if(task != null && signedDocument.getSignatureObject() != null && signedDocument.getSignatureObject().getBase64Signature() != null) {
Expand All @@ -304,8 +336,6 @@ public List<BundleWithAccessCodeOrThrowable> createMultipleERezeptsOnPrescriptio
} catch(Throwable t) {
bundleWithAccessCodes.stream().forEach(bundleWithAccessCode -> bundleWithAccessCode.setThrowable(t));
}

return bundleWithAccessCodes;
}

public BundleWithAccessCodeOrThrowable createERezeptOnPrescriptionServer(Bundle bundle)
Expand Down Expand Up @@ -406,7 +436,6 @@ public void updateERezeptTask(String taskId, String accessCode, byte[] signedByt
responseBuilder.entity(taskString);
Response responseError = responseBuilder.build();
throw new WebApplicationException(responseError);

}
}
log.fine("Task $activate Response: " + taskString);
Expand Down Expand Up @@ -507,7 +536,7 @@ public List<SignResponse> signBundleWithIdentifiers(List<Bundle> bundles, boolea
OptionalInputs optionalInputs = new OptionalInputs();
optionalInputs.setSignatureType("urn:ietf:rfc:5652");
optionalInputs.setIncludeEContent(true);

List<SignRequest> signRequests = bundles.stream().map(bundle -> {
byte[] canonXmlBytes;
try {
Expand All @@ -526,7 +555,14 @@ public List<SignResponse> signBundleWithIdentifiers(List<Bundle> bundles, boolea
base64Data.setValue(canonXmlBytes);
document.setBase64Data(base64Data);
signRequest.setOptionalInputs(optionalInputs);
signRequest.setRequestID(UUID.randomUUID().toString());
if(bundle.getIdentifier() != null) {
signRequest.setRequestID(bundle.getIdentifier().getValue());
} else if(bundle.getId() != null) {
signRequest.setRequestID(bundle.getId());
} else {
bundle.setId(UUID.randomUUID().toString());
signRequest.setRequestID(bundle.getId());
}
signRequest.setDocument(document);
signRequest.setIncludeRevocationInfo(appConfig.includeRevocationInfoEnabled());
return signRequest;
Expand All @@ -550,7 +586,7 @@ public List<SignResponse> signBundleWithIdentifiers(List<Bundle> bundles, boolea
optionalInputsC755.setSignatureType(optionalInputs.getSignatureType());
optionalInputsC755.setIncludeEContent(optionalInputs.isIncludeEContent());
signRequestV755.setOptionalInputs(optionalInputsC755);
signRequestV755.setRequestID(UUID.randomUUID().toString());
signRequestV755.setRequestID(signRequest.getRequestID());
de.gematik.ws.conn.signatureservice.v7_5_5.DocumentType documentV755 = new de.gematik.ws.conn.signatureservice.v7_5_5.DocumentType();
documentV755.setBase64Data(signRequest.getDocument().getBase64Data());
documentV755.setShortText(signRequest.getDocument().getShortText());
Expand Down Expand Up @@ -598,6 +634,7 @@ public List<SignResponse> signBundleWithIdentifiers(List<Bundle> bundles, boolea
List<SignResponse> signResponses744 = signResponsesV755.stream().map(signResponseV755 -> {
if(signResponseV755 != null) {
SignResponse signResponse744 = new SignResponse();
signResponse744.setRequestID(signResponseV755.getRequestID());
signResponse744.setSignatureObject(signResponseV755.getSignatureObject());
signResponse744.setStatus(signResponseV755.getStatus());
return signResponse744;
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/health/ere/ps/service/pdf/DocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.event.ObservesAsync;
import jakarta.inject.Inject;
import javax.xml.XMLConstants;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Result;
Expand Down Expand Up @@ -54,6 +50,11 @@
import health.ere.ps.model.pdf.ERezeptDocument;
import health.ere.ps.service.fhir.FHIRService;
import health.ere.ps.websocket.ExceptionWithReplyToException;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.event.ObservesAsync;
import jakarta.inject.Inject;

@ApplicationScoped
public class DocumentService {
Expand Down Expand Up @@ -181,21 +182,25 @@ public void onBundlesWithAccessCodes(@ObservesAsync BundlesWithAccessCodeEvent b
.subList(i, Math.min(i + MAX_NUMBER_OF_MEDICINES_PER_PRESCRIPTIONS, bundles.size()));

ByteArrayOutputStream boas = new ByteArrayOutputStream();
if(!onlyContainsThrowables(subList)) {
ERezeptDocument eRezeptDocument = null;
if(!containsThrowables(subList)) {
log.info("Now creating prescription receipts");
try {
boas = generateERezeptPdf(subList);
eRezeptDocument = new ERezeptDocument(subList, boas.size() > 0 ? boas.toByteArray() : null);
} catch (IOException | FOPException | TransformerException e) {
log.severe("Could not generate ERezept PDF:" + e);
exceptionEvent.fireAsync(new ExceptionWithReplyToException(e, bundlesWithAccessCodeEvent.getReplyTo(), bundlesWithAccessCodeEvent.getReplyToMessageId()));
boas = new ByteArrayOutputStream();
}
} else {
log.warning("E-Prescriptions contain throwables. Will not generate PDF.");
eRezeptDocument = new ERezeptDocument(subList, null);
}

ERezeptDocument eRezeptDocument = new ERezeptDocument(subList, boas.size() > 0 ? boas.toByteArray() : null);

log.info("Created prescription receipts");
eRezeptDocumentsEvent.fireAsync(new ERezeptWithDocumentsEvent(List.of(eRezeptDocument),
eRezeptDocumentsEvent.fireAsync(new ERezeptWithDocumentsEvent(eRezeptDocument != null ? List.of(eRezeptDocument) : new ArrayList(),
bundlesWithAccessCodeEvent.getReplyTo(), bundlesWithAccessCodeEvent.getReplyToMessageId()));
log.info("Sending prescription receipts results.");
}
Expand All @@ -205,8 +210,8 @@ public void onBundlesWithAccessCodes(@ObservesAsync BundlesWithAccessCodeEvent b
});
}

private boolean onlyContainsThrowables(List<BundleWithAccessCodeOrThrowable> bundles) {
return bundles.size() == bundles.stream().filter(bundle -> bundle.getThrowable() != null).count();
private boolean containsThrowables(List<BundleWithAccessCodeOrThrowable> bundles) {
return bundles.stream().filter(bundle -> bundle.getThrowable() != null).count() > 0;
}

public ByteArrayOutputStream generateERezeptPdf(List<BundleWithAccessCodeOrThrowable> bundles) throws IOException, FOPException, TransformerException {
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ quarkus.log.file.rotation.file-suffix=yyyy-MM-dd
# to more verbose levels like TRACE at buildtime! So if we want to be able to enable trace logging of
# soap request/response at runtime, we must prepare being able to do this at buildtime here.
# See: https://quarkus.io/guides/logging#configure-the-log-level-category-and-format
#quarkus.log.category."com.sun.xml.ws.transport.http.client.HttpTransportPipe".level=DEBUG
#quarkus.log.category."com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe".level=DEBUG
#quarkus.log.category."com.sun.xml.ws.transport.http.HttpAdapter".level=DEBUG
#quarkus.log.category."com.sun.xml.internal.ws.transport.http.HttpAdapter".level=DEBUG
quarkus.log.category."com.sun.xml.ws.transport.http.client.HttpTransportPipe".min-level=TRACE
quarkus.log.category."com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe".min-level=TRACE
quarkus.log.category."com.sun.xml.ws.transport.http.HttpAdapter".min-level=TRACE
quarkus.log.category."com.sun.xml.internal.ws.transport.http.HttpAdapter".min-level=TRACE

### customized log levels
#quarkus.log.category."org.apache.http".level=DEBUG
Expand Down
Loading

0 comments on commit f611cb6

Please sign in to comment.