KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > task > TorqueSQLTask


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 JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27
28 import org.apache.velocity.context.Context;
29
30 import org.apache.torque.engine.EngineException;
31 import org.apache.torque.engine.database.transform.XmlToAppData;
32 import org.apache.torque.engine.database.model.Database;
33
34
35 /**
36  * An extended Texen task used for generating SQL source from
37  * an XML schema describing a database structure.
38  *
39  * @author <a HREF="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
40  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
41  * @version $Id: TorqueSQLTask.java,v 1.6 2004/02/22 06:27:41 jmcnally Exp $
42  */

43 public class TorqueSQLTask extends TorqueDataModelTask
44 {
45     // if the database is set than all generated sql files
46
// will be placed in the specified database, the database
47
// will not be taken from the data model schema file.
48

49     private String JavaDoc database;
50     private String JavaDoc suffix = "";
51
52     private String JavaDoc idTableXMLFile = null;
53
54     /**
55      *
56      * @param database
57      */

58     public void setDatabase(String JavaDoc database)
59     {
60         this.database = database;
61     }
62
63     /**
64      *
65      * @return
66      */

67     public String JavaDoc getDatabase()
68     {
69         return database;
70     }
71
72     /**
73      *
74      * @param suffix
75      */

76     public void setSuffix(String JavaDoc suffix)
77     {
78         this.suffix = suffix;
79     }
80
81     /**
82      *
83      * @return
84      */

85     public String JavaDoc getSuffix()
86     {
87         return suffix;
88     }
89
90     /**
91      * Set the path to the xml schema file that defines the id-table, used
92      * by the idbroker method.
93      *
94      * @param idXmlFile xml schema file
95      */

96     public void setIdTableXMLFile(String JavaDoc idXmlFile)
97     {
98         idTableXMLFile = idXmlFile;
99     }
100
101     /**
102      * Gets the id-table xml schema file path.
103      *
104      * @return Path to file.
105      */

106     public String JavaDoc getIdTableXMLFile()
107     {
108         return idTableXMLFile;
109     }
110
111     /**
112      * create the sql -> database map.
113      *
114      * @throws Exception
115      */

116     private void createSqlDbMap() throws Exception JavaDoc
117     {
118         if (getSqlDbMap() == null)
119         {
120             return;
121         }
122
123         // Produce the sql -> database map
124
Properties JavaDoc sqldbmap = new Properties JavaDoc();
125
126         // Check to see if the sqldbmap has already been created.
127
File JavaDoc file = new File JavaDoc(getSqlDbMap());
128
129         if (file.exists())
130         {
131             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
132             sqldbmap.load(fis);
133             fis.close();
134         }
135
136         Iterator JavaDoc i = getDataModelDbMap().keySet().iterator();
137
138         while (i.hasNext())
139         {
140             String JavaDoc dataModelName = (String JavaDoc) i.next();
141             String JavaDoc sqlFile = dataModelName + suffix + ".sql";
142
143             String JavaDoc databaseName;
144
145             if (getDatabase() == null)
146             {
147                 databaseName = (String JavaDoc) getDataModelDbMap().get(dataModelName);
148             }
149             else
150             {
151                 databaseName = getDatabase();
152             }
153
154             sqldbmap.setProperty(sqlFile, databaseName);
155         }
156
157         sqldbmap.store(new FileOutputStream JavaDoc(getSqlDbMap()),
158                 "Sqlfile -> Database map");
159     }
160
161     /**
162      * Create the database model necessary for the IDBroker tables.
163      * We use the model to generate the necessary SQL to create
164      * these tables. This method adds an AppData object containing
165      * the model to the context under the name "idmodel".
166      */

167     public void loadIdBrokerModel()
168             throws EngineException
169     {
170         // Transform the XML database schema into
171
// data model object.
172
XmlToAppData xmlParser = new XmlToAppData(getTargetDatabase(), null);
173         Database ad = xmlParser.parseFile(getIdTableXMLFile());
174
175         ad.setName("idmodel");
176         context.put("idmodel", ad);
177     }
178
179     /**
180      * Place our target database and target platform
181      * values into the context for use in the templates.
182      *
183      * @return the context
184      * @throws Exception
185      */

186     public Context initControlContext() throws Exception JavaDoc
187     {
188         super.initControlContext();
189         try
190         {
191             createSqlDbMap();
192
193             // If the load path for the id broker table xml schema is
194
// defined then load it.
195
String JavaDoc f = getIdTableXMLFile();
196             if (f != null && f.length() > 0)
197             {
198                 loadIdBrokerModel();
199             }
200         }
201         catch (EngineException ee)
202         {
203             throw new BuildException(ee);
204         }
205
206         return context;
207     }
208 }
209
Popular Tags