1    package com.instantbank.lettertemplate.control.web.handlers;
2    
3    import java.util.HashMap;
4    import java.util.ArrayList;
5    import java.util.Enumeration;
6    import java.util.Hashtable;
7    import java.util.StringTokenizer;
8    
9    import javax.servlet.http.HttpServletRequest;
10   import javax.servlet.ServletContext;
11   import com.instantbank.lettertemplate.control.event.ComponentsEvent;
12   import com.instantbank.lettertemplate.control.util.JSPUtil;
13   import com.instantbank.lettertemplate.control.util.WebKeys;
14   import com.instantbank.common.utilcomponents.Debug;
15   import com.instantbank.common.utilcomponents.CommonUtil;
16   import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
17   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
18   
19   
20   /**
21    *  Letter Components usecase: flow handler for deciding output screen after
22    *  processing an action
23    *
24    * @author Instant-bank (Jorge Cardenas)
25    * @created August 2002
26    */
27   public class ComponentsFlowHandler
28       implements FlowHandler {
29     private Debug debug = null;
30   
31   
32     /**
33      *  optional execution before processFlow method
34      *
35      * @param request HTTP request
36      */
37     public void doStart(HttpServletRequest request) { }
38   
39   
40     /**
41      *  principal method for deciding output screen after processing an action
42      *
43      * @param request of MainServlet
44      * @param answer of the action
45      * @param context web context
46      * @return String with the number of the output screen
47      * @exception LetterTemplateEventException
48      */
49     public String processFlow(HttpServletRequest request, Object answer,
50                               ServletContext context)
51        throws LetterTemplateEventException {
52       debug = new Debug();
53       debug.setDebugginOn(true);
54       debug.setPreMessage("** ComponentsFlowHandler: ");
55       debug.println("processFlow");
56   
57       String nextScreen = null;
58       ComponentsEvent ce = null;
59       Hashtable componentsTable = null;
60       String companyId = (String)request.getSession()
61         .getAttribute(WebKeys.CompanyId);
62   
63       if(request.getAttribute(WebKeys.ComponentsEvent) != null) {
64         ce = (ComponentsEvent)request.getAttribute(WebKeys.ComponentsEvent);
65       }
66   
67       if(request.getAttribute(WebKeys.ExceptionOcurred) != null) {
68         // puts in request the current components list,
69         // obtained from the web context:
70         CommonUtil.putVariableInRequest(request, context, companyId,
71           WebKeys.ComponentsTable, WebKeys.ComponentsList);
72         nextScreen = "1";
73         // COMPONENTS_PROBLEM
74   
75       }
76       else if(request.getAttribute(WebKeys.ExistsComponentsList) != null) {
77         // puts in request the current components list,
78         // obtained from the web context:
79         CommonUtil.putVariableInRequest(request, context, companyId,
80           WebKeys.ComponentsTable, WebKeys.ComponentsList);
81         nextScreen = "2";
82         // COMPONENTS_MAIN
83   
84       }
85       else if(ce.getActionType() == ComponentsEvent.LIST_COMPONENTS) {
86         // answer is the current components list: ArrayList()
87   
88         // puts current components list in the web context :
89         CommonUtil.putVariableInContext
90           (context, companyId, WebKeys.ComponentsTable, answer);
91         // puts current components list in the web request :
92         request.setAttribute(WebKeys.ComponentsList, answer);
93         nextScreen = "2";
94         // COMPONENTS_MAIN
95   
96       }
97       else if(ce.getActionType() == ComponentsEvent.UPDATE_COMPONENTS) {
98         // answer is an ArrayList with 2 elements:
99         // 0: possible problem: String
100        // 1: current components list: ArrayList()
101  
102        // puts current components list in the web context:
103        CommonUtil.putVariableInContext(context, companyId,
104          WebKeys.ComponentsTable, ((ArrayList)answer).get(1));
105  
106        // puts current components list in the web request :
107        request.setAttribute
108          (WebKeys.ComponentsList, ((ArrayList)answer).get(1));
109  
110        String problem = (String)(((ArrayList)answer).get(0));
111        if(problem.equals(LetterTemplateGlobals.STR_UNDEF)) {
112          nextScreen = "2";
113          // COMPONENTS_MAIN
114        }
115        else {
116          // problem is: theProblem|theStackTrace
117          JSPUtil.putProblemInRequest(request, problem);
118          nextScreen = "1";
119          // COMPONENTS_PROBLEM
120        }
121      }
122      return nextScreen;
123    }
124  
125  
126    /**
127     *  optional execution after processFlow method
128     *
129     * @param request HTTP request
130     */
131    public void doEnd(HttpServletRequest request) { }
132  }
133  
134