1 package org.asyrinx.joey.gen.task;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9
10 import ognl.OgnlRuntime;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.types.FileSet;
16 import org.apache.velocity.VelocityContext;
17 import org.apache.velocity.context.Context;
18 import org.apache.velocity.texen.ant.TexenTask;
19 import org.asyrinx.brownie.core.lang.StringUtils;
20 import org.asyrinx.brownie.seasar.aop.CacheInterceptor;
21 import org.asyrinx.joey.gen.command.rdb2java.Rdb2JavaBuilder;
22 import org.asyrinx.joey.gen.command.rdb2java.standard.BasicBuilder;
23 import org.asyrinx.joey.gen.hibernate.HibernateUtils;
24 import org.asyrinx.joey.gen.model.java.AppDomain;
25 import org.asyrinx.joey.gen.model.rdb.Databases;
26 import org.asyrinx.joey.gen.model.rdb.xml.DatabasesLoader;
27 import org.asyrinx.joey.gen.model.rdb.xml.DatabasesLoaderImpl;
28 import org.asyrinx.joey.gen.model.rdb.xml.XmlToRdbImpl;
29 import org.seasar.framework.container.S2Container;
30 import org.seasar.framework.container.impl.AspectDefImpl;
31 import org.seasar.framework.container.impl.ComponentDefImpl;
32 import org.seasar.framework.container.impl.S2ContainerImpl;
33 import org.xml.sax.SAXException;
34
35 /***
36 */
37 public class DataModelTask extends TexenTask {
38
39 protected List filesets = new ArrayList();
40
41 protected Context context;
42
43 public void addFileset(FileSet set) {
44 filesets.add(set);
45 }
46
47 private static S2Container container = null;
48
49 public Context initControlContext() throws Exception {
50 if (container == null) {
51
52
53
54
55
56 container = new S2ContainerImpl();
57 container.register(XmlToRdbImpl.class);
58 final ComponentDefImpl databasesLoaderDef = new ComponentDefImpl(DatabasesLoaderImpl.class);
59 databasesLoaderDef.addAspectDef(new AspectDefImpl(new CacheInterceptor()));
60 container.register(databasesLoaderDef);
61 final ComponentDefImpl javaBuilderDef = new ComponentDefImpl(BasicBuilder.class);
62 javaBuilderDef.addAspectDef(new AspectDefImpl(new CacheInterceptor()));
63 container.register(javaBuilderDef);
64 }
65 return loadModels();
66 }
67
68 private Context loadModels() throws IOException, SAXException {
69 if (filesets.isEmpty())
70 throw new BuildException("You must specify an XML schema or " + "fileset of XML schemas!");
71 final Databases databases = loadDatabaseModels();
72
73 final Rdb2JavaBuilder builder = (Rdb2JavaBuilder) container.getComponent(Rdb2JavaBuilder.class);
74 builder.setProperties(Collections.unmodifiableMap(getProject().getProperties()));
75 final AppDomain domain = builder.execute(databases);
76
77 context = new VelocityContext();
78
79
80 context.put("databases", databases);
81 context.put("domain", domain);
82 context.put("builder", builder);
83 context.put("rdb2java", builder.getRdb2Java());
84 context.put("helper", new VelocityHelper(context));
85 context.put("stringUtils", new org.asyrinx.brownie.core.lang.StringUtils());
86 context.put("hibernateUtils", new HibernateUtils());
87 context.put("ognl", new VelocityOgnlHelper(context));
88
89 final Object[] keys = context.getKeys();
90 for (int i = 0; i < keys.length; i++) {
91 log.info("task context key: " + keys[i] + " value: " + context.get(String.valueOf(keys[i])));
92 }
93
94 OgnlRuntime.setPropertyAccessor(Map.class, new VelocityOgnlAccessor(context));
95 return context;
96 }
97
98 private Databases loadDatabaseModels() throws IOException, SAXException {
99 final DatabasesLoader databasesLoader = (DatabasesLoader) container.getComponent(DatabasesLoader.class);
100 return databasesLoader.load(this.filesets, this.project);
101 }
102
103 private final Log log = LogFactory.getLog(this.getClass());
104
105 public void setContextProperties(String file) {
106 super.setContextProperties(file);
107
108 Map env = super.getProject().getProperties();
109 for (Iterator i = env.keySet().iterator(); i.hasNext();) {
110 String key = (String) i.next();
111 if (key.startsWith("joey-gen.")) {
112 String newKey = toVelocityKey(key.substring("joey-gen.".length()));
113 contextProperties.setProperty(newKey, env.get(key));
114 log.debug("joey-gen property available: " + newKey + ":" + env.get(key));
115 }
116 }
117 for (Iterator i = env.keySet().iterator(); i.hasNext();) {
118 String key = (String) i.next();
119 if (key.startsWith("proj.")) {
120 String newKey = toVelocityKey(key);
121 contextProperties.setProperty(newKey, env.get(key));
122 log.debug("project property available: " + newKey + ":" + env.get(key));
123 }
124 }
125 }
126
127 /***
128 * @param newKey
129 * @return
130 */
131 private String toVelocityKey(String newKey) {
132 int j = newKey.indexOf(".");
133 while (j != -1) {
134 newKey = newKey.substring(0, j) + StringUtils.capitalize(newKey.substring(j + 1));
135 j = newKey.indexOf(".");
136 }
137 return newKey;
138 }
139
140 }