1
2 /*
3 LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
4
5
6 Copyright (C) 2003 Together
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
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 /***
43 * DomlDesignReader class retrieves the input data from doml file, and placed them in to
44 * ImportDefinition class.
45 * @author Radoslav Dutina
46 * @version 1.0
47 */
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 /***
95 * Construct object DomlDesignReader with associated parameters.
96 * @param generatorParameters represents the references to InputParameter object.
97 * @param document is the object of Document class.
98 * @param root is the object of Element class.
99 * @throws LoaderException
100 */
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 //28.08.2003
124 // generatorParameters.setSourceType(databaseType.toString());
125
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 // MappingOidAndVersion mappingOidAndVersion=new MappingOidAndVersion(generatorParameters);
148
149 st=new StringTokenizer(generatorParameters.getOidColumnType());
150 int countOidToken=0;
151 //e.g. oidDb=decimal (19,0)
152 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 //System.out.println("Working...."+tableName);
163 this.logger.write("normal", "Working...."+tableName);
164 //BufferOctopusClass.getInstance().writeToBuffer("Working...."+tableName);
165 //if notUsingOid is false, we must add columns oid and version
166 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 //index lenght restrictions
192 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 //primary key restrictions
215 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 //foreign key restrictions (lenght)
244 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(tableName+"_"+columnName);
254 listForeignVariables.add(foreignKeyName);
255 listForeignVariables.add(columnName);
256 listForeignVariables.add(foreignTable);
257 listForeignVariables.add(foreignKeyColumn);
258 }else{
259 //oid column
260 listForeignVariables.add(tableName);
261 // listForeignVariables.add(tableName+"_"+columnName);
262 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 parameter dbType=null, then the primary key is oid column from referenced
282 //table
283 if(dbType.equalsIgnoreCase("none")){
284 // mappingTypeData=new MappingTypeData(oidDbType, generatorParameters);
285 // listColumnType.add(mappingTypeData.getSQLType());
286 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 //Mapp the type of the parameter
314 mappingTypeData=new MappingTypeData(dbType, generatorParameters);
315 listColumnType.add(mappingTypeData.getSQLType());
316 //ZK change this 6.5.2004
317 // if((size.equalsIgnoreCase("")||size==null)&&
318 // (mappingTypeData.getSQLType().equalsIgnoreCase("varchar")||
319 // mappingTypeData.getSQLType().equalsIgnoreCase("nvarchar")||
320 // mappingTypeData.getSQLType().equalsIgnoreCase("longvarchar"))){
321 // listColumnLenght.add("32");
322 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 //add column names for columns oid and version
336 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 //System.out.println("\ndone....");
382 //BufferOctopusClass.getInstance().writeToBuffer("\ndone....");
383 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 /***
402 * This method read value of importDefinitionAttributes parameters.
403 * @return references to ImportDefinitionAttributes objects.
404 */
405 public ImportDefinitionAttributes getImportDefinition(){
406 return importDefinitionAttributes;
407 }
408
409 /***
410 * This method read value of relationshipsAttributes parameters.
411 * @return references to RelationshipsAttributes objects.
412 */
413 public RelationshipsAttributes getRelationshipsAttributes(){
414 return relationshipsAttributes;
415 }
416
417 /***
418 * This method sets value of listColumnNames parameters.
419 * @param list_ColumnNames is value of parameters.
420 */
421 public static void setColumnNames(String[] list_ColumnNames){
422 listColumnNames=Arrays.asList(list_ColumnNames);
423 }
424
425 /***
426 * This method read value of listColumnNames parameters.
427 * @return value of parameters.
428 */
429 public static String[] getColumnNames(){
430 String[] ret=new String[listColumnNames.size()];
431 listColumnNames.toArray(ret);
432 return ret;
433 }
434
435 /***
436 * This method sets value of listTargetTableNames parameters.
437 * @param list_TargetTableNames is value of parameters.
438 */
439 public static void setTargetTableNames(String[] list_TargetTableNames){
440 listTargetTableNames=Arrays.asList(list_TargetTableNames);
441 }
442
443 /***
444 * This method read value of listTargetTableNames parameters.
445 * @return value of parameters.
446 */
447 public static String[] getTargetTableNames(){
448 String[] ret=new String[listTargetTableNames.size()];
449 listTargetTableNames.toArray(ret);
450 return ret;
451 }
452
453 /***
454 * This method sets value of listTargetTableID parameters.
455 * @param list_TargetTableID is value of parameter.
456 */
457 public static void setTargetTableID(String[] list_TargetTableID){
458 listTargetTableID=Arrays.asList(list_TargetTableID);
459 }
460
461 /***
462 * This method read value of listTargetTableID parameters.
463 * @return value of parameters.
464 */
465 public static String[] getTargetTableID(){
466 String[] ret=new String[listTargetTableID.size()];
467 listTargetTableID.toArray(ret);
468 return ret;
469 }
470
471 /***
472 * This method sets value of listColumnType parameters.
473 * @param list_ColumnType is value of parameters.
474 */
475 public static void setColumnType(String[] list_ColumnType){
476 listColumnType=Arrays.asList(list_ColumnType);
477 }
478
479 /***
480 * This method read value of listColumnType parameters.
481 * @return value of parameters.
482 */
483 public static String[] getColumnType(){
484 String[] ret=new String[listColumnType.size()];
485 listColumnType.toArray(ret);
486 return ret;
487 }
488
489 /***
490 * This method sets value of listColumnLenght parameters.
491 * @param list_ColumnLenght is value of parameter.
492 */
493 public static void setColumnLenght(String[] list_ColumnLenght){
494 listColumnLenght=Arrays.asList(list_ColumnLenght);
495 }
496
497 /***
498 * This method read value of listColumnLenght parameters.
499 * @return value of parameters.
500 */
501 public static String[] getColumnLenght(){
502 String[] ret=new String[listColumnLenght.size()];
503 listColumnLenght.toArray(ret);
504 return ret;
505 }
506
507 /***
508 * This method sets value of listAllowNulls parameters.
509 * @param list_AllowNulls is value of parameters.
510 */
511 public static void setAllowNulls(String[] list_AllowNulls){
512 listAllowNulls=Arrays.asList(list_AllowNulls);
513 }
514
515 /***
516 * This method read value of listAllowNulls parameters.
517 * @return value of parameters.
518 */
519 public static String[] getAllowNulls(){
520 String[] ret=new String[listAllowNulls.size()];
521 listAllowNulls.toArray(ret);
522 return ret;
523 }
524
525
526 /***
527 * This method sets the value of listPrimaryKeys parameters.
528 * @param primary_Keys is value of parameter.
529 */
530 public static void setPrimaryKeys(String[] primary_Keys){
531 listPrimaryKeys=Arrays.asList(primary_Keys);
532 }
533
534 /***
535 * This method read the value of listPrimaryKeys parameters.
536 * @return value of parameter.
537 */
538 public static String[] getPrimaryKeys(){
539 String[] ret=new String[listPrimaryKeys.size()];
540 listPrimaryKeys.toArray(ret);
541 return ret;
542 }
543
544 /***
545 * This method sets the value of listIndexVariables parameters.
546 * @param index_Variables is value of parameter.
547 */
548 public static void setIndexVariables(String[] index_Variables){
549 listIndexVariables=Arrays.asList(index_Variables);
550 }
551
552 /***
553 * This method read the value of listIndexVariables parameters.
554 * @return value of parameter.
555 */
556 public static String[] getIndexVariables(){
557 String[] ret=new String[listIndexVariables.size()];
558 listIndexVariables.toArray(ret);
559 return ret;
560 }
561
562 /***
563 * This method sets the value of listForeignVariables parameters.
564 * @param foreign_Variables is the value of parameter.
565 */
566 public static void setForeignVariables(String[] foreign_Variables){
567 listForeignVariables=Arrays.asList(foreign_Variables);
568 }
569
570 /***
571 * This method read the value of listForeignVariables parameters.
572 * @return value of parameter.
573 */
574 public static String[] getForeignVariables(){
575 String[] ret=new String[listForeignVariables.size()];
576 listForeignVariables.toArray(ret);
577 return ret;
578 }
579 /***
580 * This method will set logger object
581 * @param logger
582 */
583 private void setLogger() {
584 this.logger = StandardLogger.getCentralLogger();
585 }
586
587 }
This page was automatically generated by Maven