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 $
039 */
040 public class WriteDataSqlToFileCommand extends Command
041 {
042 /** A single data file to read. */
043 private File _singleDataFile = null;
044 /** The input data files. */
045 private ArrayList _fileSets = new ArrayList();
046 /** The file to output the data sql to. */
047 private File _outputFile;
048
049 /**
050 * Adds a fileset specifying data files.
051 *
052 * @param fileset The additional input files
053 */
054 public void addConfiguredFileset(FileSet fileset)
055 {
056 _fileSets.add(fileset);
057 }
058
059 /**
060 * Set the xml file containing the data.
061 *
062 * @param schemaFile The data xml file
063 */
064 public void setDataFile(File dataFile)
065 {
066 _singleDataFile = dataFile;
067 }
068
069 /**
070 * Sets the file to output the data to.
071 *
072 * @param outputFile The output file
073 */
074 public void setOutputFile(File outputFile)
075 {
076 _outputFile = outputFile;
077 }
078
079 /**
080 * {@inheritDoc}
081 */
082 public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException
083 {
084 try
085 {
086 DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
087 PrintWriter writer = new PrintWriter(new FileWriter(_outputFile), true);
088
089 handling.setModel(dbModel, objModel);
090 handling.setPlatform(getPlatform());
091
092 if (_singleDataFile != null)
093 {
094 readSingleDataFile(task, handling, _singleDataFile, writer);
095 }
096 else
097 {
098 for (Iterator it = _fileSets.iterator(); it.hasNext();)
099 {
100 FileSet fileSet = (FileSet)it.next();
101 File fileSetDir = fileSet.getDir(task.getProject());
102 DirectoryScanner scanner = fileSet.getDirectoryScanner(task.getProject());
103 String[] files = scanner.getIncludedFiles();
104
105 for (int idx = 0; (files != null) && (idx < files.length); idx++)
106 {
107 readSingleDataFile(task, handling, new File(fileSetDir, files[idx]), writer);
108 }
109 }
110 }
111 }
112 catch (Exception ex)
113 {
114 if (ex instanceof BuildException)
115 {
116 throw (BuildException)ex;
117 }
118 else
119 {
120 throw new BuildException(ex);
121 }
122 }
123 }
124
125 /**
126 * Reads a single data file.
127 *
128 * @param task The parent task
129 * @param reader The data reader
130 * @param schemaFile The schema file
131 */
132 private void readSingleDataFile(Task task, DdlUtilsDataHandling handling, File dataFile, Writer output)
133 {
134 if (!dataFile.exists())
135 {
136 task.log("Could not find data file "+dataFile.getAbsolutePath(), Project.MSG_ERR);
137 }
138 else if (!dataFile.isFile())
139 {
140 task.log("Path "+dataFile.getAbsolutePath()+" does not denote a data file", Project.MSG_ERR);
141 }
142 else if (!dataFile.canRead())
143 {
144 task.log("Could not read data file "+dataFile.getAbsolutePath(), Project.MSG_ERR);
145 }
146 else
147 {
148 try
149 {
150 FileReader reader = new FileReader(dataFile.getAbsolutePath());
151
152 handling.getInsertDataSql(reader, output);
153 output.flush();
154 output.close();
155 task.log("Read data file "+dataFile.getAbsolutePath(), Project.MSG_INFO);
156 }
157 catch (Exception ex)
158 {
159 if (isFailOnError())
160 {
161 throw new BuildException("Could not read data file "+dataFile.getAbsolutePath(), ex);
162 }
163 }
164 }
165 }
166 }