Switch from javax.mail to net.jaekl.qd.SendMail.
[cfb.git] / prod / net / jaekl / cfb / analyze / Notifier.java
1 package net.jaekl.cfb.analyze;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.ArrayList;
6
7 import net.jaekl.cfb.CfbBundle;
8 import net.jaekl.cfb.Config;
9 import net.jaekl.qd.util.MailException;
10 import net.jaekl.qd.util.MimePart;
11 import net.jaekl.qd.util.SendMail;
12
13 public class Notifier {
14         private static final String TEXT_HTML = "text/html";
15         
16         CfbBundle m_bundle;
17         Config m_config;
18
19         public Notifier(CfbBundle bundle, Config config) {
20                 m_bundle = bundle;
21                 m_config = config;
22         }
23         
24         public void sendEmailIfNeeded(PrintWriter pw, HtmlReport report) {
25                 Delta delta = report.getDelta();
26                 if ((delta.getNumNew() > 0) || (delta.getNumFixed() > 0)) {
27                         sendEmail(pw, report);
28                 }
29         }
30         
31         void sendEmail(PrintWriter pw, HtmlReport report) {
32                 SendMail sendMail = new SendMail();
33                 
34                 sendMail.setSmtpHost(m_config.getMailSmtpHost());
35                 
36                 ArrayList<String> recipients = m_config.getNotify();
37                 if (recipients.size() < 1) {
38                         return;
39                 }
40                 
41                 PrintWriter mailWriter = null;
42                 
43                 try {
44                         String earlier = report.getDelta().getEarlier().constructVersionText(m_bundle);
45                         String later   = report.getDelta().getLater().constructVersionText(m_bundle);
46
47                         sendMail.setFrom(m_config.getMailFrom());
48                         sendMail.setSubject(m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later));
49                         
50                         for (String recipient : recipients) {
51                                 sendMail.addTo(recipient);
52                         }
53                         
54                         StringWriter sw = new StringWriter();
55                         mailWriter = new PrintWriter(sw);
56                         report.write(mailWriter);
57                         mailWriter.flush();
58
59                         MimePart part = new MimePart(sw.toString(), TEXT_HTML);
60                         sendMail.addPart(part);
61                         sendMail.send();
62                 }
63                 catch (MailException exc) {
64                         StringBuilder toList = new StringBuilder();
65                         for (String recipient : recipients) {
66                                 if (toList.length() > 0) {
67                                         toList.append(", ");
68                                 }
69                                 toList.append(recipient);
70                         }
71                         pw.println(m_bundle.get(CfbBundle.CANNOT_SEND_MAIL, toList.toString(), exc.toString()));
72                         exc.printStackTrace(pw);
73                 }
74                 finally {
75                         if (null != mailWriter) {
76                                 mailWriter.close();
77                         }
78                 }
79         }
80 }