1 | |
package org.kuali.db; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.io.InputStreamReader; |
5 | |
import java.io.Reader; |
6 | |
import java.util.Properties; |
7 | |
|
8 | |
import org.apache.commons.io.IOUtils; |
9 | |
import org.springframework.core.io.DefaultResourceLoader; |
10 | |
import org.springframework.core.io.Resource; |
11 | |
import org.springframework.util.PropertyPlaceholderHelper; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
public class SQLGenerator { |
18 | 0 | PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}"); |
19 | 0 | JDBCUtils jdbcUtils = new JDBCUtils(); |
20 | 0 | DefaultResourceLoader loader = new DefaultResourceLoader(); |
21 | |
String encoding; |
22 | |
Properties properties; |
23 | |
String url; |
24 | |
DatabaseCommand command; |
25 | 0 | String prefix = "classpath:org/kuali/db/"; |
26 | |
|
27 | |
public SQLGenerator() { |
28 | 0 | this(null, null, null); |
29 | 0 | } |
30 | |
|
31 | |
public SQLGenerator(Properties properties, String url, DatabaseCommand command) { |
32 | 0 | super(); |
33 | 0 | this.properties = properties; |
34 | 0 | this.url = url; |
35 | 0 | this.command = command; |
36 | 0 | } |
37 | |
|
38 | |
protected String getSQLLocation() { |
39 | 0 | return getLocation("/" + getPackageFriendlyName(command.toString()) + ".sql"); |
40 | |
} |
41 | |
|
42 | |
public String getSQL() throws IOException { |
43 | 0 | String location = getSQLLocation(); |
44 | 0 | Resource resource = loader.getResource(location); |
45 | 0 | String sql = IOUtils.toString(resource.getInputStream(), getEncoding()); |
46 | 0 | Properties customProperties = getCustomProperties(); |
47 | 0 | if (properties != null) { |
48 | 0 | customProperties.putAll(properties); |
49 | |
} |
50 | 0 | sql = helper.replacePlaceholders(sql, customProperties); |
51 | 0 | return sql; |
52 | |
} |
53 | |
|
54 | |
protected String getPackageFriendlyName(String s) { |
55 | 0 | return s.toLowerCase().replace("_", ""); |
56 | |
} |
57 | |
|
58 | |
protected String getLocation(String suffix) { |
59 | 0 | JDBCConfiguration jdbcConfiguration = jdbcUtils.getDatabaseConfiguration(url); |
60 | 0 | DatabaseType type = jdbcConfiguration.getType(); |
61 | 0 | String packageFriendlyName = getPackageFriendlyName(type.toString()); |
62 | 0 | String location = prefix + packageFriendlyName + suffix; |
63 | 0 | return location; |
64 | |
} |
65 | |
|
66 | |
protected String getCustomPropertiesLocation() { |
67 | 0 | return getLocation("/custom.properties"); |
68 | |
} |
69 | |
|
70 | |
protected Properties getCustomProperties() throws IOException { |
71 | 0 | String location = getCustomPropertiesLocation(); |
72 | 0 | Reader input = null; |
73 | |
try { |
74 | 0 | Resource resource = loader.getResource(location); |
75 | 0 | input = new InputStreamReader(resource.getInputStream(), getEncoding()); |
76 | 0 | Properties p = new Properties(); |
77 | 0 | p.load(input); |
78 | 0 | return p; |
79 | |
} finally { |
80 | 0 | IOUtils.closeQuietly(input); |
81 | |
} |
82 | |
} |
83 | |
|
84 | |
public String getEncoding() { |
85 | 0 | return encoding; |
86 | |
} |
87 | |
|
88 | |
public void setEncoding(String encoding) { |
89 | 0 | this.encoding = encoding; |
90 | 0 | } |
91 | |
|
92 | |
public Properties getProperties() { |
93 | 0 | return properties; |
94 | |
} |
95 | |
|
96 | |
public void setProperties(Properties properties) { |
97 | 0 | this.properties = properties; |
98 | 0 | } |
99 | |
|
100 | |
public String getUrl() { |
101 | 0 | return url; |
102 | |
} |
103 | |
|
104 | |
public void setUrl(String url) { |
105 | 0 | this.url = url; |
106 | 0 | } |
107 | |
|
108 | |
public DatabaseCommand getCommand() { |
109 | 0 | return command; |
110 | |
} |
111 | |
|
112 | |
public void setCommand(DatabaseCommand command) { |
113 | 0 | this.command = command; |
114 | 0 | } |
115 | |
|
116 | |
public PropertyPlaceholderHelper getHelper() { |
117 | 0 | return helper; |
118 | |
} |
119 | |
|
120 | |
public void setHelper(PropertyPlaceholderHelper helper) { |
121 | 0 | this.helper = helper; |
122 | 0 | } |
123 | |
|
124 | |
public JDBCUtils getJdbcUtils() { |
125 | 0 | return jdbcUtils; |
126 | |
} |
127 | |
|
128 | |
public void setJdbcUtils(JDBCUtils jdbcUtils) { |
129 | 0 | this.jdbcUtils = jdbcUtils; |
130 | 0 | } |
131 | |
|
132 | |
public DefaultResourceLoader getLoader() { |
133 | 0 | return loader; |
134 | |
} |
135 | |
|
136 | |
public void setLoader(DefaultResourceLoader loader) { |
137 | 0 | this.loader = loader; |
138 | 0 | } |
139 | |
|
140 | |
public String getPrefix() { |
141 | 0 | return prefix; |
142 | |
} |
143 | |
|
144 | |
public void setPrefix(String prefix) { |
145 | 0 | this.prefix = prefix; |
146 | 0 | } |
147 | |
|
148 | |
} |