View Javadoc

1   /**
2    * - Digitalis Internal Framework v2.0 - (C) 2007, Digitalis Informatica. Distribuicao e Gestao de Informatica, Lda.
3    * Estrada de Paco de Arcos num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999
4    * http://www.digitalis.pt
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   * This class is a template for the IStage interface method implementations. The CodeGen will copy these methods to a
30   * new Proxy class for each stage. Some methods will be copied "as is", other will be tweaked. These Proxy classes are
31   * intended to be Singletons kept by the {@link pt.digitalis.dif.dem.interfaces.IDEMRegistry}
32   * 
33   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
34   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
35   * @created 2007/07/12
36   */
37  public class StageCGTemplate implements IStage {
38  
39      /**
40       * Relates the exception types with the stages for redirection purposes upon an exception raising. <K = Exception
41       * FQN, V = stage id>
42       */
43      static private Map<String, String> errorStages;
44  
45      /**
46       * Relates the exception types with the views for redirection purposes upon an exception raising. <K = Exception
47       * FQN, V = Error view objects>
48       */
49      static public Map<String, ViewObject> errorViews;
50  
51      /** The list of registered event handlers */
52      static private Map<EventType, List<String>> eventHandlers;
53  
54      /** The list of injected stages */
55      static private List<String> injectedStages;
56  
57      /** The list of injected views */
58      static private List<ViewObject> injectedViews;
59  
60      /** Controls the lazy initializations of this stage proxy... */
61      static protected boolean isInitialized = false;
62  
63      /** The parameters object */
64      private IParameters parameters;
65  
66      /**
67       * Adds a new event id to the known handlers
68       * 
69       * @param type
70       *            the event type to add
71       * @param id
72       *            the id of the event
73       */
74      protected void addEvent(EventType type, String id)
75      {
76          eventHandlers.get(type).add(id);
77      }
78  
79      /**
80       * Creates a new view object. For usage of the CG methods
81       * 
82       * @param engine
83       *            the engine of the view
84       * @param type
85       *            the type of the view
86       * @param target
87       *            the target resource/template of the view
88       * @param isDefault
89       *            T if the view is the default view
90       * @return the created view object
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      /** event handlers builder. Will be overridden in the CG if they exist */
98      private void eventHandlersBuilder()
99      {
100         // By default do nothing
101     }
102 
103     /**
104      * @see pt.digitalis.dif.dem.interfaces.ICallback#getCallbackType()
105      */
106     public CallbackType getCallbackType()
107     {
108         return CallbackType.OFF;
109     }
110 
111     /**
112      * @see pt.digitalis.dif.dem.interfaces.IStage#getDefaultErrorStage()
113      */
114     public IStage getDefaultErrorStage()
115     {
116         return CGAncillaries.CG_TO_BE_IMPLEMENTED_STAGE;
117     }
118 
119     /**
120      * @see pt.digitalis.dif.dem.interfaces.IStage#getDefaultErrorView()
121      */
122     public ViewObject getDefaultErrorView()
123     {
124         return CGAncillaries.CG_TO_BE_IMPLEMENTED_VIEW;
125     }
126 
127     /**
128      * @see pt.digitalis.dif.dem.interfaces.IStage#getDefaultView()
129      */
130     public ViewObject getDefaultView()
131     {
132         return CGAncillaries.CG_TO_BE_IMPLEMENTED_VIEW;
133     }
134 
135     /**
136      * @see pt.digitalis.dif.dem.interfaces.IStage#getEventHandlers()
137      */
138     synchronized public Map<EventType, List<String>> getEventHandlers()
139     {
140         if (eventHandlers == null)
141         {
142             // Lazy loading. On first call will create the list using the CG generated builder
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      * @see pt.digitalis.dif.dem.interfaces.IEntity#getID()
160      */
161     public String getID()
162     {
163         return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
164     }
165 
166     /**
167      * @see pt.digitalis.dif.dem.interfaces.IStage#getInjectedErrorStages()
168      */
169     synchronized public Map<String, String> getInjectedErrorStages()
170     {
171         if (errorStages == null)
172         {
173             // Lazy loading. On first call will create the list using the CG generated builder
174             errorStages = new HashMap<String, String>();
175             injectedErrorStagesBuilder();
176         }
177 
178         return errorStages;
179     }
180 
181     /**
182      * @see pt.digitalis.dif.dem.interfaces.IStage#getInjectedErrorViews()
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      * @see pt.digitalis.dif.dem.interfaces.IStage#getInjectedStages()
197      */
198     synchronized public List<String> getInjectedStages()
199     {
200 
201         if (injectedStages == null)
202         {
203             // Lazy loading. On first call will create the list using the CG generated builder
204             injectedStages = new ArrayList<String>();
205             injectedStagesBuilder();
206         }
207 
208         return injectedStages;
209     }
210 
211     /**
212      * @see pt.digitalis.dif.dem.interfaces.IStage#getInjectedViews()
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      * @see pt.digitalis.dif.dem.interfaces.IStage#getInstance()
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             // This should never happen. If it does DIF will show the exception. Here we return null.
240             return null;
241         }
242         catch (ClassNotFoundException e)
243         {
244             // This should never happen. If it does DIF will show the exception. Here we return null.
245             return null;
246         }
247 
248         instance.setProxy(this);
249 
250         if (!isInitialized)
251             lazyStageProxyInitialization(instance);
252 
253         return instance;
254     }
255 
256     /**
257      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#getLicenseEdition()
258      */
259     public LicenseEditionType getLicenseEdition()
260     {
261         return TemplateResources.getRegistrationManager().getStageEdition(this.getID());
262     }
263 
264     /**
265      * @see pt.digitalis.dif.dem.interfaces.IStage#getMessageForLanguage(java.lang.String, java.lang.String)
266      */
267     public String getMessageForLanguage(String language, String messageID)
268     {
269         return getMessagesForLanguage(language).get(messageID);
270     }
271 
272     /**
273      * @see pt.digitalis.dif.dem.interfaces.IStage#getMessagesForLanguage(java.lang.String)
274      */
275 
276     public Map<String, String> getMessagesForLanguage(String language)
277     {
278         return TemplateResources.getMessageManager().getMessages(this, language);
279     }
280 
281     /**
282      * @see pt.digitalis.dif.dem.interfaces.IEntity#getName()
283      */
284     public String getName()
285     {
286         return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
287     }
288 
289     /**
290      * @see pt.digitalis.dif.dem.interfaces.IEntity#getOriginalClassName()
291      */
292     public String getOriginalClassName()
293     {
294         return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
295     }
296 
297     /**
298      * @see pt.digitalis.dif.dem.interfaces.IStage#getOverridesStageID()
299      */
300     public String getOverridesStageID()
301     {
302         return null;
303     }
304 
305     /**
306      * @see pt.digitalis.dif.dem.interfaces.IStage#getParameters()
307      */
308     @SuppressWarnings("static-access")
309     synchronized public IParameters getParameters()
310     {
311         if (parameters == null)
312         {
313             parameters = getTemplateResources().getParametersInstance(getInstance());
314             // TODO: Refactor the IParameterImpl so that we can access the parameter list with initializing a
315             // StageInstance parameters.initialize(this);
316         }
317 
318         return parameters;
319     }
320 
321     /**
322      * @see pt.digitalis.dif.dem.interfaces.IStage#getService()
323      */
324     public IService getService()
325     {
326         return TemplateResources.getDEMManager().getService(CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE);
327     }
328 
329     /**
330      * @see pt.digitalis.dif.dem.interfaces.IStage#getStageInstanceClassName()
331      */
332     public String getStageInstanceClassName()
333     {
334         return CGAncillaries.CG_TO_BE_IMPLEMENTED_MESSAGE;
335     }
336 
337     /**
338      * For usage of the CG methods
339      * 
340      * @return an instance of template resources
341      */
342     protected TemplateResources getTemplateResources()
343     {
344         return TemplateResources.getInstance();
345     }
346 
347     /**
348      * @see pt.digitalis.dif.dem.interfaces.IEntity#getUID()
349      */
350     public String getUID()
351     {
352         return "STAGE:" + this.getID();
353     }
354 
355     /**
356      * @see pt.digitalis.dif.dem.interfaces.IStage#hasAuthentication()
357      */
358     public boolean hasAuthentication()
359     {
360         return true;
361     }
362 
363     /**
364      * @see pt.digitalis.dif.dem.interfaces.IStage#hasAuthenticationErrorInjection()
365      */
366     public boolean hasAuthenticationErrorInjection()
367     {
368         return false;
369     }
370 
371     /**
372      * @see pt.digitalis.dif.dem.interfaces.IStage#hasAuthorization()
373      */
374     public boolean hasAuthorization()
375     {
376         return true;
377     }
378 
379     /**
380      * @see pt.digitalis.dif.dem.interfaces.ICallback#hasCallbackEnabled()
381      */
382     public boolean hasCallbackEnabled()
383     {
384         return false;
385     }
386 
387     /**
388      * @see pt.digitalis.dif.dem.interfaces.IStage#hasInjectedContributions()
389      */
390     public boolean hasInjectedContributions()
391     {
392         return false;
393     }
394 
395     /**
396      * @see pt.digitalis.dif.dem.interfaces.IStage#hasParameterErrorInjection()
397      */
398     public boolean hasParameterErrorInjection()
399     {
400         // By default returns false. If the stage implementation injects the parameter errors, that this is overwritten
401         // with 'true'
402         return false;
403     }
404 
405     /**
406      * @see pt.digitalis.dif.dem.interfaces.IStage#hasValidationLogicForForm(java.lang.String)
407      */
408     public boolean hasValidationLogicForForm(String formName)
409     {
410         return this.getEventHandlers().get(EventType.FORM_VALIDATION).contains(formName);
411     }
412 
413     /** Injected Error Stages builder. Will be overridden in the CG if stages are injected */
414     private void injectedErrorStagesBuilder()
415     {}
416 
417     /** Injected Views builder. Will be overridden in the CG if stages are injected */
418     private void injectedErrorViewsBuilder()
419     {}
420 
421     /** Injected Stages builder. Will be overridden in the CG if stages are injected */
422     private void injectedStagesBuilder()
423     {}
424 
425     /** Injected Views builder. Will be overridden in the CG if stages are injected */
426     private void injectedViewsBuilder()
427     {}
428 
429     /**
430      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#isRegistered()
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      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#isRegistrable()
444      */
445     public boolean isRegistrable()
446     {
447         return TemplateResources.getRegistrationManager().isStageRegistrable(getID());
448     }
449 
450     /**
451      * The lazy initializations for all stages. Things that require all DEM entities to be previously initialized
452      * 
453      * @param stage
454      *            the stage instance for accessing some stage properties in the initialization process
455      */
456     protected void lazyStageProxyInitialization(IStageInstance stage)
457     { // FIXME: lazyStageProxyInitialization is generating errors
458         /*
459          * try { for (IParameter<?> parameter:
460          * stage.getParameters().getAllAvailableParameters().getParameters().values()) if (parameter.getRules() != null
461          * && parameter.getRules().size() > 0) for (IParameterRule<?> rule: parameter.getRules()) {
462          * AbstractParameterRule<?> ruleInstance = (AbstractParameterRule<?>) rule; for (String linkedParameterID:
463          * ruleInstance.getParameters()) getParameters().getAllAvailableParameters().getParameter(linkedParameterID)
464          * .setReferencedInARuleFromAnotherParameter(true); } } catch (ParameterException e) {
465          * DIFLogger.getLogger().info("Could not initialize stage parameters: " + e.getMessage()); }
466          */
467         isInitialized = true;
468     }
469 
470     /**
471      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#register(java.lang.String, java.lang.String)
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      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#unregister()
488      */
489     public void unregister()
490     {
491         TemplateResources.getRegistrationManager().unregisterStage(getID());
492     }
493 }