Add ability to output HTML report of differences found between two versions.
[cfb.git] / prod / net / jaekl / cfb / CFB.java
index 28d2b1c698dc437113d58e1899596f4882fc32b6..94ef08f8a996cecf7ce681574b95bc3fd1ad62f9 100644 (file)
@@ -18,8 +18,11 @@ import java.util.Locale.Category;
 
 import net.jaekl.cfb.analyze.Analysis;
 import net.jaekl.cfb.analyze.Analyzer;
+import net.jaekl.cfb.analyze.Delta;
+import net.jaekl.cfb.analyze.HtmlReport;
 import net.jaekl.cfb.analyze.MessageMap;
 import net.jaekl.cfb.db.CfbSchema;
+import net.jaekl.cfb.db.TypeMismatchException;
 import net.jaekl.cfb.db.driver.DbDriver;
 import net.jaekl.cfb.db.driver.PostgresqlDriver;
 import net.jaekl.cfb.store.DbStore;
@@ -47,6 +50,8 @@ public class CFB {
        String m_user;  // db user
        String m_pass;  // db password
        String m_buildNum; // build number (version)
+       boolean m_removeSchema; // purge DB schema
+       File m_output;  // File to which we should write our output (report)
        
        CFB(Locale locale) {
                m_driver = new PostgresqlDriver();
@@ -62,18 +67,22 @@ public class CFB {
                m_pass = "";
                m_user = "user";
                m_buildNum = null;
+               m_removeSchema = false;
+               m_output = null;
        }
        
        Options createOptions() {
                Options opt = new Options();
                
-               opt.addOption("d", "dbname", true, "DB name");
-               opt.addOption("f", "fbp",    true, "FindBugsProject file");
-               opt.addOption("h", "host",   true, "DB hostname");
-               opt.addOption("n", "number", true, "Build number (version)");
-               opt.addOption("p", "pass",   true, "DB password");
-               opt.addOption("t", "port",   true, "DB port");
-               opt.addOption("u", "user",   true, "DB username");
+               opt.addOption("d",  "dbname",      true,  "DB name");
+               opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
+               opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
+               opt.addOption("h",  "host",        true,  "DB hostname");
+               opt.addOption("n",  "number",      true,  "Build number (version)");
+               opt.addOption("o",  "outfile",     true,  "Output report filename");
+               opt.addOption("p",  "pass",        true,  "DB password");
+               opt.addOption("t",  "port",        true,  "DB port");
+               opt.addOption("u",  "user",        true,  "DB username");
                
                return opt;
        }
@@ -95,9 +104,13 @@ public class CFB {
                        if (line.hasOption("n")) {
                                m_buildNum = line.getOptionValue("n");
                        }
+                       if (line.hasOption("o")) {
+                               m_output = new File(line.getOptionValue("o"));
+                       }
                        if (line.hasOption("p")) {
                                m_pass = line.getOptionValue("p");
                        }
+                       m_removeSchema = line.hasOption("drop-tables");
                        if (line.hasOption("t")) {
                                m_port = Integer.parseInt(line.getOptionValue("t"));
                        }
@@ -147,24 +160,32 @@ public class CFB {
                }
        } 
        
-       void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException {
+       void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
                initArgs();     // read environment and system properties
                if ( ! parseArgs(pw, args) ) {
                        return;
                }
+
+               File findBugsDir = getFindBugsDir();
+               File workDir = new File(".");
+               MessageMap messageMap = new MessageMap();
+               messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
                
                try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
-                       m_schema.ensureDbInitialized(con);                      
+                       m_schema.setMessageMap(messageMap);
+                       
+                       if (m_removeSchema) {
+                               m_schema.purge(con);
+                               return;
+                       }
+                       m_schema.ensureDbInitialized(con);
+                       messageMap.loadIds(con, m_driver);
                }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
                        return;
                }
                
-               File findBugsDir = getFindBugsDir();
-               File workDir = new File(".");
-               MessageMap messageMap = new MessageMap();
-               messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
                Analyzer analyzer = new Analyzer(messageMap);
                Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_buildNum);
                if (null == analysis) {
@@ -173,9 +194,14 @@ public class CFB {
                }
                
                try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
-                       DbStore store = new DbStore(con);
+                       DbStore store = new DbStore(con, m_driver, messageMap.getColl());
                        
                        store.put(analysis);
+                       Analysis prior = store.getPrior(analysis);
+                       Delta delta = new Delta(prior, analysis);
+
+                       HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl());
+                       report.write(m_output, delta);
                }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
@@ -187,6 +213,11 @@ public class CFB {
                String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
                String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
                exc.printStackTrace(pw);
+               SQLException next = exc.getNextException();
+               while (null != next) {
+                       next.printStackTrace(pw);
+                       next = next.getNextException();
+               }
                pw.println(cannotConnect);
        }
        
@@ -196,7 +227,7 @@ public class CFB {
                try (PrintWriter pw = new PrintWriter(System.out)){
                        cfb.doMain(pw, args);
                        pw.flush();
-               } catch (SQLException | IOException | XmlParseException | SAXException exc) {
+               } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
                        exc.printStackTrace();
                }
        }