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   
12   import javax.servlet.http.HttpServletRequest;
13   import javax.servlet.ServletContext;
14   import javax.servlet.http.HttpSession;
15   import com.instantbank.lettertemplate.control.event.TemplatesEvent;
16   import com.instantbank.lettertemplate.control.util.JSPUtil;
17   import com.instantbank.lettertemplate.control.util.WebKeys;
18   import com.instantbank.common.utilcomponents.Debug;
19   import com.instantbank.common.utilcomponents.CommonUtil;
20   import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
21   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
22   
23   /**
24    *  Letter Templates usecase: flow handler for deciding output screen after
25    *  processing an action
26    *
27    * @author Instant-bank (Jorge Cardenas)
28    * @created August 2002
29    */
30   public class TemplatesFlowHandler
31       implements FlowHandler {
32     private Debug debug = null;
33   
34   
35     /**
36      *  optional execution before processFlow method
37      *
38      * @param request HTTP request
39      */
40     public void doStart(HttpServletRequest request) { }
41   
42   
43     /**
44      *  principal method for deciding output screen after processing an action
45      *
46      * @param request of MainServlet
47      * @param answer of the action
48      * @param context web context
49      * @return String with the number of the output screen
50      * @exception LetterTemplateEventException Description of the Exception
51      */
52     public String processFlow(HttpServletRequest request, Object answer,
53                               ServletContext context)
54        throws LetterTemplateEventException {
55       debug = new Debug();
56       debug.setDebugginOn(true);
57       debug.setPreMessage("** TemplatesFlowHandler: ");
58       debug.println("processFlow");
59   
60       String nextScreen = null;
61       TemplatesEvent ce = null;
62       Hashtable templateTable = null;
63       Hashtable categoryTable = null;
64       HttpSession session = request.getSession();
65       String companyId = (String)request.getSession()
66         .getAttribute(WebKeys.CompanyId);
67   
68       if(request.getAttribute(WebKeys.TemplatesEvent) != null) {
69         ce = (TemplatesEvent)request.getAttribute(WebKeys.TemplatesEvent);
70       }
71   
72       if(request.getAttribute(WebKeys.ExceptionOcurred) != null) {
73   
74         // puts in request the current templates list,
75         // obtained from the web context:
76         CommonUtil.putVariableInRequest(request, context, companyId,
77           WebKeys.TemplatesTable, WebKeys.TemplatesList);
78   
79         CommonUtil.putVariableInRequest(request, context, companyId,
80           WebKeys.CategoryTable, WebKeys.CategoryList);
81   
82         Object pendingProblem = session.getAttribute(WebKeys.PendingProblem);
83   
84         if(pendingProblem != null && pendingProblem.equals("true")) {
85           session.setAttribute(WebKeys.PendingProblem, "false");
86           nextScreen = "1";
87           // PREVIEW_PROBLEM
88         }
89         nextScreen = "1";
90   
91       }
92       else if(request.getAttribute(WebKeys.ExistsTemplatesList) != null) {
93         // puts in request the current templates list,
94         // obtained from the web context:
95         CommonUtil.putVariableInRequest(request, context, companyId,
96           WebKeys.TemplatesTable, WebKeys.TemplatesList);
97   
98         // puts in request the current categories list,
99         // obtained from the web context:
100        CommonUtil.putVariableInRequest(request, context, companyId,
101          WebKeys.CategoryTable, WebKeys.CategoryList);
102  
103        nextScreen = "2";
104        // TEMPLATES_MAIN
105  
106      }
107      else if(ce.getActionType() == TemplatesEvent.LIST_TEMPLATES_NAMES) {
108        // answer is an ArrayList with 2 elements:
109        // 0: current categories list: CodeDescription[]
110        // 1: current templates list: ArrayList
111  
112        // puts current templates list in the web context :
113        CommonUtil.putVariableInContext(context, companyId,
114          WebKeys.TemplatesTable, ((ArrayList)answer).get(1));
115  
116        // puts current templates list in the web request :
117        request.setAttribute
118          (WebKeys.TemplatesList, ((ArrayList)answer).get(1));
119  
120        // puts current categories list in the web context :
121        CommonUtil.putVariableInContext(context, companyId,
122          WebKeys.CategoryTable, ((ArrayList)answer).get(0));
123  
124        // puts current categories list in the web request :
125        request.setAttribute
126          (WebKeys.CategoryList, ((ArrayList)answer).get(0));
127  
128        nextScreen = "2";
129        // TEMPLATES_MAIN
130  
131      }
132      else if(ce.getActionType() == TemplatesEvent.UPDATE_TEMPLATES_NAMES) {
133        // answer is an ArrayList with 2 elements:
134        // 0: possible problem (String)
135        // 1: current templates list: ArrayList
136  
137        // invalidates context variable corresponding to unlinked component list:
138        CommonUtil.putVariableInContext(context, companyId,
139          WebKeys.ComponentsTable, "");
140  
141        // puts current templates list in the web context :
142        CommonUtil.putVariableInContext(context, companyId,
143          WebKeys.TemplatesTable, ((ArrayList)answer).get(1));
144  
145        // puts current templates list in the web request :
146        request.setAttribute
147          (WebKeys.TemplatesList, ((ArrayList)answer).get(1));
148  
149        // puts in request the current categories list,
150        // obtained from the web context:
151        CommonUtil.putVariableInRequest(request, context, companyId,
152          WebKeys.CategoryTable, WebKeys.CategoryList);
153  
154        String problem = (String)(((ArrayList)answer).get(0));
155  
156        if(problem.equals(LetterTemplateGlobals.STR_UNDEF)) {
157          nextScreen = "2";
158          // TEMPLATES_MAIN
159        }
160        else {
161          // problem is: theProblem|theStackTrace
162          JSPUtil.putProblemInRequest(request, problem);
163          nextScreen = "1";
164          // TEMPLATES_PROBLEM
165  
166        }
167      }
168  
169      return nextScreen;
170    }
171  
172  
173    /**
174     *  optional execution after processFlow method
175     *
176     * @param request HTTP request
177     */
178    public void doEnd(HttpServletRequest request) { }
179  }
180  
181