View Javadoc

1   package org.apache.torque.task;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.tools.ant.BuildException;
29  import org.apache.tools.ant.DirectoryScanner;
30  import org.apache.tools.ant.types.FileSet;
31  
32  import org.apache.velocity.context.Context;
33  
34  import org.xml.sax.SAXException;
35  
36  import org.apache.torque.engine.EngineException;
37  import org.apache.torque.engine.database.model.AppData;
38  import org.apache.torque.engine.database.model.Database;
39  import org.apache.torque.engine.database.transform.XmlToData;
40  
41  /***
42   * An extended Texen task used for generating SQL source from an XML data file
43   *
44   * @author <a href="mailto:jvanzyl@periapt.com"> Jason van Zyl </a>
45   * @author <a href="mailto:jmcnally@collab.net"> John McNally </a>
46   * @author <a href="mailto:fedor.karpelevitch@home.com"> Fedor Karpelevitch </a>
47   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
48   * @version $Id: TorqueDataSQLTask.java,v 1.5.2.2 2004/05/20 04:35:14 seade Exp $
49   */
50  public class TorqueDataSQLTask extends TorqueDataModelTask
51  {
52      /*** the XML data file */
53      private String dataXmlFile;
54      /*** the data dtd file */
55      private String dataDTD;
56  
57      /***
58       * The target database(s) we are generating SQL for. Right now we can only
59       * deal with a single target, but we will support multiple targets soon.
60       */
61      private String targetDatabase;
62  
63      /***
64       * Sets the DataXmlFile attribute of the TorqueDataSQLTask object
65       *
66       * @param  dataXmlFile The new DataXmlFile value
67       */
68      public void setDataXmlFile(String dataXmlFile)
69      {
70          this.dataXmlFile = project.resolveFile(dataXmlFile).toString();
71      }
72  
73      /***
74       * Gets the DataXmlFile attribute of the TorqueDataSQLTask object
75       *
76       * @return  The DataXmlFile value
77       */
78      public String getDataXmlFile()
79      {
80          return dataXmlFile;
81      }
82  
83      /***
84       * Get the current target database.
85       *
86       * @return  String target database(s)
87       */
88      public String getTargetDatabase()
89      {
90          return targetDatabase;
91      }
92  
93      /***
94       * Set the current target database.  This is where generated java classes
95       * will live.
96       *
97       * @param  v The new TargetDatabase value
98       */
99      public void setTargetDatabase(String v)
100     {
101         targetDatabase = v;
102     }
103 
104     /***
105      * Gets the DataDTD attribute of the TorqueDataSQLTask object
106      *
107      * @return  The DataDTD value
108      */
109     public String getDataDTD()
110     {
111         return dataDTD;
112     }
113 
114     /***
115      * Sets the DataDTD attribute of the TorqueDataSQLTask object
116      *
117      * @param  dataDTD The new DataDTD value
118      */
119     public void setDataDTD(String dataDTD)
120     {
121         this.dataDTD = project.resolveFile(dataDTD).toString();
122     }
123 
124     /***
125      * Set up the initial context for generating the SQL from the XML schema.
126      *
127      * @return the context
128      * @throws Exception If there is an error parsing the data xml.
129      */
130     public Context initControlContext() throws Exception
131     {
132         super.initControlContext();
133 
134         if (dataXmlFile == null && filesets.isEmpty())
135         {
136             throw new BuildException("You must specify an XML data file or "
137                     + "a fileset of XML data files!");
138         }
139 
140         try
141         {
142             AppData app = (AppData) getDataModels().get(0);
143             Database db = app.getDatabase();
144             
145             List data;
146             
147             if (dataXmlFile != null)
148             {
149                 XmlToData dataXmlParser = new XmlToData(db, dataDTD);
150                 data = dataXmlParser.parseFile(dataXmlFile);
151             }
152             else
153             {
154                 data = new ArrayList();
155                 
156                 // Deal with the filesets.
157                 for (int i = 0; i < filesets.size(); i++)
158                 {
159                     FileSet fs = (FileSet) filesets.get(i);
160                     DirectoryScanner ds = fs.getDirectoryScanner(project);
161                     File srcDir = fs.getDir(project);
162 
163                     String[] dataModelFiles = ds.getIncludedFiles();
164                     
165                     // Make a transaction for each file
166                     for (int j = 0; j < dataModelFiles.length; j++)
167                     {
168                         File f = new File(srcDir, dataModelFiles[j]);
169                         XmlToData dataXmlParser = new XmlToData(db, dataDTD);
170                         List newData = dataXmlParser.parseFile(f.toString());
171 
172                         for (Iterator it = newData.iterator(); it.hasNext();)
173                         {
174                             data.add(it.next());
175                         }
176                     }
177                 }
178             }
179             context.put("data", data);
180 
181             // Place our model in the context.
182             context.put("appData", app);
183 
184             // Place the target database in the context.
185             context.put("targetDatabase", targetDatabase);
186 
187             Properties p = new Properties();
188             FileInputStream fis = new FileInputStream(getSqlDbMap());
189             p.load(fis);
190             fis.close();
191             
192             p.setProperty(getOutputFile(), db.getName());
193             p.store(new FileOutputStream(getSqlDbMap()), "Sqlfile -> Database map");
194         }
195         catch (EngineException ee)
196         {
197             throw new BuildException(ee);
198         }
199         catch (SAXException se)
200         {
201             throw new BuildException(se);
202         }
203 
204         return context;
205     }
206 }