View Javadoc

1   /**
2    *
3    */
4   package pt.digitalis.maven.ormgenerator;
5   
6   import java.io.IOException;
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import org.apache.maven.plugin.logging.Log;
11  
12  import pt.digitalis.maven.ormgenerator.utils.FreemarkerExecutor;
13  import freemarker.template.TemplateException;
14  
15  /**
16   * Creates the factory class
17   *
18   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
19   * @created Jun 20, 2008
20   */
21  public class FactoryExecutor extends BaseExecutor {
22  
23      /** The name of the template for the factory */
24      public static final String FACTORY_TEMPLATE = "factory.ftl";
25  
26      /** The logger */
27      private Log logger = null;
28  
29      /** The data source name */
30      private String dataSourceName;
31  
32      /**
33       * Class constructor instantiates the <code>ServiceExecutor</code> with the required initializations
34       *
35       * @param packageName
36       *            The base package for the generated files
37       * @param dataSourceName
38       *            The dataSource name
39       * @param templateDir
40       *            Path for the template directory, could be relative or absolute.
41       * @param destinationDir
42       *            directory where the generated files should be inserted.
43       * @param baseCodeDIR
44       *            the project base path
45       * @param serviceConfigurationFileName
46       *            Service configuration file name, this file should read from the resources
47       * @param logger
48       *            the Maven Plugin logger
49       */
50      public FactoryExecutor(String packageName, String dataSourceName, String templateDir, String destinationDir,
51              String baseCodeDIR, String serviceConfigurationFileName, Log logger) {
52          super(packageName, templateDir, destinationDir);
53          this.setLogger(logger);
54          this.dataSourceName = dataSourceName;
55      }
56  
57      /**
58       * This method is responsible to generate all the files.
59       *
60       * @return Returns <code>true</code> if the generation process went well and <code>false</code> otherwise
61       */
62      public boolean execute() {
63          boolean result = true;
64  
65          // Configures the freemarker engine
66          this.prepareExecutor(this.getDestinationDir());
67          if (this.getFreemarkerConfig() == null) {
68              this
69                      .getLogger()
70                      .error(
71                              "The service executor process was unable to build the freemarker configuration. Please check if the template directory is in classpath  under the directory "
72                                      + this.getTemplateDir());
73              result = false;
74          } else {
75              // generates the files
76              result = this.generateFactoryFiles(this.getDestinationDir());
77          }
78  
79          return result;
80      }
81  
82      /**
83       * Generates the files from the templates
84       *
85       * @param generatedDest
86       *            generated file destination dir
87       * @return The result is <code>true</code> if the files were correctly generated well and false otherwise
88       */
89      public boolean generateFactoryFiles(String generatedDest) {
90          // Get or create a template
91          boolean result = false;
92  
93          try {
94  
95              String path = obtainPath(generatedDest, this.getPackageName()) + "/" + dataSourceName + "Factory.java";
96  
97              // Builds the data to be merged with the template
98              Map<String, Object> map = new HashMap<String, Object>();
99              map.put("packageName", this.getPackageName());
100             map.put("dataSourceName", dataSourceName);
101 
102             // Generates the factory class
103             FreemarkerExecutor.processFreemarker(this.getFreemarkerConfig(), map, FACTORY_TEMPLATE, path);
104 
105             result = true;
106         } catch (IOException e) {
107             this.getLogger().error(e.getMessage(), e);
108         } catch (TemplateException e) {
109             this.getLogger().error(e.getMessage(), e);
110         }
111         return result;
112     }
113 
114     /**
115      * @return the logger
116      */
117     private Log getLogger() {
118         return logger;
119     }
120 
121     /**
122      * @param logger
123      *            the logger to set
124      */
125     private void setLogger(Log logger) {
126         this.logger = logger;
127     }
128 }