1 package net.sf.stitch.crud;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import groovy.text.SimpleTemplateEngine;
20 import groovy.text.Template;
21 import groovy.text.TemplateEngine;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29 import java.io.Writer;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Properties;
35
36 import org.apache.maven.plugin.AbstractMojo;
37 import org.apache.maven.plugin.MojoExecutionException;
38
39
40
41
42
43
44
45 public class CrudMojo extends AbstractMojo
46 {
47
48
49
50
51
52 private File outputDirectory;
53
54
55
56
57
58
59 private String [] entityClasses;
60
61
62
63
64
65 private Properties userProperties;
66
67
68
69
70
71 private String templateLocation;
72
73
74
75
76 public void execute() throws MojoExecutionException
77 {
78 getLog().info("CrudMojo.execute");
79
80
81 assert null != this.outputDirectory;
82 if (!this.outputDirectory.exists())
83 {
84 this.outputDirectory.mkdirs();
85 }
86
87
88 final List<CrudEntity> entityClassList = new ArrayList<CrudEntity>(entityClasses.length);
89 for (final String className : this.entityClasses)
90 {
91 getLog().debug("Loading class: " + className);
92 try
93 {
94 final CrudEntity entityClass = new CrudEntity(className);
95 entityClassList.add(entityClass);
96 }
97 catch (ClassNotFoundException e)
98 {
99 throw new MojoExecutionException ("Unable to load class: " + className, e);
100 }
101 }
102
103 try
104 {
105 generate(entityClassList);
106 }
107 catch (Exception e)
108 {
109 throw new MojoExecutionException ("Problem generating CRUD!", e);
110 }
111 }
112
113 public void generate (final List<CrudEntity> entityClassList)
114 throws IOException, ClassNotFoundException
115 {
116 assert null != outputDirectory && outputDirectory.isDirectory();
117
118 final String [] templateSourceFiles =
119 {
120 "Entity.page.xml",
121 "Entity.xhtml",
122 "EntityEdit.page.xml",
123 "EntityEdit.xhtml",
124 "EntityList.page.xml",
125 "EntityList.xhtml",
126 };
127
128 final TemplateEngine engine = new SimpleTemplateEngine();
129
130
131 final Map<String, Template> templateMap = new LinkedHashMap<String,Template>(templateSourceFiles.length);
132 for (final String templateSourceFile : templateSourceFiles)
133 {
134 getLog().debug("Parsing template source: " + templateSourceFile);
135
136 final InputStream templateStream = getClass().getClassLoader().getResourceAsStream(this.templateLocation + templateSourceFile);
137 assert null != templateStream;
138 final Reader templateReader = new InputStreamReader( templateStream );
139 final Template template = engine.createTemplate( templateReader );
140 templateMap.put(templateSourceFile, template);
141 templateReader.close();
142 }
143
144
145 for (final CrudEntity entityClass : entityClassList)
146 {
147 getLog().info("Creating CRUD interface for: " + entityClass.getEntityShortName());
148
149 final Map<String,Object> templateParameters = new LinkedHashMap<String,Object>();
150 templateParameters.put("crudEntity", entityClass);
151 templateParameters.put("userProperties", this.userProperties);
152
153 for (final Map.Entry<String, Template> templateEntry : templateMap.entrySet())
154 {
155 final String filename = templateEntry.getKey().replace("Entity", entityClass.getEntityShortName());
156 final Template template = templateEntry.getValue();
157 final File outputFile = new File(outputDirectory, filename);
158 final Writer outputFileWriter = new FileWriter (outputFile);
159 try
160 {
161 getLog().debug("Making " + outputFile.getPath());
162 template.make(templateParameters).writeTo(outputFileWriter);
163 }
164 finally
165 {
166 outputFileWriter.close();
167 }
168 }
169 }
170 }
171 }