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.CategoryEvent;
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 Categories usecase: flow handler for deciding output screen after
22    *  processing an action
23    *
24    * @author Instant-bank (Consuelo Franky)
25    * @created August 2002
26    */
27   public class CategoryFlowHandler
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("** CategoryFlowHandler: ");
55       debug.println("processFlow");
56   
57       String nextScreen = null;
58       CategoryEvent ce = null;
59       Hashtable categoryTable = null;
60       String companyId = (String)request.getSession()
61         .getAttribute(WebKeys.CompanyId);
62   
63       if(request.getAttribute(WebKeys.CategoryEvent) != null) {
64         ce = (CategoryEvent)request.getAttribute(WebKeys.CategoryEvent);
65       }
66   
67       if(request.getAttribute(WebKeys.ExceptionOcurred) != null) {
68         // puts in request the current categories list,
69         // obtained from the web context:
70         CommonUtil.putVariableInRequest(request, context, companyId,
71           WebKeys.CategoryTable, WebKeys.CategoryList);
72         nextScreen = "1";
73         // CATEGORY_PROBLEM
74   
75       }
76       else if(request.getAttribute(WebKeys.ExistsCategoryList) != null) {
77         // puts in request the current categories list,
78         // obtained from the web context:
79         CommonUtil.putVariableInRequest(request, context, companyId,
80           WebKeys.CategoryTable, WebKeys.CategoryList);
81         nextScreen = "2";
82         // CATEGORY_MAIN
83   
84       }
85       else if(ce.getActionType() == CategoryEvent.LIST_CATEGORIES) {
86         // answer is the current categories list: CodeDescription[]
87   
88         // puts current categories list in the web context :
89         CommonUtil.putVariableInContext
90           (context, companyId, WebKeys.CategoryTable, answer);
91         // puts current categories list in the web request :
92         request.setAttribute(WebKeys.CategoryList, answer);
93         nextScreen = "2";
94         // CATEGORY_MAIN
95   
96       }
97       else if(ce.getActionType() == CategoryEvent.UPDATE_CATEGORIES) {
98         // answer is an ArrayList with 2 elements:
99         // 0: possible problem: String
100        // 1: current categories list: CodeDescription[]
101  
102        // invalidates context variable corresponding to unlinked categories list:
103        CommonUtil.putVariableInContext(context, companyId,
104          WebKeys.CategoryTable, "");
105  
106        // invalidates context variable corresponding to unlinked templates list:
107        CommonUtil.putVariableInContext(context, companyId,
108          WebKeys.TemplatesTable, "");
109  
110        // puts current categories list in the web context:
111        CommonUtil.putVariableInContext(context, companyId,
112          WebKeys.CategoryTable, ((ArrayList)answer).get(1));
113  
114        // puts current categories list in the web request :
115        request.setAttribute
116          (WebKeys.CategoryList, ((ArrayList)answer).get(1));
117  
118        String problem = (String)(((ArrayList)answer).get(0));
119        if(problem.equals(LetterTemplateGlobals.STR_UNDEF)) {
120          nextScreen = "2";
121          // CATEGORY_MAIN
122        }
123        else {
124          // problem is: theProblem|theStackTrace
125          JSPUtil.putProblemInRequest(request, problem);
126          nextScreen = "1";
127          // CATEGORY_PROBLEM
128        }
129      }
130      return nextScreen;
131    }
132  
133  
134    /**
135     *  optional execution after processFlow method
136     *
137     * @param request HTTP request
138     */
139    public void doEnd(HttpServletRequest request) { }
140  }
141  
142