import java.sql.*;
//import java.util.Properties;
import java.io.*;
import java.util.Hashtable;
import java.util.Date;
import java.text.*;
public class Main {
public static void main(String[] args) throws java.lang.InterruptedException, FileNotFoundException {
//Connection DataSync DB
Connection aConn = null;
Hashtable<Integer, Object[]> htres = new Hashtable<Integer, Object[]>();
//Load Properties Variables
DSReadProperties.load(DSConst.DSProps);
DSwriteLog.load();
Date daynow = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfYear = new SimpleDateFormat("yy");
String Year = String.valueOf(sdfYear.format(daynow));
DSwriteLog.log("[ " + String.valueOf(sdf.format(daynow))+ " ] Start DataSync...");
try {
// Unzip File
DSZip dsZip = new DSZip();
try {
DSwriteLog.log("Start unzip inbox and backup to inbox_bak .....");
String[] inboxZipList = DSFolder.getFolderList(DSFolder.inbox,".zip");
System.out.println("Inbox contains "+inboxZipList.length);
for(int i=0; i < inboxZipList.length; i++ ) {
dsZip.unzip(DSFolder.inbox+"/"+inboxZipList[i]+".zip", DSFolder.inbox);
DSFolder.bakInboxXML(DSFolder.inbox, inboxZipList[i], ".zip");
}
//dsZip.zip("bak.zip",".\\bak");
}
catch(Exception e) {
//e.printStackTrace(System.out);
DSwriteLog.log("Unzip inbox fail.....");
}
//get file list under import folder "inbox"
DSwriteLog.log("get file list under import folder inbox .....");
String[] inboxFolderList = DSFolder.getFolderList(DSFolder.inbox,".xml");
if(inboxFolderList==null)
inboxFolderList = new String[0];
//connect main DB
aConn = DBConn.getConnection(
DSReadProperties.getProperty(DSConst.jdbc_DSDB_driverClassName),
DSReadProperties.getProperty(DSConst.jdbc_DSDB_url),
DSReadProperties.getProperty(DSConst.jdbc_DSDB_username),
DSReadProperties.getProperty(DSConst.jdbc_DSDB_password));
//initial DataSync List
String asql = "SELECT DSID,DBID,tblname,tblquery,synctype,nextNum,upQuery,upType FROM SYNCINFO WHERE direction=" +
DSReadProperties.getProperty( DSConst.jdbc_DS_direction );
htres = DBConn.SQLtoHashtable(aConn, asql);
DBConn.closeConnection(aConn);
//split into String[]
Hashtable<Integer, Object[]> SQLini = new Hashtable<Integer, Object[]>();
Hashtable<Integer, Object[]> SQLimp = new Hashtable<Integer, Object[]>();
Hashtable<Integer, Object[]> SQLcont = new Hashtable<Integer, Object[]>();
Hashtable<Integer, Object[]> SQLend = new Hashtable<Integer, Object[]>();
int countI = 1;
int countP = 1;
int countC = 1;
int countE = 1;
for(int j=3; j<= htres.size(); j++){
Object[] arres = (Object[])(htres.get(j));
int n = Integer.parseInt(arres[5].toString());
if( n==1 ){
SQLini.put(countI, arres);
countI ++;
} else if( n==2) {
SQLimp.put(countP, arres);
countP ++;
} else if(n== 3) {
SQLcont.put(countC, arres);
countC ++;
} else if(n==4) {
SQLend.put(countE, arres);
countE ++;
}
arres = null;
}
System.out.println("Total: "+ htres.size() +"\t ini:"+ SQLini.size() +"\t inbox:" + SQLimp.size()
+"\t outbox:" + SQLcont.size() +"\t end:" + SQLend.size());
htres = null;
//for(int i =1; i < SQLimp.size();i++ ){
// Object[] arres = (Object[])(SQLimp.get(i));
// System.out.print(arres[1].toString());
//}
//start process
for(int i=0; i < DSConst.DSdbDriver.length; i++) {
if (! DSReadProperties.getProperty(DSConst.DSdbDriver[i]).isEmpty()) {
aConn = DBConn.getConnection(
DSReadProperties.getProperty(DSConst.DSdbDriver[i]),
DSReadProperties.getProperty(DSConst.DSdbUrl[i]),
DSReadProperties.getProperty(DSConst.DSdbUsername[i]),
DSReadProperties.getProperty(DSConst.DSdbpassword[i]));
String DBName = DSReadProperties.getProperty(DSConst.DSdbDBID[i]);
//Update records from inBox
//-Convert xML to object[]
//object[] update DataBase
DSwriteLog.log(DBName+" - Read XML from inbox....");
for(int j=1; j<= SQLimp.size(); j++){
Object[] arres = (Object[])(SQLimp.get(j));
if( DBName.equals(arres[2].toString()) ) {
//update DB
for(int k=0; k<inboxFolderList.length; k++) {
String xmlname;
if (inboxFolderList[k].contains("_"+Year))
xmlname = inboxFolderList[k].substring(0, inboxFolderList[k].indexOf("_"+Year));
else
xmlname = inboxFolderList[k];
if (xmlname.equals(arres[3].toString())) {
DSwriteLog.log(" " + inboxFolderList[k] + ".xml" );
if (DSUtils.null2String(arres[8]).equals("XF") ) {
Hashtable<Integer, Object[]> htxfer = DSXfer.getXML(inboxFolderList[k]) ;
if ( htxfer.size() > 0) {
//System.out.println(arres[3].toString() + " Insert Row " + htxfer.size());
DSXfer.prepareSqlStatment(aConn, htxfer, arres);
}
} else {
Object[] content = DSparseXML.getXML(inboxFolderList[k]);
for(int jj = 1; jj < content.length; jj++){
if (content.toString().startsWith("'UPDATE")) {
DBConn.SQLUpdate( aConn,content[jj].toString() );
} else {
String sql = String.format(content[0].toString(),content[jj].toString());
DBConn.SQLUpdate( aConn,sql);
}
}
} //end if XF
} //end if xmlname
DSFolder.deleteInBoxXML(DSFolder.inbox, inboxFolderList[k],".xml");
} //end for k
}
} //end for j
// initial table database for data sync
// set Pflag = 4
DSwriteLog.log(DBName+" - Prepare datasync status....");
for(int j=1; j<= SQLini.size(); j++){
Object[] arres = (Object[])(SQLini.get(j));
if( DBName.equals(arres[2].toString()) ) {
//update DB
DBConn.SQLUpdate( aConn,arres[4].toString() );
}
}
//add tmp folder for generating xml
DSFolder.addFolder(DSFolder.outboxTmp);
//generate XML
DSwriteLog.log(DBName+" - Export XML to outbox....");
for(int j=1; j<= SQLcont.size(); j++){
//String[] arres = (String[])(SQLcont.get(j));
Object[] arres = (Object[])(SQLcont.get(j));
if( DBName.equals(arres[2].toString()) ) {
//Export records to exBOX
//- convert DB records to Hashtable
// Hashtable(1) = Column Name; Hashtable(2)~(size) = results
//- put Hashtable to XML with file name arres[3]
Hashtable<Integer, Object[]> htrows = new Hashtable<Integer, Object[]>();
if (DSUtils.null2String(arres[8]).equals("XF") ) {
htrows = DBConn.SQLtoHashtable(aConn, arres[4].toString() );
//Set NextNum = new
//if (htrows.size() > 3)
// SQLcont.get(j)[6] = htrows.get(htrows.size())[1];
DSXfer.genXML(htrows, arr