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 | |
@Override |
62 | |
protected DatabaseParser getDatabaseParser() { |
63 | 0 | return new KualiXmlToAppData(getTargetDatabase(), getTargetPackage()); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
protected void createSqlDbMap() throws Exception { |
72 | 0 | if (getSqlDbMap() == null) { |
73 | 0 | return; |
74 | |
} |
75 | |
|
76 | |
|
77 | 0 | Properties sqldbmap = new Properties(); |
78 | 0 | Properties sqldbmap2 = new Properties(); |
79 | |
|
80 | |
|
81 | 0 | File file = new File(getSqlDbMap()); |
82 | |
|
83 | 0 | if (file.exists()) { |
84 | 0 | FileInputStream fis = new FileInputStream(file); |
85 | 0 | sqldbmap.load(fis); |
86 | 0 | fis.close(); |
87 | |
} |
88 | |
|
89 | 0 | Iterator<String> i = getDataModelDbMap().keySet().iterator(); |
90 | |
|
91 | 0 | while (i.hasNext()) { |
92 | 0 | String dataModelName = i.next(); |
93 | |
|
94 | |
String databaseName; |
95 | |
|
96 | 0 | if (getDatabase() == null) { |
97 | 0 | databaseName = getDataModelDbMap().get(dataModelName); |
98 | |
} else { |
99 | 0 | databaseName = getDatabase(); |
100 | |
} |
101 | |
|
102 | 0 | String sqlFile = dataModelName + getSuffix() + ".sql"; |
103 | 0 | sqldbmap.setProperty(sqlFile, databaseName); |
104 | 0 | sqlFile = dataModelName + getSuffix() + "-constraints.sql"; |
105 | 0 | sqldbmap2.setProperty(sqlFile, databaseName); |
106 | 0 | } |
107 | |
|
108 | 0 | sqldbmap.store(new FileOutputStream(getSqlDbMap()), "Sqlfile -> Database map"); |
109 | 0 | sqldbmap2.store(new FileOutputStream(getSqlDbMap() + "-constraints"), "Sqlfile -> Database map"); |
110 | 0 | } |
111 | |
|
112 | |
public void onBeforeGenerate() { |
113 | 0 | prettyPrint = new PrettyPrint("[INFO] Generating schema SQL "); |
114 | 0 | utils.left(prettyPrint); |
115 | 0 | } |
116 | |
|
117 | |
public void onAfterGenerate() { |
118 | 0 | utils.right(prettyPrint); |
119 | 0 | prettyPrint = null; |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
@Override |
129 | |
public Context initControlContext() throws Exception { |
130 | 0 | super.initControlContext(); |
131 | |
|
132 | 0 | createSqlDbMap(); |
133 | |
|
134 | 0 | return context; |
135 | |
} |
136 | |
} |