View Javadoc

1   package org.appfuse.tool;
2   
3   import org.hibernate.cfg.Configuration;
4   import org.hibernate.tool.hbm2x.GenericExporter;
5   import org.hibernate.tool.hbm2x.pojo.POJOClass;
6   import org.springframework.core.io.ClassPathResource;
7   import org.springframework.core.io.Resource;
8   
9   import java.io.File;
10  import java.util.Map;
11  import java.util.Properties;
12  
13  /**
14   * This class is used to process FreeMarker templates and
15   * produce files from them.
16   *
17   * @author mraible
18   */
19  public class AppFuseExporter extends GenericExporter {
20  
21      public AppFuseExporter() {
22          init();
23      }
24  
25      public AppFuseExporter(Configuration cfg, File outputdir) {
26          super(cfg, outputdir);
27          init();
28      }
29  
30      public void init() {
31          super.setArtifactCollector(new NoXMLFormatArtifactCollector());
32      }
33  
34      public String getName() {
35          return "appfuse";
36      }
37  
38      public void doStart() {
39          String generateCore = getProperties().getProperty("generate-core");
40          if (generateCore != null && generateCore.equals("true")) {
41              generateCore();
42          }
43  
44          String generateWeb = getProperties().getProperty("generate-web");
45          if (!"true".equals(generateCore) && generateWeb != null && generateWeb.equals("true")) {
46              generateWeb();
47          }
48  
49          if (generateCore == null && generateWeb == null) {
50              generateCore();
51              generateWeb();
52          }
53      }
54  
55      private void generateCore() {
56          //noinspection UnnecessaryUnboxing
57          boolean genericCore = Boolean.valueOf(getProperties().getProperty("genericcore")).booleanValue();
58  
59          // generate sample-data.xml
60          configureExporter("appfuse/dao/sample-data.ftl", "src/test/resources/{class-name}-sample-data.xml").start();
61  
62          // Encourage use of genericCore - less code to maintain!
63          if (genericCore) {
64              configureExporter("appfuse/service/generic-beans.ftl", "src/main/resources/{class-name}-generic-beans.xml").start();
65          } else {
66              // DAO Test
67              configureExporter("appfuse/dao/dao-test.ftl", "src/test/java/{basepkg-name}/dao/{class-name}DaoTest.java").start();
68  
69              // DAO Interfaces
70              configureExporter("appfuse/dao/dao.ftl", "src/main/java/{basepkg-name}/dao/{class-name}Dao.java").start();
71  
72              // DAO Bean Definition - only used when genericCore == true
73              configureExporter("appfuse/dao/dao-bean.ftl", "src/main/resources/{class-name}Dao-bean.xml").start();
74  
75              String daoFramework = getProperties().getProperty("daoframework");
76  
77              // DAO Implementation
78              configureExporter("appfuse/dao/" + daoFramework + "/dao-impl.ftl",
79                      "src/main/java/{basepkg-name}/dao/" + daoFramework + "/{class-name}Dao" +
80                              getDaoFilename(daoFramework) + ".java").start();
81  
82              // Manager Test
83              configureExporter("appfuse/service/manager-test.ftl",
84                      "src/test/java/{basepkg-name}/service/impl/{class-name}ManagerImplTest.java").start();
85  
86              // Manager Interface
87              configureExporter("appfuse/service/manager.ftl",
88                      "src/main/java/{basepkg-name}/service/{class-name}Manager.java").start();
89  
90              // Manager Implementation
91              configureExporter("appfuse/service/manager-impl.ftl",
92                      "src/main/java/{basepkg-name}/service/impl/{class-name}ManagerImpl.java").start();
93          }
94  
95          String daoFramework = getProperties().getProperty("daoframework");
96  
97          // iBATIS SQL Map files
98          if (daoFramework.equals("ibatis")) {
99              configureExporter("appfuse/dao/ibatis/sql-map-config.ftl",
100                 "src/main/resources/{class-name}-sql-map-config.xml").start();
101             configureExporter("appfuse/dao/ibatis/sql-map.ftl",
102                 "src/main/resources/sqlmaps/{class-name}SQL.xml").start();
103             configureExporter("appfuse/dao/ibatis/compass-gps.ftl",
104                 "src/main/resources/compass-gps.xml").start();
105             configureExporter("appfuse/dao/ibatis/select-ids.ftl",
106                 "src/main/resources/{class-name}-select-ids.xml").start();
107         }
108 
109         // Manager Bean Definition - only used when genericCore == true
110         configureExporter("appfuse/service/manager-bean.ftl", "src/main/resources/{class-name}Manager-bean.xml").start();
111     }
112 
113     private void generateWeb() {
114         String packaging = getProperties().getProperty("packaging");
115         boolean isAppFuse = Boolean.valueOf(getProperties().getProperty("isAppFuse"));
116         boolean webProject = packaging != null && packaging.equalsIgnoreCase("war");
117 
118         if (!webProject) return;
119 
120         String webFramework = getProperties().getProperty("webframework");
121         Resource jwebUnitTemplate = new ClassPathResource("appfuse/web/" + webFramework + "/jwebunit-tests.ftl");
122         if (webFramework.equalsIgnoreCase("jsf")) {
123             // tests
124             configureExporter("appfuse/web/jsf/list-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}ListTest.java").start();
125             configureExporter("appfuse/web/jsf/form-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}FormTest.java").start();
126 
127             // managed beans
128             configureExporter("appfuse/web/jsf/list.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}List.java").start();
129             configureExporter("appfuse/web/jsf/form.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}Form.java").start();
130 
131             // views
132             configureExporter("appfuse/web/jsf/list-view.ftl", "src/main/webapp/{class-name}s.xhtml").start();
133             configureExporter("appfuse/web/jsf/form-view.ftl", "src/main/webapp/{class-name}Form.xhtml").start();
134 
135             // configuration
136             configureExporter("appfuse/web/jsf/navigation.ftl", "src/main/webapp/WEB-INF/{class-name}-navigation.xml").start();
137         } else if (webFramework.equalsIgnoreCase("spring") || webFramework.equalsIgnoreCase("spring-security") || webFramework.equalsIgnoreCase("spring-freemarker")) {
138             // tests
139             configureExporter("appfuse/web/spring/controller-test.ftl", "src/test/java/{basepkg-name}/webapp/controller/{class-name}ControllerTest.java").start();
140             configureExporter("appfuse/web/spring/formcontroller-test.ftl", "src/test/java/{basepkg-name}/webapp/controller/{class-name}FormControllerTest.java").start();
141 
142             // controllers
143             configureExporter("appfuse/web/spring/controller.ftl", "src/main/java/{basepkg-name}/webapp/controller/{class-name}Controller.java").start();
144             configureExporter("appfuse/web/spring/formcontroller.ftl", "src/main/java/{basepkg-name}/webapp/controller/{class-name}FormController.java").start();
145 
146             // views
147             if (!webFramework.equalsIgnoreCase("spring-freemarker")) {
148                 configureExporter("appfuse/web/spring/list-view.ftl", "src/main/webapp/WEB-INF/pages/{class-name}s.jsp").start();
149                 configureExporter("appfuse/web/spring/form-view.ftl", "src/main/webapp/WEB-INF/pages/{class-name}form.jsp").start();
150             } else {
151                 configureExporter("appfuse/web/spring-freemarker/list-view.ftl", "src/main/webapp/{class-name}list.ftl").start();
152                 configureExporter("appfuse/web/spring-freemarker/form-view.ftl", "src/main/webapp/{class-name}form.ftl").start();
153             }
154 
155             // validation
156             configureExporter("appfuse/web/spring/form-validation.ftl", "src/main/webapp/WEB-INF/{class-name}-validation.xml").start();
157         } else if (webFramework.equalsIgnoreCase("struts")) {
158             // tests
159             configureExporter("appfuse/web/struts/action-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}ActionTest.java").start();
160 
161             // controllers
162             configureExporter("appfuse/web/struts/action.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}Action.java").start();
163 
164             // views
165             configureExporter("appfuse/web/struts/list-view.ftl", "src/main/webapp/WEB-INF/pages/{class-name}List.jsp").start();
166             configureExporter("appfuse/web/struts/form-view.ftl", "src/main/webapp/WEB-INF/pages/{class-name}Form.jsp").start();
167 
168             // configuration
169             // This template is not used anymore (APF-798), but retained in case we do want to create definitions by default in the future
170             configureExporter("appfuse/web/struts/action-beans.ftl", "src/main/webapp/WEB-INF/{class-name}-struts-bean.xml").start();
171 
172             GenericExporter exporter = configureExporter("appfuse/web/struts/struts.ftl", "src/main/resources/{class-name}-struts.xml");
173             exporter.getProperties().put("pagesPath", isAppFuse ? "/WEB-INF/pages" : "");
174             exporter.start();
175 
176             // validation
177             configureExporter("appfuse/web/struts/model-validation.ftl", "src/main/resources/{basepkg-name}/model/{class-name}-validation.xml").start();
178             configureExporter("appfuse/web/struts/action-validation.ftl", "src/main/resources/{basepkg-name}/webapp/action/{class-name}Action-validation.xml").start();
179         } else if (webFramework.equalsIgnoreCase("stripes")) {
180             // tests
181             configureExporter("appfuse/web/stripes/list-action-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}ListBeanTest.java").start();
182             configureExporter("appfuse/web/stripes/form-action-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}FormBeanTest.java").start();
183 
184             // actions
185             configureExporter("appfuse/web/stripes/list-action.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}ListBean.java").start();
186             configureExporter("appfuse/web/stripes/form-action.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}FormBean.java").start();
187 
188             // views
189             configureExporter("appfuse/web/stripes/list-view.ftl", "src/main/webapp/{class-name}List.jsp").start();
190             configureExporter("appfuse/web/stripes/form-view.ftl", "src/main/webapp/{class-name}Form.jsp").start();
191         } else if (webFramework.equalsIgnoreCase("tapestry")) {
192             // tests
193             configureExporter("appfuse/web/tapestry/list-test.ftl", "src/test/java/{basepkg-name}/webapp/pages/{class-name}ListTest.java").start();
194             configureExporter("appfuse/web/tapestry/form-test.ftl", "src/test/java/{basepkg-name}/webapp/pages/{class-name}FormTest.java").start();
195 
196             // pages
197             configureExporter("appfuse/web/tapestry/list.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}List.java").start();
198             configureExporter("appfuse/web/tapestry/form.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}Form.java").start();
199 
200             // views
201             configureExporter("appfuse/web/tapestry/list-view.ftl", "src/main/webapp/{class-name}List.tml").start();
202             configureExporter("appfuse/web/tapestry/form-view.ftl", "src/main/webapp/{class-name}Form.tml").start();
203         } else if (webFramework.equalsIgnoreCase("wicket")) {
204             // tests
205             configureExporter("appfuse/web/wicket/list-test.ftl", "src/test/java/{basepkg-name}/webapp/pages/{class-name}ListTest.java").start();
206             configureExporter("appfuse/web/wicket/form-test.ftl", "src/test/java/{basepkg-name}/webapp/pages/{class-name}FormTest.java").start();
207 
208             // pages
209             configureExporter("appfuse/web/wicket/model.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}Model.java").start();
210             configureExporter("appfuse/web/wicket/data-provider.ftl", "src/main/java/{basepkg-name}/webapp/pages/Sortable{class-name}DataProvider.java").start();
211             configureExporter("appfuse/web/wicket/list.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}List.java").start();
212             configureExporter("appfuse/web/wicket/form.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}Form.java").start();
213 
214             // views
215             configureExporter("appfuse/web/wicket/list-view.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}List.html").start();
216             configureExporter("appfuse/web/wicket/form-view.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}Form.html").start();
217         } else {
218             log.warn("Your project's web framework '" + webFramework + "' is not supported by AMP at this time.");
219             log.warn("See enter an issue in JIRA if you'd like to see support. http://issues.appfuse.org/browse/APF");
220         }
221 
222         // menu
223         if (!webFramework.equalsIgnoreCase("tapestry")) {
224             configureExporter("appfuse/web/menu.ftl", "src/main/webapp/common/{class-name}-menu.jsp").start();
225             if (webFramework.equalsIgnoreCase("spring-freemarker")) {
226                 configureExporter("appfuse/web/menu-light-freemarker.ftl", "src/main/webapp/common/{class-name}-menu-light.ftl").start();
227             } else {
228                 configureExporter("appfuse/web/menu-light.ftl", "src/main/webapp/common/{class-name}-menu-light.jsp").start();
229             }
230             configureExporter("appfuse/web/menu-config.ftl", "src/main/webapp/WEB-INF/{class-name}-menu-config.xml").start();
231         } else {
232             configureExporter("appfuse/web/tapestry/menu.ftl", "src/main/webapp/{class-name}-menu.tml").start();
233         }
234 
235         // i18n
236         configureExporter("appfuse/web/ApplicationResources.ftl", "src/main/resources/{class-name}-ApplicationResources.properties").start();
237 
238         // canoo tests
239         if (!webFramework.equals("spring-security") && !webFramework.equalsIgnoreCase("spring-freemarker") && !webFramework.equalsIgnoreCase("stripes")) {
240             configureExporter("appfuse/web/" + webFramework + "/web-tests.ftl", "src/test/resources/{class-name}-web-tests.xml").start();
241         }
242         // jwebunit tests
243         if (webFramework.equalsIgnoreCase("spring-freemarker")) {
244             webFramework = "spring";
245             jwebUnitTemplate = new ClassPathResource("appfuse/web/" + webFramework + "/jwebunit-tests.ftl");
246         }
247         if (jwebUnitTemplate.exists()) {
248             configureExporter("appfuse/web/" + webFramework + "/jwebunit-tests.ftl", "src/test/java/{basepkg-name}/webapp/{class-name}WebTest.java").start();
249         }
250     }
251 
252     private String getDaoFilename(String daoFramework) {
253         if (daoFramework.equalsIgnoreCase("ibatis")) {
254             return "iBatis";
255         } else if (daoFramework.equalsIgnoreCase("jpa")) {
256             return "Jpa";
257         } else {
258             return Character.toUpperCase(daoFramework.charAt(0)) + daoFramework.substring(1);
259         }
260     }
261 
262     private GenericExporter configureExporter(String template, String pattern) {
263 
264         // Add custom template path if specified
265         String[] templatePaths;
266         if (getProperties().getProperty("templatedirectory") != null) {
267             templatePaths = new String[getTemplatePaths().length + 1];
268             templatePaths[0] = getProperties().getProperty("templatedirectory");
269             if (getTemplatePaths().length > 1) {
270                 for (int i = 1; i < getTemplatePaths().length; i++) {
271                     templatePaths[i] = getTemplatePaths()[i-1];
272                 }
273             }
274         } else {
275             templatePaths = getTemplatePaths();
276         }
277 
278         GenericExporter exporter = new GenericExporter(getConfiguration(), getOutputDirectory()) {
279             @Override
280             protected void exportPOJO(Map map, POJOClass element) {
281                 if (element.getShortName().equals(System.getProperty("appfuse.entity"))) {
282                     super.exportPOJO(map, element);
283                 }
284             }
285 
286             @Override
287             protected String resolveFilename(POJOClass element) {
288                 String filename = super.resolveFilename(element);
289                 String packageLocation = getPackageNameForFile(element).replace(".", "/");
290 
291                 String pojoName = System.getProperty("entity");
292                 if (pojoName == null) {
293                     pojoName = System.getProperty("appfuse.entity");
294                 }
295 
296                 // A dot in the entity name means the person is specifying the package.
297                 if (pojoName.contains(".")) {
298                     packageLocation = pojoName.substring(0, pojoName.indexOf(".model"));
299                     packageLocation = packageLocation.replace(".", "/");
300                 }
301 
302                 if (packageLocation.endsWith("model") && packageLocation.indexOf('/') > -1) {
303                     packageLocation = packageLocation.substring(0, packageLocation.lastIndexOf('/'));
304                 }
305                 filename = filename.replace("{basepkg-name}", packageLocation);
306                 return filename;
307             }
308         };
309         exporter.setProperties((Properties) getProperties().clone());
310         exporter.setTemplatePath(templatePaths);
311         exporter.setTemplateName(template);
312         exporter.setFilePattern(pattern);
313         exporter.setArtifactCollector(getArtifactCollector());
314         exporter.getProperties().put("data", new DataHelper());
315         exporter.getProperties().put("util", new StringUtils());
316 
317         return exporter;
318     }
319 }