+
+ static ArrayList<Object> getRaisedBills (int client_id) {
+ try {
+ Connection conn = DriverManager.getConnection(JDBC + connectionURL);
+ PreparedStatement st = conn.prepareStatement("SELECT ReferenceNumber, "
+ + "Description, BillDate, BillAmount"
+ + " FROM legalnotices WHERE BillStatus='RAISED' AND ClientId=?;");
+ st.setInt (1, client_id);
+
+ ResultSet rs = st.executeQuery ();
+ ArrayList<Object> bills = new ArrayList<>();
+ while (rs.next()) {
+ bills.add (rs.getString("ReferenceNumber"));
+ bills.add (rs.getString("Description"));
+ bills.add (getValidDate(rs, "BillDate"));
+ bills.add (rs.getInt("BillAmount"));
+ }
+ conn.close();
+ return bills;
+
+ } catch (SQLException ex) {
+ Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+ return null;
+ }
+
+ }
+
+ static boolean generateRaisedNoticesBill(String fileName, String clientName,
+ String clientAddress, String contactPerson, ArrayList<Object> bills) {
+ if (bills == null)
+ return false;
+ try {
+ InputStream templ = Utility.class.getClassLoader().getResourceAsStream("resources/noticebill.template.fodt");
+ if (templ == null)
+ return false;
+ DateFormat fmt = new SimpleDateFormat("dd MMM yyyy");
+ String templateMain = new String (templ.readAllBytes());
+ // this is for openoffice ODT - replace normal line breaks with the XML equivalent
+ String left = leftLetterHeader.replaceAll("\n", "<text:line-break/>");
+ String right = rightLetterHeader.replaceAll("\n", "<text:line-break/>");
+ String client_address = clientAddress.replaceAll ("\n", "<text:line-break/>");
+ String rows = generateBillRows (bills);
+ System.out.println (rows);
+
+ String strMain = MessageFormat.format(templateMain,
+ left, right,
+ fmt.format(new Date(System.currentTimeMillis())),
+ clientName, client_address, contactPerson, signatoryName, rows);
+
+ FileOutputStream f = new FileOutputStream (fileName);
+ f.write (strMain.getBytes());
+ f.close();
+ return true;
+ } catch (IOException ex) {
+ Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+ return false;
+ }
+ }
+
+ private static String generateBillRows(ArrayList<Object> bills) {
+ try {
+ InputStream tmpl = Utility.class.getClassLoader().getResourceAsStream("resources/tablerow.template.xml");
+ String rowtpl = new String(tmpl.readAllBytes());
+
+ DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
+
+ StringBuilder bldr = new StringBuilder ();
+ for (int i = 0; i < bills.size(); i += 4) {
+ String row = MessageFormat.format (rowtpl, (String)bills.get(i),
+ (String)bills.get(i+1), fmt.format((java.util.Date)bills.get(i+2)),
+ (int)bills.get(i+3));
+ bldr.append(row);
+ }
+ return (bldr.toString());
+ } catch (IOException ex) {
+ Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+ return null;
+ }
+ }
+
+ static boolean updateNoticeBillStatus(int client_id, String from_status,
+ String to_status) {
+ try {
+ Connection conn = DriverManager.getConnection(JDBC + connectionURL);
+ PreparedStatement st = conn.prepareStatement("UPDATE legalnotices"
+ + " SET BillStatus=? WHERE BillStatus=? AND ClientId=?;");
+ st.setString (1, to_status);
+ st.setString (2, from_status);
+ st.setInt (3, client_id);
+ st.execute();
+ conn.close();
+ return true;
+ } catch (SQLException ex) {
+ Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
+ return false;
+ }
+
+ }
+
+ static ArrayList<Object> getReportData(int report_num, int clientid) {
+ ArrayList<Object> data = new ArrayList<>();
+ try {
+ Connection conn = DriverManager.getConnection(JDBC + connectionURL);
+
+ // if no client ID is specified i.e. -1 get ll
+ ResultSet rs;
+ if (clientid == -1) {
+ Statement st = conn.createStatement();
+ rs = st.executeQuery(REPORTS_UNFILTERED[report_num]);
+ }
+ else {
+ PreparedStatement st = conn.prepareStatement(REPORTS_FILTERED[report_num]);
+ st.setInt (1, clientid);
+ rs = st.executeQuery();
+ }
+
+ while (rs.next()) {
+ ResultSetMetaData md = rs.getMetaData();
+ SimpleDateFormat fmt = new SimpleDateFormat ("dd MMM yyyy");
+ for (int i = 1; i <= md.getColumnCount(); i ++ ) {
+ // for INTEGER Columns which are date alone, handle separately
+ if (md.getColumnName(i).contains("Date"))
+ data.add (fmt.format(getValidDate(rs, md.getColumnName(i))));
+ else
+ data.add (rs.getObject (i));
+ }
+ }
+ 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());
+ }
+
+