Address some edge cases related to bootstrapping a fresh system.
[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.cfb.store.Run;
10 import net.jaekl.qd.util.MailException;
11 import net.jaekl.qd.util.MimePart;
12 import net.jaekl.qd.util.SendMail;
13
14 public class Notifier {
15         private static final String TEXT_HTML = "text/html";
16         
17         CfbBundle m_bundle;
18         Config m_config;
19
20         public Notifier(CfbBundle bundle, Config config) {
21                 m_bundle = bundle;
22                 m_config = config;
23         }
24         
25         public void sendEmailIfNeeded(PrintWriter pw, HtmlReport report) {
26                 Delta delta = report.getDelta();
27                 if ((delta.getNumNew() > 0) || (delta.getNumFixed() > 0)) {
28                         sendEmail(pw, report);
29                 }
30         }
31
32         // --- end of public interface ---
33         
34         String constructSubject(HtmlReport report) {
35                 String earlier;
36                 Run earlierRun = report.getDelta().getEarlier();
37                 if (null == earlierRun) {
38                         earlier = m_bundle.get(CfbBundle.NO_EARLIER_RUN);
39                 }
40                 else {
41                         earlier = report.getDelta().getEarlier().constructVersionText(m_bundle);
42                 }
43                 
44                 String later = report.getDelta().getLater().constructVersionText(m_bundle);
45
46                 String subject = m_bundle.get(CfbBundle.CFB_MAIL_SUBJECT, earlier, later);
47                 return subject;
48         }
49
50         SendMail createSendMail() {
51                 return new SendMail();
52         }
53         
54         void sendEmail(PrintWriter pw, HtmlReport report) {
55                 SendMail sendMail = createSendMail();
56                 sendMail.setSmtpHost(m_config.getMailSmtpHost());
57                 
58                 ArrayList<String> recipients = m_config.getNotify();
59                 if (recipients.size() < 1) {
60                         return;
61                 }
62                 
63                 PrintWriter mailWriter = null;
64                 
65                 try {
66                         sendMail.setSubject(constructSubject(report));
67                         sendMail.setFrom(m_config.getMailFrom());
68                         
69                         for (String recipient : recipients) {
70                                 sendMail.addTo(recipient);
71                         }
72                         
73                         StringWriter sw = new StringWriter();
74                         mailWriter = new PrintWriter(sw);
75                         report.write(mailWriter);
76                         mailWriter.flush();
77
78                         MimePart part = new MimePart(TEXT_HTML, sw.toString());
79                         sendMail.addPart(part);
80                         sendMail.send();
81                 }
82                 catch (MailException exc) {
83                         StringBuilder toList = new StringBuilder();
84                         for (String recipient : recipients) {
85                                 if (toList.length() > 0) {
86                                         toList.append(", ");
87                                 }
88                                 toList.append(recipient);
89                         }
90                         pw.println(m_bundle.get(CfbBundle.CANNOT_SEND_MAIL, toList.toString(), exc.toString()));
91                         exc.printStackTrace(pw);
92                 }
93                 finally {
94                         if (null != mailWriter) {
95                                 mailWriter.close();
96                         }
97                 }
98         }
99 }