View Javadoc

1   package com.salto.db.ant;
2   
3   import java.sql.Connection;
4   import java.sql.DriverManager;
5   import java.util.ArrayList;
6   import java.util.Arrays;
7   import java.util.HashSet;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.Map;
11  import java.util.Set;
12  
13  import org.apache.tools.ant.BuildException;
14  import org.apache.tools.ant.Project;
15  import org.apache.tools.ant.Task;
16  
17  import salto.tool.jdo.DataObjectCreate;
18  import salto.tool.jdo.data.JdoInfo;
19  import salto.tool.sql.ConnectionInfo;
20  import salto.tool.sql.DatabaseInfo;
21  import salto.tool.sql.data.TableInfo;
22  
23  import com.salto.db.generator.plugin.IGeneratorPlugin;
24  import com.salto.db.generator.plugin.Plugins;
25  
26  /***
27   * 
28   * Ant task to generate Salto-DB DataObjects.
29   * 
30   * 
31   * @author rflament
32   * 
33   */
34  public class DOGeneratorTask extends Task {
35  
36  	private String outputDir;
37  
38  	private String packageName;
39  
40  	private String driverClassName;
41  
42  	private String jdbcUrl;
43  
44  	private String login;
45  
46  	private String password;
47  
48  	private String tablesNames;
49  
50  	private String schema;
51  
52  	private String plugin;
53  
54  	private String prefix;
55  
56  	private String nameOffset;
57  
58  	private String generateView = "true";
59  
60  	private String followForeignKeys;
61  
62  	private String maxDepth;
63  
64  	private static final String DEFAULT_PLUGIN = "Salto-db";
65  
66  	public void execute() throws BuildException {
67  
68  		log("Salto-DB ANT Generator - Salto Consulting", Project.MSG_INFO);
69  		listPlugins();
70  		Connection defaultConn = null;
71  
72  		String tmpl = DEFAULT_PLUGIN;
73  
74  		if (plugin != null) {
75  			tmpl = plugin;
76  		}
77  
78  		try {
79  
80  			ConnectionInfo connectionInfo = new ConnectionInfo(driverClassName, jdbcUrl, login, password, schema);
81  
82  			Class.forName(driverClassName);
83  			defaultConn = DriverManager.getConnection(jdbcUrl, login, password);
84  
85  			connectionInfo.setConn(defaultConn);
86  
87  			log("Successfully connected to database");
88  
89  			String tableNames[] = tablesNames.split(",");
90  
91  			Set set = new HashSet();
92  			for (int k = 0; k < tableNames.length; k++) {
93  				TableInfo[] tableInfos = DatabaseInfo.getTables(defaultConn, null, schema, tableNames[k]);
94  				log(tableInfos.length + " database object(s) found for table name '" + tableNames[k] + "'");
95  
96  				set.addAll(Arrays.asList(tableInfos));
97  			}
98  
99  			TableInfo[] tableInfos = (TableInfo[]) set.toArray(new TableInfo[set.size()]);
100 
101 			IGeneratorPlugin p = Plugins.getInstance().getPlugin(plugin);
102 			if (p == null) {
103 				throw new Exception("Cannot find plugin " + plugin);
104 			}
105 
106 			log("******************************************");
107 			log(p.getShortDescription());
108 			log("******************************************");
109 
110 			if (prefix == null) {
111 				prefix = p.getDefaultPrefix();
112 			}
113 
114 			int nameOffsetInt = 0;
115 
116 			if (this.nameOffset != null) {
117 				nameOffsetInt = Integer.parseInt(nameOffset);
118 			}
119 
120 			// JdoInfo[] jdoInfos = new JdoInfo[tableInfos.length];
121 
122 			List jdoInfos = new ArrayList();
123 			boolean genView = Boolean.parseBoolean(generateView);
124 
125 			for (int j = 0; j < tableInfos.length; j++) {
126 
127 				if (tableInfos[j].getTableType() == null)
128 					continue;
129 				
130 				if (!tableInfos[j].getTableType().equalsIgnoreCase("VIEW") && !tableInfos[j].getTableType().equalsIgnoreCase("TABLE")) {
131 					continue;
132 				}
133 				if (!genView && tableInfos[j].getTableType().equals("VIEW")) {
134 					log("Not applying plugin '" + tmpl + "' to " + tableInfos[j].getTableType() + " '" + tableInfos[j].getTableName() + "' because it's a view and generateView is set to false.");
135 				} else {
136 					try {
137 						log("Applying plugin '" + tmpl + "' to " + tableInfos[j].getTableType() + " '" + tableInfos[j].getTableName() + "'");
138 
139 						tableInfos[j].setConnInfo(connectionInfo);
140 
141 						JdoInfo jdoInfo = DataObjectCreate.createJdoInfoFromTableInfo(tableInfos[j]);
142 						jdoInfos.add(jdoInfo);
143 					} catch (Exception e) {
144 						log("Skipping " + tableInfos[j].getTableType() + " '" + tableInfos[j].getTableName() + "' because it throws an error " + e);
145 					}
146 				}
147 			}
148 			if (jdoInfos.size() > 0) {
149 				Integer maxDepthf = null;
150 				if (followForeignKeys != null && Boolean.valueOf(followForeignKeys).booleanValue()) {
151 					log("followForeignKeys enabled");
152 					if (maxDepth != null) {
153 						maxDepthf = Integer.valueOf(this.maxDepth);
154 						log("maxDepth = " + maxDepthf);
155 					}
156 
157 				} else {
158 					maxDepthf = new Integer(0);
159 				}
160 
161 				new DataObjectCreate().creerDoSimple(prefix, nameOffsetInt, this.getProject().getBaseDir() + "/" + outputDir, packageName, (JdoInfo[]) jdoInfos.toArray(new JdoInfo[jdoInfos.size()]), tmpl, maxDepthf);
162 			}
163 
164 		} catch (Exception e) {
165 			log(e.getMessage());
166 			throw new BuildException(e);
167 		}
168 
169 		finally {
170 			try {
171 				log("Closing connection to database");
172 				defaultConn.close();
173 			} catch (Exception e) {
174 
175 			}
176 
177 		}
178 
179 	}
180 
181 	public void listPlugins() {
182 		log("-----------------------------------------");
183 		log("Loaded Plugins : ");
184 		for (Iterator i = Plugins.getInstance().getPlugins().entrySet().iterator(); i.hasNext();) {
185 			Map.Entry entry = (Map.Entry) i.next();
186 			log((String) entry.getKey());
187 		}
188 		log("-----------------------------------------");
189 	}
190 
191 	public String getTablesNames() {
192 		return tablesNames;
193 	}
194 
195 	public void setTablesNames(String tablesNames) {
196 		this.tablesNames = tablesNames;
197 	}
198 
199 	public String getJdbcUrl() {
200 		return jdbcUrl;
201 	}
202 
203 	public void setJdbcUrl(String jdbcUrl) {
204 		this.jdbcUrl = jdbcUrl;
205 	}
206 
207 	public String getLogin() {
208 		return login;
209 	}
210 
211 	public void setLogin(String login) {
212 		this.login = login;
213 	}
214 
215 	public String getPassword() {
216 		return password;
217 	}
218 
219 	public void setPassword(String password) {
220 		this.password = password;
221 	}
222 
223 	public String getDriverClassName() {
224 		return driverClassName;
225 	}
226 
227 	public void setDriverClassName(String driverClassName) {
228 		this.driverClassName = driverClassName;
229 	}
230 
231 	public String getOutputDir() {
232 		return outputDir;
233 	}
234 
235 	public void setOutputDir(String outputDir) {
236 		this.outputDir = outputDir;
237 	}
238 
239 	public String getPackageName() {
240 		return packageName;
241 	}
242 
243 	public void setPackageName(String packageName) {
244 		this.packageName = packageName;
245 	}
246 
247 	public String getSchema() {
248 		return schema;
249 	}
250 
251 	public void setSchema(String schema) {
252 		this.schema = schema;
253 	}
254 
255 	public String getPlugin() {
256 		return plugin;
257 	}
258 
259 	public void setPlugin(String plugin) {
260 		this.plugin = plugin;
261 	}
262 
263 	public String getPrefix() {
264 		return prefix;
265 	}
266 
267 	public void setPrefix(String prefix) {
268 		this.prefix = prefix;
269 	}
270 
271 	public String getNameOffset() {
272 		return nameOffset;
273 	}
274 
275 	public void setNameOffset(String nameOffset) {
276 		this.nameOffset = nameOffset;
277 	}
278 
279 	public String getGenerateView() {
280 		return generateView;
281 	}
282 
283 	public void setGenerateView(String generateView) {
284 		this.generateView = generateView;
285 	}
286 
287 	public String getFollowForeignKeys() {
288 		return followForeignKeys;
289 	}
290 
291 	public void setFollowForeignKeys(String followForeignKeys) {
292 		this.followForeignKeys = followForeignKeys;
293 	}
294 
295 	public String getMaxDepth() {
296 		return maxDepth;
297 	}
298 
299 	public void setMaxDepth(String maxDepth) {
300 		this.maxDepth = maxDepth;
301 	}
302 
303 }