View Javadoc

1   package org.appfuse.tool;
2   
3   import org.apache.maven.plugin.MojoExecutionException;
4   import org.hibernate.cfg.Configuration;
5   import org.hibernate.tool.hbm2x.GenericExporter;
6   import org.hibernate.tool.hbm2x.pojo.POJOClass;
7   import org.hibernate.util.StringHelper;
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 webProject = packaging != null && packaging.equalsIgnoreCase("war");
116 
117         if (!webProject) return;
118 
119         String webFramework = getProperties().getProperty("webframework");
120         if (webFramework.equalsIgnoreCase("jsf")) {
121             // tests
122             configureExporter("appfuse/web/jsf/list-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}ListTest.java").start();
123             configureExporter("appfuse/web/jsf/form-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}FormTest.java").start();
124 
125             // managed beans
126             configureExporter("appfuse/web/jsf/list.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}List.java").start();
127             configureExporter("appfuse/web/jsf/form.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}Form.java").start();
128 
129             // views
130             configureExporter("appfuse/web/jsf/list-view.ftl", "src/main/webapp/{class-name}s.xhtml").start();
131             configureExporter("appfuse/web/jsf/form-view.ftl", "src/main/webapp/{class-name}Form.xhtml").start();
132 
133             // configuration
134             configureExporter("appfuse/web/jsf/navigation.ftl", "src/main/webapp/WEB-INF/{class-name}-navigation.xml").start();
135 
136             // JSF managed beans configured by Spring annotations in 2.1+
137             //configureExporter("appfuse/web/jsf/managed-beans.ftl", "src/main/webapp/WEB-INF/{class-name}-managed-beans.xml").start();
138         } else if (webFramework.equalsIgnoreCase("spring")) {
139             // tests
140             configureExporter("appfuse/web/spring/controller-test.ftl", "src/test/java/{basepkg-name}/webapp/controller/{class-name}ControllerTest.java").start();
141             configureExporter("appfuse/web/spring/formcontroller-test.ftl", "src/test/java/{basepkg-name}/webapp/controller/{class-name}FormControllerTest.java").start();
142 
143             // controllers
144             configureExporter("appfuse/web/spring/controller.ftl", "src/main/java/{basepkg-name}/webapp/controller/{class-name}Controller.java").start();
145             configureExporter("appfuse/web/spring/formcontroller.ftl", "src/main/java/{basepkg-name}/webapp/controller/{class-name}FormController.java").start();
146 
147             // views
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 
151             // Controllers configured by Spring annotations in 2.1+
152             //configureExporter("appfuse/web/spring/controller-beans.ftl", "src/main/webapp/WEB-INF/{class-name}-beans.xml").start();
153 
154             // validation
155             configureExporter("appfuse/web/spring/form-validation.ftl", "src/main/webapp/WEB-INF/{class-name}-validation.xml").start();
156         } else if (webFramework.equalsIgnoreCase("struts")) {
157             // tests
158             configureExporter("appfuse/web/struts/action-test.ftl", "src/test/java/{basepkg-name}/webapp/action/{class-name}ActionTest.java").start();
159 
160             // controllers
161             configureExporter("appfuse/web/struts/action.ftl", "src/main/java/{basepkg-name}/webapp/action/{class-name}Action.java").start();
162 
163             // views
164             configureExporter("appfuse/web/struts/list-view.ftl", "src/main/webapp/WEB-INF/pages/{class-name}List.jsp").start();
165             configureExporter("appfuse/web/struts/form-view.ftl", "src/main/webapp/WEB-INF/pages/{class-name}Form.jsp").start();
166 
167             // configuration
168             // This template is not used anymore (APF-798), but retained in case we do want to create definitions by default in the future
169             configureExporter("appfuse/web/struts/action-beans.ftl", "src/main/webapp/WEB-INF/{class-name}-struts-bean.xml").start();
170 
171             configureExporter("appfuse/web/struts/struts.ftl", "src/main/resources/{class-name}-struts.xml").start();
172 
173             // validation
174             configureExporter("appfuse/web/struts/model-validation.ftl", "src/main/resources/{basepkg-name}/model/{class-name}-validation.xml").start();
175             configureExporter("appfuse/web/struts/action-validation.ftl", "src/main/resources/{basepkg-name}/webapp/action/{class-name}Action-validation.xml").start();
176         } else if (webFramework.equalsIgnoreCase("tapestry")) {
177             // tests
178             configureExporter("appfuse/web/tapestry/list-test.ftl", "src/test/java/{basepkg-name}/webapp/pages/{class-name}ListTest.java").start();
179             configureExporter("appfuse/web/tapestry/form-test.ftl", "src/test/java/{basepkg-name}/webapp/pages/{class-name}FormTest.java").start();
180 
181             // managed beans
182             configureExporter("appfuse/web/tapestry/list.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}List.java").start();
183             configureExporter("appfuse/web/tapestry/form.ftl", "src/main/java/{basepkg-name}/webapp/pages/{class-name}Form.java").start();
184 
185             // views
186             configureExporter("appfuse/web/tapestry/list-view.ftl", "src/main/webapp/{class-name}List.tml").start();
187             configureExporter("appfuse/web/tapestry/form-view.ftl", "src/main/webapp/{class-name}Form.tml").start();
188         } else {
189             log.warn("Your project's web framework '" + webFramework + "' is not supported by AMP at this time.");
190             log.warn("See http://issues.appfuse.org/browse/EQX-211 for more information.");
191 
192         }
193 
194         // menu
195         configureExporter("appfuse/web/menu.ftl", "src/main/webapp/common/{class-name}-menu.jsp").start();
196         configureExporter("appfuse/web/menu-light.ftl", "src/main/webapp/common/{class-name}-menu-light.jsp").start();
197         configureExporter("appfuse/web/menu-config.ftl", "src/main/webapp/WEB-INF/{class-name}-menu-config.xml").start();
198 
199         // i18n
200         configureExporter("appfuse/web/ApplicationResources.ftl", "src/main/resources/{class-name}-ApplicationResources.properties").start();
201 
202         // ui tests
203         if (!webFramework.equals("wicket") && !webFramework.equals("spring-security") &&
204                 !webFramework.equals("spring-freemarker") && !webFramework.equals("stripes")) {
205             configureExporter("appfuse/web/" + webFramework + "/web-tests.ftl", "src/test/resources/{class-name}-web-tests.xml").start();
206         }
207     }
208 
209     private String getDaoFilename(String daoFramework) {
210         if (daoFramework.equalsIgnoreCase("ibatis")) {
211             return "iBatis";
212         } else if (daoFramework.equalsIgnoreCase("jpa")) {
213             return "Jpa";
214         } else {
215             return Character.toUpperCase(daoFramework.charAt(0)) + daoFramework.substring(1);
216         }
217     }
218 
219     private GenericExporter configureExporter(String template, String pattern) {
220 
221         // Add custom template path if specified
222         String[] templatePaths;
223         if (getProperties().getProperty("templatedirectory") != null) {
224             templatePaths = new String[getTemplatePaths().length + 1];
225             templatePaths[0] = getProperties().getProperty("templatedirectory");
226             if (getTemplatePaths().length > 1) {
227                 for (int i = 1; i < getTemplatePaths().length; i++) {
228                     templatePaths[i] = getTemplatePaths()[i-1];
229                 }
230             }
231         } else {
232             templatePaths = getTemplatePaths();   
233         }
234 
235         GenericExporter exporter = new GenericExporter(getConfiguration(), getOutputDirectory()) {
236             @Override
237             protected void exportPOJO(Map map, POJOClass element) {
238                 if (element.getShortName().contains(System.getProperty("appfuse.entity"))) {
239                     super.exportPOJO(map, element);
240                 }
241             }
242 
243             @Override
244             protected String resolveFilename(POJOClass element) {
245                 String filename = super.resolveFilename(element);
246                 String packageLocation = StringHelper.replace(getPackageNameForFile(element), ".", "/");
247 
248                 String pojoName = System.getProperty("entity");
249 
250                 // A dot in the entity name means the person is specifying the package.
251                 if (System.getProperty("entity").contains(".")) {
252                     packageLocation = pojoName.substring(0, pojoName.indexOf(".model"));
253                     packageLocation = StringHelper.replace(packageLocation, ".", "/");
254                 }
255 
256                 if (packageLocation.endsWith("model") && packageLocation.indexOf('/') > -1) {
257                     packageLocation = packageLocation.substring(0, packageLocation.lastIndexOf('/'));
258                 }
259                 filename = StringHelper.replace(filename, "{basepkg-name}", packageLocation);
260                 return filename;
261             }
262         };
263         exporter.setProperties((Properties) getProperties().clone());
264         exporter.setTemplatePath(templatePaths);
265         exporter.setTemplateName(template);
266         exporter.setFilePattern(pattern);
267         exporter.setArtifactCollector(getArtifactCollector());
268         exporter.getProperties().put("data", new DataHelper());
269         exporter.getProperties().put("util", new StringUtils());
270 
271         return exporter;
272     }
273 }