📄 domldesignreader.java
字号:
/*
LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
Copyright (C) 2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.webdocwf.util.loader.generator;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.enhydra.xml.ElementImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.webdocwf.util.loader.LoaderException;
import org.webdocwf.util.loader.logging.Logger;
import org.webdocwf.util.loader.logging.StandardLogger;
/**
* DomlDesignReader class retrieves the input data from doml file, and placed them in to
* ImportDefinition class.
* @author Radoslav Dutina
* @version 1.0
*/
public class DomlDesignReader {
private ElementImpl searchDocument;
private Document documentXml;
private String databaseType=null;
private String tableName=null;
private String notUsingOid=null;
private String columnName=null;
private String isConstant=null;
private String isPrimaryKey=null;
private String size=null;
private String canBeNull=null;
private String dbType=null;
private String indexId=null;
private String indexUnique=null;
private String indexColumnId=null;
private String constraint=null;
private String foreignKeyColumn=null;
private String reference=null;
private String foreignTable=null;
private static List listColumnNames=null;
private static List listTargetTableNames=null;
private static List listTargetTableID=null;
private static List listColumnType=null;
private static List listColumnLenght=null;
private static List listAllowNulls=null;
private static List listPrimaryKeys=null;
private static List listIndexVariables=null;
private static List listForeignVariables=null;
private ImportDefinitionAttributes importDefinitionAttributes=new ImportDefinitionAttributes();
private RelationshipsAttributes relationshipsAttributes=new RelationshipsAttributes();
private MappingTypeData mappingTypeData=null;
private StringTokenizer st;
private String oidDbType=null;
private String oidDbSize=null;
private Logger logger;
/**
* Construct object DomlDesignReader with associated parameters.
* @param generatorParameters represents the references to InputParameter object.
* @param document is the object of Document class.
* @param root is the object of Element class.
* @throws LoaderException
*/
public DomlDesignReader(Document document, Element root, InputParameters generatorParameters)
throws LoaderException{
setLogger();
this.logger.write("normal", "DomlDesignReader is started.");
DocumentBuilderFactory factoryXml = DocumentBuilderFactory.newInstance();
try {
File file = new File( generatorParameters.getDomlPath());
DocumentBuilder builderXml = factoryXml.newDocumentBuilder();
try {
this.documentXml = builderXml.parse( file );
}catch( Exception e ) {
String msg="Exception in class DomlDesignReader: Error has occurred when trying to parse file!";
LoaderException le=new LoaderException(msg+"\n"+e.getMessage()+"\n", (Throwable)e);
this.logger.write("full", "Exception in class DomlDesignReader: Error has occurred when trying to parse file!"+"\n"+le.getStackTraceAsString());
throw le;
}
this.searchDocument = (ElementImpl)ElementImpl.newInstance(documentXml);
NodeList databaseName = searchDocument.getElementsByTagName("database");
if(databaseName.getLength()!=0){
databaseType=databaseName.item(0).getAttributes().item(0).getNodeValue();
//28.08.2003
// generatorParameters.setSourceType(databaseType.toString());
NodeList tableList=searchDocument.getElementsByTagName("table");
int countOid=0;
int counterConstraint=0;
if(tableList.getLength()!=0){
for(int i=0;i<tableList.getLength();i++){
String maxConstraintLength=generatorParameters.getMaxConstraintLength();
int maxConstraintInt=0;
if(!maxConstraintLength.equalsIgnoreCase("") && !maxConstraintLength.equalsIgnoreCase("-1")){
maxConstraintInt=Integer.parseInt(maxConstraintLength);
}
listColumnNames=new ArrayList();
listTargetTableNames=new ArrayList();
listTargetTableID=new ArrayList();
listColumnType=new ArrayList();
listColumnLenght=new ArrayList();
listAllowNulls=new ArrayList();
listPrimaryKeys=new ArrayList();
listIndexVariables=new ArrayList();
listForeignVariables=new ArrayList();
// MappingOidAndVersion mappingOidAndVersion=new MappingOidAndVersion(generatorParameters);
st=new StringTokenizer(generatorParameters.getOidColumnType());
int countOidToken=0;
//e.g. oidDb=decimal (19,0)
while(st.hasMoreTokens()){
if(countOidToken==0)
oidDbType=st.nextToken();
else
oidDbSize=(st.nextToken()).substring(1,5);
countOidToken++;
}
tableName=((ElementImpl)tableList.item(i)).getAttribute("dbTableName");
notUsingOid=((ElementImpl)tableList.item(i)).getAttribute("notUsingOid");
//System.out.println("Working...."+tableName);
this.logger.write("normal", "Working...."+tableName);
//BufferOctopusClass.getInstance().writeToBuffer("Working...."+tableName);
//if notUsingOid is false, we must add columns oid and version
boolean addOid=false;
if(notUsingOid.equalsIgnoreCase("")||notUsingOid.equalsIgnoreCase("false"))
addOid=true;
importDefinitionAttributes.setName(tableName);
importDefinitionAttributes.setTableName(tableName);
NodeList indexList=((ElementImpl)tableList.item(i)).getElementsByTagName("index");
if(indexList.getLength()!=0){
for(int l=0;l<indexList.getLength();l++){
indexId=((ElementImpl)indexList.item(l)).getAttribute("id");
indexUnique=((ElementImpl)indexList.item(l)).getAttribute("unique");
NodeList indexColumn=((ElementImpl)indexList.item(l)).getElementsByTagName("indexColumn");
indexColumnId="";
for(int j=0;j<indexColumn.getLength();j++){
indexColumnId+=((ElementImpl)indexColumn.item(j)).getAttribute("id")+",";
}
indexColumnId=indexColumnId.substring(0,indexColumnId.length()-1);
if(indexUnique.equalsIgnoreCase("false")||indexUnique.equalsIgnoreCase("")){
listIndexVariables.add("1");
}else{
listIndexVariables.add("0");
}
//index lenght restrictions
String indexName=indexId+"_"+tableName;
if( maxConstraintInt>0 && indexName.length()>maxConstraintInt ){
String newIndexName=indexName.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length());
indexName=newIndexName+String.valueOf(counterConstraint);
counterConstraint++;
}
listIndexVariables.add(indexName);
listIndexVariables.add(indexColumnId);
}
}
NodeList columnList=((ElementImpl)tableList.item(i)).getElementsByTagName("column");
if(columnList.getLength()!=0){
for(int j=0;j<columnList.getLength();j++){
columnName=((ElementImpl)columnList.item(j)).getAttribute("id");
listColumnNames.add(columnName);
listTargetTableNames.add(tableName);
listTargetTableID.add("0");
isConstant=((ElementImpl)columnList.item(j)).getAttribute("isConstant");
isPrimaryKey=((ElementImpl)columnList.item(j)).getAttribute("isPrimaryKey");
//primary key restrictions
if(isPrimaryKey.equalsIgnoreCase("true")){
String primaryKeyName=tableName+"_"+columnName;
if( maxConstraintInt>0 && primaryKeyName.length()>maxConstraintInt ){
String newPrimaryName=primaryKeyName.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length());
primaryKeyName=newPrimaryName+String.valueOf(counterConstraint);
counterConstraint++;
}
listPrimaryKeys.add(primaryKeyName);
listPrimaryKeys.add(columnName);
}
NodeList typeConstraint=((ElementImpl)columnList.item(j)).getElementsByTagName(
"referenceObject");
if(typeConstraint.getLength()!=0){
for(int m=0;m<typeConstraint.getLength();m++){
constraint=((ElementImpl)typeConstraint.item(m)).getAttribute("constraint");
foreignKeyColumn=((ElementImpl)typeConstraint.item(m)).getAttribute(
"foreignKeyColumn");
reference=((ElementImpl)typeConstraint.item(m)).getAttribute("reference");
NodeList allTable=searchDocument.getElementsByTagName("table");
for(int n=0;n<allTable.getLength();n++){
String ID=((ElementImpl)allTable.item(n)).getAttribute("id");
if(ID.equalsIgnoreCase(reference)){
foreignTable=((ElementImpl)allTable.item(n)).getAttribute("dbTableName");
break;
}
}
//foreign key restrictions (lenght)
String foreignKeyName=tableName+"_"+columnName;
if( maxConstraintInt>0 && foreignKeyName.length()>maxConstraintInt ){
String newFKName=foreignKeyName.substring(0,maxConstraintInt-String.valueOf(counterConstraint).length());
foreignKeyName=newFKName+String.valueOf(counterConstraint);
counterConstraint++;
}
if(foreignKeyColumn.equalsIgnoreCase("true")){
listForeignVariables.add(tableName);
// listForeignVariables.add(tableName+"_"+columnName);
listForeignVariables.add(foreignKeyName);
listForeignVariables.add(columnName);
listForeignVariables.add(foreignTable);
listForeignVariables.add(foreignKeyColumn);
}else{
//oid column
listForeignVariables.add(tableName);
// listForeignVariables.add(tableName+"_"+columnName);
listForeignVariables.add(foreignKeyName);
listForeignVariables.add(columnName);
listForeignVariables.add(foreignTable);
listForeignVariables.add(generatorParameters.getOidColumnName());
}
NodeList typeList=((ElementImpl)columnList.item(j)).getElementsByTagName(
"type");
if(typeList.getLength()!=0){
for(int k=0;k<typeList.getLength();k++){
size=((ElementImpl)typeList.item(k)).getAttribute("size");
canBeNull=((ElementImpl)typeList.item(k)).getAttribute("canBeNull");
dbType=((ElementImpl)typeList.item(k)).getAttribute("dbType");
if(canBeNull.equalsIgnoreCase("true")){
listAllowNulls.add("");
}else{
listAllowNulls.add(" NOT NULL");
}
//if parameter dbType=null, then the primary key is oid column from referenced
//table
if(dbType.equalsIgnoreCase("none")){
// mappingTypeData=new MappingTypeData(oidDbType, generatorParameters);
// listColumnType.add(mappingTypeData.getSQLType());
listColumnType.add(oidDbType);
if(size.equalsIgnoreCase("")||size==null&&
(generatorParameters.getIsDecimal(mappingTypeData.getSQLType()).equalsIgnoreCase("true"))){
listColumnLenght.add(" (19,0) ");
}else if (!size.equalsIgnoreCase("")){
listColumnLenght.add(" ("+size+") ");
}else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -