From 7b3591baf2fa1c09bf4f1ac0791400a2c5e9632f Mon Sep 17 00:00:00 2001 From: Harishankar Date: Wed, 1 Apr 2020 22:39:06 +0530 Subject: [PATCH] Added Reporting Functionality 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 | 17 +++ src/habeas/MainFrame.java | 38 ++++- src/habeas/NoticesReportViewDialog.form | 99 +++++++++++++ src/habeas/NoticesReportViewDialog.java | 184 ++++++++++++++++++++++++ src/habeas/Utility.java | 65 +++++++++ 5 files changed, 398 insertions(+), 5 deletions(-) create mode 100644 src/habeas/NoticesReportViewDialog.form create mode 100644 src/habeas/NoticesReportViewDialog.java diff --git a/src/habeas/MainFrame.form b/src/habeas/MainFrame.form index 4545146..d251e2f 100644 --- a/src/habeas/MainFrame.form +++ b/src/habeas/MainFrame.form @@ -56,6 +56,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/habeas/MainFrame.java b/src/habeas/MainFrame.java index f20df55..4c7d97b 100644 --- a/src/habeas/MainFrame.java +++ b/src/habeas/MainFrame.java @@ -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 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 index 0000000..7578904 --- /dev/null +++ b/src/habeas/NoticesReportViewDialog.form @@ -0,0 +1,99 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/habeas/NoticesReportViewDialog.java b/src/habeas/NoticesReportViewDialog.java new file mode 100644 index 0000000..ed0932d --- /dev/null +++ b/src/habeas/NoticesReportViewDialog.java @@ -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 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") + // //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(); + }// //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 */ + // + /* 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); + } + // + + /* 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 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); + } + } +} diff --git a/src/habeas/Utility.java b/src/habeas/Utility.java index d9a231b..c0379a0 100644 --- a/src/habeas/Utility.java +++ b/src/habeas/Utility.java @@ -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 getPendingPaymentBills() { + ArrayList 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 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 () { } -- 2.20.1