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.CategoryEvent;
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 Categories usecase: handler of user requests in the Application layer
26    *
27    * @author Instant-bank (Consuelo Franky)
28    * @created August2002
29    */
30   public class CategoryHandler 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 CategoryEvent
39      *  with the user request data
40      *
41      * @param request of MainServlet
42      * @param context of MainServlet
43      * @return CategoryEvent with the user
44      *      request data
45      * @exception LetterTemplateEventException
46      */
47     public LetterTemplateEvent processRequest(HttpServletRequest request,
48                                               ServletContext context)
49        throws LetterTemplateEventException {
50       debug = new Debug();
51       debug.setDebugginOn(true);
52       debug.setPreMessage("** CategoryHandler-application tier: ");
53   
54       gotoDB = true;
55       this.context = context;
56       String action = request.getParameter("action");
57       debug.println("Creation of an Category Event; "
58         + "CategoryHandler (web): action=" + action);
59   
60       String companyId = (String)request.getSession()
61         .getAttribute(WebKeys.CompanyId);
62   
63       Hashtable categoryTable = CommonUtil.getWebContextVariable
64         (context, WebKeys.CategoryTable);
65       Object listCategories = categoryTable.get(companyId);
66   
67       if(listCategories != null && !listCategories.equals("")) {
68         gotoDB = false;
69       }
70   
71       if(action == null) {
72         throw new LetterTemplateEventException
73           (LetterTemplateExceptionMessage.SERVICE_NOT_SELECTED);
74       }
75       else if(action.equals("listCategories")) {
76         return createListCategoryEvent(request);
77       }
78       else if(action.equals("updateCategories")) {
79         return createUpdateCategoryEvent(request);
80       }
81       return null;
82     }
83   
84   
85     /**
86      *  method for instantiating a CategoryEvent demanding a service of getting the
87      *  categories list
88      *
89      * @param request of MainServlet
90      * @return CategoryEvent with the user request data
91      * @exception LetterTemplateEventException
92      */
93     private LetterTemplateEvent createListCategoryEvent
94       (HttpServletRequest request)
95        throws LetterTemplateEventException {
96       try {
97   
98         if(!gotoDB) {
99           request.setAttribute(WebKeys.ExistsCategoryList, "true");
100          return null;
101        }
102  
103        // instantiating CategoryEvent:
104        CategoryEvent event
105           = new CategoryEvent
106          (CategoryEvent.LIST_CATEGORIES,
107          (String)request.getSession()
108          .getAttribute(WebKeys.CompanyId),
109          (Long)request.getSession().
110          getAttribute(WebKeys.UserId),
111          null);
112        request.setAttribute(WebKeys.CategoryEvent, event);
113        return event;
114      }
115      catch(Exception e) {
116        throw new LetterTemplateEventException
117          (LetterTemplateExceptionMessage.PROBLEM_PARSING + e.getMessage()
118          + LetterTemplateExceptionMessage.RETRY);
119      }
120    }
121  
122  
123    /**
124     *  method for instantiating a CategoryEvent demanding a service of updating
125     *  several letters categories
126     *
127     * @param request of MainServlet
128     * @return CategoryEvent with the user request data
129     * @exception LetterTemplateEventException
130     */
131    private LetterTemplateEvent createUpdateCategoryEvent
132      (HttpServletRequest request)
133       throws LetterTemplateEventException {
134      ArrayList items = null;
135      try {
136        // getting HTML parameters :
137        String xmlCategoryItems = request.getParameter("xmlCategoryItems").trim();
138        // parsing XML of xmlCategoryItems:
139        // resulting ArrayList has 3 elements of type String[]
140        // representing the attributes of items: code, name and status
141        items = parseXMLCategories(xmlCategoryItems);
142        String code[] = (String[])(items.get(0));
143        if(code.length == 0 && !gotoDB) {
144          request.setAttribute(WebKeys.ExistsCategoryList, "true");
145          return null;
146        }
147  
148        // instantiating CategoryEvent
149        CategoryEvent event
150           = new CategoryEvent
151          (CategoryEvent.UPDATE_CATEGORIES,
152          (String)request.getSession().
153          getAttribute(WebKeys.CompanyId),
154          (Long)request.getSession().
155          getAttribute(WebKeys.UserId),
156          items);
157        request.setAttribute(WebKeys.CategoryEvent, event);
158        return event;
159      }
160      catch(Exception e) {
161        throw new LetterTemplateEventException
162          (LetterTemplateExceptionMessage.PROBLEM_PARSING + e.getMessage()
163          + LetterTemplateExceptionMessage.RETRY);
164      }
165    }
166  
167  
168    /**
169     *  auxiliary method for parsing a XML string containing items to update in
170     *  letters categories
171     *
172     * @param categoryItems is the XML string
173     * @return a ArrayList with 3 elements of type String[]
174     *      representing the attributes of items: code, name and status
175     * @exception Exception Description of the Exception
176     */
177    private ArrayList parseXMLCategories(String categoryItems) throws Exception {
178      XMLDocument xmlDoc = CommonUtil.parseInfo(categoryItems);
179  
180      NodeList nlCode = xmlDoc.selectNodes("/CategoriesList/Category/code/text()");
181      NodeList nlName = xmlDoc.selectNodes("/CategoriesList/Category/name/text()");
182      NodeList nlStatus = xmlDoc.selectNodes("/CategoriesList/Category/status/text()");
183      int nlLength = nlCode.getLength();
184  
185      String[] code = new String[nlLength];
186      String[] name = new String[nlLength];
187      String[] status = new String[nlLength];
188      for(int i = 0; i < nlLength; i++) {
189        code[i] = nlCode.item(i).getNodeValue();
190        name[i] = nlName.item(i).getNodeValue();
191        status[i] = nlStatus.item(i).getNodeValue();
192      }
193  
194      ArrayList items = new ArrayList(3);
195      items.add(code);
196      items.add(name);
197      items.add(status);
198      return items;
199    }
200  }
201  
202