View Javadoc

1   package pt.digitalis.maven.ormgenerator;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import pt.digitalis.maven.ormgenerator.utils.FileUtils;
7   import pt.digitalis.maven.ormgenerator.utils.FreemarkerExecutor;
8   import freemarker.template.Configuration;
9   
10  /**
11   * @author Ant�nio Silva <a href="mailto:asilva@digitalis.pt">asilva@digitalis.pt</a>
12   * @created 2007/11/21
13   */
14  public abstract class BaseExecutor {
15  
16  
17      /**
18       * The extension of the properties files
19       */
20      public static final String PROPERTIES_EXTENSION = ".properties";
21  
22      /**
23       * The auto package definition
24       */
25      public static final String AUTO_PACKAGE = ".auto";
26  
27      /**
28       * The dao package definition
29       */
30      public static final String DAO_PACKAGE = ".dao";
31  
32      /**
33       * The generated files prefix
34       */
35      public static final String AUTO_PREFFIX = "Auto";
36  
37      /**
38       * The java file extension
39       */
40      public static final String JAVA_EXTENSION = ".java";
41  
42      /**
43       * The default suffix of the DAO files
44       */
45      public static final String DAO_SUFFIX = "DAO";
46  
47  
48      /**
49       * The prefix given to the interfaces
50       */
51      public static final String INTERFACE_PREFFIX = "I";
52  
53      /**
54       * The suffix given to the generated files
55       */
56      public static final String IMPL_FILE_SUFFIX = "Impl";
57  
58      /**
59       * The suffix that will be added to the base package
60       */
61      public static final String IMPL_PACKAGE_SUFFIX = ".impl";
62  
63  
64      /**
65       * Freemarker configuration.
66       */
67      private Configuration freemarkerConfig = null;
68  
69      /**
70       * The destination directory of the generated files
71       */
72      private String destinationDir = null;
73  
74      /**
75       * The directory where the freemarker template files are located
76       */
77      private String templateDir = null;
78  
79      /**
80       * The destination package of the generated files
81       */
82      private String packageName = null;
83  
84      /**
85       * private method made private to prevent anyone instantiate this class without the initializations.
86       */
87      @SuppressWarnings("unused")
88      private BaseExecutor() {
89      }
90  
91      /**
92       * Class constructor instanciates the <code>ServiceExecutor</code> with the required initializations
93       *
94       * @param packageName
95       *            The base package for the generated files
96       * @param templateDir
97       *            Path for the template directory, could be relative or absolute.
98       * @param destinationDir
99       *            directory where the generated files should be inserted.
100      */
101     public BaseExecutor(String packageName, String templateDir, String destinationDir) {
102         this.setDestinationDir(destinationDir);
103         this.setPackageName(packageName);
104         this.setTemplateDir(templateDir);
105     }
106 
107     /**
108      * Prepares the executor to run
109      *
110      * @param templateDir
111      *            The directory where the templates are saved
112      */
113     public void prepareExecutor(String templateDir) {
114         try {
115             this.setFreemarkerConfig(FreemarkerExecutor.prepareFreemarker(this.getTemplateDir(), this.getClass()));
116         } catch (IOException e) {
117             this.setFreemarkerConfig(null);
118         }
119     }
120 
121     /**
122      * Generates the files from the templates
123      *
124      * @return The result is <code>true</code> if the files were correctly generated well and false otherwise
125      */
126     public abstract boolean execute();
127 
128     /**
129      * Obtains the path where the files will be saved
130      *
131      * @param baseDir
132      *            The base directory name.
133      * @param packageName
134      *            The package name to be added to the base dir
135      * @return A <code>String</code> with.
136      */
137     public static String obtainPath(String baseDir, String packageName) {
138         String pathName = baseDir + File.separator + FileUtils.dotNotation2PathNotation(packageName);
139         File f = new File(pathName);
140         if (!f.exists())
141             f.mkdirs();
142         return pathName;
143     }
144 
145     /**
146      * @return the destinationDir
147      */
148     protected String getDestinationDir() {
149         return destinationDir;
150     }
151 
152     /**
153      * @param destinationDir
154      *            the destinationDir to set
155      */
156     private void setDestinationDir(String destinationDir) {
157         this.destinationDir = destinationDir;
158     }
159 
160     /**
161      * @return the packageName
162      */
163     protected String getPackageName() {
164         return packageName;
165     }
166 
167     /**
168      * @param packageName
169      *            the packageName to set
170      */
171     private void setPackageName(String packageName) {
172         this.packageName = packageName;
173     }
174 
175     /**
176      * @return the freemarkerConfig
177      */
178     protected Configuration getFreemarkerConfig() {
179         return freemarkerConfig;
180     }
181 
182     /**
183      * @param freemarkerConfig
184      *            the freemarkerConfig to set
185      */
186     private void setFreemarkerConfig(Configuration freemarkerConfig) {
187         this.freemarkerConfig = freemarkerConfig;
188     }
189 
190     /**
191      * @return the templateDir
192      */
193     protected String getTemplateDir() {
194         return templateDir;
195     }
196 
197     /**
198      * @param templateDir
199      *            the templateDir to set
200      */
201     private void setTemplateDir(String templateDir) {
202         this.templateDir = templateDir;
203     }
204 
205 }