KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > generator > GenerateDoml


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 org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.webdocwf.util.loader.LoaderException;
28 import org.webdocwf.util.loader.logging.Logger;
29 import org.webdocwf.util.loader.logging.StandardLogger;
30
31 /**
32  * GenerateDoml class creates the doml file as an output.
33  * @author Radoslav Dutina
34  * @version 1.0
35  */

36 public class GenerateDoml {
37
38   private Document JavaDoc documentDoml;
39   private String JavaDoc referenceTableName=null;
40   private String JavaDoc javaType=null;
41   private String JavaDoc tableNameToCase=null;
42   private String JavaDoc referenceTableNameToCase=null;
43   private Logger logger;
44
45   /**
46    * Construct object GenerateDoml with associated parameters.
47    * @param documentDoml is the reference to object of the Document class.
48    * @param rootDoml is reference to the object of Element class, which represents the root element of
49    * the documentDoml object.
50    * @param childRoot1 is the child of the root element, of the documentDoml object.
51    * @param childRoot2 is the child of the root element, of the documentDoml object. The childRoot2
52    * represent package tag.
53    * @param importDefinitionAttributes is references to ImportDefinitionAttributes object.
54    * @param relationshipsAttributes is references to RelationshipsAttributes object.
55    * @param generatorParameters represents the references to InputParameter object.
56    * @param tableName is name of the table form which we retrieve data.
57    * @throws LoaderException
58    */

59
60   public GenerateDoml(Document JavaDoc documentDoml, Element JavaDoc rootDoml, Element JavaDoc childRoot1, Element JavaDoc childRoot2,
61                       String JavaDoc tableName, ImportDefinitionAttributes importDefinitionAttributes,
62                       RelationshipsAttributes relationshipsAttributes,
63                       InputParameters generatorParameters) throws LoaderException {
64
65                         setLogger();
66         this.logger.write("full", "GenerateDoml is started.");
67     //child1=tag database
68
try{
69       boolean notUsingOid=true;
70       if(importDefinitionAttributes.getTagSourceColumnName().length!=0){
71         for(int i=0;i<importDefinitionAttributes.getTagSourceColumnName().length;i++){
72           if(importDefinitionAttributes.getTagSourceColumnName()[i].equalsIgnoreCase("oid")){
73             notUsingOid=false;
74             break;
75           }
76         }
77       }
78
79       //Crate child (table)
80
Element JavaDoc childRoot3 = (Element JavaDoc)documentDoml.createElement("table");
81       childRoot2.appendChild(childRoot3);
82       tableNameToCase=tableName.toUpperCase().substring(0,1)+tableName.toLowerCase().substring(1);
83       childRoot3.setAttribute("id", generatorParameters.getPackageName()+"."+tableNameToCase);
84       childRoot3.setAttribute("dbTableName", tableName);
85 // childRoot3.setAttribute("isLazyLoading", "true");
86
// childRoot3.setAttribute("caching", "none");
87
// if(notUsingOid){
88
// childRoot3.setAttribute("notUsingOid", "true");
89
// }else{
90
// childRoot3.setAttribute("notUsingOid", "false");
91
// }
92

93       //Crate child (column)
94
if(importDefinitionAttributes.getTagSourceColumnName().length!=0){
95         for(int i=0; i<importDefinitionAttributes.getTagSourceColumnName().length; i++){
96           //checking id the column is the foreign key
97
boolean fk=false;
98           if(relationshipsAttributes.getForeignVariables().length!=0 &&
99              relationshipsAttributes!=null){
100             for(int j=0;j<relationshipsAttributes.getForeignVariables().length;j=j+5){
101               if(relationshipsAttributes.getForeignVariables()[j+2].equalsIgnoreCase(
102                   importDefinitionAttributes.getTagSourceColumnName()[i])){
103               fk=true;
104               referenceTableName=relationshipsAttributes.getForeignVariables()[j+3];
105               referenceTableNameToCase=referenceTableName.toUpperCase().substring(0,1)+
106                                        referenceTableName.toLowerCase().substring(1);
107               break;
108             }
109             }
110           }
111           //checking id the column is the primary key
112
boolean pk=false;
113           if(relationshipsAttributes.getPrimaryKeys().length!=0 &&
114              relationshipsAttributes!=null){
115             for(int k=0;k<relationshipsAttributes.getPrimaryKeys().length;k=k+2){
116               if(importDefinitionAttributes.getTagSourceColumnName()[i].equalsIgnoreCase(
117                   relationshipsAttributes.getPrimaryKeys()[k+1])){
118               pk=true;
119               break;
120             }
121             }
122           }
123           if(!importDefinitionAttributes.getTagSourceColumnName()[i].equalsIgnoreCase("oid")&&
124              !importDefinitionAttributes.getTagSourceColumnName()[i].equalsIgnoreCase("version")){
125           Element JavaDoc childRoot4 = (Element JavaDoc)documentDoml.createElement("column");
126           childRoot3.appendChild(childRoot4);
127           childRoot4.setAttribute("id", importDefinitionAttributes.getTagSourceColumnName()[i]);
128           childRoot4.setAttribute("usedForQuery", "true");
129           if(pk){
130             childRoot4.setAttribute("isPrimaryKey", "true");
131           }
132
133           //this column is the foreign key
134
if(fk){
135             Element JavaDoc childRoot5 = (Element JavaDoc)documentDoml.createElement("referenceObject");
136             childRoot4.appendChild(childRoot5);
137             childRoot5.setAttribute("constraint", "true");
138             childRoot5.setAttribute("reference", generatorParameters.getPackageName()+"."+
139                                     referenceTableNameToCase);
140
141             Element JavaDoc childRoot6 = (Element JavaDoc)documentDoml.createElement("type");
142             childRoot4.appendChild(childRoot6);
143             if(importDefinitionAttributes.getTagAllowNulls()[i].equalsIgnoreCase("NOT NULL")){
144               childRoot6.setAttribute("canBeNull", "false");
145             }else{
146               childRoot6.setAttribute("canBeNull", "true");
147             }
148             //childRoot6.setAttribute("dbType", importDefinitionAttributes.getTagColumnType()[i]);
149
childRoot6.setAttribute("dbType", "none");
150             MappingJavaType mappingJavaType=new MappingJavaType( generatorParameters,
151                 importDefinitionAttributes.getTagColumnType()[i]);
152             javaType=mappingJavaType.getJavaType();
153             //childRoot6.setAttribute("javaType", javaType);
154
childRoot6.setAttribute("javaType", generatorParameters.getPackageName()+"."+
155                                     referenceTableNameToCase+"DO");
156             childRoot6.setAttribute("size", importDefinitionAttributes.getTagColumnLenght()[i]);
157             //column isn't the foreign key
158
}else{
159             Element JavaDoc childRoot6 = (Element JavaDoc)documentDoml.createElement("type");
160             childRoot4.appendChild(childRoot6);
161             if(importDefinitionAttributes.getTagAllowNulls()[i].equalsIgnoreCase("NOT NULL")){
162               childRoot6.setAttribute("canBeNull", "false");
163             }else{
164               childRoot6.setAttribute("canBeNull", "true");
165             }
166             childRoot6.setAttribute("dbType", importDefinitionAttributes.getTagColumnType()[i]);
167             MappingJavaType mappingJavaType1=new MappingJavaType( generatorParameters,
168                 importDefinitionAttributes.getTagColumnType()[i]);
169             javaType=mappingJavaType1.getJavaType();
170             childRoot6.setAttribute("javaType", javaType);
171             childRoot6.setAttribute("size", importDefinitionAttributes.getTagColumnLenght()[i]);
172           }
173         }
174       }
175       }
176       if(relationshipsAttributes.getIndexVariables().length!=0 &&
177          relationshipsAttributes!=null){
178         for(int i=0;i<relationshipsAttributes.getIndexVariables().length;i=i+3){
179           //Crate child (index)
180
Element JavaDoc childRoot7 = (Element JavaDoc)documentDoml.createElement("index");
181           childRoot3.appendChild(childRoot7);
182           childRoot7.setAttribute("id",relationshipsAttributes.getIndexVariables()[i+1]);
183           if(relationshipsAttributes.getIndexVariables()[i].equalsIgnoreCase("1")){
184             childRoot7.setAttribute("unique","false");
185           }else{
186             childRoot7.setAttribute("unique","true");
187           }
188           Element JavaDoc childRoot8 = (Element JavaDoc)documentDoml.createElement("indexColumn");
189           childRoot7.appendChild(childRoot8);
190           childRoot8.setAttribute("id",relationshipsAttributes.getIndexVariables()[i+2]);
191         }
192       }
193
194     }catch (Exception JavaDoc e) {
195       String JavaDoc msg="Exception in class GenerateDoml: Error has occurred when trying to generate doml file!";
196       LoaderException le=new LoaderException(msg+"\n"+e.getMessage(), (Throwable JavaDoc)e);
197         this.logger.write("full","Exception in class GenerateDoml: Error has occurred when trying to generate doml file!"+"\n"+le.getStackTraceAsString());
198       throw le;
199     }
200         this.logger.write("full", "GenerateDoml is finished.");
201   }
202   
203   /**
204        * This method will set logger object
205        * @param logger
206        */

207     private void setLogger() {
208         this.logger = StandardLogger.getCentralLogger();
209     }
210 }
Popular Tags