View Javadoc

1   package org.apache.torque.task;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  
26  import java.util.Iterator;
27  import java.util.Properties;
28  
29  import org.apache.tools.ant.BuildException;
30  
31  import org.apache.velocity.context.Context;
32  
33  import org.apache.torque.engine.EngineException;
34  import org.apache.torque.engine.database.transform.XmlToAppData;
35  import org.apache.torque.engine.database.model.Database;
36  
37  
38  /***
39   * An extended Texen task used for generating SQL source from
40   * an XML schema describing a database structure.
41   *
42   * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
43   * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
44   * @version $Id: TorqueSQLTask.java 591762 2007-11-04 11:22:06Z tfischer $
45   */
46  public class TorqueSQLTask extends TorqueDataModelTask
47  {
48      // if the database is set than all generated sql files
49      // will be placed in the specified database, the database
50      // will not be taken from the data model schema file.
51  
52      private String database;
53      private String suffix = "";
54  
55      private String idTableXMLFile = null;
56  
57      /***
58       * Sets the name of the database to generate sql for.
59       *
60       * @param database the name of the database to generate sql for.
61       */
62      public void setDatabase(String database)
63      {
64          this.database = database;
65      }
66  
67      /***
68       * Returns the name of the database to generate sql for.
69       *
70       * @return the name of the database to generate sql for.
71       */
72      public String getDatabase()
73      {
74          return database;
75      }
76  
77      /***
78       * Sets the suffix of the generated sql files.
79       *
80       * @param suffix the suffix of the generated sql files.
81       */
82      public void setSuffix(String suffix)
83      {
84          this.suffix = suffix;
85      }
86  
87      /***
88       * Returns the suffix of the generated sql files.
89       *
90       * @return the suffix of the generated sql files.
91       */
92      public String getSuffix()
93      {
94          return suffix;
95      }
96  
97      /***
98       * Set the path to the xml schema file that defines the id-table, used
99       * by the idbroker method.
100      *
101      * @param idXmlFile xml schema file
102      */
103     public void setIdTableXMLFile(String idXmlFile)
104     {
105         idTableXMLFile = idXmlFile;
106     }
107 
108     /***
109      * Gets the id-table xml schema file path.
110      *
111      * @return Path to file.
112      */
113     public String getIdTableXMLFile()
114     {
115         return idTableXMLFile;
116     }
117 
118     /***
119      * create the sql -> database map.
120      *
121      * @throws Exception
122      */
123     private void createSqlDbMap() throws Exception
124     {
125         if (getSqlDbMap() == null)
126         {
127             return;
128         }
129 
130         // Produce the sql -> database map
131         Properties sqldbmap = new Properties();
132 
133         // Check to see if the sqldbmap has already been created.
134         File file = new File(getSqlDbMap());
135 
136         if (file.exists())
137         {
138             FileInputStream fis = new FileInputStream(file);
139             sqldbmap.load(fis);
140             fis.close();
141         }
142 
143         Iterator i = getDataModelDbMap().keySet().iterator();
144 
145         while (i.hasNext())
146         {
147             String dataModelName = (String) i.next();
148             String sqlFile = dataModelName + suffix + ".sql";
149 
150             String databaseName;
151 
152             if (getDatabase() == null)
153             {
154                 databaseName = (String) getDataModelDbMap().get(dataModelName);
155             }
156             else
157             {
158                 databaseName = getDatabase();
159             }
160 
161             sqldbmap.setProperty(sqlFile, databaseName);
162         }
163 
164         sqldbmap.store(new FileOutputStream(getSqlDbMap()),
165                 "Sqlfile -> Database map");
166     }
167 
168     /***
169      * Create the database model necessary for the IDBroker tables.
170      * We use the model to generate the necessary SQL to create
171      * these tables.  This method adds an AppData object containing
172      * the model to the context under the name "idmodel".
173      */
174     public void loadIdBrokerModel()
175             throws EngineException
176     {
177         // Transform the XML database schema into
178         // data model object.
179         XmlToAppData xmlParser = new XmlToAppData(getTargetDatabase(), null);
180         Database ad = xmlParser.parseFile(getIdTableXMLFile());
181 
182         ad.setName("idmodel");
183         context.put("idmodel", ad);
184     }
185 
186     /***
187      * Place our target database and target platform
188      * values into the context for use in the templates.
189      *
190      * @return the context
191      * @throws Exception
192      */
193     public Context initControlContext() throws Exception
194     {
195         super.initControlContext();
196         try
197         {
198             createSqlDbMap();
199 
200             // If the load path for the id broker table xml schema is
201             // defined then load it.
202             String f = getIdTableXMLFile();
203             if (f != null && f.length() > 0)
204             {
205                 loadIdBrokerModel();
206             }
207         }
208         catch (EngineException ee)
209         {
210             throw new BuildException(ee);
211         }
212 
213         return context;
214     }
215 }