package reportbuilder; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.sql.Clob; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; import net.sf.jasperreports.engine.JRParameter; public class CreateReport implements Runnable { private ConnectionIndex conIndex; private String pkReport; private int conId; private String c1 = ";"; private String c2 = "!"; private String gt = ">"; private String lt = "<"; public CreateReport(ConnectionIndex conIndex, int conId, String rId) throws Exception { this.conIndex = conIndex; this.conId = conId; this.pkReport = rId; } @Override public void run() { try { Connection con = conIndex.getConnection(this.conId); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from cmreportprocessing where process_id = '" + pkReport + "'"); if (rs.next()) { if (rs.getInt("RUNTIME_STATUS") == 0) { String sqlUpdate = "update cmreportprocessing set runtime_status = 8, runtime_message = 'building', end_process = sysdate where process_id = '" + pkReport + "' "; sqlUpdate=ConnectionIndex.replaceOracle2Mysql(sqlUpdate); con.createStatement().execute(sqlUpdate); con.commit(); try { ReportBuilder.writeMessage("Accept report #" + rs.getString("label_name") + "#" + pkReport + " :: cid = " + conId + " building ... "); String criXml = null; try{criXml = clobToString(rs.getClob("criteria_xml"));}catch(Exception esd){} if(criXml==null){ criXml = rs.getString("criteria_xml"); } XmlParserReport xml = new XmlParserReport(criXml); for (int i = 0; i < xml.getRowCount(); i++) { String argText = xml.getItemString(i, "arguments").trim(); Map map = new HashMap(); map.put(JRParameter.REPORT_LOCALE, new Locale("en", "US")); if (!argText.equals("")) { String args[] = xml.getItemString(i, "arguments").split(c2); if (args[0].equals("SELECT")) { String sqlNew = args[1].replaceAll(this.gt, ">").replaceAll(this.lt, "<"); ResultSet rsNew = con.createStatement().executeQuery(sqlNew); new Exporter(rsNew, map, pkReport, rs.getString("label_name"), xml.getItemString(i, "report_name"), xml.getItemString(i, "report_file").replaceAll("[.]", ConnectionIndex.REPORT_SUFFIX+"."), xml.getItemString(i, "output_file"), xml.getItemString(i, "output_type"), xml.getItemString(i, "encoding")); } else { for (int a = 0; a < args.length; a++) { String arg[] = args[a].split(c1); String cName = arg[1]; String value = arg[2].replaceAll(this.gt, ">").replaceAll(this.lt, "<"); if (arg[0].equals("Date")) { SimpleDateFormat df ; Date date ; if(value.indexOf(":")>0) { df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = df.parse(value); }else{ df = new SimpleDateFormat("yyyy-MM-dd"); value=value.substring(0, 10).trim(); //System.out.println(value); date = df.parse(value); } map.put(cName, date); } else if (arg[0].equals("Integer")) { map.put(cName, new Integer(value)); } else if (args[0].equals("Long")) { map.put(cName, new Long(value)); } else if (args[0].equals("Float")) { map.put(cName, new Float(value)); } else if (args[0].equals("Double")) { map.put(cName, new Double(value)); } else if (args[0].equals("Time")) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = df.parse(value); map.put(cName, new java.sql.Time(date.getTime())); } else { map.put(cName, value); } } new Exporter(con, map, pkReport, rs.getString("label_name"), xml.getItemString(i, "report_name"), xml.getItemString(i, "report_file").replaceAll("[.]", ConnectionIndex.REPORT_SUFFIX+"."), xml.getItemString(i, "output_file"), xml.getItemString(i, "output_type"), xml.getItemString(i, "encoding")); } } } sqlUpdate = "update cmreportprocessing set runtime_status = 1, runtime_message = 'successful', end_process = sysdate where process_id = '" + pkReport + "' "; sqlUpdate=ConnectionIndex.replaceOracle2Mysql(sqlUpdate); con.createStatement().execute(sqlUpdate); con.commit(); ReportBuilder.writeMessage("Create report_id #" + pkReport + " finish successful\r\n"); } catch (Exception ex) { ex.printStackTrace(); sqlUpdate = "update cmreportprocessing set runtime_status = -1, runtime_message = '" + ex.getMessage().replaceAll("'", "''") + "', end_process = sysdate where process_id = '" + pkReport + "'"; sqlUpdate=ConnectionIndex.replaceOracle2Mysql(sqlUpdate); con.createStatement().execute(sqlUpdate); con.commit(); ReportBuilder.writeMessage("Error create report_id #" + pkReport + " :: " + ex.getMessage() + "\r\n"); } } else { ReportBuilder.writeMessage("Accept report_id #" + pkReport + " ignore status " + rs.getInt("RUNTIME_STATUS") + "\r\n"); } } rs.close(); } catch (Exception ex) { ex.printStackTrace(); } } private String clobToString(Clob data) { final StringBuilder sb = new StringBuilder(); try { final Reader reader = data.getCharacterStream(); final BufferedReader br = new BufferedReader(reader); int b; while (-1 != (b = br.read())) { sb.append((char) b); } br.close(); } catch (SQLException e) { //log.error("SQL. Could not convert CLOB to string", e); return e.toString(); } catch (IOException e) { //log.error("IO. Could not convert CLOB to string", e); return e.toString(); } return sb.toString(); } }