IDAO sourceDAO = GenericDAO.createDAOImpl(getSourceConnectionFactory(), SQL_RETREIVE_SOURCE, IDAO.class);
sourceDAO.set("key", startingKey);
sourceDAO.select();
AbstractGenericDAO implSourceDAO = (AbstractGenericDAO)sourceDAO.getImplementationObject();
/**
* Change the connection information of the source data in order to store to the target server.
*/
ConnectionFactory targetConnectionFactoryInTransaction = new ConnectionFactory(){
public Connection getConnection() throws Exception{
return targetConnectionInTransaction;
}
};
implSourceDAO.setConnectionFactory(targetConnectionFactoryInTransaction);
/**
* Let the SQLs auto-generated again.
*/
implSourceDAO.setConnective(true);
implSourceDAO.setSqlStmt(null);
implSourceDAO.setAutoSQLGeneration(true);
implSourceDAO.setTableName(objectName);
implSourceDAO.setKeyField(keyFieldName);
/** Create and store the insert and update SQL in order to use them later
* since we don't have to generate them again and again within the following while-loop.
*/
implSourceDAO.createInsertSql();
String insertSql = implSourceDAO.getSqlStmt();
implSourceDAO.createUpdateSql();
String updateSql = implSourceDAO.getSqlStmt();
/**
* this loop will insert(if new) or update(if exist) all of the source rowset
*/
while(sourceDAO.next() && !stopSignaled){
System.out.print(" . "+ keyFieldName +"=["+ sourceDAO.get(keyFieldName) + "]");
// this will check if the source value already exists
IDAO testDAO = ConnectiveDAO.createDAOImpl(targetConnectionFactoryInTransaction, "select 1 from " + objectName + " where " + keyFieldName +" = ?key", IDAO.class);
testDAO.set("key", sourceDAO.get(keyFieldName));
testDAO.select();
if(testDAO.size() > 0){
implSourceDAO.setSqlStmt(updateSql);
System.out.println(" [UPDATE]" + sourceDAO);
}else{
implSourceDAO.setSqlStmt(insertSql);
System.out.println(" [INSERT]" + sourceDAO);
}
sourceDAO.update();
}