KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2      LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
3     Copyright (C) 2003 Together
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11     Lesser General Public License for more details.
12     You should have received a copy of the GNU Lesser General Public
13     License along with this library; if not, write to the Free Software
14     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */

16
17 package org.webdocwf.util.loader.generator;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.*;
27 import java.util.*;
28 import org.apache.xml.serialize.OutputFormat;
29 import org.apache.xml.serialize.XMLSerializer;
30 import org.apache.xml.serialize.Method;
31
32 import org.w3c.dom.Document JavaDoc;
33
34 import org.w3c.dom.Element JavaDoc;
35
36 import org.webdocwf.util.loader.LoaderException;
37 import org.webdocwf.util.loader.logging.Logger;
38 import org.webdocwf.util.loader.logging.StandardLogger;
39
40 /**
41  * Class LoadJobWriter creates and loads LoaderJob.xml file from the input
42  * parameters and source database.
43  * @author Radoslav Dutina
44  * @version 1.0
45  */

46 public class LoadJobWriter {
47
48     private Document JavaDoc document;
49     private Logger logger;
50
51     /**
52      * Construct object LoadJobWriter with associated parameter.
53      * @param generatorParameters represents the references to InputParameter object.
54      * @throws LoaderException
55      */

56
57     public LoadJobWriter(InputParameters generatorParameters) throws LoaderException {
58
59         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
60         try {
61             setLogger();
62             this.logger.write("normal", "Generating of LoaderJob.olj file is started.");
63             loadJobWriter(generatorParameters, factory);
64             this.logger.write("normal", "Generating of LoaderJob.olj file is finished.");
65         } catch (ParserConfigurationException JavaDoc pce) {
66             // Parser with specified options can't be built
67
String JavaDoc msg = "Parser with specified options can't be built! Exception has occurred when parser is" + "try to parse the LoaderJob.olj file!";
68             LoaderException le = new LoaderException(msg + "\n" + pce.getMessage(), (Throwable JavaDoc) pce);
69             throw le;
70         }
71     }
72     /**
73      * This method generate LoaderJob.olj fajl
74      * @param generatorParameters
75      * @param factory
76      * @throws ParserConfigurationException
77      * @throws LoaderException
78      */

79     private void loadJobWriter(InputParameters generatorParameters, DocumentBuilderFactory JavaDoc factory) throws ParserConfigurationException JavaDoc, LoaderException {
80                 
81                 this.logger.write("normal", "\tloadJobWriter method is started.");
82         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
83         document = builder.newDocument(); // Create from whole cloth
84

85         // Create root element (loaderJob)
86
Element JavaDoc root = (Element JavaDoc) document.createElement("loaderJob");
87         document.appendChild(root);
88
89         LoaderJobAttributes loaderJob = new LoaderJobAttributes();
90
91         root.setAttribute("logMode", loaderJob.getLogMode());
92         root.setAttribute("objectIDIncrement", loaderJob.getObjectIDIncrement());
93         root.setAttribute("objectIDTableName", loaderJob.getObjectIDTableName());
94         root.setAttribute("objectIDColumnName", loaderJob.getObjectIDColumnName());
95
96         // Create child (jdbcDefaultParameters)
97
Element JavaDoc childRoot = (Element JavaDoc) document.createElement("jdbcDefaultParameters");
98         root.appendChild(childRoot);
99
100         // Create child (jdbcSourceParameters)
101
Element JavaDoc childRoot2 = (Element JavaDoc) document.createElement("jdbcSourceParameters");
102         childRoot.appendChild(childRoot2);
103
104         JdbcParameters sourceJdbc = new JdbcParameters("source", generatorParameters);
105
106         childRoot2.setAttribute("dbVendor", sourceJdbc.getDbVendor());
107         childRoot2.setAttribute("driverName", sourceJdbc.getDriverName());
108
109         // Create child (jdbcSourceParameter)
110
Hashtable JavaDoc sourceTable = sourceJdbc.getAllParameters();
111         Enumeration JavaDoc enumSource = sourceTable.keys();
112         while (enumSource.hasMoreElements()) {
113             Element JavaDoc childRoot3 = (Element JavaDoc) document.createElement("jdbcSourceParameter");
114             childRoot2.appendChild(childRoot3);
115             String JavaDoc key = (String JavaDoc) enumSource.nextElement();
116             childRoot3.setAttribute("name", key);
117             childRoot3.setAttribute("value", (String JavaDoc) sourceTable.get(key));
118         }
119
120         // Create child (jdbcTargetParameters)
121
Element JavaDoc childRoot5 = (Element JavaDoc) document.createElement("jdbcTargetParameters");
122         childRoot.appendChild(childRoot5);
123
124         JdbcParameters targetJdbc = new JdbcParameters("target", generatorParameters);
125
126         //Reading the value of AlterTablePrimaryKey parameter.
127
// generatorParameters.setAlterTablePrimaryKey(targetJdbc.getAlterTablePrimaryKey());
128

129         childRoot5.setAttribute("dbVendor", targetJdbc.getDbVendor());
130         childRoot5.setAttribute("driverName", targetJdbc.getDriverName());
131
132         // Create child (jdbcTargetParameter)
133
Hashtable JavaDoc targetTable = targetJdbc.getAllParameters();
134         Enumeration JavaDoc enumTarget = targetTable.keys();
135         while (enumTarget.hasMoreElements()) {
136             Element JavaDoc childRoot6 = (Element JavaDoc) document.createElement("jdbcTargetParameter");
137             childRoot5.appendChild(childRoot6);
138             String JavaDoc key = (String JavaDoc) enumTarget.nextElement();
139             childRoot6.setAttribute("name", key);
140             childRoot6.setAttribute("value", (String JavaDoc) targetTable.get(key));
141         }
142
143         Vector sqlStatement = generatorParameters.getSqlToGenerate();
144         int sqlFileType = 0;
145         while (sqlFileType < 6) {
146             // Create child (sql)
147
if (sqlFileType == 3) {
148                 // Create child (definitionInclude)
149
Element JavaDoc childRoot20 = (Element JavaDoc) document.createElement("definitionInclude");
150                 root.appendChild(childRoot20);
151                 // Create child (include)
152
Element JavaDoc childRoot21 = (Element JavaDoc) document.createElement("include");
153                 childRoot20.appendChild(childRoot21);
154
155                 IncludeTagAttributes includeTagAttributes = new IncludeTagAttributes();
156
157                 //relative path from LoaderJob.olj file
158
String JavaDoc pathXml = includeTagAttributes.getHref();
159                 pathXml.trim();
160                 childRoot21.setAttribute("href", pathXml);
161                 childRoot21.setAttribute("parse", includeTagAttributes.getParse());
162             }
163             if (sqlFileType == 4 && generatorParameters.getAlterTablePrimaryKey().equalsIgnoreCase("false")) {
164                 // only for hsql database
165
} else {
166                 if (generatorParameters.getRestoreMode().equalsIgnoreCase("false")) {
167                     if (sqlStatement.get(sqlFileType) != null) {
168                         Element JavaDoc childRoot10 = (Element JavaDoc) document.createElement("sql");
169                         root.appendChild(childRoot10);
170                         SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(sqlFileType);
171                         childRoot10.setAttribute("name", sqlTagAttributes.getName());
172                         // childRoot10.setAttribute("logMode", sqlTagAttributes.getLogMode());
173
childRoot10.setAttribute("commit", sqlTagAttributes.getCommit());
174                         //ZK added next line 7.5.2004
175
childRoot10.setAttribute("onErrorContinue", sqlTagAttributes.getOnErrorContinue());
176
177                         // Create child (sqlStmt)
178
Element JavaDoc childRoot11 = (Element JavaDoc) document.createElement("sqlStmt");
179                         childRoot10.appendChild(childRoot11);
180
181                         // Create child (include)
182
Element JavaDoc childRoot12 = (Element JavaDoc) document.createElement("include");
183                         childRoot11.appendChild(childRoot12);
184
185                         //relative path from LoaderJob.olj file
186
String JavaDoc pathSql = sqlTagAttributes.getHref();
187                         pathSql.trim();
188                         childRoot12.setAttribute("href", pathSql);
189                         childRoot12.setAttribute("parse", sqlTagAttributes.getParse());
190                     } //end of sqlStatement.get(sqlFileType)
191

192                 } else if (generatorParameters.getRestoreMode().equalsIgnoreCase("true")) {
193                     //this is use only for restoring the database
194
Element JavaDoc childRoot10 = (Element JavaDoc) document.createElement("sql");
195                     root.appendChild(childRoot10);
196                     SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(sqlFileType);
197                     childRoot10.setAttribute("name", sqlTagAttributes.getName());
198                     // childRoot10.setAttribute("logMode", sqlTagAttributes.getLogMode());
199
childRoot10.setAttribute("commit", sqlTagAttributes.getCommit());
200                     //ZK added next line 10.5.2004
201
childRoot10.setAttribute("onErrorContinue", sqlTagAttributes.getOnErrorContinue());
202                     // Create child (sqlStmt)
203
Element JavaDoc childRoot11 = (Element JavaDoc) document.createElement("sqlStmt");
204                     childRoot10.appendChild(childRoot11);
205
206                     // Create child (include)
207
Element JavaDoc childRoot12 = (Element JavaDoc) document.createElement("include");
208                     childRoot11.appendChild(childRoot12);
209                     //relative path from LoaderJob.olj file
210
String JavaDoc pathSql = sqlTagAttributes.getHref();
211                     pathSql.trim();
212                     childRoot12.setAttribute("href", pathSql);
213                     childRoot12.setAttribute("parse", sqlTagAttributes.getParse());
214                 }
215             }
216             sqlFileType++;
217         }
218         if (generatorParameters.getGenerateXml().equalsIgnoreCase("true")) {
219             try {
220                 File JavaDoc file = null;
221                 String JavaDoc slash = null;
222                 if (generatorParameters.getGeneratorOutput().equalsIgnoreCase("")) {
223                     file = new File JavaDoc("");
224                     slash = "";
225                 } else {
226                     file = new File JavaDoc(generatorParameters.getGeneratorOutput());
227                     slash = "/";
228                 }
229                 if (!file.exists())
230                     file.mkdirs();
231
232                 FileOutputStream os = new FileOutputStream(file + slash + "LoaderJob.olj");
233                 OutputFormat of = new OutputFormat(); //xerces apache.xml.serializer
234
of.setIndenting(true);
235                 of.setIndent(4);
236                 of.setMethod(Method.XML);
237                 of.setPreserveSpace(false);
238                 of.setLineWidth(0);
239                 XMLSerializer out = new XMLSerializer(os, of);
240
241                 //IOException
242
out.serialize(document);
243             } catch (Exception JavaDoc e) {
244                 String JavaDoc msg = "Exception has occurred when we try to set output format for LoaderJob.olj file.";
245                 //if (this.logger != null) {
246
LoaderException le = new LoaderException(msg + "\n" + e.getMessage(), (Throwable JavaDoc) e);
247                 this.logger.write("full", "Exception has occurred when we try to set output format for LoaderJob.olj file." + "\n" + le.getStackTraceAsString());
248                 //}
249
throw le;
250             }
251         }
252         this.logger.write("normal", "\tloadJobWriter method is finished.");
253     }
254
255     /**
256      * This method will set logger object
257      * @param logger
258      */

259     private void setLogger() {
260         this.logger = StandardLogger.getCentralLogger();
261     }
262
263 }
Popular Tags