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.TemplatesEvent;
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 Templates usecase: handler of user requests in the Application layer
26    *
27    * @author Instant-bank (Jorge Cardenas)
28    * @created August 2002
29    */
30   public class TemplatesHandler 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 TemplatesEvent
39      *  with the user request data
40      *
41      * @param request of MainServlet
42      * @param context of MainServlet
43      * @return TemplatesEvent 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("** TemplatesHandler-application tier: ");
52   
53       gotoDB = true;
54   
55       this.context = context;
56       String action = request.getParameter("action");
57       debug.println("Creation of an Templates Event; "
58         + "TemplatesHandler (web): action=" + action);
59   
60       String companyId = (String)request.getSession()
61         .getAttribute(WebKeys.CompanyId);
62   
63       Hashtable templateTable = CommonUtil.getWebContextVariable
64         (context, WebKeys.TemplatesTable);
65       Object listTemplates = templateTable.get(companyId);
66   
67       Hashtable categoryTable = CommonUtil.getWebContextVariable
68         (context, WebKeys.CategoryTable);
69       Object listCategories = categoryTable.get(companyId);
70   
71       if((listTemplates != null && !listTemplates.equals(""))
72         && listCategories != null) {
73         gotoDB = false;
74       }
75   
76       if(action == null) {
77         throw new LetterTemplateEventException
78           (LetterTemplateExceptionMessage.SERVICE_NOT_SELECTED);
79       }
80       else if(action.equals("listTemplates")) {
81         return createListTemplatesEvent(request);
82       }
83       else if(action.equals("updateTemplates")) {
84         return createUpdateTemplatesEvent(request);
85       }
86       return null;
87     }
88   
89   
90     /**
91      *  method for instantiating a TemplatesEvent demanding a service of getting
92      *  the templates list
93      *
94      * @param request of MainServlet
95      * @return TemplatesEvent with the user request data
96      * @exception LetterTemplateEventException
97      */
98     private LetterTemplateEvent createListTemplatesEvent
99       (HttpServletRequest request)
100       throws LetterTemplateEventException {
101  
102      try {
103        if(!gotoDB) {
104          request.setAttribute(WebKeys.ExistsTemplatesList, "true");
105          return null;
106        }
107        // instantiating TemplatesEvent:
108        TemplatesEvent event
109           = new TemplatesEvent
110          (TemplatesEvent.LIST_TEMPLATES_NAMES,
111          (String)request.getSession()
112          .getAttribute(WebKeys.CompanyId),
113          (Long)request.getSession().
114          getAttribute(WebKeys.UserId),
115          null);
116        request.setAttribute(WebKeys.TemplatesEvent, event);
117        return event;
118      }
119      catch(Exception e) {
120        throw new LetterTemplateEventException
121          (LetterTemplateExceptionMessage.PROBLEM_PARSING + e.getMessage()
122          + LetterTemplateExceptionMessage.RETRY);
123      }
124    }
125  
126  
127    /**
128     *  method for instantiating a TemplatesEvent demanding a service of updating
129     *  several letters templates
130     *
131     * @param request of MainServlet
132     * @return TemplatesEvent with the user request data
133     * @exception LetterTemplateEventException
134     */
135    private LetterTemplateEvent createUpdateTemplatesEvent
136      (HttpServletRequest request)
137       throws LetterTemplateEventException {
138      ArrayList items = null;
139      try {
140        // getting HTML parameters :
141        String xmlTemplateItems = request.getParameter("xmlTemplateItems").trim();
142        debug.println("xmlTemplateItems=" + xmlTemplateItems);
143        // parsing XML of xmlTemplateItems:
144        // resulting ArrayList has 4 elements of type String[]
145        // representing the attributes of items: code of template,
146        // code of category, name and status
147  
148        items = parseXMLTemplates(xmlTemplateItems);
149  
150        String code[] = (String[])(items.get(0));
151  
152        if(code.length == 0 && !gotoDB) {
153          request.setAttribute(WebKeys.ExistsTemplatesList, "true");
154          return null;
155        }
156        // instantiating TemplatesEvent in anycase, even for 0 items
157        // because the template list may be changed by editor usecase
158        TemplatesEvent event
159           = new TemplatesEvent
160          (TemplatesEvent.UPDATE_TEMPLATES_NAMES,
161          (String)request.getSession().
162          getAttribute(WebKeys.CompanyId),
163          (Long)request.getSession().
164          getAttribute(WebKeys.UserId),
165          items);
166        request.setAttribute(WebKeys.TemplatesEvent, event);
167        return event;
168      }
169      catch(Exception e) {
170        throw new LetterTemplateEventException
171          (LetterTemplateExceptionMessage.PROBLEM_PARSING + e.getMessage()
172          + LetterTemplateExceptionMessage.RETRY);
173      }
174    }
175  
176  
177    /**
178     *  auxiliary method for parsing a XML string containing items to update in
179     *  letters templates
180     *
181     * @param templateItems is the XML string
182     * @return a ArrayList with 4 elements of type String[]
183     *      representing the attributes of items: code of template, code of
184     *      category, name and status
185     * @exception Exception
186     */
187    private ArrayList parseXMLTemplates(String templateItems) throws Exception {
188      XMLDocument xmlDoc = CommonUtil.parseInfo(templateItems);
189  
190      NodeList nlCodeT = xmlDoc.selectNodes("/TemplatesList/Template/codeTemplate/text()");
191      NodeList nlCodeC = xmlDoc.selectNodes("/TemplatesList/Template/codeCategory/text()");
192      NodeList nlName = xmlDoc.selectNodes("/TemplatesList/Template/name/text()");
193      NodeList nlStatus = xmlDoc.selectNodes("/TemplatesList/Template/status/text()");
194      int nlLength = nlCodeT.getLength();
195  
196      String[] codeTemplate = new String[nlLength];
197      String[] codeCategory = new String[nlLength];
198      String[] name = new String[nlLength];
199      String[] status = new String[nlLength];
200      debug.println("******Entro parseXMLTempaltes******");
201      for(int i = 0; i < nlLength; i++) {
202        codeTemplate[i] = nlCodeT.item(i).getNodeValue();
203        codeCategory[i] = nlCodeC.item(i).getNodeValue();
204        name[i] = nlName.item(i).getNodeValue();
205        status[i] = nlStatus.item(i).getNodeValue();
206        debug.println("codeTemplate, codeCategory, name, status of item i="
207          + codeTemplate[i] + "--" + codeCategory[i]
208          + "--" + name[i] + "--" + status[i]);
209      }
210  
211      ArrayList items = new ArrayList(4);
212      items.add(codeTemplate);
213      items.add(codeCategory);
214      items.add(name);
215      items.add(status);
216      return items;
217    }
218  }
219  
220