KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > ant > WriteDataSqlToFileCommand


1 package org.apache.ojb.broker.ant;
2
3 /* Copyright 2005 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.File JavaDoc;
19 import java.io.FileReader JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.ddlutils.model.Database;
27 import org.apache.ojb.broker.metadata.DescriptorRepository;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.DirectoryScanner;
30 import org.apache.tools.ant.Project;
31 import org.apache.tools.ant.Task;
32 import org.apache.tools.ant.types.FileSet;
33
34 /**
35  * Command to write the SQL used to insert data defined in terms of the repository model into database, into an XML file.
36  *
37  * @author Thomas Dudziak
38  * @version $Revision: 1.1.2.2 $
39  */

40 public class WriteDataSqlToFileCommand extends Command
41 {
42     /** A single data file to read. */
43     private File JavaDoc _singleDataFile = null;
44     /** The input data files. */
45     private ArrayList JavaDoc _fileSets = new ArrayList JavaDoc();
46     /** The file to output the data sql to. */
47     private File JavaDoc _outputFile;
48
49     /**
50      * Adds a fileset specifying data files.
51      *
52      * @param fileset The additional input files
53      */

54     public void addConfiguredFileset(FileSet fileset)
55     {
56         _fileSets.add(fileset);
57     }
58
59     /**
60      * Set the xml file containing the data.
61      *
62      * @param schemaFile The data xml file
63      */

64     public void setDataFile(File JavaDoc dataFile)
65     {
66         _singleDataFile = dataFile;
67     }
68
69     /**
70      * Sets the file to output the data to.
71      *
72      * @param outputFile The output file
73      */

74     public void setOutputFile(File JavaDoc outputFile)
75     {
76         _outputFile = outputFile;
77     }
78
79     /**
80      * {@inheritDoc}
81      */

82     public void execute(Task task, Database dbModel, DescriptorRepository objModel) throws BuildException
83     {
84         try
85         {
86             DdlUtilsDataHandling handling = new DdlUtilsDataHandling();
87             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new FileWriter JavaDoc(_outputFile), true);
88
89             handling.setModel(dbModel, objModel);
90             handling.setPlatform(getPlatform());
91
92             if (_singleDataFile != null)
93             {
94                 readSingleDataFile(task, handling, _singleDataFile, writer);
95             }
96             else
97             {
98                 for (Iterator JavaDoc it = _fileSets.iterator(); it.hasNext();)
99                 {
100                     FileSet fileSet = (FileSet)it.next();
101                     File JavaDoc fileSetDir = fileSet.getDir(task.getProject());
102                     DirectoryScanner scanner = fileSet.getDirectoryScanner(task.getProject());
103                     String JavaDoc[] files = scanner.getIncludedFiles();
104     
105                     for (int idx = 0; (files != null) && (idx < files.length); idx++)
106                     {
107                         readSingleDataFile(task, handling, new File JavaDoc(fileSetDir, files[idx]), writer);
108                     }
109                 }
110             }
111         }
112         catch (Exception JavaDoc 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 JavaDoc dataFile, Writer JavaDoc 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 JavaDoc reader = new FileReader JavaDoc(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 JavaDoc ex)
158             {
159                 if (isFailOnError())
160                 {
161                     throw new BuildException("Could not read data file "+dataFile.getAbsolutePath(), ex);
162                 }
163             }
164         }
165     }
166 }
167
Popular Tags