1    package com.instantbank.lettertemplate.setLetterJob.web;
2    
3    import java.io.*;
4    import java.net.*;
5    import java.util.*;
6    import java.text.SimpleDateFormat;
7    import javax.naming.*;
8    import javax.rmi.*;
9    import javax.servlet.http.HttpServletRequest;
10   import javax.servlet.http.HttpSession;
11   
12   import oracle.xml.parser.v2.*;
13   import org.w3c.dom.*;
14   import org.xml.sax.*;
15   import com.instantbank.lettertemplate.control.util.WebKeys;
16   import com.instantbank.component.job.model.JobSELECTelement;
17   import com.instantbank.component.job.model.JobWHEREelement;
18   import com.instantbank.component.job.model.JobORDERelement;
19   import com.instantbank.common.utilcomponents.CodeDescription;
20   import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
21   import com.instantbank.common.utilcomponents.Debug;
22   import com.instantbank.lettertemplate.control.web.handlers.JobWebImpl;
23   
24   
25   /**
26    *  Set Letter Job usecase: helper class for setLetterJobMain.jsp
27    *
28    * @author Instant-bank (Consuelo Franky)
29    * @created November 2002
30    */
31   public class SetLetterJobTranslator
32       implements Serializable {
33   
34     private long companyId;
35     private ArrayList jobList;
36   
37     // data of jobs list:
38     private String[] jobId;
39     private String[] jobName;
40   
41     // data of templates list:
42     private String[] templateId;
43     private String[] templateCategory;
44     private String[] templatePrintType;
45     private String[] templateName;
46   
47     // data of fields of a selected template:
48     private String[] selectedTemplateId;
49   
50     // data of locations list:
51     private String[] locationId;
52     private String[] locationName;
53   
54     // data of company fields:
55     private ArrayList companyFields;
56     private long[] companyFieldsId;
57   
58     // field menus:
59     private ArrayList fieldMenus;
60   
61     // current date in String:
62     private String todayString;
63   
64     // signales that a template for a new Job has been gotten:
65     private String newJobTemplate = "false";
66   
67     // signales that the current job is consistent with its template:
68     private String consistencyJobTemplate = "true";
69   
70     // others:
71     private Debug debug = null;
72   
73   
74     /**
75      * Constructor
76      *
77      * @param companyId Description of the Parameter
78      */
79     public SetLetterJobTranslator(Long companyId) {
80       debug = new Debug();
81       debug.setDebugginOn(true);
82       debug.setPreMessage("** SetLetterJobTranslator: ");
83   
84       this.companyId = companyId.longValue();
85   
86       // calculs current date in String:
87       SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
88       java.util.Date today = new java.util.Date();
89       todayString = sdf.format(today);
90     }
91   
92   
93     /**
94      *  gets data from request and creates input data for the jsp
95      *
96      * @param request of MainServlet
97      */
98     public void setData(HttpServletRequest request) {
99       debug.println("setData()**");
100      try {
101        HttpSession session = request.getSession();
102        int nlLength = 0;
103  
104        // gets WebKeys.LetterJobsList:
105        CodeDescription[] jobs = (CodeDescription[])
106          session.getAttribute(WebKeys.LetterJobsList);
107        nlLength = jobs.length;
108        jobId = new String[nlLength];
109        jobName = new String[nlLength];
110        for(int i = 0; i < nlLength; i++) {
111          jobId[i] = String.valueOf(jobs[i].getCode());
112          jobName[i] = jobs[i].getDescription();
113        }
114  
115        // gets WebKeys.LocationsList:
116        CodeDescription[] locs = (CodeDescription[])
117          session.getAttribute(WebKeys.LocationsList);
118        nlLength = locs.length;
119        locationId = new String[nlLength];
120        locationName = new String[nlLength];
121        for(int i = 0; i < nlLength; i++) {
122          locationId[i] = String.valueOf(locs[i].getCode());
123          locationName[i] = locs[i].getDescription();
124        }
125  
126        // gets WebKeys.CompanyFields:
127        companyFields = (ArrayList)session.getAttribute(WebKeys.CompanyFields);
128  
129        // gets WebKeys.FieldMenus:
130        fieldMenus = (ArrayList)session.getAttribute(WebKeys.FieldMenus);
131  
132        // gets WebKeys.TemplatesDescriptors:
133        ArrayList templatesDescriptors
134           = (ArrayList)session.getAttribute(WebKeys.TemplatesDescriptors);
135        nlLength = templatesDescriptors.size();
136  
137        templateId = new String[nlLength];
138        templateCategory = new String[nlLength];
139        templatePrintType = new String[nlLength];
140        templateName = new String[nlLength];
141  
142        for(int i = 0; i < nlLength; i++) {
143          ArrayList template = (ArrayList)templatesDescriptors.get(i);
144          templateId[i] = ((Long)template.get(0)).toString();
145          templateCategory[i] = (String)template.get(1);
146          templatePrintType[i] = (String)template.get(2);
147          templateName[i] = (String)template.get(3);
148        }
149  
150        // gets Webkeys.TemplateFields (if not null):
151        Long[] templateVarsCodes = (Long[])request.getAttribute
152          (WebKeys.TemplateFields);
153        if(templateVarsCodes != null) {
154          debug.println("templateVarsCodes is not null");
155          nlLength = templateVarsCodes.length;
156          selectedTemplateId = new String[nlLength];
157          for(int i = 0; i < nlLength; i++) {
158            selectedTemplateId[i] = (templateVarsCodes[i]).toString();
159          }
160  
161          JobWebImpl jobView
162             = (JobWebImpl)session.getAttribute(WebKeys.JobModelKey);
163  
164          String str = (String)request.getAttribute(WebKeys.NewJobTemplate);
165          if(str != null && str.equals("true")) {  // new job: cleans jobView
166            newJobTemplate = "true";
167            jobView.setJobWHERE(null);
168          }
169          else {  // old job: checks consistency between job and its template:
170            consistencyJobTemplate = "true";
171            debug.println("begin consistencyJobTemplate=" + consistencyJobTemplate);
172            ArrayList jobSELECT = (ArrayList)jobView.getJobSELECT();
173            int sizeSELECT = (jobSELECT != null) ? jobSELECT.size() : 0;
174            if(sizeSELECT != templateVarsCodes.length) {
175              consistencyJobTemplate = "false";
176              debug.println("consistencyJobTemplate set to false: sizeSELECT="
177                + sizeSELECT + "--templateVarsCodes.length=" + templateVarsCodes.length);
178            }
179            JobSELECTelement element;
180            for(int i = 0; consistencyJobTemplate.equals("true") && i < sizeSELECT; i++) {
181              element = (JobSELECTelement)jobSELECT.get(i);
182              long fieldId = element.getFieldId();
183              if(fieldId != Long.parseLong(selectedTemplateId[i])) {
184                consistencyJobTemplate = "false";
185                debug.println("consistencyJobTemplate set to false: fieldId="
186                  + fieldId + "--selectedTemplateId[i]=" + selectedTemplateId[i]);
187              }
188            }
189          }
190        }
191        debug.println("newJobTemplate=" + newJobTemplate);
192        debug.println("consistencyJobTemplate=" + consistencyJobTemplate);
193      }
194      catch(Exception e) {
195        debug.println("Exception unexpected:" + e.getMessage());
196      }
197    }
198  
199    // GETTER METHODS:
200  
201    public String getTodayString() {
202      return this.todayString;
203    }
204  
205  
206    public String getNewJobTemplate() {
207      return this.newJobTemplate;
208    }
209  
210  
211    public String getConsistencyJobTemplate() {
212      return this.consistencyJobTemplate;
213    }
214  
215  
216    public String getJobId(JobWebImpl jobView) {
217      if(jobView.getJobId() == null) {
218        return "-1";
219      }
220      else {
221        return (jobView.getJobId()).toString();
222      }
223    }
224  
225    // COMMON LISTS FOR ALL JOBS:
226  
227    /**
228     *  Adds the loadFields() javaScript method filling company fields array
229     *  according to input data of the jsp (it does not include SYSDATE field)
230     *  Also, assigns value to companyFieldsId attribute.
231     *
232     * @param request Description of the Parameter
233     * @return String with the javaScript text of  loadFields()
234     */
235    public String getCompanyFieldsArray(HttpServletRequest request) {
236      debug.println("getCompanyFieldsArray()");
237      // gets WebKeys.BodyLoadFields:
238      HttpSession session = request.getSession();
239      String bodyLoadFields = (String)session.getAttribute(WebKeys.BodyLoadFields);
240      if(bodyLoadFields != null) {
241        companyFieldsId = (long[])session.getAttribute(WebKeys.CompanyFieldsId);
242        debug.println("companyFieldsId[] and bodyLoadFields are taken from web session");
243        return bodyLoadFields;
244      }
245  
246      String body;
247      body = "function loadFields() {\n";
248      body += "fields = new Object();\n";
249      int sizeCompanyFields = companyFields.size();
250      companyFieldsId = new long[sizeCompanyFields];
251  
252      int i = 0;  // number of fields
253      for(i = 0; i < companyFields.size(); i++) {
254        ArrayList field = (ArrayList)companyFields.get(i);
255        long fieldId = ((Long)field.get(0)).longValue();
256        String displayName = (String)field.get(1);
257        String dataType = (String)field.get(2);
258        Long menuId = (Long)field.get(3);
259        String menuName = (String)field.get(4);
260        String offsetType = (String)field.get(5);
261  
262        companyFieldsId[i] = fieldId;
263        body += "fields[" + i + "] = new field("
264          + "'" + fieldId + "',"
265          + "'" + displayName + "',"
266          + "'" + dataType + "',"
267          + "'" + menuId + "',"
268          + "'" + menuName + "',"
269          + "'" + offsetType + "');\n";
270  
271      }
272      body += "numFields = " + i + ";\n";
273      body += "}";
274      request.getSession().setAttribute(WebKeys.BodyLoadFields, body);
275      request.getSession().setAttribute(WebKeys.CompanyFieldsId, companyFieldsId);
276      return body;
277    }
278  
279  
280    /**
281     *  Adds the loadMenus() javaScript method filling field menus array
282     *  according to input data of the jsp
283     *
284     * @param request Description of the Parameter
285     * @return String with the javaScript text of  loadFields()
286     */
287    public String getMenusArray(HttpServletRequest request) {
288      debug.println("getMenusArray()");
289      HttpSession session = request.getSession();
290      // gets WebKeys.BodyLoadMenus:
291      String bodyLoadMenus = (String)session.getAttribute(WebKeys.BodyLoadMenus);
292      if(bodyLoadMenus != null) {
293        return bodyLoadMenus;
294      }
295  
296      String body;
297  
298      body = "function loadMenus() {\n";
299      body += "var values;\n";
300      body += "menus = new Object();\n";
301  
302      int numMenus = fieldMenus.size();
303  
304      int i = 0;
305      for(i = 0; i < numMenus; i++) {
306        ArrayList menu = (ArrayList)fieldMenus.get(i);
307        String menuName = (String)menu.get(0);
308        body += "values = new Object();\n";
309        ArrayList menuValues = (ArrayList)menu.get(1);
310        int numValues = menuValues.size();
311        int k;
312        for(k = 0; k < numValues; k++) {
313          String[] value = (String[])menuValues.get(k);
314          String internalValue = value[0];
315          String display = value[1];
316          body += "values[" + k + "] = new value("
317            + "'" + internalValue + "',"
318            + "'" + display + "');\n";
319        }
320        body += "menus[" + i + "] = new menu("
321          + "'" + menuName + "',"
322          + k
323          + ",values);\n\n";
324      }
325      body += "numMenus = " + i + ";\n";
326      body += "}";
327      request.getSession().setAttribute(WebKeys.BodyLoadMenus, body);
328      return body;
329    }
330  
331  
332    /**
333     *  Extends the initialize() javaScript method initializing cmpLocationVector
334     *  array with locations of current company
335     *  according to input data of the jsp
336     *
337     * @param request Description of the Parameter
338     * @return String to append to onLoad()
339     */
340    public String getCompanyLocations(HttpServletRequest request) {
341      debug.println("getCompanyLocations()");
342  
343      String body = "var cmpLocationVector = new Array();\n";
344  
345      for(int i = 0; i < locationId.length; i++) {
346        body += "cmpLocationVector[" + i + "]= new Array();\n";
347        body += "cmpLocationVector[" + i + "][0]='"
348          + locationId[i]
349          + "';\n";
350        body += "cmpLocationVector[" + i + "][1]='"
351          + locationName[i]
352          + "';\n";
353      }
354      return body;
355    }
356  
357  
358    /**
359     *  Adds the loadJobList() javaScript method filling jobs array
360     *  according to input data of the jsp
361     *
362     * @param request Description of the Parameter
363     * @return String with the javaScript text of  loadJobList()
364     */
365    public String getJobsArray(HttpServletRequest request) {
366      debug.println("getJobsArray()");
367  
368      String body;
369      body = "function loadJobList() {\n";
370      body += "jobs = new Object();\n";
371      int sizeJobs = jobId.length;
372  
373      int i = 0;
374      for(i = 0; i < sizeJobs; i++) {
375        body += "jobs[" + i + "] = new job("
376          + "'" + jobId[i] + "',"
377          + "'" + jobName[i] + "');\n";
378      }
379      body += "numJobs = " + i + ";\n";
380      body += "}";
381      return body;
382    }
383  
384  
385    /**
386     *  Adds the loadTemplateList() javaScript method filling jobs array
387     *  according to input data of the jsp
388     *
389     * @param request Description of the Parameter
390     * @return String with the javaScript text of  loadTemplateList()
391     */
392    public String getTemplatesArray(HttpServletRequest request) {
393      debug.println("getTemplatesArray()");
394      HttpSession session = request.getSession();
395  
396      String body;
397      body = "function loadTemplateList() {\n";
398      body += "templates = new Object();\n";
399      int sizeTemplates = templateId.length;
400  
401      int i = 0;
402      for(i = 0; i < sizeTemplates; i++) {
403        body += "templates[" + i + "] = new template("
404          + "'" + templateId[i] + "',"
405          + "'" + templateCategory[i] + "',"
406          + "'" + templatePrintType[i] + "',"
407          + "'" + templateName[i] + "');\n";
408      }
409      body += "numTemplates = " + i + ";\n";
410      body += "}";
411      return body;
412    }
413  
414  
415    // COLLECTIONS OF CURRENT JOB:
416  
417    /**
418     *  Extends the initialize() javaScript method filling selectedFields array
419     *  with SELECT part of current job (if job is old)
420     *  or with fields of selected template (if job is new)
421     *  according to input data of the jsp
422     *
423     * @param jobSELECT Description of the Parameter
424     * @return String to append to onLoad()
425     */
426    public String getSELECTarray(ArrayList jobSELECT) {
427      debug.println("getSELECTarray");
428      String body = "selectedFields = new Object();\n";
429      int i = 0;
430  
431      if(newJobTemplate.equals("true")   // new Job
432      || consistencyJobTemplate.equals("false")) {  // old Job with its
433        //         template changed
434        for(i = 0; i < selectedTemplateId.length; i++) {
435          long fieldId = Long.parseLong(selectedTemplateId[i]);
436          int fieldPosition = getFieldPosition(fieldId);
437          body += "selectedFields[" + i + "] = " + fieldPosition + ";\n";
438        }
439      }
440      else {  // old job consistent with its template
441        JobSELECTelement element;
442        int sizeSELECT = (jobSELECT != null) ? jobSELECT.size() : 0;
443        for(i = 0; i < sizeSELECT; i++) {
444          element = (JobSELECTelement)jobSELECT.get(i);
445          long fieldId = element.getFieldId();
446          int fieldPosition = getFieldPosition(fieldId);
447          body += "selectedFields[" + i + "] = " + fieldPosition + ";\n";
448        }
449      }
450      body += "numSelectedFields = " + i + ";\n\n";
451      return body;
452    }
453  
454  
455    /**
456     *  Extends the initialize() javaScript method filling rules array
457     *  with WHERE part of current job
458     *  according to input data of the jsp
459     *
460     * @param jobWHERE Description of the Parameter
461     * @return String to append to onLoad()
462     */
463    public String getWHEREarray(ArrayList jobWHERE) {
464      debug.println("getWHEREarray");
465      JobWHEREelement element;
466      String body = "rules = new Object();\n";
467      int sizeWHERE = (jobWHERE != null) ? jobWHERE.size() : 0;
468      int i = 0;
469      for(i = 0; i < sizeWHERE; i++) {
470        element = (JobWHEREelement)jobWHERE.get(i);
471        long fieldId = element.getFieldId();
472        int fieldPosition = getFieldPosition(fieldId);
473        body += "rules[" + i + "] = new rule("
474          + "'" + element.getClause() + "',"
475          + "'" + fieldPosition + "',"
476          + "'" + operatorPosition(element.getRuleOperator()) + "',"
477          + "'" + element.getValue() + "',"
478          + "'" + connectorPosition(element.getConnector()) + "');\n";
479      }
480      body += "numRules = " + i + ";\n\n";
481      return body;
482    }
483  
484  
485    /**
486     *  Extends the initialize() javaScript method filling sort array
487     *  with ORDER part of current job
488     *  according to input data of the jsp
489     *
490     * @param jobORDER Description of the Parameter
491     * @return String to append to onLoad()
492     */
493    public String getORDERarray(ArrayList jobORDER) {
494      debug.println("getORDERarray");
495      String body = "sorts = new Object();\n";
496      int i = 0;
497  
498      if(newJobTemplate.equals("true")   // new Job;
499      || consistencyJobTemplate.equals("false")) {  // old Job with its
500        //     template changed
501        for(i = 0; i < selectedTemplateId.length; i++) {
502          long fieldId = Long.parseLong(selectedTemplateId[i]);
503          int fieldPosition = getFieldPosition(fieldId);
504          body += "sorts[" + i + "] = new sort("
505            + "'" + fieldPosition + "',"
506            + "'ascending',"
507            + "'yes');\n";
508        }
509      }
510      else {  // old job consitent with its template
511        JobORDERelement element;
512        int sizeORDER = (jobORDER != null) ? jobORDER.size() : 0;
513        for(i = 0; i < sizeORDER; i++) {
514          element = (JobORDERelement)jobORDER.get(i);
515          long fieldId = element.getFieldId();
516          int fieldPosition = getFieldPosition(fieldId);
517          body += "sorts[" + i + "] = new sort("
518            + "'" + fieldPosition + "',"
519            + "'" + element.getDirection() + "',"
520            + "'" + element.getUnicityMember() + "');\n";
521        }
522      }
523      body += "numSort = " + i + ";\n\n";
524      return body;
525    }
526  
527  
528    // AUXILIARY METHODS:
529  
530    /**
531     * Associates a position to each connector
532     *
533     * @param value Description of the Parameter
534     * @return Description of the Return Value
535     */
536    private String connectorPosition(String value) {
537      if(value.equals(LetterTemplateGlobals.EMPTY_CONNECTOR)) {
538        return "0";
539      }
540      if(value.equals(LetterTemplateGlobals.AND_CONNECTOR)) {
541        return "1";
542      }
543      if(value.equals(LetterTemplateGlobals.OR_CONNECTOR)) {
544        return "2";
545      }
546      if(value.equals(LetterTemplateGlobals.ORTHE_CONNECTOR)) {
547        return "3";
548      }
549      return "-1";
550    }
551  
552  
553    /**
554     * Finds the position of a fieldId in companyFieldsId array
555     *
556     * @param fieldId Description of the Parameter
557     * @return The fieldPosition value
558     */
559    private int getFieldPosition(long fieldId) {
560      int answer;
561      int i = 0;
562      while(i < companyFieldsId.length && companyFieldsId[i] != fieldId) {
563        i++;
564      }
565      answer = (i < companyFieldsId.length) ? i : -1;
566      return answer;
567    }
568  
569  
570    /**
571     * Associates a position to each operator
572     * (Note: IN operator is transformed in = for user interface)
573     *
574     * @param value Description of the Parameter
575     * @return Description of the Return Value
576     */
577    private String operatorPosition(String value) {
578      if(value.equals("=") || value.equals("IN")) {
579        return "0";
580      }
581      if(value.equals("BETWEEN")) {
582        return "1";
583      }
584      if(value.equals("<>")) {
585        return "2";
586      }
587      if(value.equals(">")) {
588        return "3";
589      }
590      if(value.equals(">=")) {
591        return "4";
592      }
593      if(value.equals("<")) {
594        return "5";
595      }
596      if(value.equals("<=")) {
597        return "6";
598      }
599      return "-1";
600    }
601  }
602  
603