Added Reporting Functionality
authorHarishankar <v.harishankar@gmail.com>
Wed, 1 Apr 2020 17:09:06 +0000 (22:39 +0530)
committerHarishankar <v.harishankar@gmail.com>
Wed, 1 Apr 2020 17:09:06 +0000 (22:39 +0530)
Added functionality for reports and exporting reports to CSV files.
Also added the functionality for the first report:
bills awaiting payments.

src/habeas/MainFrame.form
src/habeas/MainFrame.java
src/habeas/NoticesReportViewDialog.form [new file with mode: 0644]
src/habeas/NoticesReportViewDialog.java [new file with mode: 0644]
src/habeas/Utility.java

index 4545146..d251e2f 100644 (file)
                 <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="menuGenerateRaisedBillsActionPerformed"/>
               </Events>
             </MenuItem>
+            <Menu class="javax.swing.JMenu" name="menuNoticesReports">
+              <Properties>
+                <Property name="mnemonic" type="int" value="114"/>
+                <Property name="text" type="java.lang.String" value="Reports"/>
+              </Properties>
+              <SubComponents>
+                <MenuItem class="javax.swing.JMenuItem" name="menuAwaitingPayment">
+                  <Properties>
+                    <Property name="mnemonic" type="int" value="97"/>
+                    <Property name="text" type="java.lang.String" value="Bills awaiting payment..."/>
+                  </Properties>
+                  <Events>
+                    <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="menuAwaitingPaymentActionPerformed"/>
+                  </Events>
+                </MenuItem>
+              </SubComponents>
+            </Menu>
           </SubComponents>
         </Menu>
         <Menu class="javax.swing.JMenu" name="menuSettings">
index f20df55..4c7d97b 100644 (file)
@@ -41,6 +41,8 @@ public class MainFrame extends javax.swing.JFrame {
         menuManageNotices = new javax.swing.JMenuItem();
         jSeparator1 = new javax.swing.JPopupMenu.Separator();
         menuGenerateRaisedBills = new javax.swing.JMenuItem();
+        menuNoticesReports = new javax.swing.JMenu();
+        menuAwaitingPayment = new javax.swing.JMenuItem();
         menuSettings = new javax.swing.JMenu();
         menuDatabaseSettings = new javax.swing.JMenuItem();
         menuStationery = new javax.swing.JMenuItem();
@@ -94,6 +96,20 @@ public class MainFrame extends javax.swing.JFrame {
         });
         menuLawyerNotices.add(menuGenerateRaisedBills);
 
+        menuNoticesReports.setMnemonic('r');
+        menuNoticesReports.setText("Reports");
+
+        menuAwaitingPayment.setMnemonic('a');
+        menuAwaitingPayment.setText("Bills awaiting payment...");
+        menuAwaitingPayment.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                menuAwaitingPaymentActionPerformed(evt);
+            }
+        });
+        menuNoticesReports.add(menuAwaitingPayment);
+
+        menuLawyerNotices.add(menuNoticesReports);
+
         jMenuBar1.add(menuLawyerNotices);
 
         menuSettings.setMnemonic('S');
@@ -171,11 +187,21 @@ public class MainFrame extends javax.swing.JFrame {
         frm.setVisible(true);
 
     }//GEN-LAST:event_menuGenerateRaisedBillsActionPerformed
-    private static final String ERROR_RETRIEVING_RAISED_BILLS = "Error retrieving bills"
-            + " or no bills are raised at this time.";
-    private static final String FODT_EXT = "fodt";
-    private static final String FLAT_OPENDOCUMENT_TEXT = "Flat OpenDocument text";
-    private static final String ERROR_GENERATING = "Error generating";
+
+    private void menuAwaitingPaymentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAwaitingPaymentActionPerformed
+        // TODO add your handling code here:
+        NoticesReportViewDialog frm = new NoticesReportViewDialog(this, false);
+        String [] cols = {"Reference Number", "Description", "Bill Date", "Bill Amount", "Client"};
+        ArrayList<Object> data = Utility.getPendingPaymentBills ();
+        if (data == null) {
+            JOptionPane.showMessageDialog(this, ERROR_DISPLAYING_REPORT);
+            return;
+        }
+        frm.setupReportTable(REPORT_TITLE_BILLS_AWAITING_PAYMENT, cols, data);
+        frm.setVisible(true);
+    }//GEN-LAST:event_menuAwaitingPaymentActionPerformed
+    private static final String ERROR_DISPLAYING_REPORT = "Error displaying report";
+    private static final String REPORT_TITLE_BILLS_AWAITING_PAYMENT = "Bills Awaiting Payment";
 
     /**
      * @param args the command line arguments
@@ -215,6 +241,7 @@ public class MainFrame extends javax.swing.JFrame {
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JMenuBar jMenuBar1;
     private javax.swing.JPopupMenu.Separator jSeparator1;
+    private javax.swing.JMenuItem menuAwaitingPayment;
     private javax.swing.JMenuItem menuClientMaster;
     private javax.swing.JMenuItem menuCreateNotice;
     private javax.swing.JMenuItem menuDatabaseSettings;
@@ -222,6 +249,7 @@ public class MainFrame extends javax.swing.JFrame {
     private javax.swing.JMenu menuLawyerNotices;
     private javax.swing.JMenuItem menuManageNotices;
     private javax.swing.JMenu menuMaster;
+    private javax.swing.JMenu menuNoticesReports;
     private javax.swing.JMenu menuSettings;
     private javax.swing.JMenuItem menuStationery;
     // End of variables declaration//GEN-END:variables
diff --git a/src/habeas/NoticesReportViewDialog.form b/src/habeas/NoticesReportViewDialog.form
new file mode 100644 (file)
index 0000000..7578904
--- /dev/null
@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
+  <NonVisualComponents>
+    <Container class="javax.swing.JPopupMenu" name="popMenu">
+      <Properties>
+        <Property name="label" type="java.lang.String" value="Popup"/>
+      </Properties>
+
+      <Layout class="org.netbeans.modules.form.compat2.layouts.DesignAbsoluteLayout">
+        <Property name="useNullLayout" type="boolean" value="true"/>
+      </Layout>
+      <SubComponents>
+        <MenuItem class="javax.swing.JMenuItem" name="menuExportCSV">
+          <Properties>
+            <Property name="mnemonic" type="int" value="67"/>
+            <Property name="text" type="java.lang.String" value="Export as CSV..."/>
+          </Properties>
+          <Events>
+            <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="menuExportCSVActionPerformed"/>
+          </Events>
+        </MenuItem>
+      </SubComponents>
+    </Container>
+  </NonVisualComponents>
+  <Properties>
+    <Property name="defaultCloseOperation" type="int" value="2"/>
+    <Property name="title" type="java.lang.String" value="View Report"/>
+  </Properties>
+  <SyntheticProperties>
+    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
+    <SyntheticProperty name="generateCenter" type="boolean" value="false"/>
+  </SyntheticProperties>
+  <AuxValues>
+    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
+    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
+    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
+    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
+    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
+    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
+  </AuxValues>
+
+  <Layout>
+    <DimensionLayout dim="0">
+      <Group type="103" groupAlignment="0" attributes="0">
+          <Group type="102" attributes="0">
+              <Component id="labelReportTitle" max="32767" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+          </Group>
+          <Component id="jScrollPane2" pref="682" max="32767" attributes="0"/>
+      </Group>
+    </DimensionLayout>
+    <DimensionLayout dim="1">
+      <Group type="103" groupAlignment="0" attributes="0">
+          <Group type="102" alignment="0" attributes="0">
+              <Component id="labelReportTitle" min="-2" pref="21" max="-2" attributes="0"/>
+              <EmptySpace max="-2" attributes="0"/>
+              <Component id="jScrollPane2" pref="354" max="32767" attributes="0"/>
+          </Group>
+      </Group>
+    </DimensionLayout>
+  </Layout>
+  <SubComponents>
+    <Component class="javax.swing.JLabel" name="labelReportTitle">
+      <Properties>
+        <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
+          <Font name="Noto Sans" size="15" style="1"/>
+        </Property>
+        <Property name="horizontalAlignment" type="int" value="0"/>
+        <Property name="text" type="java.lang.String" value="REPORT TITLE"/>
+      </Properties>
+    </Component>
+    <Container class="javax.swing.JScrollPane" name="jScrollPane2">
+      <AuxValues>
+        <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
+      </AuxValues>
+
+      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
+      <SubComponents>
+        <Component class="javax.swing.JTable" name="tableReport">
+          <Properties>
+            <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
+              <Table columnCount="0" rowCount="0"/>
+            </Property>
+            <Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor">
+              <TableColumnModel selectionModel="0"/>
+            </Property>
+            <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor">
+              <TableHeader reorderingAllowed="true" resizingAllowed="true"/>
+            </Property>
+          </Properties>
+        </Component>
+      </SubComponents>
+    </Container>
+  </SubComponents>
+</Form>
diff --git a/src/habeas/NoticesReportViewDialog.java b/src/habeas/NoticesReportViewDialog.java
new file mode 100644 (file)
index 0000000..ed0932d
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package habeas;
+
+import java.util.ArrayList;
+import javax.swing.JFileChooser;
+import javax.swing.JOptionPane;
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableModel;
+
+/**
+ *
+ * @author hari
+ */
+public class NoticesReportViewDialog extends javax.swing.JDialog {
+
+    private String reportTitle;
+    private String[] reportCols;
+    private ArrayList<Object> reportData;
+
+    /**
+     * Creates new form NoticesReportViewDialog
+     */
+    public NoticesReportViewDialog(java.awt.Frame parent, boolean modal) {
+        super(parent, modal);
+        initComponents();
+    }
+
+    /**
+     * This method is called from within the constructor to initialize the form.
+     * WARNING: Do NOT modify this code. The content of this method is always
+     * regenerated by the Form Editor.
+     */
+    @SuppressWarnings("unchecked")
+    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
+    private void initComponents() {
+
+        popMenu = new javax.swing.JPopupMenu();
+        menuExportCSV = new javax.swing.JMenuItem();
+        labelReportTitle = new javax.swing.JLabel();
+        jScrollPane2 = new javax.swing.JScrollPane();
+        tableReport = new javax.swing.JTable();
+
+        popMenu.setLabel("Popup");
+
+        menuExportCSV.setMnemonic('C');
+        menuExportCSV.setText("Export as CSV...");
+        menuExportCSV.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                menuExportCSVActionPerformed(evt);
+            }
+        });
+        popMenu.add(menuExportCSV);
+
+        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
+        setTitle("View Report");
+
+        labelReportTitle.setFont(new java.awt.Font("Noto Sans", 1, 15)); // NOI18N
+        labelReportTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
+        labelReportTitle.setText("REPORT TITLE");
+
+        tableReport.setModel(new javax.swing.table.DefaultTableModel(
+            new Object [][] {
+
+            },
+            new String [] {
+
+            }
+        ));
+        jScrollPane2.setViewportView(tableReport);
+
+        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
+        getContentPane().setLayout(layout);
+        layout.setHorizontalGroup(
+            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(layout.createSequentialGroup()
+                .addComponent(labelReportTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                .addContainerGap())
+            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
+        );
+        layout.setVerticalGroup(
+            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(layout.createSequentialGroup()
+                .addComponent(labelReportTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
+        );
+
+        pack();
+    }// </editor-fold>//GEN-END:initComponents
+
+    private void menuExportCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExportCSVActionPerformed
+        // TODO add your handling code here:
+        JFileChooser chooser = new JFileChooser ();
+        int rt = chooser.showSaveDialog(this);
+        if (rt == JFileChooser.APPROVE_OPTION) {
+            String filePath = chooser.getSelectedFile().getAbsolutePath();
+            boolean ret = Utility.saveReportCSV (filePath, reportCols, reportData);
+            if (ret == false) 
+                JOptionPane.showMessageDialog(this, ERROR_SAVING_REPORT);
+            else 
+                JOptionPane.showMessageDialog(this, REPORT_SAVED_SUCCESSFULLY);
+        }
+    }//GEN-LAST:event_menuExportCSVActionPerformed
+    private static final String REPORT_SAVED_SUCCESSFULLY = "Report saved successfully";
+    private static final String ERROR_SAVING_REPORT = "Error saving report";
+
+    /**
+     * @param args the command line arguments
+     */
+    public static void main(String args[]) {
+        /* Set the Nimbus look and feel */
+        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
+        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
+         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
+         */
+        try {
+            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
+                if ("Nimbus".equals(info.getName())) {
+                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
+                    break;
+                }
+            }
+        } catch (ClassNotFoundException ex) {
+            java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        } catch (InstantiationException ex) {
+            java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        } catch (IllegalAccessException ex) {
+            java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
+            java.util.logging.Logger.getLogger(NoticesReportViewDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+        }
+        //</editor-fold>
+
+        /* Create and display the dialog */
+        java.awt.EventQueue.invokeLater(new Runnable() {
+            public void run() {
+                NoticesReportViewDialog dialog = new NoticesReportViewDialog(new javax.swing.JFrame(), true);
+                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
+                    @Override
+                    public void windowClosing(java.awt.event.WindowEvent e) {
+                        System.exit(0);
+                    }
+                });
+                dialog.setVisible(true);
+            }
+        });
+    }
+
+    // Variables declaration - do not modify//GEN-BEGIN:variables
+    private javax.swing.JScrollPane jScrollPane2;
+    private javax.swing.JLabel labelReportTitle;
+    private javax.swing.JMenuItem menuExportCSV;
+    private javax.swing.JPopupMenu popMenu;
+    private javax.swing.JTable tableReport;
+    // End of variables declaration//GEN-END:variables
+
+    public void setupReportTable(String title, String[] columns, 
+            ArrayList<Object> data) {
+        reportTitle = title;
+        reportCols = columns;
+        reportData = data;
+        DefaultTableModel mdl = new DefaultTableModel (columns, 0) {
+            @Override
+            public boolean isCellEditable (int row, int col) {
+                return false;
+            }
+        };
+        tableReport.setModel(mdl);
+        tableReport.setComponentPopupMenu(popMenu);
+        
+        labelReportTitle.setText(title);
+        for (int i = 0; i < data.size(); i += columns.length) {
+            Object[] row = new Object[columns.length];
+            for (int j = 0; j < columns.length; j ++)
+                row[j] = data.get(i+j);
+            mdl.addRow (row);
+        }
+    }
+}
index d9a231b..c0379a0 100644 (file)
@@ -7,6 +7,7 @@ package habeas;
 
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -501,6 +502,70 @@ public class Utility {
         }
         
     }
+
+    static ArrayList<Object> getPendingPaymentBills() {
+        ArrayList<Object> data = new ArrayList<>();
+        try {
+            Connection conn = DriverManager.getConnection(JDBC + connectionURL);
+            Statement st = conn.createStatement();
+            ResultSet rs = st.executeQuery("SELECT ReferenceNumber, Description, "
+                    + "BillDate, BillAmount, ClientName"
+                    + " FROM legalnotices INNER JOIN clients ON ClientId=clients.id "
+                    + "WHERE BillStatus='AWAITING PAYMENT';");
+            
+            DateFormat fmt = new SimpleDateFormat ("dd/MM/yyyy");
+            while (rs.next()) {
+                data.add (rs.getString("ReferenceNumber"));
+                data.add (rs.getString("Description"));
+                data.add (fmt.format (getValidDate(rs, "BillDate")));
+                data.add (rs.getInt ("BillAmount"));
+                data.add (rs.getString("ClientName"));
+            }
+            conn.close ();
+            return data;
+        } catch (SQLException ex) {
+            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+            return null;
+        }
+        
+    }
+
+    static boolean saveReportCSV(String filePath, String[] reportCols, ArrayList<Object> reportData) {
+        try {
+            FileWriter f = new FileWriter (filePath);
+            for (int i = 0; i < reportCols.length; i ++) {
+                f.append (escapeQuote(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 (", ");
+                }
+                f.append ("\n");
+            }
+            f.flush();
+            f.close();
+            return true;         
+        } catch (IOException ex) {
+            Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+            return false;
+        }
+        
+    }
+
+    private static String escapeQuote(Object bareStr) {
+        if (bareStr.getClass() == String.class) {
+            String str = (String)bareStr;
+            String escapedStr = str.replace ("\"", "\"\"");
+            System.out.println(escapedStr);
+            String finalStr = String.format ("\"%s\"", escapedStr);
+            return finalStr;
+        }
+        else
+            return String.format("\"%s\"", bareStr.toString());
+    }
     public Utility () {
         
     }