001: package org.apache.ojb.broker.ant;
002:
003: /* Copyright 2005 The Apache Software Foundation.
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.File;
019: import java.io.FileReader;
020: import java.io.FileWriter;
021: import java.io.PrintWriter;
022: import java.io.Writer;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025:
026: import org.apache.ddlutils.model.Database;
027: import org.apache.ojb.broker.metadata.DescriptorRepository;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.DirectoryScanner;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.Task;
032: import org.apache.tools.ant.types.FileSet;
033:
034: /**
035: * Command to write the SQL used to insert data defined in terms of the repository model into database, into an XML file.
036: *
037: * @author Thomas Dudziak
038: * @version $Revision: 1.1.2.2 $
039: */
040: public class WriteDataSqlToFileCommand extends Command {
041: /** A single data file to read. */
042: private File _singleDataFile = null;
043: /** The input data files. */
044: private ArrayList _fileSets = new ArrayList();
045: /** The file to output the data sql to. */
046: private File _outputFile;
047:
048: /**
049: * Adds a fileset specifying data files.
050: *
051: * @param fileset The additional input files
052: */
053: public void addConfiguredFileset(FileSet fileset) {
054: _fileSets.add(fileset);
055: }
056:
057: /**
058: * Set the xml file containing the data.
059: *
060: * @param schemaFile The data xml file
061: */
062: public void setDataFile(File dataFile) {
063: _singleDataFile = dataFile;
064: }
065:
066: /**
067: * Sets the file to output the data to.
068: *
069: * @param outputFile The output file
070: */
071: public void setOutputFile(File outputFile) {
072: _outputFile = outputFile;
073: }
074:
075: /**
076: * {@inheritDoc}
077: */
078: public void execute(Task task, Database dbModel,
079: DescriptorRepository objModel) throws BuildException {
080: try {
081: DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
082: PrintWriter writer = new PrintWriter(new FileWriter(
083: _outputFile), true);
084:
085: handling.setModel(dbModel, objModel);
086: handling.setPlatform(getPlatform());
087:
088: if (_singleDataFile != null) {
089: readSingleDataFile(task, handling, _singleDataFile,
090: writer);
091: } else {
092: for (Iterator it = _fileSets.iterator(); it.hasNext();) {
093: FileSet fileSet = (FileSet) it.next();
094: File fileSetDir = fileSet.getDir(task.getProject());
095: DirectoryScanner scanner = fileSet
096: .getDirectoryScanner(task.getProject());
097: String[] files = scanner.getIncludedFiles();
098:
099: for (int idx = 0; (files != null)
100: && (idx < files.length); idx++) {
101: readSingleDataFile(task, handling, new File(
102: fileSetDir, files[idx]), writer);
103: }
104: }
105: }
106: } catch (Exception ex) {
107: if (ex instanceof BuildException) {
108: throw (BuildException) ex;
109: } else {
110: throw new BuildException(ex);
111: }
112: }
113: }
114:
115: /**
116: * Reads a single data file.
117: *
118: * @param task The parent task
119: * @param reader The data reader
120: * @param schemaFile The schema file
121: */
122: private void readSingleDataFile(Task task,
123: DdlUtilsDataHandling handling, File dataFile, Writer output) {
124: if (!dataFile.exists()) {
125: task.log("Could not find data file "
126: + dataFile.getAbsolutePath(), Project.MSG_ERR);
127: } else if (!dataFile.isFile()) {
128: task.log("Path " + dataFile.getAbsolutePath()
129: + " does not denote a data file", Project.MSG_ERR);
130: } else if (!dataFile.canRead()) {
131: task.log("Could not read data file "
132: + dataFile.getAbsolutePath(), Project.MSG_ERR);
133: } else {
134: try {
135: FileReader reader = new FileReader(dataFile
136: .getAbsolutePath());
137:
138: handling.getInsertDataSql(reader, output);
139: output.flush();
140: output.close();
141: task.log(
142: "Read data file " + dataFile.getAbsolutePath(),
143: Project.MSG_INFO);
144: } catch (Exception ex) {
145: if (isFailOnError()) {
146: throw new BuildException(
147: "Could not read data file "
148: + dataFile.getAbsolutePath(), ex);
149: }
150: }
151: }
152: }
153: }
|