1
2
3
4
5
6
7
8
9
10
11 package org.mod4j.common.generator.admin;
12
13 import java.io.File;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.mod4j.dslcommon.generator.helpers.ProjectProperties;
20
21 public class Mod4jTracker {
22
23 static private Mod4jTracker mod4jTracker = null;
24
25 private Map<String, ProjectTrack> projects = null;
26
27 private ProjectTrack currentProject = null;
28
29
30
31
32 public ProjectTrack getCurrentProject() {
33 return currentProject;
34 }
35
36
37
38
39 public Collection<ProjectTrack> getProjects() {
40 return Collections.unmodifiableCollection(projects.values());
41 }
42
43 public void clear() {
44 projects = new HashMap<String, ProjectTrack>();
45 currentProject = null;
46 }
47
48
49
50
51 public FileTrack getCurrentTrack() {
52 return currentProject.getCurrentFileTrack();
53 }
54
55
56
57
58 static public Mod4jTracker getFileTracker() {
59 if (mod4jTracker == null) {
60 mod4jTracker = new Mod4jTracker();
61 }
62 return mod4jTracker;
63 }
64
65 private Mod4jTracker() {
66 projects = new HashMap<String, ProjectTrack>();
67 }
68
69
70
71
72
73
74 public void initResource(String resource) {
75 currentProject = findOrCreateProject(resource);
76 currentProject.setApplicationPath(resource);
77 currentProject.setCurrentFileTrack(currentProject.getTrack(resource));
78 }
79
80
81
82
83
84
85
86 public void initResource(String resource, String applicationPath, String projectPath) {
87 System.err.println("Mod4jTracker initResource [" + resource + "] [" + applicationPath + "] [" + projectPath
88 + "]");
89 currentProject = findOrCreateProject(projectPath);
90 currentProject.setApplicationPath(applicationPath);
91 currentProject.setCurrentFileTrack(currentProject.getTrack(resource));
92 }
93
94
95
96
97
98
99 public void finishResource(String resource, String applicationPath, String projectPath) {
100
101 for (GeneratedFile gen : getCurrentTrack().getExtensionFiles()) {
102 if (gen.isChanged()) {
103
104 String name = gen.getSourcePath();
105 name = gen.getOwner().getProject().getApplicationPath() + "/" + name;
106 File file = new File(name);
107
108
109
110 gen.setModifiedDate(file.lastModified());
111 gen.setChanged(false);
112 } else if (gen.isRetained()) {
113
114 gen.setRetained(false);
115 } else {
116
117 }
118 }
119 for (GeneratedFile gen : getCurrentTrack().getGeneratedFiles()) {
120 if (gen.isChanged()) {
121
122 String name = gen.getSourcePath();
123 name = gen.getOwner().getProject().getApplicationPath() + "/" + name;
124 File file = new File(name);
125
126
127
128 gen.setModifiedDate(file.lastModified());
129 gen.setChanged(false);
130 } else if (gen.isRetained()) {
131
132 gen.setRetained(false);
133 } else {
134
135 }
136 }
137 }
138
139
140
141
142
143
144
145
146 public void addProjectTrack(ProjectTrack projectTrack) {
147 projects.put(projectTrack.getProjectPath(), projectTrack);
148 }
149
150
151
152
153
154
155
156 private ProjectTrack findOrCreateProject(String projectPath) {
157 ProjectTrack result = projects.get(projectPath);
158 if (result == null) {
159 result = new ProjectTrack(projectPath);
160 projects.put(projectPath, result);
161 }
162 return result;
163 }
164
165
166
167
168
169
170
171
172
173 static public String generate(String moduleName, String filename) {
174
175 String result = null;
176 if (filename.endsWith(".java")) {
177 result = moduleName + "/" + ProjectProperties.getSrcGenPath() + "/" + filename;
178 } else if (filename.equals("pom.xml")) {
179 result = moduleName + "/" + filename;
180 } else {
181 result = moduleName + "/" + ProjectProperties.getResourceGenPath() + "/" + filename;
182 }
183 GeneratedFile file = getFileTracker().getCurrentTrack().generatedFile(result);
184 file.setChanged(true);
185 file.setRetained(false);
186 return result;
187 }
188
189
190
191
192
193
194
195
196 static public String extend(String moduleName, String filename) {
197
198 String result = getDefaultModuleManFilePath(moduleName, filename);
199 GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
200 file.setChanged(true);
201 file.setRetained(false);
202 return result;
203 }
204
205
206
207
208
209
210
211
212
213 static public String extend(final String moduleName, final String subFolderPath, final String fileName) {
214
215 String result = getFilePath(moduleName, subFolderPath, fileName);
216 GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
217 file.setChanged(true);
218 file.setRetained(false);
219 return result;
220 }
221
222
223
224
225
226
227
228
229 static public String retain(String moduleName, String filename) {
230
231 String result = getDefaultModuleManFilePath(moduleName, filename);
232 GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
233 file.setChanged(false);
234 file.setRetained(true);
235 return result;
236 }
237
238
239
240
241
242
243
244
245
246 static public String retain(final String moduleName, final String subFolderPath, final String filename) {
247
248 String result = getFilePath(moduleName, subFolderPath, filename);
249 GeneratedFile file = getFileTracker().getCurrentTrack().extensionFile(result);
250 file.setChanged(false);
251 file.setRetained(true);
252 return result;
253 }
254
255
256
257
258
259
260
261
262 static public String fullExtendPath(String moduleName, String filename) {
263 String result = getDefaultModuleManFilePath(moduleName, filename);
264 return ProjectProperties.getApplicationPath() + "/" + result;
265 }
266
267
268
269
270
271
272
273
274 static public String fullExtendPath(String moduleName, String subFolderPath, String filename) {
275 String result = getFilePath(moduleName, subFolderPath, filename);
276 return ProjectProperties.getApplicationPath() + "/" + result;
277 }
278
279
280
281
282
283
284
285
286 static public String fullGeneratePath(String moduleName, String filename) {
287 String result = moduleName + "/" + ProjectProperties.getSrcGenPath() + "/" + filename;
288 return ProjectProperties.getApplicationPath() + "/" + result;
289 }
290
291
292
293
294
295
296
297
298 static public String fullGenerateResourcePath(String moduleName, String filename) {
299 String result = moduleName + "/" + ProjectProperties.getResourceGenPath() + "/" + filename;
300 return ProjectProperties.getApplicationPath() + "/" + result;
301 }
302
303
304
305
306
307
308
309
310
311
312
313 static public String getDefaultModuleManFilePath(String moduleName, String fileName) {
314 String result = null;
315 String prefix;
316 if ((moduleName == null) || (moduleName.length() == 0)) {
317 prefix = "";
318 } else {
319 prefix = moduleName + "/";
320 }
321 if (fileName.endsWith(".java")) {
322 result = prefix + ProjectProperties.getSrcManPath() + "/" + fileName;
323 } else if (fileName.equals("pom.xml")) {
324 result = prefix + fileName;
325 } else {
326 result = prefix + ProjectProperties.getResourceManPath() + "/" + fileName;
327 }
328 return result;
329 }
330
331
332
333
334
335
336
337
338
339
340
341 static public String getFilePath(String moduleName, String subFolderPath, String fileName) {
342 String result = "";
343
344 if ((moduleName != null) && (moduleName.length() > 0)) {
345 result = result + moduleName;
346 }
347 if ((subFolderPath != null) && (subFolderPath.length() > 0)) {
348 result = result + "/" + subFolderPath;
349 }
350 if (fileName != null && fileName.length() > 0) {
351 result = result + "/" + fileName;
352 }
353 return result;
354 }
355 }