Moved email configuration for stroke listener plugin to separate prop file. Added...
authormccreedy@NIH.GOV <mccreedy@NIH.GOV@ba61647d-9d00-f842-95cd-605cb4296b96>
Thu, 22 Mar 2018 19:53:08 +0000 (19:53 +0000)
committermccreedy@NIH.GOV <mccreedy@NIH.GOV@ba61647d-9d00-f842-95cd-605cb4296b96>
Thu, 22 Mar 2018 19:53:08 +0000 (19:53 +0000)
git-svn-id: https://citdcbmipav.cit.nih.gov/repos-pub/mipav/trunk@15428 ba61647d-9d00-f842-95cd-605cb4296b96

mipav/src/plugins/PlugInDialogStrokeSegmentationListener.java
mipav/src/plugins/StrokeSegmentationDicomReceiver.java
mipav/src/plugins/stroke_seg_listener.properties [new file with mode: 0644]

index 648a88a..8ea8d75 100644 (file)
@@ -26,6 +26,8 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
     private JTextField outputDirField;\r
 //    private JTextField emailField;\r
     \r
+    private JCheckBox emailCheckbox;\r
+    \r
     private WidgetFactory.ScrollTextArea logOutputArea;\r
     \r
     private String ae = "MIPAV-stroke";\r
@@ -36,7 +38,7 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
 \r
     private String outputDir = new String(System.getProperty("user.home") + File.separator + "mipav" + File.separator + "dicom_catcher" + File.separator);\r
     \r
-    private String emailAddress = "mccreedy@mail.nih.gov";\r
+    private boolean doEmailReport = false;\r
     \r
     private StrokeSegmentationDicomReceiver dicomReceiver;\r
     \r
@@ -131,10 +133,16 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
         gbc.gridy++;\r
         gbc.gridx = 0;\r
         \r
-        JLabel labelEmail = new JLabel("Send email report to");\r
-        labelEmail.setForeground(Color.black);\r
-        labelEmail.setFont(serif12);\r
-        mainPanel.add(labelEmail, gbc);\r
+        emailCheckbox = new JCheckBox("Email report to pre-defined address", doEmailReport);\r
+        emailCheckbox.setForeground(Color.black);\r
+        emailCheckbox.setFont(serif12);\r
+        mainPanel.add(emailCheckbox, gbc);\r
+        \r
+//        \r
+//        JLabel labelEmail = new JLabel("Send email report to");\r
+//        labelEmail.setForeground(Color.black);\r
+//        labelEmail.setFont(serif12);\r
+//        mainPanel.add(labelEmail, gbc);\r
         \r
 //        emailField = new JTextField(40);\r
 //        emailField.setText("" + emailAddress);\r
@@ -200,14 +208,14 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
         ae = scriptParameters.getParams().getString("AE_title");\r
         port = scriptParameters.getParams().getInt("port");\r
         outputDir = scriptParameters.getParams().getString("output_dir");\r
-        emailAddress = scriptParameters.getParams().getString("email_address");\r
+        doEmailReport = scriptParameters.getParams().getBoolean("do_email_report");\r
     }\r
 \r
     protected void storeParamsFromGUI() throws ParserException {\r
         scriptParameters.getParams().put(ParameterFactory.newParameter("AE_title", ae));\r
         scriptParameters.getParams().put(ParameterFactory.newParameter("port", port));\r
         scriptParameters.getParams().put(ParameterFactory.newParameter("output_dir", outputDir));\r
-        scriptParameters.getParams().put(ParameterFactory.newParameter("email_address", emailAddress));\r
+        scriptParameters.getParams().put(ParameterFactory.newParameter("do_email_report", doEmailReport));\r
     }\r
     \r
     /**\r
@@ -221,6 +229,7 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
         ae = aeField.getText();\r
         port = Integer.parseInt(portField.getText());\r
         outputDir = outputDirField.getText();\r
+        doEmailReport = emailCheckbox.isSelected();\r
 //        emailAddress = emailField.getText();\r
         \r
         return true;\r
@@ -230,7 +239,7 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
         try {\r
             log("Starting DICOM receiver: " + ae + " @ " + ipAddress + ":" + port);\r
             \r
-            dicomReceiver = new StrokeSegmentationDicomReceiver(ipAddress, port, ae, outputDir, emailAddress, logOutputArea);\r
+            dicomReceiver = new StrokeSegmentationDicomReceiver(ipAddress, port, ae, outputDir, doEmailReport, logOutputArea);\r
         } catch (IOException e) {\r
             // TODO Auto-generated catch block\r
             e.printStackTrace();\r
index 6469a6f..24c2baa 100644 (file)
@@ -41,10 +41,13 @@ import gov.nih.mipav.model.file.FileInfoDicom;
 \r
 import gov.nih.mipav.model.structures.ModelImage;\r
 import gov.nih.mipav.view.MipavUtil;\r
+import gov.nih.mipav.view.Preferences;\r
 import gov.nih.mipav.view.components.WidgetFactory;\r
 \r
 import java.io.*;\r
 import java.security.GeneralSecurityException;\r
+import java.text.DecimalFormat;\r
+import java.text.SimpleDateFormat;\r
 import java.util.*;\r
 import java.util.concurrent.ExecutorService;\r
 import java.util.concurrent.Executors;\r
@@ -137,16 +140,28 @@ public class StrokeSegmentationDicomReceiver {
     private int serverPort;\r
     private String serverAE;\r
     \r
-    private String emailAddress;\r
+    private boolean doEmailReport;\r
+    \r
+    private static final String reportDwiCid = "cid:dwi-image";\r
+    private static final String reportAdcCid = "cid:adc-image";\r
     \r
     private WidgetFactory.ScrollTextArea logOutputArea;\r
-\r
-    public StrokeSegmentationDicomReceiver(final String ip, final int port, final String curAE, final String outputDir, final String email, final WidgetFactory.ScrollTextArea area) throws IOException {\r
+    \r
+    private static final String configFileName = "stroke_seg_listener.properties";\r
+    \r
+    String emailFrom;\r
+    String emailTo;\r
+    String emailUsername;\r
+    String emailPassword;\r
+    String emailHost;\r
+    String emailPort;\r
+\r
+    public StrokeSegmentationDicomReceiver(final String ip, final int port, final String curAE, final String outputDir, final boolean doEmail, final WidgetFactory.ScrollTextArea area) throws IOException {\r
         serverIP = ip;\r
         serverPort = port;\r
         serverAE = curAE;\r
         \r
-        emailAddress = email;\r
+        doEmailReport = doEmail;\r
         \r
         logOutputArea = area;\r
         \r
@@ -369,10 +384,6 @@ public class StrokeSegmentationDicomReceiver {
     public void emailReport(ModelImage adcImage, File adcLightboxFile, File dwiLightboxFile, double coreVolCC) {\r
         String reportTxt = generateReport(adcImage, adcLightboxFile, dwiLightboxFile, coreVolCC);\r
         \r
-        if (emailAddress == null || emailAddress.equals("")) {\r
-            return;\r
-        }\r
-        \r
         if (reportTxt == null) {\r
                return;\r
         }\r
@@ -382,6 +393,9 @@ public class StrokeSegmentationDicomReceiver {
         \r
         PrintWriter out;\r
         try {\r
+            reportTxt.replaceAll(reportDwiCid, dwiLightboxFile.getName());\r
+            reportTxt.replaceAll(reportAdcCid, adcLightboxFile.getName());\r
+            \r
             out = new PrintWriter(htmlReportPath);\r
             out.println("<html>");\r
             out.print(reportTxt);\r
@@ -392,75 +406,74 @@ public class StrokeSegmentationDicomReceiver {
                MipavUtil.displayError("Unable to write core segmentation report: " + e.getMessage());\r
         }\r
         \r
-        // TODO\r
-        final String fromAddress = "evan.mccreedy@gmail.com";\r
-        final String username = "evan.mccreedy@gmail.com";\r
-        final String password = "";\r
-        final String mailHost = "smtp.gmail.com";\r
-        final String mailPort = "465";\r
-        \r
-        Properties props = new Properties();\r
-               props.put("mail.smtp.auth", "true");\r
-               props.put("mail.smtp.starttls.enable", "true");\r
-               props.put("mail.smtp.host", mailHost);\r
-               props.put("mail.smtp.port", mailPort);\r
-               props.put("mail.smtp.socketFactory.port", mailPort);\r
-               props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");\r
-\r
-               Session session = Session.getInstance(props, new javax.mail.Authenticator() {\r
-                       protected PasswordAuthentication getPasswordAuthentication() {\r
-                               return new PasswordAuthentication(username, password);\r
-                       }\r
-               });\r
-\r
-               try {\r
-                       MimeMultipart multipartContent = new MimeMultipart("related");\r
-                       \r
-                       BodyPart messageBody = new MimeBodyPart();\r
-                       messageBody.setContent(reportTxt, "text/html");\r
-                       multipartContent.addBodyPart(messageBody);\r
-                       \r
-                       messageBody = new MimeBodyPart();\r
-                       DataSource imgDS = new FileDataSource(adcLightboxFile);\r
-                       messageBody.setDataHandler(new DataHandler(imgDS));\r
-                       messageBody.setHeader("Content-ID", "<adc-image>");\r
-                       multipartContent.addBodyPart(messageBody);\r
-                       \r
-                       messageBody = new MimeBodyPart();\r
-                       imgDS = new FileDataSource(dwiLightboxFile);\r
-                       messageBody.setDataHandler(new DataHandler(imgDS));\r
-                       messageBody.setHeader("Content-ID", "<dwi-image>");\r
-                       multipartContent.addBodyPart(messageBody);\r
-                       \r
-                       Message message = new MimeMessage(session);\r
-\r
-                       // Set From: header field of the header.\r
-                       message.setFrom(new InternetAddress(fromAddress));\r
-\r
-                       // Set To: header field of the header.\r
-                       message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));\r
-\r
-                       // Set Subject: header field\r
-                       message.setSubject("MIPAV Stroke Core Segmentation Report");\r
-\r
-                       message.setContent(multipartContent);\r
-\r
-                       // Send message\r
-                       Transport.send(message);\r
-\r
-                       log("Segmentation report successfully emailed.");\r
-               } catch (MessagingException e) {\r
-                       e.printStackTrace();\r
-               }\r
+        if (doEmailReport && readEmailConfig()) {\r
+            Properties props = new Properties();\r
+               props.put("mail.smtp.auth", "true");\r
+               props.put("mail.smtp.starttls.enable", "true");\r
+               props.put("mail.smtp.host", emailHost);\r
+               props.put("mail.smtp.port", emailPort);\r
+               props.put("mail.smtp.socketFactory.port", emailPort);\r
+               props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");\r
+    \r
+               Session session = Session.getInstance(props, new javax.mail.Authenticator() {\r
+                       protected PasswordAuthentication getPasswordAuthentication() {\r
+                               return new PasswordAuthentication(emailUsername, emailPassword);\r
+                       }\r
+               });\r
+    \r
+               try {\r
+                       MimeMultipart multipartContent = new MimeMultipart("related");\r
+                       \r
+                       BodyPart messageBody = new MimeBodyPart();\r
+                       messageBody.setContent(reportTxt, "text/html");\r
+                       multipartContent.addBodyPart(messageBody);\r
+                       \r
+                       messageBody = new MimeBodyPart();\r
+                       DataSource imgDS = new FileDataSource(adcLightboxFile);\r
+                       messageBody.setDataHandler(new DataHandler(imgDS));\r
+                       messageBody.setHeader("Content-ID", "<adc-image>");\r
+                       multipartContent.addBodyPart(messageBody);\r
+                       \r
+                       messageBody = new MimeBodyPart();\r
+                       imgDS = new FileDataSource(dwiLightboxFile);\r
+                       messageBody.setDataHandler(new DataHandler(imgDS));\r
+                       messageBody.setHeader("Content-ID", "<dwi-image>");\r
+                       multipartContent.addBodyPart(messageBody);\r
+                       \r
+                       Message message = new MimeMessage(session);\r
+    \r
+                       // Set From: header field of the header.\r
+                       message.setFrom(new InternetAddress(emailFrom));\r
+    \r
+                       // Set To: header field of the header.\r
+                       message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailTo));\r
+    \r
+                       // Set Subject: header field\r
+                       message.setSubject("MIPAV Stroke Core Segmentation Report");\r
+    \r
+                       message.setContent(multipartContent);\r
+    \r
+                       // Send message\r
+                       Transport.send(message);\r
+    \r
+                       log("Segmentation report successfully emailed.");\r
+               } catch (MessagingException e) {\r
+                       e.printStackTrace();\r
+               }\r
+        }\r
     }\r
     \r
     private String generateReport(ModelImage adcImage, File adcLightboxFile, File dwiLightboxFile, double coreVolCC) {\r
+        final DecimalFormat format = new DecimalFormat("#######.####");\r
+        \r
         FileInfoDicom fileInfoDicom = (FileInfoDicom) adcImage.getFileInfo(0);\r
         \r
-        String dateStr = (String) fileInfoDicom.getTagTable().getValue("0008,0020");\r
-        String timeStr = (String) fileInfoDicom.getTagTable().getValue("0008,0030");\r
+        String curDateTimeStr = new SimpleDateFormat("yyyy-MM-dd'T'HH.mm.ss").format(new Date());\r
+        \r
+        String studyDateStr = (String) fileInfoDicom.getTagTable().getValue("0008,0020");\r
+        String studyTimeStr = (String) fileInfoDicom.getTagTable().getValue("0008,0030");\r
         String patientName = (String) fileInfoDicom.getTagTable().getValue("0010,0010");\r
-        String coreSegVol = "" + coreVolCC;\r
+        String coreSegVol = format.format(coreVolCC);\r
         \r
         String dwiPdfImage = dwiLightboxFile.getName();\r
         String adcPdfImage = adcLightboxFile.getName();\r
@@ -471,17 +484,18 @@ public class StrokeSegmentationDicomReceiver {
         String reportTxt = "";\r
         reportTxt += "<h1>" + "MIPAV Stroke Core Segmentation Report" + "</h1>\n";\r
         reportTxt += "<ul>\n";\r
-        reportTxt += "<li>" + "<b>" + "Study date and time: " + "</b>" + convertDateTimeToISOFormat(dateStr, timeStr) + "</li>\n";\r
+        reportTxt += "<li>" + "<b>" + "Time of segmentation run: " + "</b>" + curDateTimeStr + "</li>\n";\r
+        reportTxt += "<li>" + "<b>" + "Study date and time: " + "</b>" + convertDateTimeToISOFormat(studyDateStr, studyTimeStr) + "</li>\n";\r
         reportTxt += "<li>" + "<b>" + "Patient last name initial: " + "</b>" + getInitialFromName(patientName) + "</li>\n";\r
         reportTxt += "<li>" + "<b>" + "Core segmentation volume (CC): " + "</b>" + coreSegVol + "</li>\n";\r
         //reportTxt += "<li>" + "<b>" + "" + "</b>" + "" + "</li>";\r
         reportTxt += "</ul>\n";\r
         reportTxt += "<h3>" + "DWI volume with core segmentation" + "</h3>\n";\r
         //reportTxt += "<a href='" + dwiPdfImage + "'><img src='" + dwiPdfImage + "' alt='DWI volume with core segmentation' width='" + imgDisplay + "'/></a>\n";\r
-        reportTxt += "<img src='cid:dwi-image' alt='DWI volume with core segmentation'/>\n";\r
+        reportTxt += "<img src='" + reportDwiCid + "' alt='DWI volume with core segmentation'/>\n";\r
         reportTxt += "<h3>" + "ADC volume with thresholded regions" + "</h3>\n";\r
         //reportTxt += "<a href='" + adcPdfImage + "'><img src='" + adcPdfImage + "' alt='ADC volume with thresholded regions' width='" + imgDisplay + "'/></a>\n";\r
-        reportTxt += "<img src='cid:adc-image' alt='ADC volume with thresholded regions'/>\n";\r
+        reportTxt += "<img src='" + reportAdcCid + "' alt='ADC volume with thresholded regions'/>\n";\r
         //reportTxt += "</html>\n";\r
         \r
         return reportTxt;\r
@@ -558,4 +572,53 @@ public class StrokeSegmentationDicomReceiver {
     private String getInitialFromName(final String dicomName) {\r
         return dicomName.substring(0, 1);\r
     }\r
+    \r
+    private boolean readEmailConfig() {\r
+        final InputStream in = getClass().getResourceAsStream(configFileName);\r
+        if (in != null) {\r
+            final Properties prop = new Properties();\r
+            try {\r
+                prop.load(in);\r
+            } catch (final IOException e) {\r
+                Preferences.debug("Unable to load stroke segementation listener plugin preferences file: " + configFileName + "\n", Preferences.DEBUG_MINOR);\r
+                e.printStackTrace();\r
+                return false;\r
+            }\r
+            \r
+            emailFrom = prop.getProperty("emailFrom");\r
+            if (emailFrom == null || emailFrom.equals("")) {\r
+                return false;\r
+            }\r
+            \r
+            emailTo = prop.getProperty("emailTo");\r
+            if (emailTo == null || emailTo.equals("")) {\r
+                return false;\r
+            }\r
+            \r
+            emailUsername = prop.getProperty("emailUsername");\r
+            if (emailUsername == null || emailUsername.equals("")) {\r
+                return false;\r
+            }\r
+            \r
+            emailPassword = prop.getProperty("emailPassword");\r
+            if (emailPassword == null || emailPassword.equals("")) {\r
+                return false;\r
+            }\r
+            \r
+            emailHost = prop.getProperty("emailHost");\r
+            if (emailHost == null || emailHost.equals("")) {\r
+                return false;\r
+            }\r
+            \r
+            emailPort = prop.getProperty("emailPort");\r
+            if (emailPort == null || emailPort.equals("")) {\r
+                return false;\r
+            }\r
+            \r
+            return true;\r
+        } else {\r
+            // couldn't load file\r
+            return false;\r
+        }\r
+    }\r
 }
\ No newline at end of file
diff --git a/mipav/src/plugins/stroke_seg_listener.properties b/mipav/src/plugins/stroke_seg_listener.properties
new file mode 100644 (file)
index 0000000..330c03b
--- /dev/null
@@ -0,0 +1,7 @@
+emailFrom=\r
+emailTo=\r
+emailHost=\r
+emailPort=465\r
+emailUsername=\r
+emailPassword=\r
+\r