1
2
3
4
5 package net.sourceforge.addam.ddlgen.generic;
6
7 import net.sourceforge.addam.ddlgen.Filter;
8 import net.sourceforge.addam.ddlgen.Generator;
9
10 import org.apache.velocity.Template;
11 import org.apache.velocity.VelocityContext;
12 import org.apache.velocity.app.Velocity;
13 import org.apache.velocity.exception.ResourceNotFoundException;
14
15 import java.io.BufferedWriter;
16 import java.sql.Connection;
17 import java.sql.DatabaseMetaData;
18 import java.sql.ResultSet;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Properties;
22
23 /**
24 * ENTER A DESCRIPTION HERE
25 *
26 * @author TIM3
27 * @since Oct 9, 2005
28 */
29 public abstract class VelocityGenerator implements Generator {
30
31 public List<String> getObjects(String catalog, String schema, Filter filter,
32 Connection connection, DatabaseMetaData metadata) throws Exception {
33 if (metadata.storesUpperCaseIdentifiers()) {
34 catalog = (catalog == null) ? null : catalog.toUpperCase();
35 schema = (schema == null) ? null : schema.toUpperCase();
36 } else if (metadata.storesLowerCaseIdentifiers()) {
37 catalog = (catalog == null) ? null : catalog.toLowerCase();
38 schema = (schema == null) ? null : schema.toLowerCase();
39 }
40 ResultSet tablesRS = metadata.getTables(catalog, schema, "%", new String[]{"TABLE"});
41 List<String> objects = new ArrayList<String>();
42 while (tablesRS.next()) {
43 String dbObject = tablesRS.getString(3);
44 if (filter.includes(dbObject)) {
45 objects.add(dbObject);
46 }
47 }
48 return objects;
49 }
50
51 public void generate(String catalog, String schema, String object,
52 String vendor, Connection connection, DatabaseMetaData metadata, BufferedWriter writer)
53 throws Exception {
54 if (metadata.storesUpperCaseIdentifiers()) {
55 catalog = (catalog == null) ? null : catalog.toUpperCase();
56 schema = (schema == null) ? null : schema.toUpperCase();
57 } else if (metadata.storesLowerCaseIdentifiers()) {
58 catalog = (catalog == null) ? null : catalog.toLowerCase();
59 schema = (schema == null) ? null : schema.toLowerCase();
60 }
61 if (vendor == null) vendor = metadata.getDatabaseProductName();
62 Template template = getTemplate(metadata, vendor);
63 VelocityContext context = getContext(catalog, schema, object, connection, metadata);
64 template.merge(context, writer);
65 }
66
67 protected abstract VelocityContext getContext(String catalog, String schema, String object,
68 Connection connection, DatabaseMetaData metadata) throws Exception;
69
70 protected Template getTemplate(DatabaseMetaData metadata, String vendor) throws Exception {
71
72
73
74 Properties props = new Properties();
75 props.put("resource.loader", "class");
76 props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
77 Velocity.init(props);
78
79 Template template;
80 try {
81 String templateName = this.getTemplateName(vendor);
82 template = Velocity.getTemplate(templateName);
83 } catch (ResourceNotFoundException e) {
84 String templateName = this.getTemplateName("");
85 template = Velocity.getTemplate(templateName);
86 }
87 return template;
88 }
89
90 protected String getTemplateName(String vendor) {
91 String name = getClass().getName().replace('.', '/');
92 int lastIndex = name.lastIndexOf("/");
93 String dir = name.substring(0, lastIndex);
94 String file = name.substring(lastIndex + 1);
95 String suffix = (vendor == null || vendor.length()==0) ? "" : "-" + vendor;
96 String templateName = dir + "/" + file + suffix + ".vm";
97 templateName = templateName.replace("Generator","");
98 return templateName;
99 }
100
101 }