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.Locale;
7    import java.util.Hashtable;
8    import javax.servlet.http.HttpServletRequest;
9    import javax.servlet.ServletContext;
10   
11   import oracle.xml.parser.v2.*;
12   import org.w3c.dom.*;
13   import org.xml.sax.*;
14   
15   import com.instantbank.lettertemplate.control.event.ComponentsEvent;
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.LetterTemplateExceptionMessage;
21   import com.instantbank.lettertemplate.control.LetterTemplateEventException;
22   import com.instantbank.lettertemplate.control.event.LetterTemplateEvent;
23   
24   /**
25    *  Letter Components usecase: handler of user requests in the Application layer
26    *
27    * @author Instant-bank (Jorge Cardenas)
28    * @created August 2002
29    */
30   public class ComponentsHandler extends RequestHandlerSupport {
31   
32     private Debug debug = null;
33     ServletContext context;
34     private boolean gotoDB;
35   
36   
37     /**
38      *  principal method that process a user request instantiating a
39      *  ComponentsEvent with the user request data
40      *
41      * @param request of MainServlet
42      * @param context of MainServlet
43      * @return ComponentsEvent with the user request data
44      * @exception LetterTemplateEventException
45      */
46     public LetterTemplateEvent processRequest(HttpServletRequest request,
47                                               ServletContext context)
48        throws LetterTemplateEventException {
49       debug = new Debug();
50       debug.setDebugginOn(true);
51       debug.setPreMessage("** ComponentsHandler-application tier: ");
52   
53       this.context = context;
54       String action = request.getParameter("action");
55       debug.println("Creation of an Components Event; "
56         + "ComponentsHandler (web): action=" + action);
57       gotoDB = true;
58   
59       String companyId = (String)request.getSession()
60         .getAttribute(WebKeys.CompanyId);
61   
62       Hashtable componentsTable = CommonUtil.getWebContextVariable
63         (context, WebKeys.ComponentsTable);
64       Object listComponents = componentsTable.get(companyId);
65   
66       if((listComponents != null && !listComponents.equals(""))) {
67         gotoDB = false;
68       }
69   
70       if(action == null) {
71         throw new LetterTemplateEventException
72           (LetterTemplateExceptionMessage.SERVICE_NOT_SELECTED);
73       }
74       else if(action.equals("listComponents")) {
75         return createListComponentsEvent(request);
76       }
77       else if(action.equals("updateComponents")) {
78         return createUpdateComponentsEvent(request);
79       }
80       return null;
81     }
82   
83   
84     /**
85      *  method for instantiating a ComponentsEvent demanding a service of getting
86      *  the components list
87      *
88      * @param request of MainServlet
89      * @return ComponentsEvent with the user request data
90      * @exception LetterTemplateEventException
91      */
92     private LetterTemplateEvent createListComponentsEvent
93       (HttpServletRequest request)
94        throws LetterTemplateEventException {
95       Hashtable componentsTable = null;
96       String companyId = (String)request.getSession()
97         .getAttribute(WebKeys.CompanyId);
98       try {
99         if(!gotoDB) {
100          request.setAttribute(WebKeys.ExistsComponentsList, "true");
101          return null;
102        }
103  
104        // instantiating ComponentsEvent:
105        ComponentsEvent event
106           = new ComponentsEvent
107          (ComponentsEvent.LIST_COMPONENTS,
108          (String)request.getSession()
109          .getAttribute(WebKeys.CompanyId),
110          (Long)request.getSession().
111          getAttribute(WebKeys.UserId),
112          null);
113        request.setAttribute(WebKeys.ComponentsEvent, event);
114        return event;
115      }
116      catch(Exception e) {
117        throw new LetterTemplateEventException
118          (LetterTemplateExceptionMessage.PROBLEM_PARSING + e.getMessage()
119          + LetterTemplateExceptionMessage.RETRY);
120      }
121    }
122  
123  
124    /**
125     *  method for instantiating a ComponentsEvent demanding a service of updating
126     *  several letters components
127     *
128     * @param request of MainServlet
129     * @return ComponentsEvent with the user request data
130     * @exception LetterTemplateEventException
131     */
132    private LetterTemplateEvent createUpdateComponentsEvent
133      (HttpServletRequest request)
134       throws LetterTemplateEventException {
135      debug.println("Update");
136      ArrayList items = null;
137      try {
138        // getting HTML parameters :
139        String xmlComponentsItems = request.getParameter("xmlComponentsItems").trim();
140        debug.println("xmlComponentsItems=" + xmlComponentsItems);
141        // parsing XML of xmlComponentsItems:
142        // resulting ArrayList has 3 elements of type String[]
143        // representing the attributes of items: code, name and status
144        items = parseXMLComponents(xmlComponentsItems);
145        String code[] = (String[])(items.get(0));
146        if(code.length == 0 && !gotoDB) {
147          request.setAttribute(WebKeys.ExistsComponentsList, "true");
148          return null;
149        }
150  
151        // instantiating ComponentsEvent
152        ComponentsEvent event
153           = new ComponentsEvent
154          (ComponentsEvent.UPDATE_COMPONENTS,
155          (String)request.getSession().
156          getAttribute(WebKeys.CompanyId),
157          (Long)request.getSession().
158          getAttribute(WebKeys.UserId),
159          items);
160        request.setAttribute(WebKeys.ComponentsEvent, event);
161        return event;
162      }
163      catch(Exception e) {
164        throw new LetterTemplateEventException
165          (LetterTemplateExceptionMessage.PROBLEM_PARSING + e.getMessage()
166          + LetterTemplateExceptionMessage.RETRY);
167      }
168    }
169  
170  
171    /**
172     *  auxiliary method for parsing a XML string containing items to update in
173     *  letters components
174     *
175     * @param componentsItems is the XML string
176     * @return a ArrayList with 3 elements of type String[]
177     *      representing the attributes of items: code, name and status
178     * @exception Exception
179     */
180    private ArrayList parseXMLComponents(String componentsItems) throws Exception {
181      XMLDocument xmlDoc = CommonUtil.parseInfo(componentsItems);
182  
183      NodeList nlCode = xmlDoc.selectNodes("/ComponentsList/Components/codeComponent/text()");
184      NodeList nlName = xmlDoc.selectNodes("/ComponentsList/Components/nameComponent/text()");
185      NodeList nlStatus = xmlDoc.selectNodes("/ComponentsList/Components/status/text()");
186      int nlLength = nlCode.getLength();
187  
188      String[] codeComponent = new String[nlLength];
189      String[] nameComponent = new String[nlLength];
190      String[] status = new String[nlLength];
191      for(int i = 0; i < nlLength; i++) {
192        codeComponent[i] = nlCode.item(i).getNodeValue();
193        nameComponent[i] = nlName.item(i).getNodeValue();
194        status[i] = nlStatus.item(i).getNodeValue();
195        debug.println("codeComponent, nameComponent, status of item i="
196          + codeComponent[i] + "--"
197          + nameComponent[i] + "--" + status[i]);
198      }
199  
200      ArrayList items = new ArrayList(3);
201      items.add(codeComponent);
202      items.add(nameComponent);
203      items.add(status);
204      return items;
205    }
206  }
207  
208