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.ParmLevel1ValuesEvent;
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 Level1 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 ParmLevel1ValuesHandler extends RequestHandlerSupport {
31   
32     private Debug debug = null;
33     ServletContext context;
34   
35   
36     /**
37      *  principal method that process a user request instantiating a ParmLevel1ValuesEvent
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("** ParmLevel1ValuesHandler-application tier: ");
52   
53       this.context = context;
54       String action = request.getParameter("action");
55       debug.println("Creation of an ParmLevel1Values Event; "
56         + "ParmLevel1ValuesHandler (web): action=" + action);
57   
58       if(action == null) {
59         throw new ServicingEventException
60           (ServicingExceptionMessage.SERVICE_NOT_SELECTED);
61       }
62       else if(action.equals("listParmLevel1Values")) {
63         return createListParmLevel1ValuesEvent(request);
64       }
65       else if(action.equals("updateParmLevel1Values")) {
66         return createUpdateParmLevel1ValuesEvent(request);
67       }
68       return null;
69     }
70   
71   
72     /**
73      * method for instantiating a ParmLevel1ValuesEvent demanding a service of
74      * getting the parameter level1 values
75      *
76      * @param request of MainServlet
77      * @return ParmLevel1ValuesEvent with the user request data
78      * @exception ServicingEventException
79      */
80     private ServicingEvent createListParmLevel1ValuesEvent
81       (HttpServletRequest request)
82        throws ServicingEventException {
83   
84       try {
85   
86         // instantiating ParmLevel1ValuesEvent:
87         ParmLevel1ValuesEvent event
88            = new ParmLevel1ValuesEvent
89           (ParmLevel1ValuesEvent.LIST_PARM_LEVEL1_VALUES,
90           (String)request.getSession()
91           .getAttribute(WebKeys.CompanyId),
92           (Long)request.getSession().
93           getAttribute(WebKeys.UserId),
94           null);
95         request.setAttribute(WebKeys.ParmLevel1ValuesEvent, 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 ParmLevel1ValuesEvent demanding a service of
108     * updating parameter level1 values
109     *
110     * @param request of MainServlet
111     * @return ParmLevelsNameEvent with the user request data
112     * @exception ServicingEventException
113     */
114    private ServicingEvent createUpdateParmLevel1ValuesEvent
115      (HttpServletRequest request)
116       throws ServicingEventException {
117  
118      ArrayList items = null;
119      debug.println("****Entro al Update****");
120  
121      try {
122  
123        // getting HTML parameters :
124        String xmlParmLevel1ValuesItems = request.getParameter("xmlParmLevel1ValuesItems").trim();
125        debug.println("xmlParmLevel1ValuesItems=" + xmlParmLevel1ValuesItems);
126        // parsing XML of xmlParmLevel1ValuesItems:
127        // resulting ArrayList has 3 elements of type String[]
128        // representing the attributes of items: code, name and status
129        items = parseXMLParmLevel1Values(xmlParmLevel1ValuesItems);
130  
131        // instantiating ParmLevel1ValuesEvent
132        ParmLevel1ValuesEvent event
133           = new ParmLevel1ValuesEvent
134          (ParmLevel1ValuesEvent.UPDATE_PARM_LEVEL1_VALUES,
135          (String)request.getSession().
136          getAttribute(WebKeys.CompanyId),
137          (Long)request.getSession().
138          getAttribute(WebKeys.UserId),
139          items);
140        request.setAttribute(WebKeys.ParmLevel1ValuesEvent, event);
141        return event;
142      }
143      catch(Exception e) {
144        throw new ServicingEventException
145          (ServicingExceptionMessage.PROBLEM_PARSING + e.getMessage()
146          + ServicingExceptionMessage.RETRY);
147      }
148    }
149  
150  
151    /**
152     *  auxiliary method for parsing a XML string containing items to update
153     *  values of parameter of Level 1
154     *
155     * @param xmlParmLevel1Values Description of the Parameter
156     * @return a ArrayList with 3 elements of type String[]
157     *  representing the attributes of items: code, name and status
158     * @exception Exception Description of the Exception
159     */
160  
161    private ArrayList parseXMLParmLevel1Values(String xmlParmLevel1Values) throws Exception {
162      XMLDocument xmlDoc = CommonUtil.parseInfo(xmlParmLevel1Values);
163  
164      NodeList nlCode = xmlDoc.selectNodes("/ParmLevel1ValuesList/ParmLevel1Values/code/text()");
165      NodeList nlName = xmlDoc.selectNodes("/ParmLevel1ValuesList/ParmLevel1Values/name/text()");
166      NodeList nlStatus = xmlDoc.selectNodes("/ParmLevel1ValuesList/ParmLevel1Values/status/text()");
167      int nlLength = nlCode.getLength();
168  
169      String[] code = new String[nlLength];
170      String[] name = new String[nlLength];
171      String[] status = new String[nlLength];
172      for(int i = 0; i < nlLength; i++) {
173        code[i] = nlCode.item(i).getNodeValue();
174        name[i] = nlName.item(i).getNodeValue();
175        status[i] = nlStatus.item(i).getNodeValue();
176        debug.println("code, name, status of item i="
177          + code[i] + "--" + name[i] + "--" + status[i]);
178      }
179  
180      ArrayList items = new ArrayList(3);
181      items.add(code);
182      items.add(name);
183      items.add(status);
184      return items;
185    }
186  }
187  
188