1
2
3
4
5
6 package pt.digitalis.dif.codegen.templates;
7
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import pt.digitalis.dif.codegen.CGAncillaries;
14 import pt.digitalis.dif.dem.CallbackType;
15 import pt.digitalis.dif.dem.interfaces.IService;
16 import pt.digitalis.dif.dem.interfaces.IStage;
17 import pt.digitalis.dif.dem.interfaces.IStageInstance;
18 import pt.digitalis.dif.dem.objects.EventType;
19 import pt.digitalis.dif.dem.objects.LicenseEditionType;
20 import pt.digitalis.dif.dem.objects.ViewObject;
21 import pt.digitalis.dif.dem.objects.ViewType;
22 import pt.digitalis.dif.dem.objects.parameters.IParameters;
23 import pt.digitalis.dif.exception.manager.RegistrationManagerException;
24 import pt.digitalis.dif.utils.logging.DIFLogger;
25 import pt.digitalis.utils.inspection.ReflectionUtils;
26 import pt.digitalis.utils.inspection.exception.AuxiliaryOperationException;
27
28
29
30
31
32
33
34
35
36
37 public class StageCGTemplate implements IStage {
38
39
40
41
42
43 static private Map<String, String> errorStages;
44
45
46
47
48
49 static public Map<String, ViewObject> errorViews;
50
51
52 static private Map<EventType, List<String>> eventHandlers;
53
54
55 static private List<String> injectedStages;
56
57
58 static private List<ViewObject> injectedViews;
59
60
61 static protected boolean isInitialized = false;
62
63
64 private IParameters parameters;
65
66
67
68
69
70
71
72
73
74 protected void addEvent(EventType type, String id)
75 {
76 eventHandlers.get(type).add(id);
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 protected ViewObject createView(String engine, String type, String target, boolean isDefault)
93 {
94 return new ViewObject(engine, ViewType.valueOf(type), target, isDefault);
95 }
96
97
98 private void eventHandlersBuilder()
99 {
100
101 }
102
103
104
105
106 public CallbackType getCallbackType()
107 {
108 return CallbackType.OFF;
109 }
110
111
112
113
114 public IStage getDefaultErrorStage()
115 {
116 return CGAncillaries.CG_TO_BE_IMPLEMENTED_STAGE;
117 }
118
119
120
121
122 public ViewObject getDefaultErrorView()
123 {
124 return CGAncillaries.CG_TO_BE_IMPLEMENTED_VIEW;
125 }
126
127
128
129
130 public ViewObject getDefaultView()
131 {
132 return CGAncillaries.CG_TO_BE_IMPLEMENTED_VIEW;
133 }
134
135
136
137
138 synchronized public Map<EventType, List<String>> getEventHandlers()
139 {
140 if (eventHandlers == null)
141 {
142
143 eventHandlers = new HashMap<EventType, List<String>>();
144
145 eventHandlers.put(EventType.FORM_SUBMIT, new ArrayList<String>());
146 eventHandlers.put(EventType.FORM_SUBMIT_AJAX_REQUEST, new ArrayList<String>());
147 eventHandlers.put(EventType.FORM_SUBMIT_SAVE_ACTION, new ArrayList<String>());
148 eventHandlers.put(EventType.FORM_VALIDATION, new ArrayList<String>());
149 eventHandlers.put(EventType.AJAX_REQUEST, new ArrayList<String>());
150 eventHandlers.put(EventType.DOCUMENT_TYPE, new ArrayList<String>());
151
152 eventHandlersBuilder();
153 }
154
155 return eventHandlers;
156 }
157
158
159
160
161 public String getID()
162 {
163 return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
164 }
165
166
167
168
169 synchronized public Map<String, String> getInjectedErrorStages()
170 {
171 if (errorStages == null)
172 {
173
174 errorStages = new HashMap<String, String>();
175 injectedErrorStagesBuilder();
176 }
177
178 return errorStages;
179 }
180
181
182
183
184 synchronized public Map<String, ViewObject> getInjectedErrorViews()
185 {
186 if (errorViews == null)
187 {
188 errorViews = new HashMap<String, ViewObject>();
189 injectedErrorViewsBuilder();
190 }
191
192 return errorViews;
193 }
194
195
196
197
198 synchronized public List<String> getInjectedStages()
199 {
200
201 if (injectedStages == null)
202 {
203
204 injectedStages = new ArrayList<String>();
205 injectedStagesBuilder();
206 }
207
208 return injectedStages;
209 }
210
211
212
213
214 synchronized public List<ViewObject> getInjectedViews()
215 {
216 if (injectedViews == null)
217 {
218 injectedViews = new ArrayList<ViewObject>();
219 injectedViewsBuilder();
220 }
221
222 return injectedViews;
223 }
224
225
226
227
228 synchronized public IStageInstance getInstance()
229 {
230 IStageInstance instance;
231
232 try
233 {
234 Class<?> clazz = Class.forName(getStageInstanceClassName());
235 instance = (IStageInstance) ReflectionUtils.instantiateObjectFromClass(clazz);
236 }
237 catch (AuxiliaryOperationException e)
238 {
239
240 return null;
241 }
242 catch (ClassNotFoundException e)
243 {
244
245 return null;
246 }
247
248 instance.setProxy(this);
249
250 if (!isInitialized)
251 lazyStageProxyInitialization(instance);
252
253 return instance;
254 }
255
256
257
258
259 public LicenseEditionType getLicenseEdition()
260 {
261 return TemplateResources.getRegistrationManager().getStageEdition(this.getID());
262 }
263
264
265
266
267 public String getMessageForLanguage(String language, String messageID)
268 {
269 return getMessagesForLanguage(language).get(messageID);
270 }
271
272
273
274
275
276 public Map<String, String> getMessagesForLanguage(String language)
277 {
278 return TemplateResources.getMessageManager().getMessages(this, language);
279 }
280
281
282
283
284 public String getName()
285 {
286 return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
287 }
288
289
290
291
292 public String getOriginalClassName()
293 {
294 return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
295 }
296
297
298
299
300 public String getOverridesStageID()
301 {
302 return null;
303 }
304
305
306
307
308 @SuppressWarnings("static-access")
309 synchronized public IParameters getParameters()
310 {
311 if (parameters == null)
312 {
313 parameters = getTemplateResources().getParametersInstance(getInstance());
314
315
316 }
317
318 return parameters;
319 }
320
321
322
323
324 public IService getService()
325 {
326 return TemplateResources.getDEMManager().getService(CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE);
327 }
328
329
330
331
332 public String getStageInstanceClassName()
333 {
334 return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
335 }
336
337
338
339
340
341
342 protected TemplateResources getTemplateResources()
343 {
344 return TemplateResources.getInstance();
345 }
346
347
348
349
350 public String getUID()
351 {
352 return "STAGE:" + this.getID();
353 }
354
355
356
357
358 public boolean hasAuthentication()
359 {
360 return true;
361 }
362
363
364
365
366 public boolean hasAuthenticationErrorInjection()
367 {
368 return false;
369 }
370
371
372
373
374 public boolean hasAuthorization()
375 {
376 return true;
377 }
378
379
380
381
382 public boolean hasCallbackEnabled()
383 {
384 return false;
385 }
386
387
388
389
390 public boolean hasInjectedContributions()
391 {
392 return false;
393 }
394
395
396
397
398 public boolean hasParameterErrorInjection()
399 {
400
401
402 return false;
403 }
404
405
406
407
408 public boolean hasValidationLogicForForm(String formName)
409 {
410 return this.getEventHandlers().get(EventType.FORM_VALIDATION).contains(formName);
411 }
412
413
414 private void injectedErrorStagesBuilder()
415 {}
416
417
418 private void injectedErrorViewsBuilder()
419 {}
420
421
422 private void injectedStagesBuilder()
423 {}
424
425
426 private void injectedViewsBuilder()
427 {}
428
429
430
431
432 public boolean isRegistered()
433 {
434 boolean result = this.getService().isRegistered();
435
436 if (this.isRegistrable())
437 result = TemplateResources.getRegistrationManager().isStageRegistered(getID());
438
439 return result;
440 }
441
442
443
444
445 public boolean isRegistrable()
446 {
447 return TemplateResources.getRegistrationManager().isStageRegistrable(getID());
448 }
449
450
451
452
453
454
455
456 protected void lazyStageProxyInitialization(IStageInstance stage)
457 {
458
459
460
461
462
463
464
465
466
467 isInitialized = true;
468 }
469
470
471
472
473 public boolean register(String name, String key)
474 {
475 try
476 {
477 return TemplateResources.getRegistrationManager().registerStage(getID(), name, key);
478 }
479 catch (RegistrationManagerException e)
480 {
481 DIFLogger.getLogger().debug(e);
482 return false;
483 }
484 }
485
486
487
488
489 public void unregister()
490 {
491 TemplateResources.getRegistrationManager().unregisterStage(getID());
492 }
493 }