| 1 | |
package org.kuali.core.db.torque; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileInputStream; |
| 5 | |
import java.io.FileOutputStream; |
| 6 | |
import java.util.Iterator; |
| 7 | |
import java.util.Properties; |
| 8 | |
|
| 9 | |
import org.apache.torque.task.TorqueDataModelTask; |
| 10 | |
import org.apache.velocity.context.Context; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | 0 | public class KualiTorqueSQLTask extends TorqueDataModelTask { |
| 16 | 0 | Utils utils = new Utils(); |
| 17 | |
PrettyPrint prettyPrint; |
| 18 | |
|
| 19 | |
private String database; |
| 20 | |
|
| 21 | 0 | private String suffix = ""; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public void setDatabase(String database) { |
| 30 | 0 | this.database = database; |
| 31 | 0 | } |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public String getDatabase() { |
| 39 | 0 | return database; |
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public void setSuffix(String suffix) { |
| 49 | 0 | this.suffix = suffix; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public String getSuffix() { |
| 58 | 0 | return suffix; |
| 59 | |
} |
| 60 | |
|
| 61 | |
protected DatabaseParser getDatabaseParser() { |
| 62 | 0 | return new KualiXmlToAppData(getTargetDatabase(), getTargetPackage()); |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
protected void createSqlDbMap() throws Exception { |
| 71 | 0 | if (getSqlDbMap() == null) { |
| 72 | 0 | return; |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | 0 | Properties sqldbmap = new Properties(); |
| 77 | 0 | Properties sqldbmap_c = new Properties(); |
| 78 | |
|
| 79 | |
|
| 80 | 0 | File file = new File(getSqlDbMap()); |
| 81 | |
|
| 82 | 0 | if (file.exists()) { |
| 83 | 0 | FileInputStream fis = new FileInputStream(file); |
| 84 | 0 | sqldbmap.load(fis); |
| 85 | 0 | fis.close(); |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | Iterator<String> i = getDataModelDbMap().keySet().iterator(); |
| 89 | |
|
| 90 | 0 | while (i.hasNext()) { |
| 91 | 0 | String dataModelName = (String) i.next(); |
| 92 | |
|
| 93 | |
String databaseName; |
| 94 | |
|
| 95 | 0 | if (getDatabase() == null) { |
| 96 | 0 | databaseName = (String) getDataModelDbMap().get(dataModelName); |
| 97 | |
} else { |
| 98 | 0 | databaseName = getDatabase(); |
| 99 | |
} |
| 100 | |
|
| 101 | 0 | String sqlFile = dataModelName + getSuffix() + ".sql"; |
| 102 | 0 | sqldbmap.setProperty(sqlFile, databaseName); |
| 103 | 0 | sqlFile = dataModelName + getSuffix() + "-constraints.sql"; |
| 104 | 0 | sqldbmap_c.setProperty(sqlFile, databaseName); |
| 105 | 0 | } |
| 106 | |
|
| 107 | 0 | sqldbmap.store(new FileOutputStream(getSqlDbMap()), "Sqlfile -> Database map"); |
| 108 | 0 | sqldbmap_c.store(new FileOutputStream(getSqlDbMap() + "-constraints"), "Sqlfile -> Database map"); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
public void onBeforeGenerate() { |
| 112 | 0 | prettyPrint = new PrettyPrint("[INFO] Generating schema SQL "); |
| 113 | 0 | utils.left(prettyPrint); |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
public void onAfterGenerate() { |
| 117 | 0 | utils.right(prettyPrint); |
| 118 | 0 | prettyPrint = null; |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public Context initControlContext() throws Exception { |
| 128 | 0 | super.initControlContext(); |
| 129 | |
|
| 130 | 0 | createSqlDbMap(); |
| 131 | |
|
| 132 | 0 | return context; |
| 133 | |
} |
| 134 | |
} |