version 1.68, 2004/08/05 11:17:10
|
version 1.78, 2004/09/22 11:14:10
|
Line 26 import java.io.PrintStream;
|
Line 26 import java.io.PrintStream;
|
import java.io.UnsupportedEncodingException; |
import java.io.UnsupportedEncodingException; |
import java.net.URL; |
import java.net.URL; |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
|
import java.sql.ResultSet; |
import java.sql.SQLException; |
import java.sql.SQLException; |
import java.sql.Statement; |
import java.sql.Statement; |
import java.sql.Types; |
import java.sql.Types; |
import java.text.ParseException; |
import java.text.ParseException; |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.Hashtable; |
import java.util.Iterator; |
import java.util.Iterator; |
import java.util.List; |
import java.util.List; |
import java.util.Properties; |
|
import java.util.StringTokenizer; |
import java.util.StringTokenizer; |
import java.util.TreeSet; |
import java.util.TreeSet; |
import java.util.Vector; |
import java.util.Vector; |
|
|
|
import javax.swing.JDialog; |
|
import javax.swing.JLabel; |
|
import javax.swing.JPanel; |
|
|
import com.exploringxml.xml.Node; |
import com.exploringxml.xml.Node; |
import com.exploringxml.xml.Xparse; |
import com.exploringxml.xml.Xparse; |
|
|
class Convert |
class Convert |
{ |
{ |
|
/**Helper class for index creation |
|
* @author rogo |
|
* |
|
*/ |
|
public static class IndexList extends Vector |
|
{ |
|
public String toString() |
|
{ |
|
StringBuffer buff = new StringBuffer(1000); |
|
int count = 0; |
|
for (Iterator iter = this.iterator(); iter.hasNext();) |
|
{ |
|
String element = (String) iter.next(); |
|
if (count < elementCount - 1) |
|
{ |
|
buff.append(element).append(", "); |
|
count++; |
|
} else |
|
buff.append(element); |
|
|
|
} |
|
|
|
return buff.toString(); |
|
} |
|
} |
static DBBean bean = new DBBean(); |
static DBBean bean = new DBBean(); |
static DBBean beanDest = new DBBean(); |
static DBBean beanDest = new DBBean(); |
|
|
Line 51 class Convert
|
Line 81 class Convert
|
static boolean batchRun = false; |
static boolean batchRun = false; |
static Vector databases = new Vector(); |
static Vector databases = new Vector(); |
final static int numHits = 5000; |
final static int numHits = 5000; |
final static int numIntervalls = 2; |
final static int numIntervalls = 4; |
|
static boolean debug = true; |
public static void main(String args[]) |
public static void main(String args[]) |
{ |
{ |
/* try |
/* try |
Line 76 class Convert
|
Line 107 class Convert
|
{ |
{ |
e1.printStackTrace(); |
e1.printStackTrace(); |
} |
} |
PrintStream stream = new PrintStream(file); |
PrintStream stream = new PrintStream(file, false); |
// System.setOut(stream); |
if (!debug) |
//System.setErr(stream); |
{ |
|
System.setOut(stream); |
|
System.setErr(stream); |
|
} |
readXMLFile(args[0]); |
readXMLFile(args[0]); |
if (!(new File(args[0]).exists())) |
if (!(new File(args[0]).exists())) |
System.exit(0); |
System.exit(0); |
Line 1127 class Convert
|
Line 1160 class Convert
|
Vector selects = new Vector(); |
Vector selects = new Vector(); |
Vector creates = new Vector(); |
Vector creates = new Vector(); |
Vector ids = new Vector(); |
Vector ids = new Vector(); |
|
Vector indexListVec = new Vector(); |
String delimiter = "|"; |
String delimiter = "|"; |
int mode = -1; |
int mode = -1; |
|
|
Line 1146 class Convert
|
Line 1180 class Convert
|
selects = new Vector(); |
selects = new Vector(); |
creates = new Vector(); |
creates = new Vector(); |
ids = new Vector(); |
ids = new Vector(); |
|
indexListVec = new Vector(); |
// parse dataBase |
// parse dataBase |
Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 }); |
Node node = root.find("convert/source/database/url", new int[] { 1, 1, i, 1 }); |
Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 }); |
Node node1 = root.find("convert/source/database/user", new int[] { 1, 1, i, 1, 1 }); |
Line 1220 class Convert
|
Line 1255 class Convert
|
String name = (String) node4.attributes.get("name"); |
String name = (String) node4.attributes.get("name"); |
String layout = (String) node4.attributes.get("layout"); |
String layout = (String) node4.attributes.get("layout"); |
String id = (String) node4.attributes.get("id"); |
String id = (String) node4.attributes.get("id"); |
|
String indexList = (String) node4.attributes.get("indexList"); |
|
|
System.out.println("id was " + id); |
System.out.println("id was " + id); |
if (name == null) |
if (name == null) |
throw new Error("parse error required table tag attribute name missing"); |
throw new Error("parse error required table tag attribute name missing"); |
Line 1227 class Convert
|
Line 1264 class Convert
|
layout = ""; |
layout = ""; |
if (id == null) |
if (id == null) |
id = ""; |
id = ""; |
|
if (indexList == null) |
|
indexList = ""; |
if (name.equals("")) |
if (name.equals("")) |
throw new Error("parse error table tag attribute must not be empty"); |
throw new Error("parse error table tag attribute must not be empty"); |
tables.add(name); |
tables.add(name); |
layouts.add(layout); |
layouts.add(layout); |
|
|
ids.add(id); |
ids.add(id); |
|
indexListVec.add(indexList); |
String query = (node5 == null) ? "" : node5.getCharacters(); |
String query = (node5 == null) ? "" : node5.getCharacters(); |
if (query.equals("")) |
if (query.equals("")) |
System.err.println("Warning empty select tag or select tag missing !!"); |
System.err.println("Warning empty select tag or select tag missing !!"); |
Line 1245 class Convert
|
Line 1286 class Convert
|
} |
} |
DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode); |
DataBase dataBase = new DataBase(database, tables, layouts, selects, creates, ids, mode); |
dataBase.delimiter = delimiter; |
dataBase.delimiter = delimiter; |
|
dataBase.buildIndexTable(indexListVec); |
databases.add(dataBase); |
databases.add(dataBase); |
} |
} |
DBBean database = new DBBean(); |
DBBean database = new DBBean(); |
Line 1268 class Convert
|
Line 1310 class Convert
|
update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode); |
update(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode); |
else if (mode == DataBase.SYNCHRONIZE_MODE) |
else if (mode == DataBase.SYNCHRONIZE_MODE) |
{ |
{ |
user = bean.user; |
Convert.user = db.bean.user; |
passwd = bean.passwd; |
Convert.passwd =db.bean.passwd; |
userDest = database.user; |
userDest = database.user; |
passwdDest = database.passwd; |
passwdDest = database.passwd; |
|
|
synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode, db.delimiter); |
synchronize(db.bean.url, database.url, db.tables, db.layouts, db.selects, db.creates, db.ids, mode, db.delimiter, new Vector(db.htIndex.values())); |
} |
} |
} |
} |
// printContents(node3); |
// printContents(node3); |
Line 1575 class Convert
|
Line 1617 class Convert
|
Vector tables; |
Vector tables; |
Vector ids; |
Vector ids; |
String delimiter = "//"; |
String delimiter = "//"; |
|
/** |
|
* maps table name to index fields |
|
*/ |
|
Hashtable htIndex = new Hashtable(); |
boolean useNormanToUnicodeMapper = false; |
boolean useNormanToUnicodeMapper = false; |
|
|
final static int CONVERT_MODE = 1; |
final static int CONVERT_MODE = 1; |
Line 1597 class Convert
|
Line 1643 class Convert
|
this.bean.setIDVector(ids); |
this.bean.setIDVector(ids); |
} |
} |
/** |
/** |
|
* @param indexListVec |
|
*/ |
|
public void buildIndexTable(Vector indexListVec) |
|
{ |
|
for (int i = 0; i < tables.size(); i++) |
|
{ |
|
fillIndexList((String) tables.get(i), (String) indexListVec.get(i)); |
|
} |
|
} |
|
/** |
* writes the data contained in this object to the buffered writer |
* writes the data contained in this object to the buffered writer |
* * @param buffr |
* * @param buffr |
* @throws Exception |
* @throws Exception |
Line 1630 class Convert
|
Line 1686 class Convert
|
String select = (String) selects.get(index); |
String select = (String) selects.get(index); |
String create = (String) creates.get(index); |
String create = (String) creates.get(index); |
String id = (String) ids.get(index); |
String id = (String) ids.get(index); |
|
IndexList indexList = (IndexList) htIndex.get(table); |
buffr.write(" <table name = \"" + table + "\" layout = \"" + layout + "\" id = \"" + id + "\" >\n"); |
if (indexList == null) |
|
indexList = new IndexList(); |
|
buffr.write(" <table name = \"" + table + "\" layout = \"" + layout + "\" id = \"" + id + "\" indexList =\"" + indexList + "\">\n"); |
buffr.write(" <select>" + convertToEntities(select) + "</select>\n"); |
buffr.write(" <select>" + convertToEntities(select) + "</select>\n"); |
if (!create.equals("")) |
if (!create.equals("")) |
buffr.write(" <create>" + create + " </create>\n"); |
buffr.write(" <create>" + create + " </create>\n"); |
Line 1640 class Convert
|
Line 1698 class Convert
|
} |
} |
buffr.write(" </database>\n"); |
buffr.write(" </database>\n"); |
} |
} |
|
|
|
public void fillIndexList(String table, String list) |
|
{ |
|
IndexList indexList = new IndexList(); |
|
StringTokenizer tokenizer = new StringTokenizer(list, ","); |
|
while (tokenizer.hasMoreTokens()) |
|
{ |
|
indexList.add(tokenizer.nextToken()); |
|
} |
|
System.out.println(indexList); |
|
|
|
htIndex.put(table, indexList); |
|
} |
public String toString() |
public String toString() |
{ |
{ |
return bean.url + " " + tables; |
return bean.url + " " + tables; |
Line 1897 class Convert
|
Line 1968 class Convert
|
*/ |
*/ |
// TODO implement append,update and delete in one method |
// TODO implement append,update and delete in one method |
// TODO using id based algorithm |
// TODO using id based algorithm |
public static void synchronize(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter) throws Exception |
public static void synchronize(String source, String destination, Vector names, Vector layouts, Vector selects, Vector creates, Vector ids, int mode, String delimiter, Vector indexList) |
|
throws Exception |
{ |
{ |
System.out.println(" bin in synchronize!!!"); |
System.out.println(" bin in synchronize!!!"); |
FM2SQL.ProgressDialog dialog = null; |
FM2SQL.ProgressDialog dialog = null; |
Line 1994 class Convert
|
Line 2066 class Convert
|
int endIndex = -1; |
int endIndex = -1; |
String tempQuery = query; |
String tempQuery = query; |
String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName); |
String destQuery = query.replaceAll(names.get(tbIndex).toString(), destTableName); |
|
destQuery = destQuery.replaceAll(bean.getQC(), beanDest.getQC()); |
|
destQuery = removeLayoutPartFromQuery(destQuery, layouts.get(tbIndex).toString()); |
|
// TODO remove layout part for destQuery |
String tempQueryDest = destQuery; |
String tempQueryDest = destQuery; |
// remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1); |
// remove extra query parts destQuery.substring(0,destQuery.lastIndexOf(destTableName)+destTableName.length()+1); |
System.out.println("new Query " + tempQueryDest); |
System.out.println("new Query " + tempQueryDest); |
|
System.out.println("idfield "+idField+" "+ids.get(tbIndex).toString()); |
if (!idField.equals("")) |
if (!idField.equals("")) |
{ |
{ |
long startTime = System.currentTimeMillis(); |
long startTime = System.currentTimeMillis(); |
Line 2010 class Convert
|
Line 2086 class Convert
|
dialog.title.setText("Check if data is available"); |
dialog.title.setText("Check if data is available"); |
else if (dialog != null) |
else if (dialog != null) |
dialog.title.setText("Check if more data is available"); |
dialog.title.setText("Check if more data is available"); |
|
|
myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, 0); |
myIds = bean.getIDVector(ids.get(tbIndex).toString(), (String) names.get(tbIndex), tempQuery, 0); |
myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, 0); |
myIdsDest = beanDest.getIDVector(convertText(idField), destTableName, tempQueryDest, 0); |
//System.out.println("status of remove "+myIds.remove("b015892")); |
//System.out.println("status of remove "+myIds.remove("b015892")); |
|
System.out.println("ids found for "+idField+" "+!myIds.isEmpty()); |
if (myIds.isEmpty()) |
if (myIds.isEmpty()) |
break; |
break; |
vec = new Vector(myIds); |
vec = new Vector(myIds); |
Line 2028 class Convert
|
Line 2106 class Convert
|
linesToAppend.removeAll(vecDest); |
linesToAppend.removeAll(vecDest); |
// remove all lines that should not be deleted |
// remove all lines that should not be deleted |
linesToDelete.removeAll(deleted); |
linesToDelete.removeAll(deleted); |
System.out.println("linesToAppend " + linesToAppend + " " + destTableName); |
System.out.println("linesToAppend " + linesToAppend.size() + " " + destTableName); |
System.out.println("linesToDelete " + linesToDelete + " " + destTableName); |
System.out.println("linesToDelete " + linesToDelete.size() + " " + destTableName); |
System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size()); |
System.out.println("ID LIST SIZE " + Math.round((double) myIds.size() / (double) numIntervalls) + " " + myIds.size()); |
deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls); |
deltaID = (int) Math.round((double) myIds.size() / (double) numIntervalls); |
ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(), query, names.get(tbIndex).toString()); |
ConversionProperties prop = getFieldNamesAndDestTableName(creates.get(tbIndex).toString(), query, names.get(tbIndex).toString()); |
Line 2095 class Convert
|
Line 2173 class Convert
|
tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'"; |
tempQuery = query + " where " + tempID + ">'" + vec.lastElement() + "'"; |
|
|
} |
} |
|
String tableName = names.get(tbIndex).toString(); |
|
IndexList idList = (IndexList) indexList.get(0); |
|
System.out.println("found list " + idList); |
|
Statement stm = beanDest.getConnection().createStatement(); |
|
Vector destTables = beanDest.getTableNames(); |
|
System.out.println("tempQueryDest" + tempQueryDest); |
|
beanDest.makeQuery(tempQueryDest, 0); |
|
for (Iterator iter = idList.iterator(); iter.hasNext();) |
|
{ |
|
String indexField = (String) iter.next(); |
|
indexField = convertText(indexField); |
|
String indexName = destTableName + "_" + indexField; |
|
if (destTables.contains(indexName)) |
|
{ |
|
stm.execute("DROP INDEX " + destTableName + "_" + indexField); |
|
// continue; |
|
} |
|
// stm.execute("DROP INDEX "+destTableName+"_"+indexField); |
|
|
|
String type = beanDest.getColumnType(indexField).toLowerCase(); |
|
// System.out.println(indexField+" "+type+" "+(type.indexOf("text") >= 0 || type.indexOf("varchar") >= 0 || type.indexOf("char") >= 0)); |
|
if (type.indexOf("text") >= 0 || type.indexOf("varchar") >= 0 || type.indexOf("char") >= 0) |
|
{ |
|
if (beanDest.url.indexOf("mysql") >= 0) |
|
{ |
|
//System.out.println("CREATE INDEX " + indexName + " ON " + destTableName + " (" + indexField + "(10))"); |
|
// TODO problem if index exist !!! |
|
stm.execute("CREATE INDEX " + indexName + " ON " + destTableName + " (" + indexField + "(10))"); |
|
} else |
|
{ |
|
stm.execute("CREATE INDEX " + indexName + " ON " + destTableName + " (lower( " + indexField + "))"); |
|
|
|
} |
|
|
|
} else |
|
{ |
|
stm.execute("CREATE INDEX " + destTableName + "_" + indexField + " ON " + destTableName + "(" + indexField + ")"); |
|
|
|
} |
|
|
|
// stm.execute("DROP INDEX "+destTableName+"_"+indexField); |
|
|
|
} |
|
// CREATE UNIQUE INDEX title_idx ON films (title); |
for (Iterator iter = linesToDelete.iterator(); iter.hasNext();) |
for (Iterator iter = linesToDelete.iterator(); iter.hasNext();) |
{ |
{ |
String id = (String) iter.next(); |
String id = (String) iter.next(); |
Line 2134 class Convert
|
Line 2255 class Convert
|
dialog.setVisible(false); |
dialog.setVisible(false); |
} |
} |
} |
} |
|
/** |
|
* @param destQuery |
|
* @param string |
|
* @return |
|
*/ |
|
private static String removeLayoutPartFromQuery(String destQuery, String layoutName) |
|
{ |
|
String removeString = "layout " + beanDest.getQC() + layoutName + beanDest.getQC(); |
|
destQuery = destQuery.replaceFirst(removeString, ""); |
|
System.out.println("destQuery change to " + destQuery); |
|
return destQuery; |
|
} |
private static void performSynchronize( |
private static void performSynchronize( |
String idField, |
String idField, |
Vector vec, |
Vector vec, |
Line 2149 class Convert
|
Line 2282 class Convert
|
{ |
{ |
Vector[] vectors = bean.getQueryData(tempQuery, deltaID); |
Vector[] vectors = bean.getQueryData(tempQuery, deltaID); |
int idIndex = vectors[1].indexOf(idField); |
int idIndex = vectors[1].indexOf(idField); |
|
System.out.println(idIndex+" "+vectors[1]+" "+idField); |
// todo arraylist code has to be added |
// todo arraylist code has to be added |
for (Iterator iter = vectors[0].iterator(); iter.hasNext();) |
for (Iterator iter = vectors[0].iterator(); iter.hasNext();) |
{ |
{ |
Line 2183 class Convert
|
Line 2317 class Convert
|
updPst.setNull(l + 1, Types.NULL); |
updPst.setNull(l + 1, Types.NULL); |
} |
} |
updPst.setString(line.size() + 1, line.get(idIndex).toString()); |
updPst.setString(line.size() + 1, line.get(idIndex).toString()); |
|
//updPst.addBatch(); |
updPst.execute(); |
updPst.execute(); |
|
|
} |
} |
} |
} |
|
//updPst.executeBatch(); |
} // to method |
} // to method |
|
|
/** |
/** |
Line 3709 class Convert
|
Line 3843 class Convert
|
while (tokenizer.hasMoreTokens()) |
while (tokenizer.hasMoreTokens()) |
{ |
{ |
String fieldName = tokenizer.nextToken().trim(); |
String fieldName = tokenizer.nextToken().trim(); |
fieldNames[fieldIndex] = convertText(fieldName); |
fieldNames[fieldIndex] = beanDest.getQC() + convertText(fieldName) + beanDest.getQC(); |
// System.out.println("field "+ fieldNames[fieldIndex]); |
// System.out.println("field "+ fieldNames[fieldIndex]); |
fieldIndex++; |
fieldIndex++; |
} |
} |
Line 3722 class Convert
|
Line 3856 class Convert
|
for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();) |
for (Iterator iter = fieldNamesVec.iterator(); iter.hasNext();) |
{ |
{ |
String element = (String) iter.next(); |
String element = (String) iter.next(); |
fieldNames[++fieldIndex] = bean.getQC() + convertText(element) + bean.getQC(); |
fieldNames[++fieldIndex] = beanDest.getQC() + convertText(element) + beanDest.getQC(); |
// System.out.println("field " + fieldNames[fieldIndex]); |
// System.out.println("field " + fieldNames[fieldIndex]); |
} |
} |
} |
} |
Line 3789 class Convert
|
Line 3923 class Convert
|
command.append("WHERE " + idField + " = ?"); |
command.append("WHERE " + idField + " = ?"); |
return command; |
return command; |
} |
} |
|
public void makeTest(String table,String idField,String tempQuery) throws Exception |
|
{ |
|
int counter=0; |
|
|
|
// ****** test code ***** |
|
|
|
bean.getConnection(); |
|
ResultSet resultSet=null; |
|
String lastResult ="P227634.11";//"P227625.79554";//"P227625.77391";//"P116034.970998"; |
|
String myQuery="select "+bean.getQC()+idField+bean.getQC()+",serial "+" from "+bean.getQC()+table+bean.getQC(); |
|
System.out.println("Query is now "+myQuery); |
|
JDialog statusDialog=new JDialog(); |
|
statusDialog.setTitle("Status Information"); |
|
JLabel status=new JLabel("actual DataSet : "); |
|
JLabel status2=new JLabel(Integer.toString(++counter)); |
|
JLabel status3=new JLabel(lastResult); |
|
|
|
JPanel statusPanel = new JPanel(); |
|
JPanel statusPanel2 = new JPanel(); |
|
statusPanel.add(status); |
|
statusPanel.add(status2); |
|
statusPanel2.add(status3); |
|
statusDialog.getContentPane().add(statusPanel,"North"); |
|
statusDialog.getContentPane().add(statusPanel2,"Center"); |
|
statusDialog.setLocation(400,500); |
|
statusDialog.setSize(300,150); |
|
statusDialog.setVisible(true); |
|
while (true) |
|
{ |
|
if (!statusDialog.isVisible()) |
|
statusDialog.setVisible(true); |
|
tempQuery = myQuery + " where " + bean.getQC() + idField + bean.getQC() + ">'" + lastResult + "'"; |
|
resultSet = bean.makeQuery(tempQuery, 1); |
|
if (resultSet == null) |
|
{ |
|
System.out.println("lastResult was " + lastResult + " counter was " + counter); |
|
break; |
|
} else |
|
{ |
|
resultSet.next(); |
|
lastResult = resultSet.getString(1); |
|
counter++; |
|
status2.setText(Integer.toString(counter)); |
|
status3.setText(lastResult+" "+resultSet.getString(2)); |
|
if(counter%100==0) |
|
{ |
|
System.out.println("actual Result was " + lastResult + " counter was " + counter); |
|
// break; |
|
} |
|
} |
|
resultSet = null; |
|
} |
|
System.exit(0); |
|
|
|
//****** end Test ****** |
|
|
|
} |
} |
} |
|
|