001: /*
002: LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
003:
004:
005: Copyright (C) 2003 Together
006:
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public
009: License as published by the Free Software Foundation; either
010: version 2.1 of the License, or (at your option) any later version.
011:
012: This library is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: Lesser General Public License for more details.
016:
017: You should have received a copy of the GNU Lesser General Public
018: License along with this library; if not, write to the Free Software
019: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.webdocwf.util.loader.generator;
023:
024: import java.io.BufferedWriter;
025: import java.io.File;
026: import java.io.FileWriter;
027: import java.io.Writer;
028: import java.util.Vector;
029:
030: import org.webdocwf.util.loader.LoaderException;
031: import org.webdocwf.util.loader.logging.Logger;
032: import org.webdocwf.util.loader.logging.StandardLogger;
033:
034: /**
035: *
036: * WriteSqlFiles class creates sql files from input data.
037: * @author Radoslav Dutina
038: * @version 1.0
039: */
040: public class WriteSqlFiles {
041: private Logger logger;
042:
043: /**
044: * Construct object WriteSqlFiles with associated parameters.
045: * @param generatorParameters represents the references to InputParameter object.
046: * @param tableName is name of the table form which we retrieve data.
047: * @param i is the number of tables in generatorOutput directories.
048: * @param importDefinitionAttributes is references to ImportDefinitionAttributes object.
049: * @param relationshipsAttributes is references to RelationshipsAttributes object.
050: * @throws LoaderException
051: */
052:
053: public WriteSqlFiles(String tableName, int i,
054: ImportDefinitionAttributes importDefinitionAttributes,
055: RelationshipsAttributes relationshipsAttributes,
056: InputParameters generatorParameters) throws LoaderException {
057: setLogger();
058: this .logger.write("full", "WriteSqlFiles is started.");
059: Writer out;
060: SqlTagAttributes sqlTagAttributes = null;
061: String nameSQL;
062: Vector sqlStatement = generatorParameters.getSqlToGenerate();
063: try {
064: File sqlFile = null;
065: if (generatorParameters.getGeneratorOutput()
066: .equalsIgnoreCase("")) {
067: sqlFile = new File("sql");
068: } else {
069: sqlFile = new File(generatorParameters
070: .getGeneratorOutput()
071: + "/sql");
072: }
073: if (!sqlFile.exists())
074: sqlFile.mkdirs();
075: int x = 0;
076: while (x < 6) {
077: if (sqlStatement.get(x) != null) {
078: sqlTagAttributes = new SqlTagAttributes(x);
079: nameSQL = sqlTagAttributes.getHref()
080: .substring(
081: sqlTagAttributes.getHref()
082: .lastIndexOf("/") + 1,
083: sqlTagAttributes.getHref()
084: .lastIndexOf("."));
085: SqlStatements sqlStatements = new SqlStatements(
086: nameSQL, tableName,
087: importDefinitionAttributes,
088: relationshipsAttributes, i,
089: generatorParameters);
090: String lineSeparator = System
091: .getProperty("line.separator");
092: String[] lines = sqlStatements.getCreateStream();
093:
094: if (i == 0) {
095: //this is the first time, that we are write sql files.
096: out = new BufferedWriter(new FileWriter(sqlFile
097: + sqlTagAttributes.getHref().substring(
098: sqlTagAttributes.getHref()
099: .lastIndexOf("/")),
100: false));
101: } else {
102: out = new BufferedWriter(new FileWriter(sqlFile
103: + sqlTagAttributes.getHref().substring(
104: sqlTagAttributes.getHref()
105: .lastIndexOf("/")),
106: true));
107: }
108: for (int k = 0; k < lines.length; k++) {
109: out.write(lines[k]);
110: out.write(lineSeparator);
111: }
112: out.close();
113: }
114: x++;
115:
116: } //end while
117: } catch (Exception e) {
118: String msg = "Exception in class WriteSqlFiles: Error has occurred when trying to write sql file!";
119: LoaderException le = new LoaderException(msg + "\n"
120: + e.getMessage(), (Throwable) e);
121: this .logger.write("full", "Exception:" + msg + "\n"
122: + le.getStackTraceAsString());
123: throw le;
124: }
125: this .logger.write("full", "WriteSqlFiles is finished.");
126:
127: }
128:
129: /**
130: * This method will set logger object
131: * @param logger
132: */
133: private void setLogger() {
134: this.logger = StandardLogger.getCentralLogger();
135: }
136: }
|