1 package org.apache.torque.task; 2 3 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 23 import java.util.Iterator ; 24 import java.util.Properties ; 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 43 public class TorqueSQLTask extends TorqueDataModelTask 44 { 45 49 private String database; 50 private String suffix = ""; 51 52 private String idTableXMLFile = null; 53 54 58 public void setDatabase(String database) 59 { 60 this.database = database; 61 } 62 63 67 public String getDatabase() 68 { 69 return database; 70 } 71 72 76 public void setSuffix(String suffix) 77 { 78 this.suffix = suffix; 79 } 80 81 85 public String getSuffix() 86 { 87 return suffix; 88 } 89 90 96 public void setIdTableXMLFile(String idXmlFile) 97 { 98 idTableXMLFile = idXmlFile; 99 } 100 101 106 public String getIdTableXMLFile() 107 { 108 return idTableXMLFile; 109 } 110 111 116 private void createSqlDbMap() throws Exception 117 { 118 if (getSqlDbMap() == null) 119 { 120 return; 121 } 122 123 Properties sqldbmap = new Properties (); 125 126 File file = new File (getSqlDbMap()); 128 129 if (file.exists()) 130 { 131 FileInputStream fis = new FileInputStream (file); 132 sqldbmap.load(fis); 133 fis.close(); 134 } 135 136 Iterator i = getDataModelDbMap().keySet().iterator(); 137 138 while (i.hasNext()) 139 { 140 String dataModelName = (String ) i.next(); 141 String sqlFile = dataModelName + suffix + ".sql"; 142 143 String databaseName; 144 145 if (getDatabase() == null) 146 { 147 databaseName = (String ) getDataModelDbMap().get(dataModelName); 148 } 149 else 150 { 151 databaseName = getDatabase(); 152 } 153 154 sqldbmap.setProperty(sqlFile, databaseName); 155 } 156 157 sqldbmap.store(new FileOutputStream (getSqlDbMap()), 158 "Sqlfile -> Database map"); 159 } 160 161 167 public void loadIdBrokerModel() 168 throws EngineException 169 { 170 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 186 public Context initControlContext() throws Exception 187 { 188 super.initControlContext(); 189 try 190 { 191 createSqlDbMap(); 192 193 String 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 |