001: package org.apache.torque.task;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025:
026: import java.util.Iterator;
027: import java.util.Properties;
028:
029: import org.apache.tools.ant.BuildException;
030:
031: import org.apache.velocity.context.Context;
032:
033: import org.apache.torque.engine.EngineException;
034: import org.apache.torque.engine.database.transform.XmlToAppData;
035: import org.apache.torque.engine.database.model.Database;
036:
037: /**
038: * An extended Texen task used for generating SQL source from
039: * an XML schema describing a database structure.
040: *
041: * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
042: * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
043: * @version $Id: TorqueSQLTask.java 591762 2007-11-04 11:22:06Z tfischer $
044: */
045: public class TorqueSQLTask extends TorqueDataModelTask {
046: // if the database is set than all generated sql files
047: // will be placed in the specified database, the database
048: // will not be taken from the data model schema file.
049:
050: private String database;
051: private String suffix = "";
052:
053: private String idTableXMLFile = null;
054:
055: /**
056: * Sets the name of the database to generate sql for.
057: *
058: * @param database the name of the database to generate sql for.
059: */
060: public void setDatabase(String database) {
061: this .database = database;
062: }
063:
064: /**
065: * Returns the name of the database to generate sql for.
066: *
067: * @return the name of the database to generate sql for.
068: */
069: public String getDatabase() {
070: return database;
071: }
072:
073: /**
074: * Sets the suffix of the generated sql files.
075: *
076: * @param suffix the suffix of the generated sql files.
077: */
078: public void setSuffix(String suffix) {
079: this .suffix = suffix;
080: }
081:
082: /**
083: * Returns the suffix of the generated sql files.
084: *
085: * @return the suffix of the generated sql files.
086: */
087: public String getSuffix() {
088: return suffix;
089: }
090:
091: /**
092: * Set the path to the xml schema file that defines the id-table, used
093: * by the idbroker method.
094: *
095: * @param idXmlFile xml schema file
096: */
097: public void setIdTableXMLFile(String idXmlFile) {
098: idTableXMLFile = idXmlFile;
099: }
100:
101: /**
102: * Gets the id-table xml schema file path.
103: *
104: * @return Path to file.
105: */
106: public String getIdTableXMLFile() {
107: return idTableXMLFile;
108: }
109:
110: /**
111: * create the sql -> database map.
112: *
113: * @throws Exception
114: */
115: private void createSqlDbMap() throws Exception {
116: if (getSqlDbMap() == null) {
117: return;
118: }
119:
120: // Produce the sql -> database map
121: Properties sqldbmap = new Properties();
122:
123: // Check to see if the sqldbmap has already been created.
124: File file = new File(getSqlDbMap());
125:
126: if (file.exists()) {
127: FileInputStream fis = new FileInputStream(file);
128: sqldbmap.load(fis);
129: fis.close();
130: }
131:
132: Iterator i = getDataModelDbMap().keySet().iterator();
133:
134: while (i.hasNext()) {
135: String dataModelName = (String) i.next();
136: String sqlFile = dataModelName + suffix + ".sql";
137:
138: String databaseName;
139:
140: if (getDatabase() == null) {
141: databaseName = (String) getDataModelDbMap().get(
142: dataModelName);
143: } else {
144: databaseName = getDatabase();
145: }
146:
147: sqldbmap.setProperty(sqlFile, databaseName);
148: }
149:
150: sqldbmap.store(new FileOutputStream(getSqlDbMap()),
151: "Sqlfile -> Database map");
152: }
153:
154: /**
155: * Create the database model necessary for the IDBroker tables.
156: * We use the model to generate the necessary SQL to create
157: * these tables. This method adds an AppData object containing
158: * the model to the context under the name "idmodel".
159: */
160: public void loadIdBrokerModel() throws EngineException {
161: // Transform the XML database schema into
162: // data model object.
163: XmlToAppData xmlParser = new XmlToAppData(getTargetDatabase(),
164: null);
165: Database ad = xmlParser.parseFile(getIdTableXMLFile());
166:
167: ad.setName("idmodel");
168: context.put("idmodel", ad);
169: }
170:
171: /**
172: * Place our target database and target platform
173: * values into the context for use in the templates.
174: *
175: * @return the context
176: * @throws Exception
177: */
178: public Context initControlContext() throws Exception {
179: super .initControlContext();
180: try {
181: createSqlDbMap();
182:
183: // If the load path for the id broker table xml schema is
184: // defined then load it.
185: String f = getIdTableXMLFile();
186: if (f != null && f.length() > 0) {
187: loadIdBrokerModel();
188: }
189: } catch (EngineException ee) {
190: throw new BuildException(ee);
191: }
192:
193: return context;
194: }
195: }
|