1    package com.instantbank.servicing.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.servicing.control.event.ParmLevel2ValuesEvent;
16   import com.instantbank.servicing.control.util.JSPUtil;
17   import com.instantbank.servicing.control.util.WebKeys;
18   import com.instantbank.common.utilcomponents.Debug;
19   import com.instantbank.common.utilcomponents.CommonUtil;
20   import com.instantbank.common.utilcomponents.ServicingExceptionMessage;
21   import com.instantbank.servicing.control.ServicingEventException;
22   import com.instantbank.servicing.control.event.ServicingEvent;
23   
24   /**
25    *  Parameter Level2 Values usecase: handler of user requests in the Application layer
26    *
27    * @author Instant-bank (Jorge Cardenas)
28    * @created September 2002
29    */
30   public class ParmLevel2ValuesHandler extends RequestHandlerSupport {
31   
32     private Debug debug = null;
33     ServletContext context;
34   
35   
36     /**
37      *  principal method that process a user request instantiating a ParmLevel2ValuesEvent
38      *  with the user request data
39      *
40      * @param request of MainServlet
41      * @param context of MainServlet
42      * @return ParmLevesNameEvent with the user
43      *      request data
44      * @exception ServicingEventException
45      */
46     public ServicingEvent processRequest(HttpServletRequest request,
47                                          ServletContext context)
48        throws ServicingEventException {
49       debug = new Debug();
50       debug.setDebugginOn(true);
51       debug.setPreMessage("** ParmLevel2ValuesHandler-application tier: ");
52   
53       this.context = context;
54       String action = request.getParameter("action");
55       debug.println("Creation of an ParmLevel2Values Event; "
56         + "ParmLevel2ValuesHandler (web): action=" + action);
57   
58       if(action == null) {
59         throw new ServicingEventException
60           (ServicingExceptionMessage.SERVICE_NOT_SELECTED);
61       }
62       else if(action.equals("listParmLevel2Values")) {
63         return createListParmLevel2ValuesEvent(request);
64       }
65       else if(action.equals("updateParmLevel2Values")) {
66         return createUpdateParmLevel2ValuesEvent(request);
67       }
68       return null;
69     }
70   
71   
72     /**
73      * method for instantiating a ParmLevel2ValuesEvent demanding a service of
74      * getting the parameter Level2 values
75      *
76      * @param request of MainServlet
77      * @return ParmLevel2ValuesEvent with the user request data
78      * @exception ServicingEventException
79      */
80     private ServicingEvent createListParmLevel2ValuesEvent
81       (HttpServletRequest request)
82        throws ServicingEventException {
83   
84       try {
85   
86         // instantiating ParmLevel2ValuesEvent:
87         ParmLevel2ValuesEvent event
88            = new ParmLevel2ValuesEvent
89           (ParmLevel2ValuesEvent.LIST_PARM_LEVEL2_VALUES,
90           (String)request.getSession()
91           .getAttribute(WebKeys.CompanyId),
92           (Long)request.getSession().
93           getAttribute(WebKeys.UserId),
94           null);
95         request.setAttribute(WebKeys.ParmLevel2ValuesEvent, event);
96         return event;
97       }
98       catch(Exception e) {
99         throw new ServicingEventException
100          (ServicingExceptionMessage.PROBLEM_PARSING + e.getMessage()
101          + ServicingExceptionMessage.RETRY);
102      }
103    }
104  
105  
106    /**
107     * method for instantiating a ParmLevel2ValuesEvent demanding a service of
108     * updating parameter Level2 values
109     *
110     * @param request of MainServlet
111     * @return ParmLevelsNameEvent with the user request data
112     * @exception ServicingEventException
113     */
114    private ServicingEvent createUpdateParmLevel2ValuesEvent
115      (HttpServletRequest request)
116       throws ServicingEventException {
117  
118      ArrayList items = null;
119  
120      try {
121        // getting HTML parameters :
122        String xmlParmLevel2ValuesItems = request.getParameter("xmlParmLevel2ValuesItems").trim();
123        debug.println("xmlParmLevel2ValuesItems=" + xmlParmLevel2ValuesItems);
124        // parsing XML of xmlParmLevel2ValuesItems:
125        // resulting ArrayList has 3 elements of type String[]
126        // representing the attributes of items: code, name and status
127        items = parseXMLParmLevel2Values(xmlParmLevel2ValuesItems);
128  
129        // instantiating ParmLevel2ValuesEvent
130        ParmLevel2ValuesEvent event
131           = new ParmLevel2ValuesEvent
132          (ParmLevel2ValuesEvent.UPDATE_PARM_LEVEL2_VALUES,
133          (String)request.getSession().
134          getAttribute(WebKeys.CompanyId),
135          (Long)request.getSession().
136          getAttribute(WebKeys.UserId),
137          items);
138        request.setAttribute(WebKeys.ParmLevel2ValuesEvent, event);
139        return event;
140      }
141      catch(Exception e) {
142        throw new ServicingEventException
143          (ServicingExceptionMessage.PROBLEM_PARSING + e.getMessage()
144          + ServicingExceptionMessage.RETRY);
145      }
146    }
147  
148  
149    /**
150     *  auxiliary method for parsing a XML string containing items to update
151     *  values of parameter of Level 2
152     *
153     * @param xmlParmLevel2Values Description of the Parameter
154     * @return a ArrayList with 3 elements of type String[]
155     *  representing the attributes of items: code, name and status
156     * @exception Exception Description of the Exception
157     */
158  
159    private ArrayList parseXMLParmLevel2Values(String xmlParmLevel2Values) throws Exception {
160      XMLDocument xmlDoc = CommonUtil.parseInfo(xmlParmLevel2Values);
161  
162      NodeList nlCode = xmlDoc.selectNodes("/ParmLevel2ValuesList/ParmLevel2Values/code/text()");
163      NodeList nlName = xmlDoc.selectNodes("/ParmLevel2ValuesList/ParmLevel2Values/name/text()");
164      NodeList nlStatus = xmlDoc.selectNodes("/ParmLevel2ValuesList/ParmLevel2Values/status/text()");
165      int nlLength = nlCode.getLength();
166  
167      String[] code = new String[nlLength];
168      String[] name = new String[nlLength];
169      String[] status = new String[nlLength];
170      for(int i = 0; i < nlLength; i++) {
171        code[i] = nlCode.item(i).getNodeValue();
172        name[i] = nlName.item(i).getNodeValue();
173        status[i] = nlStatus.item(i).getNodeValue();
174        debug.println("code, name, status of item i="
175          + code[i] + "--" + name[i] + "--" + status[i]);
176      }
177  
178      ArrayList items = new ArrayList(3);
179      items.add(code);
180      items.add(name);
181      items.add(status);
182      return items;
183    }
184  }
185  
186