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
17
18
19
20
21 public class FactoryExecutor extends BaseExecutor {
22
23
24 public static final String FACTORY_TEMPLATE = "factory.ftl";
25
26
27 private Log logger = null;
28
29
30 private String dataSourceName;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
59
60
61
62 public boolean execute() {
63 boolean result = true;
64
65
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
76 result = this.generateFactoryFiles(this.getDestinationDir());
77 }
78
79 return result;
80 }
81
82
83
84
85
86
87
88
89 public boolean generateFactoryFiles(String generatedDest) {
90
91 boolean result = false;
92
93 try {
94
95 String path = obtainPath(generatedDest, this.getPackageName()) + "/" + dataSourceName + "Factory.java";
96
97
98 Map<String, Object> map = new HashMap<String, Object>();
99 map.put("packageName", this.getPackageName());
100 map.put("dataSourceName", dataSourceName);
101
102
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
116
117 private Log getLogger() {
118 return logger;
119 }
120
121
122
123
124
125 private void setLogger(Log logger) {
126 this.logger = logger;
127 }
128 }