1 2 22 23 package org.webdocwf.util.loader.generator; 24 25 import java.io.File ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 import java.util.StringTokenizer ; 30 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 34 import org.enhydra.xml.ElementImpl; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.NodeList ; 38 import org.webdocwf.util.loader.LoaderException; 39 import org.webdocwf.util.loader.logging.Logger; 40 import org.webdocwf.util.loader.logging.StandardLogger; 41 42 48 public class DomlDesignReader { 49 50 private ElementImpl searchDocument; 51 private Document documentXml; 52 private String databaseType=null; 53 54 private String tableName=null; 55 56 private String notUsingOid=null; 57 private String columnName=null; 58 private String isConstant=null; 59 private String isPrimaryKey=null; 60 61 private String size=null; 62 private String canBeNull=null; 63 private String dbType=null; 64 65 private String indexId=null; 66 private String indexUnique=null; 67 private String indexColumnId=null; 68 69 private String constraint=null; 70 private String foreignKeyColumn=null; 71 private String reference=null; 72 private String foreignTable=null; 73 74 private static List listColumnNames=null; 75 private static List listTargetTableNames=null; 76 private static List listTargetTableID=null; 77 private static List listColumnType=null; 78 private static List listColumnLenght=null; 79 private static List listAllowNulls=null; 80 81 private static List listPrimaryKeys=null; 82 private static List listIndexVariables=null; 83 private static List listForeignVariables=null; 84 85 private ImportDefinitionAttributes importDefinitionAttributes=new ImportDefinitionAttributes(); 86 private RelationshipsAttributes relationshipsAttributes=new RelationshipsAttributes(); 87 private MappingTypeData mappingTypeData=null; 88 89 private StringTokenizer st; 90 private String oidDbType=null; 91 private String oidDbSize=null; 92 93 private Logger logger; 94 101 102 public DomlDesignReader(Document document, Element root, InputParameters generatorParameters) 103 throws LoaderException{ 104 setLogger(); 105 this.logger.write("normal", "DomlDesignReader is started."); 106 DocumentBuilderFactory factoryXml = DocumentBuilderFactory.newInstance(); 107 try { 108 File file = new File ( generatorParameters.getDomlPath()); 109 DocumentBuilder builderXml = factoryXml.newDocumentBuilder(); 110 try { 111 this.documentXml = builderXml.parse( file ); 112 }catch( Exception e ) { 113 String msg="Exception in class DomlDesignReader: Error has occurred when trying to parse file!"; 114 LoaderException le=new LoaderException(msg+"\n"+e.getMessage()+"\n", (Throwable )e); 115 this.logger.write("full", "Exception in class DomlDesignReader: Error has occurred when trying to parse file!"+"\n"+le.getStackTraceAsString()); 116 throw le; 117 } 118 this.searchDocument = (ElementImpl)ElementImpl.newInstance(documentXml); 119 120 NodeList databaseName = searchDocument.getElementsByTagName("database"); 121 if(databaseName.getLength()!=0){ 122 databaseType=databaseName.item(0).getAttributes().item(0).getNodeValue(); 123 126 NodeList tableList=searchDocument.getElementsByTagName("table"); 127 int countOid=0; 128 int counterConstraint=0; 129 if(tableList.getLength()!=0){ 130 for(int i=0;i<tableList.getLength();i++){ 131 String maxConstraintLength=generatorParameters.getMaxConstraintLength(); 132 int maxConstraintInt=0; 133 if(!maxConstraintLength.equalsIgnoreCase("") && !maxConstraintLength.equalsIgnoreCase("-1")){ 134 maxConstraintInt=Integer.parseInt(maxConstraintLength); 135 } 136 listColumnNames=new ArrayList (); 137 listTargetTableNames=new ArrayList (); 138 listTargetTableID=new ArrayList (); 139 listColumnType=new ArrayList (); 140 listColumnLenght=new ArrayList (); 141 listAllowNulls=new ArrayList (); 142 143 listPrimaryKeys=new ArrayList (); 144 listIndexVariables=new ArrayList (); 145 listForeignVariables=new ArrayList (); 146 147 149 st=new StringTokenizer (generatorParameters.getOidColumnType()); 150 int countOidToken=0; 151 while(st.hasMoreTokens()){ 153 if(countOidToken==0) 154 oidDbType=st.nextToken(); 155 else 156 oidDbSize=(st.nextToken()).substring(1,5); 157 countOidToken++; 158 } 159 160 tableName=((ElementImpl)tableList.item(i)).getAttribute("dbTableName"); 161 notUsingOid=((ElementImpl)tableList.item(i)).getAttribute("notUsingOid"); 162 this.logger.write("normal", "Working...."+tableName); 164 boolean addOid=false; 167 if(notUsingOid.equalsIgnoreCase("")||notUsingOid.equalsIgnoreCase("false")) 168 addOid=true; 169 170 importDefinitionAttributes.setName(tableName); 171 importDefinitionAttributes.setTableName(tableName); 172 173 NodeList indexList=((ElementImpl)tableList.item(i)).getElementsByTagName("index"); 174 if(indexList.getLength()!=0){ 175 for(int l=0;l<indexList.getLength();l++){ 176 indexId=((ElementImpl)indexList.item(l)).getAttribute("id"); 177 indexUnique=((ElementImpl)indexList.item(l)).getAttribute("unique"); 178 179 NodeList indexColumn=((ElementImpl)indexList.item(l)).getElementsByTagName("indexColumn"); 180 indexColumnId=""; 181 for(int j=0;j<indexColumn.getLength();j++){ 182 indexColumnId+=((ElementImpl)indexColumn.item(j)).getAttribute("id")+","; 183 } 184 indexColumnId=indexColumnId.substring(0,indexColumnId.length()-1); 185 186 if(indexUnique.equalsIgnoreCase("false")||indexUnique.equalsIgnoreCase("")){ 187 listIndexVariables.add("1"); 188 }else{ 189 listIndexVariables.add("0"); 190 } 191 String indexName=indexId+"_"+tableName; 193 if( maxConstraintInt>0 && indexName.length()>maxConstraintInt ){ 194 String newIndexName=indexName.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length()); 195 indexName=newIndexName+String.valueOf(counterConstraint); 196 counterConstraint++; 197 } 198 listIndexVariables.add(indexName); 199 listIndexVariables.add(indexColumnId); 200 } 201 } 202 203 NodeList columnList=((ElementImpl)tableList.item(i)).getElementsByTagName("column"); 204 if(columnList.getLength()!=0){ 205 for(int j=0;j<columnList.getLength();j++){ 206 columnName=((ElementImpl)columnList.item(j)).getAttribute("id"); 207 208 listColumnNames.add(columnName); 209 listTargetTableNames.add(tableName); 210 listTargetTableID.add("0"); 211 212 isConstant=((ElementImpl)columnList.item(j)).getAttribute("isConstant"); 213 isPrimaryKey=((ElementImpl)columnList.item(j)).getAttribute("isPrimaryKey"); 214 if(isPrimaryKey.equalsIgnoreCase("true")){ 216 String primaryKeyName=tableName+"_"+columnName; 217 if( maxConstraintInt>0 && primaryKeyName.length()>maxConstraintInt ){ 218 String newPrimaryName=primaryKeyName.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length()); 219 primaryKeyName=newPrimaryName+String.valueOf(counterConstraint); 220 counterConstraint++; 221 } 222 listPrimaryKeys.add(primaryKeyName); 223 listPrimaryKeys.add(columnName); 224 } 225 226 NodeList typeConstraint=((ElementImpl)columnList.item(j)).getElementsByTagName( 227 "referenceObject"); 228 if(typeConstraint.getLength()!=0){ 229 for(int m=0;m<typeConstraint.getLength();m++){ 230 constraint=((ElementImpl)typeConstraint.item(m)).getAttribute("constraint"); 231 foreignKeyColumn=((ElementImpl)typeConstraint.item(m)).getAttribute( 232 "foreignKeyColumn"); 233 reference=((ElementImpl)typeConstraint.item(m)).getAttribute("reference"); 234 235 NodeList allTable=searchDocument.getElementsByTagName("table"); 236 for(int n=0;n<allTable.getLength();n++){ 237 String ID=((ElementImpl)allTable.item(n)).getAttribute("id"); 238 if(ID.equalsIgnoreCase(reference)){ 239 foreignTable=((ElementImpl)allTable.item(n)).getAttribute("dbTableName"); 240 break; 241 } 242 } 243 String foreignKeyName=tableName+"_"+columnName; 245 if( maxConstraintInt>0 && foreignKeyName.length()>maxConstraintInt ){ 246 String newFKName=foreignKeyName.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length()); 247 foreignKeyName=newFKName+String.valueOf(counterConstraint); 248 counterConstraint++; 249 } 250 251 if(foreignKeyColumn.equalsIgnoreCase("true")){ 252 listForeignVariables.add(tableName); 253 listForeignVariables.add(foreignKeyName); 255 listForeignVariables.add(columnName); 256 listForeignVariables.add(foreignTable); 257 listForeignVariables.add(foreignKeyColumn); 258 }else{ 259 listForeignVariables.add(tableName); 261 listForeignVariables.add(foreignKeyName); 263 listForeignVariables.add(columnName); 264 listForeignVariables.add(foreignTable); 265 listForeignVariables.add(generatorParameters.getOidColumnName()); 266 267 } 268 NodeList typeList=((ElementImpl)columnList.item(j)).getElementsByTagName( 269 "type"); 270 if(typeList.getLength()!=0){ 271 for(int k=0;k<typeList.getLength();k++){ 272 size=((ElementImpl)typeList.item(k)).getAttribute("size"); 273 canBeNull=((ElementImpl)typeList.item(k)).getAttribute("canBeNull"); 274 dbType=((ElementImpl)typeList.item(k)).getAttribute("dbType"); 275 276 if(canBeNull.equalsIgnoreCase("true")){ 277 listAllowNulls.add(""); 278 }else{ 279 listAllowNulls.add(" NOT NULL"); 280 } 281 if(dbType.equalsIgnoreCase("none")){ 284 listColumnType.add(oidDbType); 287 if(size.equalsIgnoreCase("")||size==null&& 288 289 (generatorParameters.getIsDecimal(mappingTypeData.getSQLType()).equalsIgnoreCase("true"))){ 290 listColumnLenght.add(" (19,0) "); 291 }else if (!size.equalsIgnoreCase("")){ 292 listColumnLenght.add(" ("+size+") "); 293 294 }else 295 listColumnLenght.add(size); 296 } 297 } 298 } 299 } 300 }else { 301 NodeList typeList=((ElementImpl)columnList.item(j)).getElementsByTagName("type"); 302 if(typeList.getLength()!=0){ 303 for(int k=0;k<typeList.getLength();k++){ 304 size=((ElementImpl)typeList.item(k)).getAttribute("size"); 305 canBeNull=((ElementImpl)typeList.item(k)).getAttribute("canBeNull"); 306 dbType=((ElementImpl)typeList.item(k)).getAttribute("dbType"); 307 308 if(canBeNull.equalsIgnoreCase("true")){ 309 listAllowNulls.add(""); 310 }else{ 311 listAllowNulls.add(" NOT NULL "); 312 } 313 mappingTypeData=new MappingTypeData(dbType, generatorParameters); 315 listColumnType.add(mappingTypeData.getSQLType()); 316 if((size.equalsIgnoreCase("")||size==null) && 323 (generatorParameters.getHasSize(mappingTypeData.getSQLType()).equalsIgnoreCase("true"))){ 324 listColumnLenght.add("32"); 325 }else if (!size.equalsIgnoreCase("")){ 326 listColumnLenght.add(" ("+size+") "); 327 }else{ 328 listColumnLenght.add(size); 329 } 330 } 331 } 332 } 333 } 334 } 335 if(addOid==true&&countOid<tableList.getLength()){ 337 listColumnNames.add(generatorParameters.getOidColumnName()); 338 listTargetTableNames.add(tableName); 339 listTargetTableID.add("0"); 340 listAllowNulls.add(" NOT NULL "); 341 listColumnLenght.add(" (" + oidDbSize + ") "); 342 listColumnType.add(oidDbType); 343 344 listColumnNames.add(generatorParameters.getVersionColumnName()); 345 listTargetTableNames.add(tableName); 346 listTargetTableID.add("0"); 347 listAllowNulls.add(" NOT NULL "); 348 listColumnLenght.add(""); 349 listColumnType.add(generatorParameters.getVersionColumnType()); 350 351 String primaryKeyNameOid=tableName+"_"+generatorParameters.getOidColumnName(); 352 if( maxConstraintInt>0 && primaryKeyNameOid.length()>maxConstraintInt ){ 353 String newPrimaryName=primaryKeyNameOid.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length()); 354 primaryKeyNameOid=newPrimaryName+String.valueOf(counterConstraint); 355 counterConstraint++; 356 } 357 listPrimaryKeys.add(primaryKeyNameOid); 358 listPrimaryKeys.add(generatorParameters.getOidColumnName()); 359 } 360 361 importDefinitionAttributes.setTagSourceColumnName(getColumnNames()); 362 importDefinitionAttributes.setTagTargetColumnName(getColumnNames()); 363 importDefinitionAttributes.setTagTargetTableName(getTargetTableNames()); 364 importDefinitionAttributes.setTagTargetTableID(getTargetTableID()); 365 importDefinitionAttributes.setTagColumnType(getColumnType()); 366 importDefinitionAttributes.setTagAllowNulls(getAllowNulls()); 367 importDefinitionAttributes.setTagColumnLenght(getColumnLenght()); 368 369 relationshipsAttributes.setTableName(tableName); 370 relationshipsAttributes.setPrimaryKeys(getPrimaryKeys()); 371 relationshipsAttributes.setIndexVariables(getIndexVariables()); 372 relationshipsAttributes.setForeignVariables(getForeignVariables()); 373 374 WriteSqlFiles writeSql= new WriteSqlFiles(tableName, countOid, 375 getImportDefinition(), getRelationshipsAttributes(), generatorParameters); 376 377 WriteImportDefinition writeImportDefinition=new WriteImportDefinition(document,root, 378 getImportDefinition(), generatorParameters); 379 countOid++; 380 } 381 this.logger.write("normal", "\ndone...."); 384 } 385 }else{ 386 String msg = "Your file don't have database tag!"; 387 LoaderException le=new LoaderException("Exception:",(Throwable ) new Exception (msg)); 388 this.logger.write("full", "Exception in class DomlDesignReader:"+le.getStackTraceAsString()); 389 throw le; 390 } 391 } catch( Exception e ) { 392 String msg="Exception in class DomlDesignReader: Error has occurred when trying to set parameters!"+"\n"; 393 394 LoaderException le=new LoaderException(msg+e.getMessage(), (Throwable )e); 395 this.logger.write("full", "Exception in class DomlDesignReader: Error has occurred when trying to set parameters!"+"\n"+ le.getStackTraceAsString()); 396 throw le; 397 } 398 this.logger.write("normal", "DomlDesignReader is finished."); 399 } 400 401 405 public ImportDefinitionAttributes getImportDefinition(){ 406 return importDefinitionAttributes; 407 } 408 409 413 public RelationshipsAttributes getRelationshipsAttributes(){ 414 return relationshipsAttributes; 415 } 416 417 421 public static void setColumnNames(String [] list_ColumnNames){ 422 listColumnNames=Arrays.asList(list_ColumnNames); 423 } 424 425 429 public static String [] getColumnNames(){ 430 String [] ret=new String [listColumnNames.size()]; 431 listColumnNames.toArray(ret); 432 return ret; 433 } 434 435 439 public static void setTargetTableNames(String [] list_TargetTableNames){ 440 listTargetTableNames=Arrays.asList(list_TargetTableNames); 441 } 442 443 447 public static String [] getTargetTableNames(){ 448 String [] ret=new String [listTargetTableNames.size()]; 449 listTargetTableNames.toArray(ret); 450 return ret; 451 } 452 453 457 public static void setTargetTableID(String [] list_TargetTableID){ 458 listTargetTableID=Arrays.asList(list_TargetTableID); 459 } 460 461 465 public static String [] getTargetTableID(){ 466 String [] ret=new String [listTargetTableID.size()]; 467 listTargetTableID.toArray(ret); 468 return ret; 469 } 470 471 475 public static void setColumnType(String [] list_ColumnType){ 476 listColumnType=Arrays.asList(list_ColumnType); 477 } 478 479 483 public static String [] getColumnType(){ 484 String [] ret=new String [listColumnType.size()]; 485 listColumnType.toArray(ret); 486 return ret; 487 } 488 489 493 public static void setColumnLenght(String [] list_ColumnLenght){ 494 listColumnLenght=Arrays.asList(list_ColumnLenght); 495 } 496 497 501 public static String [] getColumnLenght(){ 502 String [] ret=new String [listColumnLenght.size()]; 503 listColumnLenght.toArray(ret); 504 return ret; 505 } 506 507 511 public static void setAllowNulls(String [] list_AllowNulls){ 512 listAllowNulls=Arrays.asList(list_AllowNulls); 513 } 514 515 519 public static String [] getAllowNulls(){ 520 String [] ret=new String [listAllowNulls.size()]; 521 listAllowNulls.toArray(ret); 522 return ret; 523 } 524 525 526 530 public static void setPrimaryKeys(String [] primary_Keys){ 531 listPrimaryKeys=Arrays.asList(primary_Keys); 532 } 533 534 538 public static String [] getPrimaryKeys(){ 539 String [] ret=new String [listPrimaryKeys.size()]; 540 listPrimaryKeys.toArray(ret); 541 return ret; 542 } 543 544 548 public static void setIndexVariables(String [] index_Variables){ 549 listIndexVariables=Arrays.asList(index_Variables); 550 } 551 552 556 public static String [] getIndexVariables(){ 557 String [] ret=new String [listIndexVariables.size()]; 558 listIndexVariables.toArray(ret); 559 return ret; 560 } 561 562 566 public static void setForeignVariables(String [] foreign_Variables){ 567 listForeignVariables=Arrays.asList(foreign_Variables); 568 } 569 570 574 public static String [] getForeignVariables(){ 575 String [] ret=new String [listForeignVariables.size()]; 576 listForeignVariables.toArray(ret); 577 return ret; 578 } 579 583 private void setLogger() { 584 this.logger = StandardLogger.getCentralLogger(); 585 } 586 587 } | Popular Tags |