Added Reporting Functionality
[habeas.git] / src / habeas / Utility.java
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 () {
         
     }