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.http.HttpSession;
10   import javax.servlet.ServletContext;
11   
12   import oracle.xml.parser.v2.*;
13   import org.w3c.dom.*;
14   import org.xml.sax.*;
15   
16   import com.instantbank.servicing.control.event.SetParameterXValueEvent;
17   import com.instantbank.servicing.control.util.JSPUtil;
18   import com.instantbank.servicing.control.util.WebKeys;
19   import com.instantbank.common.utilcomponents.Debug;
20   import com.instantbank.common.utilcomponents.ServicingExceptionMessage;
21   import com.instantbank.servicing.control.ServicingEventException;
22   import com.instantbank.servicing.control.event.ServicingEvent;
23   import com.instantbank.component.parameter.util.*;
24   
25   /**
26    * Set Parameter Value usecase: handler of user requests in the Application layer
27    *
28    * @author Instant-bank (Jorge Cardenas)
29    * @created October 2002
30    */
31   public class SetParameterXValueHandler extends RequestHandlerSupport {
32   
33     private Debug debug = null;
34     ServletContext context;
35   
36   
37     /**
38      *  principal method that process a user request instantiating a SetParameterXValueEvent
39      *  with the user request data
40      *
41      * @param request of MainServlet
42      * @param context of MainServlet
43      * @return SetParameterXValueEvent with the user 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("** SetParameterXValueHandler-application tier: ");
52   
53       HttpSession session = request.getSession();
54   
55       this.context = context;
56       String action = request.getParameter("action");
57       debug.println("Creation of an SetParameterXValueEvent Event; "
58         + "SetParameterXValueHandler (web): action=" + action);
59   
60       if(action == null) {
61         throw new ServicingEventException
62           (ServicingExceptionMessage.SERVICE_NOT_SELECTED);
63       }
64       else if(action.equals("listParameterValues")) {
65         createListSetParametersValues(request);
66       }
67       else if(action.equals("listParameterXValues")) {
68         return createListSetParameterValueEvent(request);
69       }
70       else if(action.equals("updateParameterValues")) {
71         return createUpdateSetParameterValueEvent(request);
72       }
73       else if(action.equals("verifyParameterValues")) {
74         return createVerifySetParameterValueEvent(request);
75       }
76       return null;
77     }
78   
79   
80     /**
81      * method for the list of parameters.
82      *
83      * @param request of MainServlet
84      */
85     private void createListSetParametersValues
86       (HttpServletRequest request) {
87       request.setAttribute
88         (WebKeys.SetParametersValues, "list");
89     }
90   
91   
92     /**
93      * method for instantiating a SetParameterXValueEvent demanding a service of
94      * getting the parameter values
95      *
96      * @param request of MainServlet
97      * @return SetParameterXValueEvent with the user request data
98      * @exception ServicingEventException
99      */
100    private ServicingEvent createListSetParameterValueEvent
101      (HttpServletRequest request)
102       throws ServicingEventException {
103  
104      String codeParameter = request.getParameter("codeParam");
105      request.setAttribute
106        (WebKeys.SetParametersValues, "nolist");
107      try {
108  
109        // instantiating SetParameterXValueEvent:
110        SetParameterXValueEvent event
111           = new SetParameterXValueEvent
112          (SetParameterXValueEvent.LIST_PARAM_XVALUE,
113          (String)request.getSession()
114          .getAttribute(WebKeys.CompanyId),
115          (Long)request.getSession().
116          getAttribute(WebKeys.UserId),
117          codeParameter,
118          null, null);
119  
120        request.setAttribute(WebKeys.SetParameterXValueEvent, event);
121        return event;
122      }
123      catch(Exception e) {
124        throw new ServicingEventException
125          (ServicingExceptionMessage.PROBLEM_PARSING + e.getMessage()
126          + ServicingExceptionMessage.RETRY);
127      }
128    }
129  
130  
131    /**
132     * method for instantiating a SerParameterXValueEvent demanding a service of
133     * updating parameter restrictions
134     *
135     * @param request of MainServlet
136     * @return SerParameterXValueEvent with the user request data
137     * @exception ServicingEventException
138     */
139    private ServicingEvent createUpdateSetParameterValueEvent
140      (HttpServletRequest request)
141       throws ServicingEventException {
142  
143      ArrayList point1 = new ArrayList();
144      ArrayList point2 = new ArrayList();
145  
146      String codeParam = request.getParameter("code");
147      String typeParam = request.getParameter("type");
148  
149      HttpSession session = request.getSession();
150  
151      point1 = (ArrayList)session.getAttribute(WebKeys.SetParmXValuePoint1);
152      point2 = (ArrayList)session.getAttribute(WebKeys.SetParmXValuePoint2);
153  
154      Hashtable result = initValuesTable(point1, point2);
155  
156      request.setAttribute
157        (WebKeys.SetParametersValues, "nolist");
158  
159      try {
160        int j = 0;
161        int index;
162        int index1;
163        Enumeration parameters = request.getParameterNames();
164        while((parameters != null) && parameters.hasMoreElements()) {
165          String parameterName = new String(((String)parameters.nextElement()).trim());
166          if(!parameterName.equals("action") && !parameterName.equals("code")
167            && !parameterName.equals("type")) {
168  
169            String parameterValue = request.getParameter(parameterName);
170            String punto_1 = parameterName.substring(0, parameterName.indexOf("_"));
171            punto_1 = punto_1.replace('Ü', ' ');
172            String temp = parameterName.substring(parameterName.indexOf("_") + 1, parameterName.length());
173            String punto_2 = temp.substring(0, temp.indexOf("_"));
174            punto_2 = punto_2.replace('Ü', ' ');
175            String label = temp.substring(temp.indexOf("_") + 1, temp.length());
176  
177            //If parameterName end with: [1_2 | 1_ANY | ANY_2 | ANY_ANY]
178            if(parameterName.indexOf('Ö') == -1) {
179              if(parameterName.endsWith("1_2")) {
180                result.put(punto_1 + "|" + punto_2, parameterValue);
181  
182              }
183              else if(parameterName.endsWith("1_any")) {
184                result.put(punto_1 + "|" + "*", parameterValue);
185  
186              }
187              else if(parameterName.endsWith("any_2")) {
188                result.put("*" + "|" + punto_2, parameterValue);
189  
190              }
191              else if(parameterName.endsWith("any_any")) {
192                result.put("*" + "|" + "*", parameterValue);
193              }
194            }
195            else if(parameterName.indexOf('Ö') != -1) {
196              String paramTemp = parameterName.substring(0, parameterName.indexOf('Ö'));
197              String parmStr = parameterName.substring(parameterName.indexOf('Ö') + 1,
198                parameterName.length());
199              parmStr = parmStr.replace('Ü', ' ');
200              parmStr = parmStr.replace('Ä', '+');
201              parmStr = parmStr.replace('ÿ', '/');
202  
203              if(paramTemp.endsWith("1_2")) {
204                String obj = (String)result.get(punto_1 + "|" + punto_2);
205                if(obj.equals("undefined")) {
206                  obj = parmStr;
207                  result.put(punto_1 + "|" + punto_2, obj);
208                }
209                else if(!obj.equals("undefined")) {
210                  obj = obj + "," + parmStr;
211                  result.put(punto_1 + "|" + punto_2, obj);
212                }
213              }
214              else if(paramTemp.endsWith("1_any")) {
215                String obj = (String)result.get(punto_1 + "|" + "*");
216                if(obj.equals("undefined")) {
217                  obj = parmStr;
218                  result.put(punto_1 + "|" + "*", obj);
219                }
220                else if(!obj.equals("undefined")) {
221                  obj = obj + "," + parmStr;
222                  result.put(punto_1 + "|" + "*", obj);
223                }
224              }
225              else if(paramTemp.endsWith("any_2")) {
226                String obj = (String)result.get("*" + "|" + punto_2);
227                if(obj.equals("undefined")) {
228                  obj = parmStr;
229                  result.put("*" + "|" + punto_2, obj);
230                }
231                else if(!obj.equals("undefined")) {
232                  obj = obj + "," + parmStr;
233                  result.put("*" + "|" + punto_2, obj);
234                }
235              }
236              else if(paramTemp.endsWith("any_any")) {
237                String obj = (String)result.get("*" + "|" + "*");
238                if(obj.equals("undefined")) {
239                  obj = parmStr;
240                  result.put("*" + "|" + "*", obj);
241                }
242                else if(!obj.equals("undefined")) {
243                  obj = obj + "," + parmStr;
244                  result.put("*" + "|" + "*", obj);
245                }
246              }
247            }
248          }
249        }
250        // Order the object string in a Hashtable for paramterType = "set"
251  
252        if(typeParam.equals("set")) {
253          result = orderStringInHashTable(result);
254        }
255  
256        // instantiating SetParameterXValueEvent
257        ParameterValue parmValue = new ParameterValue(codeParam, typeParam, result);
258  
259        SetParameterXValueEvent event
260           = new SetParameterXValueEvent
261          (SetParameterXValueEvent.UPDATE_PARAM_XVALUE,
262          (String)request.getSession().
263          getAttribute(WebKeys.CompanyId),
264          (Long)request.getSession().
265          getAttribute(WebKeys.UserId),
266          codeParam,
267          null, parmValue);
268        request.setAttribute(WebKeys.SetParameterXValueEvent, event);
269        return event;
270      }
271      catch(Exception e) {
272        throw new ServicingEventException
273          (ServicingExceptionMessage.PROBLEM_PARSING + e.getMessage()
274          + ServicingExceptionMessage.RETRY);
275      }
276    }
277  
278  
279    /**
280     * method for instantiating a SerParameterXValueEvent demanding a service of
281     * verify parameter restrictions
282     *
283     * @param request of MainServlet
284     * @return SerParameterXValueEvent with the user request data
285     * @exception ServicingEventException
286     */
287    private ServicingEvent createVerifySetParameterValueEvent
288      (HttpServletRequest request)
289       throws ServicingEventException {
290  
291      ArrayList point1 = new ArrayList();
292      ArrayList point2 = new ArrayList();
293  
294      String codeParam = request.getParameter("code");
295      String typeParam = request.getParameter("type");
296  
297      HttpSession session = request.getSession();
298  
299      point1 = (ArrayList)session.getAttribute(WebKeys.SetParmXValuePoint1);
300      point2 = (ArrayList)session.getAttribute(WebKeys.SetParmXValuePoint2);
301  
302      Hashtable result = initValuesTable(point1, point2);
303  
304      request.setAttribute
305        (WebKeys.SetParametersValues, "nolist");
306  
307      try {
308        int j = 0;
309        int index;
310        int index1;
311        Enumeration parameters = request.getParameterNames();
312        while((parameters != null) && parameters.hasMoreElements()) {
313          String parameterName = new String(((String)parameters.nextElement()).trim());
314          if(!parameterName.equals("action") && !parameterName.equals("code")
315            && !parameterName.equals("type")) {
316  
317            String parameterValue = request.getParameter(parameterName);
318            String punto_1 = parameterName.substring(0, parameterName.indexOf("_"));
319            punto_1 = punto_1.replace('Ü', ' ');
320            String temp = parameterName.substring(parameterName.indexOf("_") + 1, parameterName.length());
321            String punto_2 = temp.substring(0, temp.indexOf("_"));
322            punto_2 = punto_2.replace('Ü', ' ');
323            String label = temp.substring(temp.indexOf("_") + 1, temp.length());
324  
325            //If parameterName end with: [1_2 | 1_ANY | ANY_2 | ANY_ANY]
326            if(parameterName.indexOf('Ö') == -1) {
327              if(parameterName.endsWith("1_2")) {
328                result.put(punto_1 + "|" + punto_2, parameterValue);
329  
330              }
331              else if(parameterName.endsWith("1_any")) {
332                result.put(punto_1 + "|" + "*", parameterValue);
333  
334              }
335              else if(parameterName.endsWith("any_2")) {
336                result.put("*" + "|" + punto_2, parameterValue);
337  
338              }
339              else if(parameterName.endsWith("any_any")) {
340                result.put("*" + "|" + "*", parameterValue);
341              }
342            }
343            else if(parameterName.indexOf('Ö') != -1) {
344              String paramTemp = parameterName.substring(0, parameterName.indexOf('Ö'));
345              String parmStr = parameterName.substring(parameterName.indexOf('Ö') + 1,
346                parameterName.length());
347              parmStr = parmStr.replace('Ü', ' ');
348              parmStr = parmStr.replace('Ä', '+');
349              parmStr = parmStr.replace('ÿ', '/');
350  
351              if(paramTemp.endsWith("1_2")) {
352                String obj = (String)result.get(punto_1 + "|" + punto_2);
353                if(obj.equals("undefined")) {
354                  obj = parmStr;
355                  result.put(punto_1 + "|" + punto_2, obj);
356                }
357                else {
358                  obj = obj + "," + parmStr;
359                  result.put(punto_1 + "|" + punto_2, obj);
360                }
361              }
362              else if(paramTemp.endsWith("1_any")) {
363                String obj = (String)result.get(punto_1 + "|" + "*");
364                if(obj.equals("undefined")) {
365                  obj = parmStr;
366                  result.put(punto_1 + "|" + "*", obj);
367                }
368                else {
369                  obj = obj + "," + parmStr;
370                  result.put(punto_1 + "|" + "*", obj);
371                }
372              }
373              else if(paramTemp.endsWith("any_2")) {
374                String obj = (String)result.get("*" + "|" + punto_2);
375                if(obj.equals("undefined")) {
376                  obj = parmStr;
377                  result.put("*" + "|" + punto_2, obj);
378                }
379                else {
380                  obj = obj + "," + parmStr;
381                  result.put("*" + "|" + punto_2, obj);
382                }
383              }
384              else if(paramTemp.endsWith("any_any")) {
385                String obj = (String)result.get("*" + "|" + "*");
386                if(obj.equals("undefined")) {
387                  obj = parmStr;
388                  result.put("*" + "|" + "*", obj);
389                }
390                else {
391                  obj = obj + "," + parmStr;
392                  result.put("*" + "|" + "*", obj);
393                }
394              }
395            }
396          }
397        }
398        // Order the object string in a Hashtable for paramterType = "set"
399  
400        if(typeParam.equals("set")) {
401          result = orderStringInHashTable(result);
402        }
403  
404        // instantiating SetParameterXValueEvent
405        ParameterValue parmValue = new ParameterValue(codeParam, typeParam, result);
406  
407        SetParameterXValueEvent event
408           = new SetParameterXValueEvent
409          (SetParameterXValueEvent.VERIFY_PARAM_XVALUE,
410          (String)request.getSession().
411          getAttribute(WebKeys.CompanyId),
412          (Long)request.getSession().
413          getAttribute(WebKeys.UserId),
414          codeParam,
415          null, parmValue);
416        request.setAttribute(WebKeys.SetParameterXValueEvent, event);
417        return event;
418      }
419      catch(Exception e) {
420        throw new ServicingEventException
421          (ServicingExceptionMessage.PROBLEM_PARSING + e.getMessage()
422          + ServicingExceptionMessage.RETRY);
423      }
424    }
425  
426  
427    /**
428     *auxiliary method for build a HashTable with default values "undefined"
429     *
430     * @param point1 Description of the Parameter
431     * @param point2 Description of the Parameter
432     * @return Hashtable initialized
433     */
434    private Hashtable initValuesTable(ArrayList point1, ArrayList point2) {
435  
436      Hashtable result = new Hashtable();
437      int i;
438      int j;
439  
440      // hashtable initialized with undefined in 1_2
441      for(i = 0; i < point1.size(); i++) {
442        for(j = 0; j < point2.size(); j++) {
443          String valuePoint1 = (String)point1.get(i);
444          String valuePoint2 = (String)point2.get(j);
445          result.put(valuePoint1 + "|" + valuePoint2, "undefined");
446        }
447      }
448  
449      // hashtable initialized with undefined in 1_any in addition
450      // to 1_2
451      for(i = 0; i < point1.size(); i++) {
452        String valuePoint1 = (String)point1.get(i);
453        result.put(valuePoint1 + "|" + "*", "undefined");
454      }
455  
456      // hashtable initialized with undefined in any_2 in addition
457      // to 1_2, 1_any
458      for(j = 0; j < point2.size(); j++) {
459        String valuePoint2 = (String)point2.get(j);
460        result.put("*" + "|" + valuePoint2, "undefined");
461      }
462  
463      // hashtable initialized with undefined in any_any in addition
464      // to 1_2, 1_any, any_2
465      result.put("*" + "|" + "*", "undefined");
466  
467      return result;
468    }
469  
470  
471    /**
472     *auxiliary method for order values (String) in a Hashtable.
473     *
474     * @param parmTable table initialized with values of the form.
475     * @return Hashtable ordered.
476     */
477    private Hashtable orderStringInHashTable(Hashtable parmTable) {
478  
479      for(Enumeration ee = parmTable.keys(); ee.hasMoreElements(); ) {
480        String code = (String)ee.nextElement();
481        String element = (String)parmTable.get(code);
482        String temp = orderString(element);
483        parmTable.put(code, temp);
484      }
485  
486      return parmTable;
487    }
488  
489  
490    /**
491     *auxiliary method for order strings separated with ','.
492     *
493     * @param value String obtained of a Hashtable.
494     * @return result String ordered.
495     */
496    public String orderString(String value) {
497  
498      int i;
499  
500      int pos = 0;
501      int num = 0;
502  
503      for(i = 0; i < value.length(); i++) {
504        if(value.charAt(i) == ',') {
505          num = num + 1;
506        }
507      }
508  
509      if(num == 0) {
510        return value;
511      }
512      else {
513  
514        String[] arrayValues = new String[num + 1];
515  
516        // Convert a string separated by ',' in an array for
517        // to order.
518        for(i = 0; i < arrayValues.length; i++) {
519          pos = value.indexOf(",");
520          if(pos != -1) {
521            arrayValues[i] = value.substring(0, pos);
522            value = value.substring(pos + 1, value.length());
523          }
524          else if(pos == -1) {
525            arrayValues[i] = value;
526          }
527        }
528  
529        //sort array of strings
530        java.util.Arrays.sort(arrayValues);
531  
532        String[] vec = new String[arrayValues.length];
533  
534        int j;
535  
536        int k = 0;
537  
538        //delete one more occurrence of the same string.
539        for(i = 0, j = 1; i < arrayValues.length - 1; i++, j++) {
540          if(i == 0) {
541            vec[k] = arrayValues[i];
542            k++;
543          }
544          if(!arrayValues[i].equals(arrayValues[j])) {
545            vec[k] = arrayValues[j];
546            k++;
547          }
548        }
549  
550        String result = "";
551        //Convert every position of the array strings in one
552        // string separated by ',' and ordered.
553        for(i = 0; i < vec.length; i++) {
554          if(vec[i] != null) {
555            result = result + vec[i] + ',';
556          }
557        }
558        result = result.substring(0, result.lastIndexOf(','));
559  
560        return result;
561      }
562    }
563  }
564  
565