View Javadoc
1 package org.apache.torque.task; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.io.File; 58 import java.io.FileInputStream; 59 import java.io.FileOutputStream; 60 61 import java.util.Iterator; 62 import java.util.Properties; 63 64 import org.apache.tools.ant.BuildException; 65 66 import org.apache.velocity.context.Context; 67 68 import org.apache.torque.engine.EngineException; 69 import org.apache.torque.engine.database.transform.XmlToAppData; 70 import org.apache.torque.engine.database.model.AppData; 71 72 73 /*** 74 * An extended Texen task used for generating SQL source from 75 * an XML schema describing a database structure. 76 * 77 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> 78 * @author <a href="mailto:jmcnally@collab.net>John McNally</a> 79 * @version $Id: TorqueSQLTask.java,v 1.2 2003/07/25 16:47:42 henning Exp $ 80 */ 81 public class TorqueSQLTask extends TorqueDataModelTask 82 { 83 // if the database is set than all generated sql files 84 // will be placed in the specified database, the database 85 // will not be taken from the data model schema file. 86 87 private String database; 88 private String suffix = ""; 89 90 private String idTableXMLFile = null; 91 92 /*** 93 * 94 * @param database 95 */ 96 public void setDatabase(String database) 97 { 98 this.database = database; 99 } 100 101 /*** 102 * 103 * @return 104 */ 105 public String getDatabase() 106 { 107 return database; 108 } 109 110 /*** 111 * 112 * @param suffix 113 */ 114 public void setSuffix(String suffix) 115 { 116 this.suffix = suffix; 117 } 118 119 /*** 120 * 121 * @return 122 */ 123 public String getSuffix() 124 { 125 return suffix; 126 } 127 128 /*** 129 * Set the path to the xml schema file that defines the id-table, used 130 * by the idbroker method. 131 * 132 * @param idXmlFile xml schema file 133 */ 134 public void setIdTableXMLFile(String idXmlFile) 135 { 136 idTableXMLFile = idXmlFile; 137 } 138 139 /*** 140 * Gets the id-table xml schema file path. 141 * 142 * @return Path to file. 143 */ 144 public String getIdTableXMLFile() 145 { 146 return idTableXMLFile; 147 } 148 149 /*** 150 * create the sql -> database map. 151 * 152 * @throws Exception 153 */ 154 private void createSqlDbMap() throws Exception 155 { 156 if (getSqlDbMap() == null) 157 { 158 return; 159 } 160 161 // Produce the sql -> database map 162 Properties sqldbmap = new Properties(); 163 164 // Check to see if the sqldbmap has already been created. 165 File file = new File(getSqlDbMap()); 166 167 if (file.exists()) 168 { 169 FileInputStream fis = new FileInputStream(file); 170 sqldbmap.load(fis); 171 fis.close(); 172 } 173 174 Iterator i = getDataModelDbMap().keySet().iterator(); 175 176 while (i.hasNext()) 177 { 178 String dataModelName = (String) i.next(); 179 String sqlFile = dataModelName + suffix + ".sql"; 180 181 String databaseName; 182 183 if (getDatabase() == null) 184 { 185 databaseName = (String) getDataModelDbMap().get(dataModelName); 186 } 187 else 188 { 189 databaseName = getDatabase(); 190 } 191 192 sqldbmap.setProperty(sqlFile, databaseName); 193 } 194 195 sqldbmap.store(new FileOutputStream(getSqlDbMap()), 196 "Sqlfile -> Database map"); 197 } 198 199 /*** 200 * Create the database model necessary for the IDBroker tables. 201 * We use the model to generate the necessary SQL to create 202 * these tables. This method adds an AppData object containing 203 * the model to the context under the name "idmodel". 204 */ 205 public void loadIdBrokerModel() 206 throws EngineException 207 { 208 // Transform the XML database schema into 209 // data model object. 210 XmlToAppData xmlParser = new XmlToAppData(getTargetDatabase(), null, 211 getBasePathToDbProps()); 212 AppData ad = xmlParser.parseFile(getIdTableXMLFile()); 213 214 ad.setName("idmodel"); 215 context.put("idmodel", ad); 216 } 217 218 /*** 219 * Place our target database and target platform 220 * values into the context for use in the templates. 221 * 222 * @return the context 223 * @throws Exception 224 */ 225 public Context initControlContext() throws Exception 226 { 227 super.initControlContext(); 228 try 229 { 230 createSqlDbMap(); 231 232 // If the load path for the id broker table xml schema is 233 // defined then load it. 234 String f = getIdTableXMLFile(); 235 if (f != null && f.length() > 0) 236 { 237 loadIdBrokerModel(); 238 } 239 } 240 catch (EngineException ee) 241 { 242 throw new BuildException(ee); 243 } 244 245 return context; 246 } 247 }

This page was automatically generated by Maven