1 21 22 package org.webdocwf.util.loader.transformation; 23 24 import java.lang.reflect.Constructor ; 25 import java.sql.Connection ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.sql.Statement ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Vector ; 32 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 import org.webdocwf.util.loader.ConfigReader; 37 import org.webdocwf.util.loader.LoaderException; 38 import org.webdocwf.util.loader.OctopusXMLUtil; 39 import org.webdocwf.util.loader.logging.Logger; 40 41 45 public class Transformation { 46 47 48 49 private String transformationName; 50 private String transformationClassName; 51 private String transformatorConfigName; 52 53 private Vector sourceColumnNames = new Vector (); 54 55 private List transformationTargetColumns = new ArrayList (); 56 57 private Transformer transformer; 58 private Logger logger; 59 private JavaScriptEvaluator jsEvaluator; 60 private Element transformationDocFragment; 61 62 63 public Transformation(String name, String className, String configString, Element doc) throws Exception { 64 65 this.transformationName = name; 66 this.transformationClassName = className; 67 this.transformatorConfigName = configString; 68 this.transformationDocFragment = doc; 69 String javaScriptExpression = ""; 70 71 NodeList childNodes = this.transformationDocFragment.getElementsByTagName("javaScript"); 72 if ( childNodes.item(0) != null ){ 73 javaScriptExpression = childNodes.item(0).getFirstChild().getNodeValue(); 74 } 75 try { 76 init(); 77 if (javaScriptExpression != ""){ 78 jsEvaluator = new JavaScriptEvaluator(); 80 jsEvaluator.setExpression(javaScriptExpression); 81 jsEvaluator.setVariableNames(this.sourceColumnNames); 82 this.transformer = (Transformer) jsEvaluator; 83 }else{ 84 Class transformatorClass = Class.forName(this.transformationClassName); 86 Class [] ArgClassArray = new Class [] { }; 87 Object [] ArgObject = new Object [] { }; 88 Constructor transConstructor = transformatorClass.getDeclaredConstructor(ArgClassArray); 89 this.transformer = (Transformer)(transConstructor.newInstance(ArgObject)); 90 } 91 if(transformatorConfigName != null && this.transformer != null) { 92 this.transformer.configure(transformatorConfigName); 93 } 94 95 }catch(Exception e) { 96 throw new LoaderException("Error during transformation!",e); 97 } 98 } 99 100 public Transformation(String name, String className, Element doc) throws Exception { 101 new Transformation(name,className,null,doc); 102 } 103 104 107 public void setLogger(Logger logger) { 108 if (this.transformer instanceof JavaScriptEvaluator){ 109 jsEvaluator.setLogger(logger); 110 }else{ 111 this.logger = logger; 112 } 113 114 } 115 116 private void init() { 117 initSourceColumns(); 118 initTargetColumns(); 119 } 120 121 122 123 127 private void initSourceColumns() { 128 if (this.transformationDocFragment!=null){ 130 this.sourceColumnNames = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "sourceColumn","name"); 131 } 132 } 133 134 private void initTargetColumns() { 135 Vector targetColumnNames = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","name"); 136 Vector targetTableNames = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","tableName"); 137 Vector targetTableIDs = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","tableID"); 138 Vector targetValueModes = OctopusXMLUtil.importValueForTransform(this.transformationDocFragment, "targetColumn","valueMode"); 139 for(int i = 0; i < targetColumnNames.size(); i++) { 140 this.transformationTargetColumns.add(new TransformationTargetColumn( 141 (String )targetColumnNames.get(i), 142 (String )targetTableNames.get(i), 143 (String )targetTableIDs.get(i), 144 (String )targetValueModes.get(i) 145 )); 146 } 147 148 } 149 150 151 155 public Vector getSourceColumnNames() { 156 return this.sourceColumnNames; 157 } 158 159 163 public Vector getTargetColumnNames() { 164 Vector retVal = new Vector (); 165 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 166 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() ); 167 } 168 return retVal; 169 } 170 171 176 public Vector getTargetColumnNames(int index) { 177 Vector retVal = new Vector (); 178 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 179 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) ) 180 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() ); 181 } 182 183 return retVal; 184 } 185 186 191 public Vector getTargetColumnTypes(int index) { 192 Vector retVal = new Vector (); 193 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 194 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) ) 195 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getType() ); 196 } 199 return retVal; 200 } 201 202 208 public Vector getOrderedTargetColumnTypes(int index) { 209 Vector retVal = new Vector (); 210 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 211 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) ) 212 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getType() ); 213 else 214 retVal.add(null); 215 } 216 return retVal; 217 } 218 219 224 public Vector getTargetValueModes(int index) { 225 Vector retVal = new Vector (); 226 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 227 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) ) 228 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode() ); 229 } 230 return retVal; 231 } 232 233 238 public Vector getTargetKeyColumnNames(int index) { 239 Vector retVal = new Vector (); 240 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 241 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableID().equals( String.valueOf(index) ) ) { 242 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode().equals("Key") ) 243 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() ); 244 } 245 } 246 return retVal; 247 } 248 249 254 public Vector getTargetColumnNames(String tableName) { 255 Vector retVal = new Vector (); 256 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 257 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableName().equals(tableName) ) 258 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() ); 259 } 260 return retVal; 261 } 262 263 264 268 public Vector getTargetKeyColumnNames() { 269 Vector retVal = new Vector (); 270 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 271 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode().equals("Key") ) 272 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName() ); 273 } 274 return retVal; 275 } 276 277 281 public Vector getTargetValueMode() { 282 Vector retVal = new Vector (); 283 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 284 retVal.add( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getValueMode() ); 285 } 286 return retVal; 287 } 288 289 290 294 302 303 309 public Vector transformValues(Vector sourceValues) throws Exception { 310 Vector results = new Vector (); 311 try { 312 results.addAll(this.transformer.transformValue(sourceValues)); 313 } catch (Exception except) { 314 throw except; 315 } 316 return results; 317 318 } 319 323 public String getName() { 324 return this.transformationName; 325 } 326 327 328 339 public void transformationColumnTypes( 340 Connection c, 341 int firstColumn, 342 boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws SQLException , NullPointerException { 343 int iCnt = 0; 344 try { 345 List tableNames = this.getTargetTableNames(); 346 for(int k = 0; k < tableNames.size(); k++) { 347 Vector columnNames = this.getTargetColumnNames(tableNames.get(k).toString()); 348 Statement stmtTrans = c.createStatement(); 349 Vector subTyps = new Vector (); 351 String strQuery = "select "; 352 ResultSet rsetTrans=null; 353 if (columnNames.size() != 0) { 354 for (int i = 0; i < columnNames.size(); i++) { 355 strQuery += columnNames.get(i).toString() + 356 ", "; 357 } 358 strQuery = strQuery.substring(0, strQuery.length() - 2); 359 strQuery += " from " + tableNames.get(k).toString(); 360 if (columnsSuportedTarget){ 361 rsetTrans = c.getMetaData().getColumns( c.getCatalog(), null, tableNames.get(k).toString(), "%" ); 362 String columnName = ""; 363 String columnType = ""; 364 while(rsetTrans.next()){ 365 columnName = rsetTrans.getString(3+firstColumn); 366 columnType = rsetTrans.getString(5+firstColumn); 367 for (int j = 0; j < columnNames.size(); j++) { 368 if( columnNames.get(j).toString().equalsIgnoreCase( columnName ) ){ 369 this.setType(tableNames.get(k).toString(),columnNames.get(j).toString(),columnType); 370 } 372 } 373 } 374 }else{ if (configReaderTarget.getMaxRowsSupported()){ 376 stmtTrans.setMaxRows(1); 377 } 378 rsetTrans = stmtTrans.executeQuery(strQuery); 379 for (int j = 0; j < columnNames.size(); j++) { 380 this.setType( tableNames.get(k).toString(), 381 columnNames.get(j).toString(), 382 rsetTrans.getMetaData().getColumnTypeName(j + firstColumn) ); 383 } 385 } 386 rsetTrans.close(); 387 } 388 stmtTrans.close(); 389 } 390 } 392 catch (SQLException ex) { 393 throw ex; 394 } 395 catch (NullPointerException ex) { 396 throw ex; 397 } 398 } 399 400 public List getTargetTableNames() { 401 List retVal = new ArrayList (); 402 Element transformation = this.transformationDocFragment; 403 NodeList nodeList = transformation.getChildNodes(); 404 String tableName = ""; 405 for(int i = 0; i < nodeList.getLength(); i++) { 406 if( nodeList.item(i).getNodeType() != Node.ELEMENT_NODE ) 407 continue; 408 if( !nodeList.item(i).getNodeName().equals("targetColumns")) 409 continue; 410 Element targetColumns = (Element )nodeList.item(i); 411 NodeList targetColumnElements = targetColumns.getElementsByTagName("targetColumn"); 412 for(int j = 0; j < targetColumnElements.getLength();j++) { 413 Element targetColumn = (Element )targetColumnElements.item(j); 414 tableName = targetColumn.getAttribute("tableName"); 415 if( !retVal.contains(tableName) ) 416 retVal.add(tableName); 417 } 418 } 419 return retVal; 420 } 421 422 public String getTargetTableID() { 423 Element transformation = this.transformationDocFragment; 424 NodeList nodeList = transformation.getChildNodes(); 425 for(int i = 0; i < nodeList.getLength(); i++) { 426 if( nodeList.item(i).getNodeType() != Node.ELEMENT_NODE ) 427 continue; 428 if( !nodeList.item(i).getNodeName().equals("targetColumns")) 429 continue; 430 Element targetColumns = (Element )nodeList.item(i); 431 Element targetColumn = (Element )targetColumns.getElementsByTagName("targetColumn").item(0); 432 return targetColumn.getAttribute("tableID"); 433 } 434 return null; 435 } 436 437 private void setType(String tableName, String columnName, String type) { 438 for(int i = 0; i < this.transformationTargetColumns.size(); i++) { 439 if( ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getTableName().equals(tableName) && 440 ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).getName().equals(columnName) ) 441 ((TransformationTargetColumn)this.transformationTargetColumns.get(i)).setType(type); 442 } 443 } 444 445 448 public void reset() { 449 this.sourceColumnNames = new Vector (); 450 this.transformationTargetColumns = new ArrayList (); 451 } 452 453 public void release() throws Exception { 454 this.transformer.release(); 455 } 456 } 457 | Popular Tags |