Reporting Functionality - added HTML export master
authorHarishankar <v.harishankar@gmail.com>
Fri, 3 Apr 2020 09:16:20 +0000 (14:46 +0530)
committerHarishankar <v.harishankar@gmail.com>
Fri, 3 Apr 2020 09:20:09 +0000 (14:50 +0530)
Added HTML export option to the report viewer.

src/habeas/NoticesReportViewDialog.form
src/habeas/NoticesReportViewDialog.java
src/habeas/Utility.java

index 2503a57..18268cb 100644 (file)
             <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="menuExportCSVActionPerformed"/>
           </Events>
         </MenuItem>
+        <MenuItem class="javax.swing.JMenuItem" name="menuExportHTML">
+          <Properties>
+            <Property name="mnemonic" type="int" value="72"/>
+            <Property name="text" type="java.lang.String" value="Export as HTML..."/>
+          </Properties>
+          <Events>
+            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="menuExportHTMLActionPerformed"/>
+          </Events>
+        </MenuItem>
       </SubComponents>
     </Container>
   </NonVisualComponents>
index 8852409..4563f64 100644 (file)
@@ -48,6 +48,7 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
 
         popMenu = new javax.swing.JPopupMenu();
         menuExportCSV = new javax.swing.JMenuItem();
+        menuExportHTML = new javax.swing.JMenuItem();
         labelReportTitle = new javax.swing.JLabel();
         jScrollPane2 = new javax.swing.JScrollPane();
         tableReport = new javax.swing.JTable();
@@ -66,6 +67,15 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
         });
         popMenu.add(menuExportCSV);
 
+        menuExportHTML.setMnemonic('H');
+        menuExportHTML.setText("Export as HTML...");
+        menuExportHTML.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                menuExportHTMLActionPerformed(evt);
+            }
+        });
+        popMenu.add(menuExportHTML);
+
         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
         setTitle("View Report");
         setLocationByPlatform(true);
@@ -166,6 +176,25 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
         }
         
     }//GEN-LAST:event_buttonApplyActionPerformed
+
+    private void menuExportHTMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportHTMLActionPerformed
+        // TODO add your handling code here:
+        JFileChooser chooser = new JFileChooser ();
+        FileNameExtensionFilter flt = new FileNameExtensionFilter("Hypertext Markup File (.html)", "html");
+        chooser.setFileFilter(flt);
+        int rt = chooser.showSaveDialog(this);
+        if (rt == JFileChooser.APPROVE_OPTION) {
+            String filepath = chooser.getSelectedFile().getAbsolutePath();
+            if (! (filepath.endsWith(".html") || filepath.endsWith(".htm")) ) 
+                filepath = filepath.concat(".html");
+            boolean ret= Utility.saveReportHTML (filepath, reportTitle, reportCols, reportData);
+            if (ret == false)
+                JOptionPane.showMessageDialog (this, ERROR_SAVING_REPORT);
+            else
+                JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
+            
+        }
+    }//GEN-LAST:event_menuExportHTMLActionPerformed
     private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
     private static final String ERROR_SAVING_REPORT = "Error saving report";
 
@@ -218,6 +247,7 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
     private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JLabel labelReportTitle;
     private javax.swing.JMenuItem menuExportCSV;
+    private javax.swing.JMenuItem menuExportHTML;
     private javax.swing.JPopupMenu popMenu;
     private javax.swing.JTable tableReport;
     // End of variables declaration//GEN-END:variables
index 8602583..192b113 100644 (file)
@@ -17,6 +17,7 @@ import java.text.MessageFormat;
 import java.text.SimpleDateFormat;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import org.sqlite.util.StringUtils;
 
 /**
  *
@@ -592,13 +593,13 @@ public class Utility {
         try {
             FileWriter f = new FileWriter (filePath);
             for (int i = 0; i < reportCols.length; i ++) {
-                f.append (escapeQuote(reportCols[i]));
+                f.append (escapeQuoteCSV(reportCols[i]));
                 f.append (",");
             }
             f.append("\n");
             for (int i = 0; i < reportData.size(); i += reportCols.length) {
                 for (int j = 0; j < reportCols.length; j ++) {
-                    f.append (escapeQuote(reportData.get(i+j)));
+                    f.append (escapeQuoteCSV(reportData.get(i+j)));
                     f.append (",");
                 }
                 f.append ("\n");
@@ -613,8 +614,10 @@ public class Utility {
         
     }
 
-    private static String escapeQuote(Object bareStr) {
-        if (bareStr.getClass() == String.class) {
+    private static String escapeQuoteCSV(Object bareStr) {
+        if (bareStr == null) 
+            return "";
+        else if (bareStr.getClass() == String.class) {
             String str = (String)bareStr;
             String escapedStr = str.replace ("\"", "\"\"");
             System.out.println(escapedStr);
@@ -625,6 +628,60 @@ public class Utility {
             return String.format("\"%s\"", bareStr.toString());
     }
 
+    static boolean saveReportHTML(String filepath, String report_title, 
+            String[] report_cols, ArrayList<Object> report_data) {
+        FileWriter htmlFile;
+        try {
+            htmlFile = new FileWriter(filepath);
+            htmlFile.append("<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<title>");
+            htmlFile.append(htmlEscape(report_title));
+            htmlFile.append("</title>\n</head>\n");
+            htmlFile.append ("<body>\n");
+            htmlFile.append("<h1>");
+            htmlFile.append(htmlEscape(report_title));
+            htmlFile.append("</h1>\n");
+            htmlFile.append ("<table>\n");
+            htmlFile.append ("\t<tr>\n");
+            for (int i = 0; i < report_cols.length; i ++) {
+                htmlFile.append ("\t\t<th>");
+                htmlFile.append (htmlEscape(report_cols[i]));
+                htmlFile.append("</th>\n");
+            }
+            htmlFile.append ("\t</tr>\n");
+            for (int r = 0; r < report_data.size(); r += report_cols.length) {
+                htmlFile.append("\t<tr>\n");
+                for (int c = 0; c < report_cols.length; c ++) {
+                    htmlFile.append("\t\t<td>");
+                    htmlFile.append(htmlEscape(report_data.get(r+c)));
+                    htmlFile.append("</td>\n");
+                }
+                htmlFile.append ("\t</tr>\n");
+            }
+            htmlFile.append("</table>\n");
+            htmlFile.append("</body>\n");
+            htmlFile.append("</html>");
+            htmlFile.flush();
+            htmlFile.close();
+            return true;
+        } catch (IOException ex) {
+            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+            return false;
+        }
+    }
+
+    private static String htmlEscape(Object barestr) {
+        if (barestr == null)
+            return "";
+        else if (barestr.getClass() == String.class) {
+            String barestrs = (String)barestr;
+            String str = barestrs.replace("&", "&amp;").replace("<", "&lt;").
+                    replace(">", "&gt;").replace("\"", "&quot;").replace("\n", "<br>");
+            return str;
+        }
+        else
+            return barestr.toString();
+    }
+
 
     public Utility () {