Example report email prototype.
authormccreedy@NIH.GOV <mccreedy@NIH.GOV@ba61647d-9d00-f842-95cd-605cb4296b96>
Wed, 21 Mar 2018 20:09:02 +0000 (20:09 +0000)
committermccreedy@NIH.GOV <mccreedy@NIH.GOV@ba61647d-9d00-f842-95cd-605cb4296b96>
Wed, 21 Mar 2018 20:09:02 +0000 (20:09 +0000)
git-svn-id: https://citdcbmipav.cit.nih.gov/repos-pub/mipav/trunk@15427 ba61647d-9d00-f842-95cd-605cb4296b96

mipav/src/plugins/PlugInDialogStrokeSegmentationListener.java
mipav/src/plugins/StrokeSegmentationDicomReceiver.java

index 785c097..648a88a 100644 (file)
@@ -24,7 +24,7 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
     private JTextField aeField;\r
     private JTextField portField;\r
     private JTextField outputDirField;\r
-    private JTextField emailField;\r
+//    private JTextField emailField;\r
     \r
     private WidgetFactory.ScrollTextArea logOutputArea;\r
     \r
@@ -36,7 +36,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 = "";\r
+    private String emailAddress = "mccreedy@mail.nih.gov";\r
     \r
     private StrokeSegmentationDicomReceiver dicomReceiver;\r
     \r
@@ -136,11 +136,11 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
         labelEmail.setFont(serif12);\r
         mainPanel.add(labelEmail, gbc);\r
         \r
-        emailField = new JTextField(40);\r
-        emailField.setText("" + emailAddress);\r
-        gbc.fill = GridBagConstraints.NONE;\r
-        gbc.gridx++;\r
-        mainPanel.add(emailField, gbc);\r
+//        emailField = new JTextField(40);\r
+//        emailField.setText("" + emailAddress);\r
+//        gbc.fill = GridBagConstraints.NONE;\r
+//        gbc.gridx++;\r
+//        mainPanel.add(emailField, gbc);\r
   \r
         getContentPane().add(mainPanel, BorderLayout.NORTH);\r
         \r
@@ -221,7 +221,7 @@ public class PlugInDialogStrokeSegmentationListener extends JDialogStandaloneScr
         ae = aeField.getText();\r
         port = Integer.parseInt(portField.getText());\r
         outputDir = outputDirField.getText();\r
-        emailAddress = emailField.getText();\r
+//        emailAddress = emailField.getText();\r
         \r
         return true;\r
     }\r
index 1960d70..6469a6f 100644 (file)
@@ -38,8 +38,9 @@
 \r
 \r
 import gov.nih.mipav.model.file.FileInfoDicom;\r
-import gov.nih.mipav.model.structures.ModelImage;\r
 \r
+import gov.nih.mipav.model.structures.ModelImage;\r
+import gov.nih.mipav.view.MipavUtil;\r
 import gov.nih.mipav.view.components.WidgetFactory;\r
 \r
 import java.io.*;\r
@@ -51,6 +52,10 @@ import java.util.concurrent.ScheduledExecutorService;
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
+import javax.activation.*;\r
+import javax.mail.*;\r
+import javax.mail.internet.*;\r
+\r
 import org.dcm4che3.data.Tag;\r
 import org.dcm4che3.data.Attributes;\r
 import org.dcm4che3.data.VR;\r
@@ -362,47 +367,108 @@ public class StrokeSegmentationDicomReceiver {
     }\r
     \r
     public void emailReport(ModelImage adcImage, File adcLightboxFile, File dwiLightboxFile, double coreVolCC) {\r
-        String reportPath = generateReport(adcImage, adcLightboxFile, dwiLightboxFile, 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
+        \r
+        String outputDir = adcLightboxFile.getParent();\r
+        final String htmlReportPath = outputDir + File.separator + "core_seg_report.html";\r
+        \r
+        PrintWriter out;\r
+        try {\r
+            out = new PrintWriter(htmlReportPath);\r
+            out.println("<html>");\r
+            out.print(reportTxt);\r
+            out.println("</html>");\r
+            out.close();\r
+        } catch (FileNotFoundException e) {\r
+            e.printStackTrace();\r
+               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
     }\r
     \r
     private String generateReport(ModelImage adcImage, File adcLightboxFile, File dwiLightboxFile, double coreVolCC) {\r
         FileInfoDicom fileInfoDicom = (FileInfoDicom) adcImage.getFileInfo(0);\r
         \r
-        // TODO create PDF and write to disk, return path (or null if error)\r
-        \r
-        //File pdfFile = new File(dwiLightbox.getImageDirectory() + File.separator + "core_seg_report.pdf");\r
-        //File pdfFile = new File("C:\\Users\\mccreedy\\mipav\\dicom_catcher\\20180302.100509" + File.separator + "core_seg_report.pdf");\r
-        \r
-        //String dateStr = "2000-01-01";\r
         String dateStr = (String) fileInfoDicom.getTagTable().getValue("0008,0020");\r
         String timeStr = (String) fileInfoDicom.getTagTable().getValue("0008,0030");\r
         String patientName = (String) fileInfoDicom.getTagTable().getValue("0010,0010");\r
         String coreSegVol = "" + coreVolCC;\r
         \r
-        String outputDir = adcLightboxFile.getParent();\r
-        \r
         String dwiPdfImage = dwiLightboxFile.getName();\r
         String adcPdfImage = adcLightboxFile.getName();\r
-        //String dwiPdfImage = "20180302_1.100509_CoreSeg_DWI_core_lightbox.png";\r
-        //String adcPdfImage = "20180302_1.100509_CoreSeg_ADC_thresh_lightbox.png";\r
-        \r
-        // study date and time\r
-        // first initial of last name\r
-        // core seg volume in CC\r
-        // DWI with core seg + red lut\r
-        // ADC with thesh vol + red lut\r
-        // volumes generated by lightbox 8x5 (for 40 slices) 100 or 125 zoom, no border, include segmentation imageB w/ red lut\r
-        //pdfCreate(pdfFile, dateStr, timeStr, patientName, coreSegVol, dwiPdfImagePath, adcPdfImagePath);\r
         \r
         final int imgDisplay = 1027;\r
         \r
-        String reportTxt = "<html>\n";\r
+        //String reportTxt = "<html>\n";\r
+        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
@@ -411,25 +477,14 @@ public class StrokeSegmentationDicomReceiver {
         //reportTxt += "<li>" + "<b>" + "" + "</b>" + "" + "</li>";\r
         reportTxt += "</ul>\n";\r
         reportTxt += "<h3>" + "DWI volume with core segmentation" + "</h3>\n";\r
-        reportTxt += "<a href='" + adcPdfImage + "'><img src='" + dwiPdfImage + "' alt='DWI volume with core segmentation' width='" + imgDisplay + "'/></a>\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 += "<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 += "</html>\n";\r
-        \r
-        final String htmlReportPath = outputDir + File.separator + "core_seg_report.html";\r
-        \r
-        PrintWriter out;\r
-        try {\r
-            out = new PrintWriter(htmlReportPath);\r
-            out.print(reportTxt);\r
-            out.close();\r
-        } catch (FileNotFoundException e) {\r
-            // TODO Auto-generated catch block\r
-            e.printStackTrace();\r
-        }\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 += "</html>\n";\r
         \r
-        \r
-        return htmlReportPath;\r
+        return reportTxt;\r
     }\r
     \r
     /**\r
@@ -503,212 +558,4 @@ public class StrokeSegmentationDicomReceiver {
     private String getInitialFromName(final String dicomName) {\r
         return dicomName.substring(0, 1);\r
     }\r
-    \r
-/*\r
-    *//**\r
-     * Creates the PDF file and creates several tables for scanner information as well\r
-     * as the VOI statistic information\r
-     *\r
-     * @return the content stream for adding statistics to the first page\r
-     *//*\r
-    protected void pdfCreate(File pdfFile, String dateStr, String timeStr, String patientName, String coreSegVol, String dwiPdfImagePath, String adcPdfImagePath) {\r
-        PDDocument doc;\r
-        \r
-        // the document\r
-        doc = null;\r
-\r
-        //initialize blank PDF\r
-        doc = new PDDocument();\r
-        \r
-        try {\r
-            doc.save(pdfFile.toString());\r
-        } catch (IOException e) {\r
-            System.out.println("44 IOException " + e + " on doc.save(pdfFile.toString())");\r
-            return;\r
-        }\r
-        \r
-        try {\r
-            doc.close();\r
-        } catch (IOException e) {\r
-            System.out.println("45 IOException " + e + " on doc.close()");\r
-            return;\r
-        }\r
-        \r
-        PDPageContentStream contentStream = null;\r
-        //System.out.println("46 Have executed PDPageContentStream contentStream = null");\r
-        \r
-        try {\r
-            try {\r
-                doc = PDDocument.load( pdfFile );\r
-            } catch(IOException e) {\r
-                System.out.println("47 IOException "+ e + " on doc = PDDocument.load( pdfFile )");\r
-                return;\r
-            }\r
-            \r
-            PDFont boldFont = PDType1Font.HELVETICA_BOLD;\r
-            int headerFontSize = 18;\r
-            PDFont regFont = PDType1Font.HELVETICA;\r
-            int regFontSize = 12;\r
-\r
-            doc.addPage(new PDPage());\r
-            doc.addPage(new PDPage());\r
-            doc.addPage(new PDPage());\r
-            \r
-            PDPageTree pageTree = doc.getPages();\r
-            \r
-            PDPage textPage = pageTree.get(0);\r
-            \r
-            PDRectangle pageSize = textPage.getMediaBox();\r
-            \r
-            String title = "MIPAV Stroke Core Segmentation Report";\r
-            \r
-            float stringWidth = boldFont.getStringWidth( title );\r
-            \r
-            //System.out.println("70 stringWidth = " + stringWidth);\r
-            \r
-            float centeredPosition = (pageSize.getWidth() - (stringWidth*headerFontSize)/1000f)/2f;\r
-            // (float) (PDPage.PAGE_SIZE_LETTER.getWidth()/2.0)-120\r
-            \r
-            float valueOffset = 250f;\r
-            float newLineOffset = -15f;\r
-            \r
-            //System.out.println("71 centeredPosition = " + centeredPosition);\r
-            \r
-            try {\r
-                contentStream = new PDPageContentStream(doc, textPage);\r
-            } catch(IOException e) {\r
-                System.out.println("72 IOException " + e + " on contentStream = new PDPageContentStream(doc, page, false, true)");\r
-                return;\r
-            }\r
-            \r
-            try {\r
-                contentStream.beginText();\r
-            } catch(IOException e) {\r
-                System.out.println("73 IOException " + e + "on contentStream.beginText()");\r
-                return;\r
-            }\r
-\r
-            contentStream.setFont(boldFont, headerFontSize);\r
-            contentStream.moveTextPositionByAmount(centeredPosition, 750 );\r
-            contentStream.drawString( title );\r
-\r
-            // move to left side\r
-            contentStream.moveTextPositionByAmount(-centeredPosition + 10, newLineOffset - 10);\r
-            \r
-            // study date and time\r
-            contentStream.setFont(boldFont, regFontSize);\r
-            contentStream.drawString("Study date and time: ");\r
-            contentStream.moveTextPositionByAmount(valueOffset, 0);\r
-            contentStream.setFont(regFont, regFontSize);\r
-            contentStream.drawString(dateStr + " @ " + timeStr);\r
-\r
-            contentStream.moveTextPositionByAmount(-valueOffset, newLineOffset);\r
-            \r
-            // first initial of last name\r
-            contentStream.setFont(boldFont, regFontSize);\r
-            contentStream.drawString("Patient last name initial: ");\r
-            contentStream.moveTextPositionByAmount(valueOffset, 0);\r
-            contentStream.setFont(regFont, regFontSize);\r
-            contentStream.drawString(patientName);\r
-            \r
-            contentStream.moveTextPositionByAmount(-valueOffset, newLineOffset);\r
-            \r
-            // core seg volume in CC\r
-            contentStream.setFont(boldFont, regFontSize);\r
-            contentStream.drawString("Core segmentation volume (CC): ");\r
-            contentStream.moveTextPositionByAmount(valueOffset, 0);\r
-            contentStream.setFont(regFont, regFontSize);\r
-            contentStream.drawString(coreSegVol);\r
-            \r
-            contentStream.endText();\r
-            \r
-            contentStream.close();\r
-            \r
-            PDPage dwiPage = pageTree.get(1);\r
-            \r
-            try {\r
-                contentStream = new PDPageContentStream(doc, dwiPage);\r
-            } catch(IOException e) {\r
-                System.out.println("72 IOException " + e + " on contentStream = new PDPageContentStream(doc, page, false, true)");\r
-                return;\r
-            }\r
-\r
-            // DWI with core seg + red lut\r
-            \r
-            PDImageXObject pdDwiImage = PDImageXObject.createFromFile(dwiPdfImagePath, doc);\r
-            \r
-//            InputStream is = new BufferedInputStream(new FileInputStream(dwiPdfImagePath));\r
-//            PDXObjectImage pdDwiImage = new PDPixelMap(new PDStream(doc, is));\r
-//            is.close();\r
-//            is = null;\r
-//            \r
-//            contentStream.drawImage(pdDwiImage, pdDwiImage.getWidth(), pdDwiImage.getHeight());\r
-            contentStream.drawImage(pdDwiImage, pageSize.getWidth(), pageSize.getHeight());\r
-            \r
-            contentStream.close();\r
-            \r
-            // ADC with thesh vol + red lut\r
-            \r
-            PDPage adcPage = pageTree.get(2);\r
-            \r
-            try {\r
-                contentStream = new PDPageContentStream(doc, adcPage);\r
-            } catch(IOException e) {\r
-                System.out.println("72 IOException " + e + " on contentStream = new PDPageContentStream(doc, page, false, true)");\r
-                return;\r
-            }\r
-            \r
-            PDImageXObject pdAdcImage = PDImageXObject.createFromFile(adcPdfImagePath, doc);\r
-            \r
-//            is = new BufferedInputStream(new FileInputStream(adcPdfImagePath));\r
-//            PDXObjectImage pdAdcImage = new PDPixelMap(new PDStream(doc, is));\r
-//            is.close();\r
-//            is = null;\r
-            \r
-//            contentStream.drawImage(pdAdcImage, pdAdcImage.getWidth(), pdAdcImage.getHeight());\r
-            contentStream.drawImage(pdAdcImage, pageSize.getWidth(), pageSize.getHeight());\r
-            \r
-            contentStream.close();\r
-    \r
-            //System.out.println("200 pdfFile.toString() = " + pdfFile.toString());\r
-            try {\r
-                doc.save( pdfFile.toString() );\r
-            } catch (IOException e) {\r
-                System.out.println("202 IOException " + e + " on doc.save( pdfFile.toString()");\r
-                return;\r
-            }\r
-            \r
-            MipavUtil.displayInfo("PDF saved to: " + pdfFile);\r
-            ViewUserInterface.getReference().getMessageFrame().append("PDF saved to: " + pdfFile +  "\n", ViewJFrameMessage.DATA);\r
-        } catch (Exception e) {\r
-            ViewUserInterface.getReference().getMessageFrame().append("Error occured in PDF generation calling method\n", ViewJFrameMessage.DEBUG);\r
-            e.printStackTrace();\r
-            \r
-            if(contentStream != null) {\r
-                try {\r
-                    contentStream.close();\r
-                } catch (IOException e1) {\r
-                    e.printStackTrace();\r
-                    MipavUtil.displayError("Content stream could not be closed, please restart MIPAV.");\r
-                }\r
-            }\r
-            if( doc != null ) {\r
-                try {\r
-                    doc.close();\r
-                } catch (IOException e1) {\r
-                    e.printStackTrace();\r
-                    MipavUtil.displayError("PDF document could not be closed, please restart MIPAV.");\r
-                }\r
-            }\r
-        }  finally {\r
-            if( doc != null ) {\r
-                try {\r
-                    doc.close();\r
-                } catch (IOException e) {\r
-                    // TODO Auto-generated catch block\r
-                    e.printStackTrace();\r
-                }\r
-            }\r
-        }\r
-    }*/\r
 }
\ No newline at end of file