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.List;
10  import java.util.Map;
11  
12  import pt.digitalis.dif.codegen.CGAncillaries;
13  import pt.digitalis.dif.controller.interfaces.IDIFContext;
14  import pt.digitalis.dif.dem.CallbackType;
15  import pt.digitalis.dif.dem.annotations.parameter.CustomParameters;
16  import pt.digitalis.dif.dem.interfaces.IService;
17  import pt.digitalis.dif.dem.interfaces.IStage;
18  import pt.digitalis.dif.dem.interfaces.IStageInstance;
19  import pt.digitalis.dif.dem.objects.EventType;
20  import pt.digitalis.dif.dem.objects.LicenseEditionType;
21  import pt.digitalis.dif.dem.objects.ViewObject;
22  import pt.digitalis.dif.dem.objects.ViewType;
23  import pt.digitalis.dif.dem.objects.parameters.IParameter;
24  import pt.digitalis.dif.dem.objects.parameters.IParameters;
25  import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrors;
26  import pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule;
27  import pt.digitalis.dif.exception.controller.BusinessFlowException;
28  import pt.digitalis.dif.exception.controller.ControllerException;
29  import pt.digitalis.dif.exception.objects.ParameterException;
30  import pt.digitalis.dif.ioc.DIFIoCRegistry;
31  import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters;
32  
33  /**
34   * This class is a template for the IStageInstance interface method implementations. The CodeGen will copy these methods
35   * to a new Instance class for each stage. Some methods will be copied "as is", others will be tweaked.
36   * 
37   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
38   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a>
39   * @created 2007/07/12
40   */
41  public class StageInstanceCGTemplate implements IStageInstance {
42  
43      /** The errors of the parameter processing */
44      private ControllerException _CG_authenticationException;
45  
46      /** The execution context. */
47      private IDIFContext _CG_context;
48  
49      /** T if the stage jas an annotated {@link CustomParameters} method */
50      private boolean _CG_hasCustomParameters = false;
51  
52      /** The errors of the parameter processing */
53      private ParameterErrors _CG_parameterErrors;
54  
55      /** The parameters object */
56      private IParameters _CG_parameters;
57  
58      /** the _CG_proxy instance */
59      private IStage _CG_proxy;
60  
61      /** the active/present features */
62      private List<String> activeStageFeatures = null;
63  
64      /** T if the stage has already been initialized by calling the "_CG_init" method */
65      private boolean isInitialized;
66  
67      /**
68       * a private instance of template resources. Used for optimizations and live debug purposes instead of direct code
69       * in this template that would not be as easily debuged or fast compiled in run time
70       */
71      private TemplateResources templateResources = null;
72  
73      /**
74       * This method will be used by the code gen module to set the annotated attributes.
75       * 
76       * @param difContext
77       *            the context
78       */
79      public void __Stage__InjectedAttributesInitMethod__(IDIFContext difContext)
80      {}
81  
82      /**
83       * This method will be used by the framework to do some post processing work.
84       * 
85       * @param difContext
86       *            the context
87       */
88      public void __Stage__PostProcessingMethod__(IDIFContext difContext)
89      {}
90  
91      /**
92       * @see pt.digitalis.dif.dem.interfaces.IStageInstance#_CG_execute(IDIFContext)
93       */
94      public ViewObject _CG_execute(IDIFContext context) throws BusinessFlowException
95      {
96          return this.getTemplateResources().stageExecute(this, context);
97      }
98  
99      /**
100      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#_CG_finalize(IDIFContext)
101      */
102     public boolean _CG_finalize(IDIFContext context) throws BusinessFlowException
103     {
104         // Post process parameters
105         __Stage__PostProcessingMethod__(context);
106 
107         // Call the wrapper to the user defined method
108         return callFinalizeMethod(context);
109     }
110 
111     /**
112      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#_CG_init(IDIFContext)
113      */
114     public boolean _CG_init(IDIFContext context) throws BusinessFlowException
115     {
116         // If needed inject IoC contributions
117         if (this.hasInjectedContributions())
118             DIFIoCRegistry.getRegistry().injectDependencies(this);
119 
120         // Init context
121         this.setContext(context);
122 
123         // Initialize an empty parameter error report
124         this._CG_parameterErrors = new ParameterErrors(this);
125 
126         // Initialize injected attributes
127         this.__Stage__InjectedAttributesInitMethod__(context);
128 
129         // Refresh remaining parameters (created by the CustomParameters annotation)
130         try
131         {
132             this._CG_parameterErrors.refreshParameters(this);
133         }
134         catch (ParameterException e)
135         {
136             throw new BusinessFlowException(e, context);
137         }
138 
139         // Is called prior to the business stage init method since this initialization is the DIF internal
140         // initialization process. Not dependent of the business initialization
141         this.isInitialized = true;
142 
143         // Call the wrapper to the user defined method
144         return callInitMethod(context);
145     }
146 
147     /**
148      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#callEventMethod(pt.digitalis.dif.controller.interfaces.IDIFContext,
149      *      pt.digitalis.dif.dem.objects.EventType, java.lang.String)
150      */
151     public Object callEventMethod(IDIFContext context, EventType type, String eventName)
152     {
153         return CGAncillaries.CG_TO_BE_IMPLEMENTED_OBJECT;
154     }
155 
156     /**
157      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#callExecuteMethod(pt.digitalis.dif.controller.interfaces.IDIFContext)
158      */
159     public ViewObject callExecuteMethod(IDIFContext context)
160     {
161         return CGAncillaries.CG_TO_BE_IMPLEMENTED_VIEW;
162     }
163 
164     /**
165      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#callExecuteOnEventMethod(pt.digitalis.dif.controller.interfaces.IDIFContext,
166      *      pt.digitalis.dif.dem.objects.EventType, java.lang.String)
167      */
168     public ViewObject callExecuteOnEventMethod(IDIFContext context, EventType type, String eventName)
169     {
170         return CGAncillaries.CG_TO_BE_IMPLEMENTED_VIEW;
171     }
172 
173     /**
174      * The method that will be enhanced with the call to the method annotated with <code>@Finalize</code>.
175      * 
176      * @param context
177      *            the execution context
178      * @return as defined on the user-defined <code>@Finalize</code>-annotated method
179      */
180     protected boolean callFinalizeMethod(IDIFContext context)
181     {
182         return CGAncillaries.CG_TO_BE_IMPLEMENTED_BOOLEAN;
183     }
184 
185     /**
186      * The method that will be enhanced with the users custom parameters (call to the method annotated with
187      * <code>@CustomParameters</code>.
188      * 
189      * @param parameters
190      *            the stage parameters to be customized
191      */
192     protected void callInitCustomParametersMethod(IParameters parameters)
193     {}
194 
195     /**
196      * The method that will be enhanced with the call to the method annotated with <code>@DIFInitializer</code>.
197      * 
198      * @param context
199      *            the execution context
200      * @return as defined on the user-defined <code>@DIFInitializer</code>-annotated method
201      */
202     protected boolean callInitMethod(IDIFContext context)
203     {
204         return CGAncillaries.CG_TO_BE_IMPLEMENTED_BOOLEAN;
205     }
206 
207     /**
208      * Creates a new view object. For usage of the CG methods
209      * 
210      * @param engine
211      *            the engine of the view
212      * @param type
213      *            the type of the view
214      * @param target
215      *            the target resource/template of the view
216      * @param isDefault
217      *            T if the view is the default view
218      * @return the created view object
219      */
220     protected ViewObject createView(String engine, String type, String target, boolean isDefault)
221     {
222         return new ViewObject(engine, ViewType.valueOf(type), target, isDefault);
223     }
224 
225     /**
226      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#declareFeatureActive(java.lang.String)
227      */
228     public void declareFeatureActive(String featureID)
229     {
230         this.getActiveStageFeatures().add(featureID);
231     }
232 
233     /**
234      * Inspector for the 'activeStageFeatures' attribute.
235      * 
236      * @return the activeStageFeatures value
237      */
238     private List<String> getActiveStageFeatures()
239     {
240         if (this.activeStageFeatures == null)
241             this.activeStageFeatures = new ArrayList<String>();
242 
243         return this.activeStageFeatures;
244     }
245 
246     /**
247      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getAuthenticationError()
248      */
249     public ControllerException getAuthenticationError()
250     {
251         return _CG_authenticationException;
252     }
253 
254     /**
255      * @see pt.digitalis.dif.dem.interfaces.ICallback#getCallbackType()
256      */
257     public CallbackType getCallbackType()
258     {
259         return _CG_proxy.getCallbackType();
260     }
261 
262     /**
263      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getContext()
264      */
265     public IDIFContext getContext()
266     {
267         return this._CG_context;
268     }
269 
270     /**
271      * The default error stage is constant and common to all instances and as such can be placed on the _CG_proxy. This
272      * method simply delegates the default error stage fetching to the _CG_proxy.
273      * 
274      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getDefaultErrorStage()
275      */
276     public IStage getDefaultErrorStage()
277     {
278         return this._CG_proxy.getDefaultErrorStage();
279     }
280 
281     /**
282      * The default error view is constant and common to all instances and as such can be placed on the _CG_proxy. This
283      * method simply delegates the default error view fetching to the _CG_proxy.
284      * 
285      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getDefaultErrorView()
286      */
287     public ViewObject getDefaultErrorView()
288     {
289         return this._CG_proxy.getDefaultErrorView();
290     }
291 
292     /**
293      * The default view is constant and common to all instances and as such can be placed on the _CG_proxy. This method
294      * simply delegates the default view fetching to the _CG_proxy.
295      * 
296      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getDefaultView()
297      */
298     public ViewObject getDefaultView()
299     {
300         return this._CG_proxy.getDefaultView();
301     }
302 
303     /**
304      * @see pt.digitalis.dif.dem.interfaces.IStage#getEventHandlers()
305      */
306     public Map<EventType, List<String>> getEventHandlers()
307     {
308         return _CG_proxy.getEventHandlers();
309     }
310 
311     /**
312      * The ID is constant and common to all instances and as such can be placed on the _CG_proxy. This method simply
313      * delegates the ID fetching to the _CG_proxy.
314      * 
315      * @see pt.digitalis.dif.dem.interfaces.IEntity#getID()
316      */
317     public String getID()
318     {
319         return _CG_proxy.getID();
320     }
321 
322     /**
323      * @see pt.digitalis.dif.dem.interfaces.IStage#getInjectedErrorStages()
324      */
325     public Map<String, String> getInjectedErrorStages()
326     {
327         return _CG_proxy.getInjectedErrorStages();
328     }
329 
330     /**
331      * @see pt.digitalis.dif.dem.interfaces.IStage#getInjectedErrorViews()
332      */
333     public Map<String, ViewObject> getInjectedErrorViews()
334     {
335         return _CG_proxy.getInjectedErrorViews();
336     }
337 
338     /**
339      * The injected stages are constant and common to all instances and as such can be placed on the _CG_proxy. This
340      * method simply delegates the injected stages fetching to the _CG_proxy.
341      * 
342      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getInjectedStages()
343      */
344     public List<String> getInjectedStages()
345     {
346         return this._CG_proxy.getInjectedStages();
347     }
348 
349     /**
350      * The injected views are constant and common to all instances and as such can be placed on the _CG_proxy. This
351      * method simply delegates the injected views fetching to the _CG_proxy.
352      * 
353      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getInjectedViews()
354      */
355     public List<ViewObject> getInjectedViews()
356     {
357         return _CG_proxy.getInjectedViews();
358     }
359 
360     /**
361      * @see pt.digitalis.dif.dem.interfaces.IStage#getInstance()
362      */
363     public IStageInstance getInstance()
364     {
365         return this;
366     }
367 
368     /**
369      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#getLicenseEdition()
370      */
371     public LicenseEditionType getLicenseEdition()
372     {
373         return TemplateResources.getRegistrationManager().getStageEdition(this.getID());
374     }
375 
376     /**
377      * @see pt.digitalis.dif.dem.interfaces.IMessage#getMessage(java.lang.String)
378      */
379     public String getMessage(String messageID)
380     {
381         return this.getMessages().get(messageID);
382     }
383 
384     /**
385      * @see pt.digitalis.dif.dem.interfaces.IStage#getMessageForLanguage(java.lang.String, java.lang.String)
386      */
387     public String getMessageForLanguage(String language, String messageID)
388     {
389         return _CG_proxy.getMessageForLanguage(language, messageID);
390     }
391 
392     /**
393      * @see pt.digitalis.dif.dem.interfaces.IMessage#getMessages()
394      */
395     public Map<String, String> getMessages()
396     {
397         if (getContext() == null || getContext().getSession() == null)
398             return _CG_proxy.getMessagesForLanguage(DIFGeneralConfigurationParameters.getInstance()
399                     .getDefaultLanguage());
400         else
401             return _CG_proxy.getMessagesForLanguage(getContext().getSession().getLanguage());
402     }
403 
404     /**
405      * @see pt.digitalis.dif.dem.interfaces.IStage#getMessagesForLanguage(java.lang.String)
406      */
407     public Map<String, String> getMessagesForLanguage(String language)
408     {
409         return _CG_proxy.getMessagesForLanguage(language);
410     }
411 
412     /**
413      * The name is constant and common to all instances and as such can be placed on the _CG_proxy. This method simply
414      * delegates the name fetching to the _CG_proxy.
415      * 
416      * @see pt.digitalis.dif.dem.interfaces.IEntity#getName()
417      */
418     public String getName()
419     {
420         return _CG_proxy.getName();
421     }
422 
423     /**
424      * @see pt.digitalis.dif.dem.interfaces.IEntity#getOriginalClassName()
425      */
426     public String getOriginalClassName()
427     {
428         return _CG_proxy.getOriginalClassName();
429     }
430 
431     /**
432      * @see pt.digitalis.dif.dem.interfaces.IStage#getOverridesStageID()
433      */
434     public String getOverridesStageID()
435     {
436         return _CG_proxy.getOverridesStageID();
437     }
438 
439     /**
440      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getParameterErrors()
441      */
442     public ParameterErrors getParameterErrors()
443     {
444         return _CG_parameterErrors;
445     }
446 
447     /**
448      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getParameters()
449      */
450     @SuppressWarnings("static-access")
451     public IParameters getParameters()
452     {
453         if (_CG_parameters == null)
454         {
455             _CG_parameters = getTemplateResources().getParametersInstance(this);
456             this.callInitCustomParametersMethod(_CG_parameters);
457 
458             if (_CG_hasCustomParameters)
459             {
460                 // Process rule dependencies, after custom rule definition
461                 try
462                 {
463                     for (IParameter<?> parameter: _CG_parameters.getStageParameters().getParameters().values())
464                     {
465                         for (IParameterRule<?> rule: parameter.getRules())
466                         {
467                             for (String parameterDependID: rule.getParameters())
468                             {
469                                 IParameter<?> parameterDepend = _CG_parameters.getStageParameters().getParameters()
470                                         .get(parameterDependID);
471 
472                                 if (parameterDepend != null
473                                         && !parameterDepend.isReferencedInARuleFromAnotherParameter())
474                                     parameterDepend.setReferencedInARuleFromAnotherParameter(true);
475                             }
476                         }
477                     }
478                 }
479                 catch (ParameterException e)
480                 {
481                     e.printStackTrace();
482                 }
483             }
484         }
485 
486         return _CG_parameters;
487     }
488 
489     /**
490      * The service is constant and common to all instances and as such can be placed on the _CG_proxy. This method
491      * simply delegates the service fetching to the _CG_proxy.
492      * 
493      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#getService()
494      */
495     public IService getService()
496     {
497         return this._CG_proxy.getService();
498     }
499 
500     /**
501      * @see pt.digitalis.dif.dem.interfaces.IStage#getStageInstanceClassName()
502      */
503     public String getStageInstanceClassName()
504     {
505         return _CG_proxy.getStageInstanceClassName();
506     }
507 
508     /**
509      * For usage of the CG methods
510      * 
511      * @return an instance of template resources
512      */
513     protected TemplateResources getTemplateResources()
514     {
515         if (this.templateResources == null)
516             this.templateResources = TemplateResources.getInstance();
517 
518         return templateResources;
519     }
520 
521     /**
522      * @see pt.digitalis.dif.dem.interfaces.IEntity#getUID()
523      */
524     public String getUID()
525     {
526         return _CG_proxy.getUID();
527     }
528 
529     /**
530      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#hasAuthentication()
531      */
532     public boolean hasAuthentication()
533     {
534         return _CG_proxy.hasAuthentication();
535     }
536 
537     /**
538      * @see pt.digitalis.dif.dem.interfaces.IStage#hasAuthenticationErrorInjection()
539      */
540     public boolean hasAuthenticationErrorInjection()
541     {
542         return _CG_proxy.hasAuthenticationErrorInjection();
543     }
544 
545     /**
546      * @see pt.digitalis.dif.dem.interfaces.IStage#hasAuthorization()
547      */
548     public boolean hasAuthorization()
549     {
550         return _CG_proxy.hasAuthorization();
551     }
552 
553     /**
554      * @see pt.digitalis.dif.dem.interfaces.ICallback#hasCallbackEnabled()
555      */
556     public boolean hasCallbackEnabled()
557     {
558         return _CG_proxy.hasCallbackEnabled();
559     }
560 
561     /**
562      * @see pt.digitalis.dif.dem.interfaces.IStage#hasInjectedContributions()
563      */
564     public boolean hasInjectedContributions()
565     {
566         return _CG_proxy.hasInjectedContributions();
567     }
568 
569     /**
570      * @see pt.digitalis.dif.dem.interfaces.IStage#hasParameterErrorInjection()
571      */
572     public boolean hasParameterErrorInjection()
573     {
574         return _CG_proxy.hasParameterErrorInjection();
575     }
576 
577     /**
578      * @see pt.digitalis.dif.dem.interfaces.IStage#hasValidationLogicForForm(java.lang.String)
579      */
580     public boolean hasValidationLogicForForm(String formName)
581     {
582         return this._CG_proxy.hasValidationLogicForForm(formName);
583     }
584 
585     /**
586      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#isFeatureEnabled(java.lang.String)
587      */
588     public boolean isFeatureEnabled(String featureID)
589     {
590         return this.getActiveStageFeatures().contains(featureID);
591     }
592 
593     /**
594      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#isInitialized()
595      */
596     public boolean isInitialized()
597     {
598         return this.isInitialized;
599     }
600 
601     /**
602      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#isRegistered()
603      */
604     public boolean isRegistered()
605     {
606         return _CG_proxy.isRegistered();
607     }
608 
609     /**
610      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#isRegistrable()
611      */
612     public boolean isRegistrable()
613     {
614         return _CG_proxy.isRegistrable();
615     }
616 
617     /**
618      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#register(java.lang.String, java.lang.String)
619      */
620     public boolean register(String name, String key)
621     {
622         return _CG_proxy.register(name, key);
623     }
624 
625     /**
626      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#setAuthenticationError(pt.digitalis.dif.exception.controller.ControllerException)
627      */
628     public void setAuthenticationError(ControllerException exception)
629     {
630         this._CG_authenticationException = exception;
631     }
632 
633     /**
634      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#setContext(IDIFContext)
635      */
636     public void setContext(IDIFContext newContext)
637     {
638         this._CG_context = newContext;
639     }
640 
641     /**
642      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#setParameterErrors(pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrors)
643      */
644     public void setParameterErrors(ParameterErrors errors)
645     {
646         _CG_parameterErrors = errors;
647     }
648 
649     /**
650      * @see pt.digitalis.dif.dem.interfaces.IStageInstance#setProxy(pt.digitalis.dif.dem.interfaces.IStage)
651      */
652     public void setProxy(IStage _CG_proxy)
653     {
654         this._CG_proxy = _CG_proxy;
655     }
656 
657     /**
658      * @see pt.digitalis.dif.dem.interfaces.IRegistrable#unregister()
659      */
660     public void unregister()
661     {
662         this._CG_proxy.unregister();
663 
664     }
665 }