1 package org.melati.poem.prepro;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import org.apache.maven.plugin.AbstractMojo;
48 import org.apache.maven.plugin.MojoExecutionException;
49
50 import java.io.File;
51
52
53
54
55
56
57
58
59 public class MelatiDsdProcessorMojo extends AbstractMojo {
60
61
62
63
64
65 private String dsdPackage;
66
67
68
69
70
71
72 private String dsdFile;
73
74
75
76
77
78
79
80 private File sourceDirectory;
81
82
83
84
85
86
87
88 private File testSourceDirectory;
89
90
91
92
93
94 private String groupId;
95
96
97
98
99
100 private String artifactId;
101
102
103
104
105 private boolean checkUptodate;
106
107
108
109
110 private boolean isMain = true;
111
112
113 private String searchedLocations = "";
114
115 public void execute()
116 throws MojoExecutionException {
117
118
119 ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
120
121 File f = null;
122 if(isMain)
123 f = sourceDirectory;
124 else
125 f = testSourceDirectory;
126 if (f == null || !f.exists()) {
127 throw new MojoExecutionException("Source directory (" + f + ")could not be found");
128 }
129
130 String dsdPath = null;
131 if (dsdPackage != null) {
132 String lookupDir = f.getPath() + "/"
133 + dsdPackage.replace('.', '/') + "/";
134 dsdPath = dsdFileName(lookupDir, dsdFile);
135 if (dsdPath == null) {
136 if (dsdFile != null) {
137 throw new MojoExecutionException("Configured DSD file " + lookupDir
138 + dsdFile + " could not be found.");
139 } else {
140 throw new MojoExecutionException(
141 "DSD file could not be found on configured path "
142 + dsdPackage
143 + " in any of: \n"
144 + searchedLocations
145 + "Add an explicit dsdPackage and/or dsdFile parameter to your configuration.");
146
147 }
148 }
149 } else {
150 String groupDir = groupId.replace('.', '/');
151 String sourceDir = f.getPath() + "/" + groupDir + "/";
152 String foundDsdName = existingDsdFileName(sourceDir, dsdFile);
153 if (foundDsdName == null)
154 foundDsdName = existingDsdFileName(f + artifactId + "/",
155 dsdFile);
156 if (foundDsdName == null)
157 throw new MojoExecutionException(
158 "DSD file could not be found in any of: \n"
159 + searchedLocations
160 + "Add an explicit dsdPackage and/or dsdFile parameter to your configuration.");
161 getLog().info("Found DSD at " + foundDsdName + ":");
162 dsdPath = foundDsdName;
163 }
164 String modelDir = dsdPath.substring(0, dsdPath.lastIndexOf('/') + 1);
165 String modelName = capitalised(dsdPath.substring(dsdPath.lastIndexOf('/') + 1, dsdPath
166 .lastIndexOf('.')));
167 String databaseTablesFileName = modelDir + "generated/" + modelName
168 + "DatabaseBase.java";
169
170 File databaseTablesFile = new File(databaseTablesFileName);
171 long dsdTimestamp = new File(dsdPath).lastModified();
172 long databaseTablesTimestamp = 1;
173 if (databaseTablesFile.exists()) {
174 databaseTablesTimestamp = databaseTablesFile.lastModified();
175 }
176 boolean doIt = true;
177 if (checkUptodate) {
178 getLog().info(" Checking " + databaseTablesFileName);
179 if (dsdTimestamp < databaseTablesTimestamp) {
180 getLog().info("Generated files are uptodate. No action required.");
181 doIt = false;
182 }
183 } else
184 getLog().info(" Not checking - doing regardless");
185 if (doIt) {
186 DSD dsd;
187 try {
188 dsd = new DSD(dsdPath);
189 dsd.generateJava();
190 } catch (Exception e) {
191 throw new MojoExecutionException("Error processing DSD", e);
192 }
193 }
194 }
195
196 private String existingDsdFileName(String dir, String dsdFileName) {
197 String modelDirName = dir + "poem/";
198 File modelDir = new File(modelDirName);
199 if (!modelDir.exists()) {
200 searchedLocations += " " + modelDirName + "\n";
201 modelDirName = dir + "model/";
202 modelDir = new File(modelDirName);
203 }
204 if (!modelDir.exists()) {
205 searchedLocations += " " + modelDirName + "\n";
206 modelDirName = dir;
207 modelDir = new File(modelDirName);
208 }
209 if (!modelDir.exists()) {
210 searchedLocations += " " + modelDirName + "\n";
211 return null;
212 }
213 return dsdFileName(modelDirName, dsdFileName);
214 }
215
216 private String dsdFileName(String dir, String dsdFileName) {
217 if (dsdFileName != null) {
218 dsdFileName = dir + dsdFileName;
219 File foundDsdFile = new File(dsdFileName);
220 if ( !foundDsdFile.exists()) {
221 searchedLocations += " " + dsdFileName + "\n";
222 return null;
223 }
224 } else {
225 dsdFileName = dir + artifactId + ".dsd";
226 File foundDsdFile = new File(dsdFileName);
227 if (!foundDsdFile.exists()) {
228 searchedLocations += " " + dsdFileName + "\n";
229 dsdFileName = dir + capitalised(artifactId) + ".dsd";
230 foundDsdFile = new File(dsdFileName);
231 }
232 if (!foundDsdFile.exists()) {
233 searchedLocations += " " + dsdFileName + "\n";
234 return null;
235 }
236 }
237 return dsdFileName;
238 }
239
240
241
242
243
244
245
246 public static String capitalised(String name) {
247 char suffix[] = name.toCharArray();
248 suffix[0] = Character.toUpperCase(suffix[0]);
249 return new String(suffix);
250 }
251 }