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.io.FileOutputStream;
7    import java.io.File;
8    import java.util.Hashtable;
9    import java.lang.String;
10   
11   import javax.servlet.http.HttpServletRequest;
12   import javax.servlet.ServletContext;
13   import javax.servlet.http.HttpSession;
14   import com.instantbank.lettertemplate.control.event.ImagesRepositoryEvent;
15   import com.instantbank.lettertemplate.control.util.JSPUtil;
16   import com.instantbank.lettertemplate.control.util.WebKeys;
17   import com.instantbank.common.utilcomponents.Debug;
18   import com.instantbank.common.utilcomponents.CommonUtil;
19   import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
20   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
21   
22   /**
23    *  Letter Images usecase: flow handler for deciding output screen after
24    *  processing an action
25    *
26    * @author Instant-bank (Consuelo Franky, Jorge Cardenas)
27    * @created August 2002
28    */
29   public class ImagesRepositoryFlowHandler
30       implements FlowHandler {
31     private Debug debug = null;
32   
33   
34     /**
35      *  optional execution before processFlow method
36      *
37      * @param request HTTP request
38      */
39     public void doStart(HttpServletRequest request) { }
40   
41   
42     /**
43      *  principal method for deciding output screen after processing an action
44      *
45      * @param request of MainServlet
46      * @param answer of the action
47      * @param context web context
48      * @return String with the number of the output screen
49      * @exception LetterTemplateEventException
50      */
51     public String processFlow(HttpServletRequest request, Object answer,
52                               ServletContext context)
53        throws LetterTemplateEventException {
54       debug = new Debug();
55       debug.setDebugginOn(true);
56       debug.setPreMessage("** ImagesRepositoryFlowHandler: ");
57       debug.println("processFlow");
58   
59       String nextScreen = null;
60       ImagesRepositoryEvent ce = null;
61       Hashtable imageTable = null;
62       HttpSession session = request.getSession();
63       String companyId = (String)request.getSession()
64         .getAttribute(WebKeys.CompanyId);
65   
66       if(request.getAttribute(WebKeys.ImagesRepositoryEvent) != null) {
67         ce = (ImagesRepositoryEvent)request.getAttribute
68           (WebKeys.ImagesRepositoryEvent);
69       }
70   
71       if(request.getAttribute(WebKeys.ExceptionOcurred) != null) {
72         // puts in request the current images list, obtained from the web context:
73         CommonUtil.putVariableInRequest(request, context, companyId,
74           WebKeys.ImageTable, WebKeys.ImagesRepositoryList);
75         nextScreen = "1";
76         // IMAGES_PROBLEM
77   
78       }
79       else if(request.getAttribute(WebKeys.ExistsImagesList) != null) {
80         // puts in request the current images list,
81         // obtained from the web context:
82         CommonUtil.putVariableInRequest(request, context, companyId,
83           WebKeys.ImageTable, WebKeys.ImagesRepositoryList);
84   
85         debug.println("FlowHandler===>action="
86           + request.getAttribute(WebKeys.ActionImage));
87         String actionImage = (String)(request.getAttribute(WebKeys.ActionImage));
88         Object pendingProblem = session.getAttribute(WebKeys.PendingProblem);
89   
90         if(actionImage.equals("uploadImage")) {
91           nextScreen = "3";
92           // IMAGES_UPLOAD
93         }
94         else if(pendingProblem != null && pendingProblem.equals("true")) {
95           session.setAttribute(WebKeys.PendingProblem, "false");
96           nextScreen = "1";
97           // IMAGES_PROBLEM
98         }
99         else {
100          nextScreen = "2";
101          // IMAGES_MAIN
102        }
103  
104      }
105      else if(ce.getActionType() == ImagesRepositoryEvent.LIST_IMAGES_NAMES
106        || ce.getActionType() == ImagesRepositoryEvent.STORE_IMAGE) {
107        // answer is the current images list: CodeDescription[]
108  
109        // puts current images list in the web context :
110        CommonUtil.putVariableInContext
111          (context, companyId, WebKeys.ImageTable, answer);
112        // puts current images list in the web request :
113        request.setAttribute(WebKeys.ImagesRepositoryList, answer);
114  
115        nextScreen = "2";
116        // IMAGES_MAIN
117  
118      }
119      else if(ce.getActionType() == ImagesRepositoryEvent.UPDATE_IMAGES_NAMES) {
120        // answer is an ArrayList with 2 elements:
121        // 0: possible problem: String,
122        // 1: current images list: CodeDescription[]
123  
124        // puts current images list in the web context :
125        CommonUtil.putVariableInContext(context, companyId,
126          WebKeys.ImageTable, ((ArrayList)answer).get(1));
127        // puts current images list in the web request :
128        request.setAttribute
129          (WebKeys.ImagesRepositoryList, ((ArrayList)answer).get(1));
130  
131        String problem = (String)(((ArrayList)answer).get(0));
132        if(problem.equals(LetterTemplateGlobals.STR_UNDEF)) {
133          nextScreen = "2";
134          // IMAGES_MAIN
135        }
136        else {
137          // problem is: theProblem|theStackTrace
138          JSPUtil.putProblemInRequest(request, problem);
139          nextScreen = "1";
140          // IMAGES_PROBLEM
141        }
142  
143      }
144      else if(ce.getActionType() == ImagesRepositoryEvent.PRE_STORE_IMAGE) {
145        // answer is an ArrayList with 2 elements:
146        // 0: possible problem: String,
147        // 1: current images list: CodeDescription[]
148  
149        // puts current images list in the web context :
150        CommonUtil.putVariableInContext(context, companyId,
151          WebKeys.ImageTable, ((ArrayList)answer).get(1));
152        // puts current images list in the web request :
153        request.setAttribute
154          (WebKeys.ImagesRepositoryList, ((ArrayList)answer).get(1));
155  
156        String problem = (String)(((ArrayList)answer).get(0));
157        if(problem.equals(LetterTemplateGlobals.STR_UNDEF)) {
158          nextScreen = "3";
159          // IMAGES_UPLOAD
160        }
161        else {
162          // problem is: theProblem|theStackTrace
163          JSPUtil.putProblemInRequest(request, problem);
164          nextScreen = "1";
165          // IMAGES_PROBLEM
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