001: /*
002: LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
003: Copyright (C) 2003 Together
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008: This library is distributed in the hope that it will be useful,
009: but WITHOUT ANY WARRANTY; without even the implied warranty of
010: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: Lesser General Public License for more details.
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the Free Software
014: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
015: */
016:
017: package org.webdocwf.util.loader.generator;
018:
019: import javax.xml.parsers.DocumentBuilder;
020: import javax.xml.parsers.DocumentBuilderFactory;
021: import javax.xml.parsers.ParserConfigurationException;
022:
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import java.io.File;
026: import java.io.*;
027: import java.util.*;
028: import org.apache.xml.serialize.OutputFormat;
029: import org.apache.xml.serialize.XMLSerializer;
030: import org.apache.xml.serialize.Method;
031:
032: import org.w3c.dom.Document;
033:
034: import org.w3c.dom.Element;
035:
036: import org.webdocwf.util.loader.LoaderException;
037: import org.webdocwf.util.loader.logging.Logger;
038: import org.webdocwf.util.loader.logging.StandardLogger;
039:
040: /**
041: * Class LoadJobWriter creates and loads LoaderJob.xml file from the input
042: * parameters and source database.
043: * @author Radoslav Dutina
044: * @version 1.0
045: */
046: public class LoadJobWriter {
047:
048: private Document document;
049: private Logger logger;
050:
051: /**
052: * Construct object LoadJobWriter with associated parameter.
053: * @param generatorParameters represents the references to InputParameter object.
054: * @throws LoaderException
055: */
056:
057: public LoadJobWriter(InputParameters generatorParameters)
058: throws LoaderException {
059:
060: DocumentBuilderFactory factory = DocumentBuilderFactory
061: .newInstance();
062: try {
063: setLogger();
064: this .logger.write("normal",
065: "Generating of LoaderJob.olj file is started.");
066: loadJobWriter(generatorParameters, factory);
067: this .logger.write("normal",
068: "Generating of LoaderJob.olj file is finished.");
069: } catch (ParserConfigurationException pce) {
070: // Parser with specified options can't be built
071: String msg = "Parser with specified options can't be built! Exception has occurred when parser is"
072: + "try to parse the LoaderJob.olj file!";
073: LoaderException le = new LoaderException(msg + "\n"
074: + pce.getMessage(), (Throwable) pce);
075: throw le;
076: }
077: }
078:
079: /**
080: * This method generate LoaderJob.olj fajl
081: * @param generatorParameters
082: * @param factory
083: * @throws ParserConfigurationException
084: * @throws LoaderException
085: */
086: private void loadJobWriter(InputParameters generatorParameters,
087: DocumentBuilderFactory factory)
088: throws ParserConfigurationException, LoaderException {
089:
090: this .logger.write("normal",
091: "\tloadJobWriter method is started.");
092: DocumentBuilder builder = factory.newDocumentBuilder();
093: document = builder.newDocument(); // Create from whole cloth
094:
095: // Create root element (loaderJob)
096: Element root = (Element) document.createElement("loaderJob");
097: document.appendChild(root);
098:
099: LoaderJobAttributes loaderJob = new LoaderJobAttributes();
100:
101: root.setAttribute("logMode", loaderJob.getLogMode());
102: root.setAttribute("objectIDIncrement", loaderJob
103: .getObjectIDIncrement());
104: root.setAttribute("objectIDTableName", loaderJob
105: .getObjectIDTableName());
106: root.setAttribute("objectIDColumnName", loaderJob
107: .getObjectIDColumnName());
108:
109: // Create child (jdbcDefaultParameters)
110: Element childRoot = (Element) document
111: .createElement("jdbcDefaultParameters");
112: root.appendChild(childRoot);
113:
114: // Create child (jdbcSourceParameters)
115: Element childRoot2 = (Element) document
116: .createElement("jdbcSourceParameters");
117: childRoot.appendChild(childRoot2);
118:
119: JdbcParameters sourceJdbc = new JdbcParameters("source",
120: generatorParameters);
121:
122: childRoot2.setAttribute("dbVendor", sourceJdbc.getDbVendor());
123: childRoot2.setAttribute("driverName", sourceJdbc
124: .getDriverName());
125:
126: // Create child (jdbcSourceParameter)
127: Hashtable sourceTable = sourceJdbc.getAllParameters();
128: Enumeration enumSource = sourceTable.keys();
129: while (enumSource.hasMoreElements()) {
130: Element childRoot3 = (Element) document
131: .createElement("jdbcSourceParameter");
132: childRoot2.appendChild(childRoot3);
133: String key = (String) enumSource.nextElement();
134: childRoot3.setAttribute("name", key);
135: childRoot3.setAttribute("value", (String) sourceTable
136: .get(key));
137: }
138:
139: // Create child (jdbcTargetParameters)
140: Element childRoot5 = (Element) document
141: .createElement("jdbcTargetParameters");
142: childRoot.appendChild(childRoot5);
143:
144: JdbcParameters targetJdbc = new JdbcParameters("target",
145: generatorParameters);
146:
147: //Reading the value of AlterTablePrimaryKey parameter.
148: // generatorParameters.setAlterTablePrimaryKey(targetJdbc.getAlterTablePrimaryKey());
149:
150: childRoot5.setAttribute("dbVendor", targetJdbc.getDbVendor());
151: childRoot5.setAttribute("driverName", targetJdbc
152: .getDriverName());
153:
154: // Create child (jdbcTargetParameter)
155: Hashtable targetTable = targetJdbc.getAllParameters();
156: Enumeration enumTarget = targetTable.keys();
157: while (enumTarget.hasMoreElements()) {
158: Element childRoot6 = (Element) document
159: .createElement("jdbcTargetParameter");
160: childRoot5.appendChild(childRoot6);
161: String key = (String) enumTarget.nextElement();
162: childRoot6.setAttribute("name", key);
163: childRoot6.setAttribute("value", (String) targetTable
164: .get(key));
165: }
166:
167: Vector sqlStatement = generatorParameters.getSqlToGenerate();
168: int sqlFileType = 0;
169: while (sqlFileType < 6) {
170: // Create child (sql)
171: if (sqlFileType == 3) {
172: // Create child (definitionInclude)
173: Element childRoot20 = (Element) document
174: .createElement("definitionInclude");
175: root.appendChild(childRoot20);
176: // Create child (include)
177: Element childRoot21 = (Element) document
178: .createElement("include");
179: childRoot20.appendChild(childRoot21);
180:
181: IncludeTagAttributes includeTagAttributes = new IncludeTagAttributes();
182:
183: //relative path from LoaderJob.olj file
184: String pathXml = includeTagAttributes.getHref();
185: pathXml.trim();
186: childRoot21.setAttribute("href", pathXml);
187: childRoot21.setAttribute("parse", includeTagAttributes
188: .getParse());
189: }
190: if (sqlFileType == 4
191: && generatorParameters.getAlterTablePrimaryKey()
192: .equalsIgnoreCase("false")) {
193: // only for hsql database
194: } else {
195: if (generatorParameters.getRestoreMode()
196: .equalsIgnoreCase("false")) {
197: if (sqlStatement.get(sqlFileType) != null) {
198: Element childRoot10 = (Element) document
199: .createElement("sql");
200: root.appendChild(childRoot10);
201: SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(
202: sqlFileType);
203: childRoot10.setAttribute("name",
204: sqlTagAttributes.getName());
205: // childRoot10.setAttribute("logMode", sqlTagAttributes.getLogMode());
206: childRoot10.setAttribute("commit",
207: sqlTagAttributes.getCommit());
208: //ZK added next line 7.5.2004
209: childRoot10.setAttribute("onErrorContinue",
210: sqlTagAttributes.getOnErrorContinue());
211:
212: // Create child (sqlStmt)
213: Element childRoot11 = (Element) document
214: .createElement("sqlStmt");
215: childRoot10.appendChild(childRoot11);
216:
217: // Create child (include)
218: Element childRoot12 = (Element) document
219: .createElement("include");
220: childRoot11.appendChild(childRoot12);
221:
222: //relative path from LoaderJob.olj file
223: String pathSql = sqlTagAttributes.getHref();
224: pathSql.trim();
225: childRoot12.setAttribute("href", pathSql);
226: childRoot12.setAttribute("parse",
227: sqlTagAttributes.getParse());
228: } //end of sqlStatement.get(sqlFileType)
229:
230: } else if (generatorParameters.getRestoreMode()
231: .equalsIgnoreCase("true")) {
232: //this is use only for restoring the database
233: Element childRoot10 = (Element) document
234: .createElement("sql");
235: root.appendChild(childRoot10);
236: SqlTagAttributes sqlTagAttributes = new SqlTagAttributes(
237: sqlFileType);
238: childRoot10.setAttribute("name", sqlTagAttributes
239: .getName());
240: // childRoot10.setAttribute("logMode", sqlTagAttributes.getLogMode());
241: childRoot10.setAttribute("commit", sqlTagAttributes
242: .getCommit());
243: //ZK added next line 10.5.2004
244: childRoot10.setAttribute("onErrorContinue",
245: sqlTagAttributes.getOnErrorContinue());
246: // Create child (sqlStmt)
247: Element childRoot11 = (Element) document
248: .createElement("sqlStmt");
249: childRoot10.appendChild(childRoot11);
250:
251: // Create child (include)
252: Element childRoot12 = (Element) document
253: .createElement("include");
254: childRoot11.appendChild(childRoot12);
255: //relative path from LoaderJob.olj file
256: String pathSql = sqlTagAttributes.getHref();
257: pathSql.trim();
258: childRoot12.setAttribute("href", pathSql);
259: childRoot12.setAttribute("parse", sqlTagAttributes
260: .getParse());
261: }
262: }
263: sqlFileType++;
264: }
265: if (generatorParameters.getGenerateXml().equalsIgnoreCase(
266: "true")) {
267: try {
268: File file = null;
269: String slash = null;
270: if (generatorParameters.getGeneratorOutput()
271: .equalsIgnoreCase("")) {
272: file = new File("");
273: slash = "";
274: } else {
275: file = new File(generatorParameters
276: .getGeneratorOutput());
277: slash = "/";
278: }
279: if (!file.exists())
280: file.mkdirs();
281:
282: FileOutputStream os = new FileOutputStream(file + slash
283: + "LoaderJob.olj");
284: OutputFormat of = new OutputFormat(); //xerces apache.xml.serializer
285: of.setIndenting(true);
286: of.setIndent(4);
287: of.setMethod(Method.XML);
288: of.setPreserveSpace(false);
289: of.setLineWidth(0);
290: XMLSerializer out = new XMLSerializer(os, of);
291:
292: //IOException
293: out.serialize(document);
294: } catch (Exception e) {
295: String msg = "Exception has occurred when we try to set output format for LoaderJob.olj file.";
296: //if (this.logger != null) {
297: LoaderException le = new LoaderException(msg + "\n"
298: + e.getMessage(), (Throwable) e);
299: this .logger
300: .write(
301: "full",
302: "Exception has occurred when we try to set output format for LoaderJob.olj file."
303: + "\n"
304: + le.getStackTraceAsString());
305: //}
306: throw le;
307: }
308: }
309: this .logger.write("normal",
310: "\tloadJobWriter method is finished.");
311: }
312:
313: /**
314: * This method will set logger object
315: * @param logger
316: */
317: private void setLogger() {
318: this.logger = StandardLogger.getCentralLogger();
319: }
320:
321: }
|