Skip to content

Commit

Permalink
Pull request #175: Update prod from cert
Browse files Browse the repository at this point in the history
Merge in TELIMA/karnak from update_prod_from_cert to master

* commit '5388b5b8fe6fdad948c710428eb033475cba2981':
  feat: fix following unit tests
  feat: change pom version
  feat: modify ForwardService/multiple destinations: gather prepare and transfer of files
  feat: logs
  feat: logs
  feat: add logs for ForwardService
  • Loading branch information
jdcshug committed Feb 15, 2023
2 parents 13fa316 + 5388b5b commit 0034c06
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 101 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</licenses>

<!-- Version -->
<version>1.0.54</version>
<version>1.0.58</version>

<!-- Packaging -->
<packaging>jar</packaging>
Expand All @@ -32,8 +32,8 @@
<javax.el.version>3.0.0</javax.el.version>
<javax.mail.version>1.6.2</javax.mail.version>
<vaadin.version>21.0.9</vaadin.version>
<weasis-dicom-tools.version>5.27.0.1</weasis-dicom-tools.version>
<weasis.opencv.native.version>4.5.5-dcm</weasis.opencv.native.version>
<weasis-dicom-tools.version>5.29.0.2</weasis-dicom-tools.version>
<weasis.opencv.native.version>4.6.0-dcm</weasis.opencv.native.version>
<springdoc.version>1.6.0</springdoc.version>
<mockito.version>4.2.0</mockito.version>
<h2.version>1.4.200</h2.version>
Expand Down
178 changes: 80 additions & 98 deletions src/main/java/org/karnak/backend/service/ForwardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.UID;
import org.dcm4che3.img.DicomOutputData;
import org.dcm4che3.img.op.MaskArea;
import org.dcm4che3.img.stream.BytesWithImageDescriptor;
import org.dcm4che3.img.stream.ImageAdapter;
Expand Down Expand Up @@ -62,11 +61,10 @@
@Service
public class ForwardService {

private static final String ERROR_WHEN_FORWARDING =
"Error when forwarding to the final destination";

private static final Logger LOGGER = LoggerFactory.getLogger(ForwardService.class);

private static final String ERROR_WHEN_FORWARDING = "Error when forwarding to the final destination";

private final ApplicationEventPublisher applicationEventPublisher;

@Autowired
Expand All @@ -75,115 +73,85 @@ public ForwardService(final ApplicationEventPublisher applicationEventPublisher)
}

public void storeMultipleDestination(
ForwardDicomNode fwdNode, List<ForwardDestination> destList, Params p) throws IOException {
if (destList == null || destList.isEmpty()) {
ForwardDicomNode forwardNode, List<ForwardDestination> destinations, Params p)
throws IOException {
if (destinations == null || destinations.isEmpty()) {
throw new IllegalStateException(
"Cannot find the DICOM destination from " + fwdNode.toString());
"Cannot find destinations from " + forwardNode.toString());
}
// Exclude DICOMDIR
if ("1.2.840.10008.1.3.10".equals(p.getCuid())) {
LOGGER.warn("Cannot send DICOMDIR {}", p.getIuid());
return;
}

if (destList.size() == 1) {
storeOneDestination(fwdNode, destList.get(0), p);
} else {
List<ForwardDestination> destConList = new ArrayList<>();
for (ForwardDestination fwDest : destList) {
try {
if (fwDest instanceof DicomForwardDestination) {
prepareTransfer((DicomForwardDestination) fwDest, p);
}
destConList.add(fwDest);
} catch (Exception e) {
LOGGER.error("Cannot connect to the final destination", e);
}
// Prepare and transfer files
Attributes attributes = new Attributes();
List<File> files = new ArrayList<>();
try {
int nbDestinations = destinations.size();
for (int i = 0; i < nbDestinations; i++) {
prepareAndTransfer(forwardNode, p, i, destinations.get(i), attributes,
nbDestinations, files);
}

if (destConList.isEmpty()) {
return;
} else if (destConList.size() == 1) {
storeOneDestination(fwdNode, destConList.get(0), p);
} else {
List<File> files = null;
try {
Attributes attributes = new Attributes();
ForwardDestination fistDest = destConList.get(0);
if (fistDest instanceof DicomForwardDestination) {
files = transfer(fwdNode, (DicomForwardDestination) fistDest, attributes, p);
} else if (fistDest instanceof WebForwardDestination) {
files = transfer(fwdNode, (WebForwardDestination) fistDest, attributes, p);
}
if (!attributes.isEmpty()) {
for (int i = 1; i < destConList.size(); i++) {
ForwardDestination dest = destConList.get(i);
if (dest instanceof DicomForwardDestination) {
transferOther(fwdNode, (DicomForwardDestination) dest, attributes, p);
} else if (dest instanceof WebForwardDestination) {
transferOther(fwdNode, (WebForwardDestination) dest, attributes, p);
}
}
}
} finally {
if (files != null) {
// Force to clean if tmp bulk files
for (File file : files) {
FileUtil.delete(file);
}
}
} catch (IOException e) {
LOGGER.error("Cannot connect to the final destination", e);
throw e;
} finally {
if (!files.isEmpty()) {
// Force to clean if tmp bulk files
for (File file : files) {
FileUtil.delete(file);
}
}
}
}

public void storeOneDestination(
ForwardDicomNode fwdNode, ForwardDestination destination, Params p) throws IOException {
/**
* Prepare and transfer files
*
* @param fwdNode Forward node
* @param p Params
* @param index Current index
* @param destination Destination
* @param attributes Attributes
* @param nbDestinations Number of destinations to handle
* @param files Temp files to delete
*/
private void prepareAndTransfer(ForwardDicomNode fwdNode, Params p, int index,
ForwardDestination destination, Attributes attributes, int nbDestinations, List<File> files)
throws IOException {
if (destination instanceof DicomForwardDestination) {
DicomForwardDestination dest = (DicomForwardDestination) destination;
prepareTransfer(dest, p);
transfer(fwdNode, dest, null, p);
} else if (destination instanceof WebForwardDestination) {
transfer(fwdNode, (WebForwardDestination) destination, null, p);
// Prepare transfer only for dicom destination
prepareTransfer((DicomForwardDestination) destination, p);
}
if (index == 0) {
// Case first iteration: handle first destination of the forward node
Attributes attToApply = nbDestinations > 1 ? attributes : null;
if (destination instanceof DicomForwardDestination) {
files.addAll(transfer(fwdNode, (DicomForwardDestination) destination, attToApply, p));
} else if (destination instanceof WebForwardDestination) {
files.addAll(transfer(fwdNode, (WebForwardDestination) destination, attToApply, p));
}
} else {
// Case other iterations: handle other destinations of the forward node
if (!attributes.isEmpty()) {
if (destination instanceof DicomForwardDestination) {
transferOther(fwdNode, (DicomForwardDestination) destination, attributes, p);
} else if (destination instanceof WebForwardDestination) {
transferOther(fwdNode, (WebForwardDestination) destination, attributes, p);
}
}
}
}

public static synchronized StoreFromStreamSCU prepareTransfer(
public static StoreFromStreamSCU prepareTransfer(
DicomForwardDestination destination, Params p) throws IOException {
String cuid = p.getCuid();
String tsuid = p.getTsuid();
String dstTsuid = destination.getOutputTransferSyntax(tsuid);
StoreFromStreamSCU streamSCU = destination.getStreamSCU();

if (streamSCU.hasAssociation()) {
// Handle dynamically new SOPClassUID
Set<String> tss = streamSCU.getTransferSyntaxesFor(cuid);
if (!tss.contains(dstTsuid)) {
streamSCU.close(true);
}

// Add Presentation Context for the association
streamSCU.addData(cuid, dstTsuid);
if (DicomOutputData.isAdaptableSyntax(dstTsuid)) {
streamSCU.addData(cuid, UID.JPEGLosslessSV1);
}

if (!streamSCU.isReadyForDataTransfer()) {
// If connection has been closed just reopen
streamSCU.open();
}
} else {
destination.getStreamSCUService().start();
// Add Presentation Context for the association
streamSCU.addData(cuid, dstTsuid);
if (!dstTsuid.equals(UID.ExplicitVRLittleEndian)) {
streamSCU.addData(cuid, UID.ExplicitVRLittleEndian);
}
if (DicomOutputData.isAdaptableSyntax(dstTsuid)) {
streamSCU.addData(cuid, UID.JPEGLosslessSV1);
}
streamSCU.open();
}
streamSCU.prepareTransfer(destination.getStreamSCUService(), p.getIuid(), cuid, dstTsuid);
return streamSCU;
}

Expand Down Expand Up @@ -250,7 +218,7 @@ public List<File> transfer(
launchCStore(p, streamSCU, dataWriter, cuid, iuid, syntax, transformedPlanarImage);

progressNotify(
destination, p.getIuid(), p.getCuid(), false, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), false, streamSCU);
monitor(
sourceNode.getId(),
destination.getId(),
Expand All @@ -260,7 +228,7 @@ public List<File> transfer(
null);
} catch (AbortException e) {
progressNotify(
destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), true, streamSCU);
monitor(
sourceNode.getId(),
destination.getId(),
Expand All @@ -273,7 +241,7 @@ public List<File> transfer(
}
} catch (IOException e) {
progressNotify(
destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), true, streamSCU);
monitor(
sourceNode.getId(),
destination.getId(),
Expand All @@ -287,7 +255,7 @@ public List<File> transfer(
Thread.currentThread().interrupt();
}
progressNotify(
destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), true, streamSCU);
monitor(
sourceNode.getId(),
destination.getId(),
Expand Down Expand Up @@ -427,12 +395,12 @@ public void transferOther(
launchCStore(p, streamSCU, dataWriter, cuid, iuid, syntax, transformedPlanarImage);

progressNotify(
destination, p.getIuid(), p.getCuid(), false, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), false, streamSCU);
monitor(
fwdNode.getId(), destination.getId(), attributesOriginal, attributesToSend, true, null);
} catch (AbortException e) {
progressNotify(
destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), true, streamSCU);
monitor(
fwdNode.getId(),
destination.getId(),
Expand All @@ -445,7 +413,7 @@ public void transferOther(
}
} catch (IOException e) {
progressNotify(
destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), true, streamSCU);
monitor(
fwdNode.getId(),
destination.getId(),
Expand All @@ -459,7 +427,7 @@ public void transferOther(
Thread.currentThread().interrupt();
}
progressNotify(
destination, p.getIuid(), p.getCuid(), true, streamSCU.getNumberOfSuboperations());
destination, p.getIuid(), p.getCuid(), true, streamSCU);
monitor(
fwdNode.getId(),
destination.getId(),
Expand Down Expand Up @@ -680,6 +648,20 @@ public void transferOther(
}
}

private static void progressNotify(
ForwardDestination destination, String iuid, String cuid, boolean failed,
StoreFromStreamSCU streamSCU) {
streamSCU.removeIUIDProcessed(iuid);
ServiceUtil.notifyProgession(
destination.getState(),
iuid,
cuid,
failed ? Status.ProcessingFailure : Status.Success,
failed ? ProgressStatus.FAILED : ProgressStatus.COMPLETED,
streamSCU.getNumberOfSuboperations());
}


private static void progressNotify(
ForwardDestination destination, String iuid, String cuid, boolean failed, int subOperations) {
ServiceUtil.notifyProgession(
Expand Down

0 comments on commit 0034c06

Please sign in to comment.