1    package com.instantbank.collections.commonQueuing.web;
2    
3    import java.io.ByteArrayInputStream;
4    import java.io.IOException;
5    import java.util.Hashtable;
6    import oracle.xml.parser.v2.DOMParser;
7    import oracle.xml.parser.v2.XMLDocument;
8    import oracle.xml.parser.v2.XMLNode;
9    import oracle.xml.parser.v2.XMLParseException;
10   import oracle.xml.parser.v2.XSLException;
11   import org.w3c.dom.Element;
12   import org.w3c.dom.NodeList;
13   import org.xml.sax.SAXException;
14   import com.instantbank.collections.commonQueuing.ejb.QueueServices;
15   import com.instantbank.collections.commonQueuing.ejb.QueueServicesHome;
16   import com.instantbank.collections.security.ejb.SecurityServices;
17   import com.instantbank.collections.security.ejb.SecurityServicesHome;
18   import com.instantbank.collections.util.InstantbankException;
19   import com.instantbank.collections.util.ServiceLocator;
20   import com.instantbank.collections.util.StringFormat;
21   
22   class Field {
23     public String tableName;
24     public String tableAlias;
25     public String displayName;
26     public String name;
27     public String type;
28     public String menu;
29   }
30   
31   class GroupItem {
32     public String id;
33     public int level;
34     public String name;
35   
36   
37     public GroupItem(String id, int level, String name) {
38       this.id = id;
39       this.level = level;
40       this.name = name;
41     }
42   }
43   
44   public class FieldsTransformer {
45   
46   
47     private Long companyId;
48     private Hashtable fieldsTable;
49     private Hashtable groupsUserTable;
50     private String objectType;
51     private QueueServices queueServices;
52     private int vectorPosition = 0;
53   
54   
55     public FieldsTransformer(Long companyId, String objectType) throws InstantbankException {
56       this.companyId = companyId;
57       this.objectType = objectType;
58       init();
59       initUsers();
60     }
61   
62   
63     private String connectorPosition(String value) {
64       if(value.equals("")) {
65         return "0";
66       }
67       if(value.equals("AND")) {
68         return "1";
69       }
70       if(value.equals("OR")) {
71         return "2";
72       }
73       if(value.equals("OR THE")) {
74         return "3";
75       }
76       return "0";
77     }
78   
79   
80     public String getCategoryOptions() {
81       DOMParser docParser = new DOMParser();
82       XMLDocument inxml;
83       XMLNode node;
84       NodeList nodeList;
85       String queueSelect = "";
86       Element root;
87       ByteArrayInputStream stream = null;
88       String xmlString;
89   
90       try {
91         QueueServicesHome home = (QueueServicesHome)ServiceLocator.instance().createEJB("QueueServicesHome", QueueServicesHome.class, false);
92         QueueServices queueServices = home.create();
93         xmlString = queueServices.getCategories(objectType);
94         stream = new ByteArrayInputStream(xmlString.getBytes());
95         docParser.setValidationMode(false);
96         docParser.parse(stream);
97         inxml = docParser.getDocument();
98         root = inxml.getDocumentElement();
99         nodeList = root.getChildNodes();
100        for(int i = 0; i < nodeList.getLength(); i++) {
101          node = (XMLNode)nodeList.item(i);
102          queueSelect += "<option value='" + node.valueOf("./categories") + "'>" + node.valueOf("./name") + "</option>";
103        }
104      }
105      catch(Exception e) {
106      }
107      return queueSelect;
108    }
109  
110  
111    private int getFieldPosition(XMLNode node) throws XSLException {
112      Field f;
113      boolean fieldFound;
114      int fieldPosition = -1;
115  
116      for(int j = 0; j < fieldsTable.size(); j++) {
117        f = (Field)fieldsTable.get(new Integer(j));
118        fieldFound = true;
119        if(!f.name.equals(node.valueOf("./fieldname"))) {
120          fieldFound = false;
121        }
122        if(!f.tableAlias.equals(node.valueOf("./tablealias"))) {
123          fieldFound = false;
124        }
125        if(fieldFound) {
126          fieldPosition = j;
127          break;
128        }
129      }
130      return fieldPosition;
131    }
132  
133  
134    public String getFieldsArray() {
135  
136      Field f;
137      String fieldsArray = "";
138      int i = 0;
139  
140      fieldsArray = "function loadFields() {\n";
141      fieldsArray += "fields = new Object();\n";
142      try {
143        for(i = 0; i < fieldsTable.size(); i++) {
144          f = (Field)fieldsTable.get(new Integer(i));
145          fieldsArray += "fields[" + i + "] = new field('" + f.tableName + "','" + f.tableAlias + "',";
146          fieldsArray += "'" + f.name + "','" + f.displayName + "',";
147          fieldsArray += "'" + f.type + "','" + f.menu + "');\n";
148        }
149        fieldsArray += "numFields = " + i + ";\n";
150        fieldsArray += "}";
151      }
152      catch(Exception e) {
153        fieldsArray += "/* Error: " + e.getClass().getName() + " " + e.getMessage() + " */\n";
154        fieldsArray += "numFields = 0\n";
155        fieldsArray += "}";
156      }
157      return fieldsArray;
158    }
159  
160  
161    public String getGroupsUserArray() {
162      GroupItem item;
163      String usersArray;
164      int i = 0;
165  
166      usersArray = "";
167      try {
168        for(i = 0; i < groupsUserTable.size(); i++) {
169          item = (GroupItem)groupsUserTable.get(new Integer(i));
170          usersArray += "groupsUsers[" + i + "] = new groupUser('" + item.id + "'," + item.level + ",'" + StringFormat.toSafeJavaString(item.name) + "',false);\n";
171        }
172        usersArray += "numUsers = " + i + ";\n";
173      }
174      catch(Exception e) {
175        usersArray += "/* Error: " + e.getClass().getName() + " " + e.getMessage() + " */\n";
176        usersArray += "numUsers = 0\n";
177      }
178      return usersArray;
179    }
180  
181  
182    public String getMenus() {
183      DOMParser docParser = new DOMParser();
184      Hashtable ht = new Hashtable();
185      String htObject;
186      String htObjectValues;
187      Hashtable htValues = new Hashtable();
188      int i = 0;
189      XMLDocument inxml;
190      String menuArray = "";
191      NodeList nodeList;
192      XMLNode node;
193      NodeList nodeListValues;
194      XMLNode nodeValues;
195      Element root;
196      Element rootValues;
197      ByteArrayInputStream stream = null;
198      String xmlString;
199      int y = 0;
200  
201      try {
202        menuArray = "function loadMenus() {\n";
203        menuArray += "var values;\n";
204        menuArray += "menus = new Object();\n";
205  
206        xmlString = queueServices.getMenus(companyId, objectType);
207        stream = new ByteArrayInputStream(xmlString.getBytes());
208        docParser.setValidationMode(false);
209        docParser.parse(stream);
210        inxml = docParser.getDocument();
211        root = inxml.getDocumentElement();
212        nodeList = root.getChildNodes();
213        for(i = 0; i < nodeList.getLength(); i++) {
214          node = (XMLNode)nodeList.item(i);
215          htObject = (String)ht.get(node.valueOf("./Name"));
216          if(htObject == null) {
217            nodeListValues = nodeList.item(i).getChildNodes();
218            menuArray += "values = new Object();\n";
219            for(y = 3; y < nodeListValues.getLength(); y++) {
220              nodeValues = (XMLNode)nodeListValues.item(y);
221              htObjectValues = (String)htValues.get(nodeValues.valueOf("./Value/InternalValue"));
222              if(htObjectValues == null) {
223                menuArray += "values[" + (y - 3) + "] = new value('" + nodeValues.valueOf("./InternalValue") + "','" + nodeValues.valueOf("./Display") + "');\n";
224              }
225            }
226            menuArray += "menus[" + i + "] = new menu('" + node.valueOf("./Name") + "'," + (y - 3) + ",values);\n";
227            menuArray += "\n";
228          }
229        }
230        menuArray += "numMenus = " + i + ";\n";
231        menuArray += "}";
232      }
233      catch(Exception e) {
234        menuArray += "/* Error: " + e.getClass().getName() + " " + e.getMessage() + " */\n";
235        menuArray += "numMenus = 0\n";
236        menuArray += "}";
237      }
238      return menuArray;
239    }
240  
241  
242    public String getQueueTypesOptions() {
243      DOMParser docParser = new DOMParser();
244      XMLDocument inxml;
245      XMLNode node;
246      NodeList nodeList;
247      String queueSelect = "";
248      Element root;
249      ByteArrayInputStream stream = null;
250      String xmlString;
251  
252      try {
253        QueueServicesHome home = (QueueServicesHome)ServiceLocator.instance().createEJB("QueueServicesHome", QueueServicesHome.class, false);
254        QueueServices queueServices = home.create();
255        xmlString = queueServices.getQueueTypes(companyId, objectType);
256        stream = new ByteArrayInputStream(xmlString.getBytes());
257        docParser.setValidationMode(false);
258        docParser.parse(stream);
259        inxml = docParser.getDocument();
260        root = inxml.getDocumentElement();
261        nodeList = root.getChildNodes();
262        for(int i = 0; i < nodeList.getLength(); i++) {
263          node = (XMLNode)nodeList.item(i);
264          queueSelect += "<option value='" + node.valueOf("./id") + "'>" + node.valueOf("./name") + " - " + node.valueOf("./description") + "</option>";
265        }
266      }
267      catch(Exception e) {
268        queueSelect += "<-- " + e.getClass().getName() + " " + e.getMessage() + " -->";
269      }
270      return queueSelect;
271    }
272  
273  
274    public String getQueueTypesVector() {
275      DOMParser docParser = new DOMParser();
276      XMLDocument inxml;
277      XMLNode node;
278      NodeList nodeList;
279      Element parent;
280      String resultVector = "var queueTypesVector = new Array();\n";
281      ByteArrayInputStream stream = null;
282      String xmlString;
283  
284      try {
285        QueueServicesHome home = (QueueServicesHome)ServiceLocator.instance().createEJB("QueueServicesHome", QueueServicesHome.class, false);
286        QueueServices queueServices = home.create();
287        xmlString = queueServices.getQueueTypes(companyId, objectType);
288        stream = new ByteArrayInputStream(xmlString.getBytes());
289        docParser.setValidationMode(false);
290        docParser.parse(stream);
291        inxml = docParser.getDocument();
292        parent = inxml.getDocumentElement();
293        nodeList = parent.getChildNodes();
294        for(int i = 0; i < nodeList.getLength(); i++) {
295          node = (XMLNode)nodeList.item(i);
296          resultVector += "queueTypesVector[" + i + "]= new Array();\n";
297          resultVector += "queueTypesVector[" + i + "][0]='" + node.valueOf("./id") + "';\n";
298          resultVector += "queueTypesVector[" + i + "][1]='" + StringFormat.toSafeJavaString(node.valueOf("./category")) + "';\n";
299          resultVector += "queueTypesVector[" + i + "][2]='" + StringFormat.toSafeJavaString(node.valueOf("./name")) + "';\n";
300          resultVector += "queueTypesVector[" + i + "][3]='" + StringFormat.toSafeJavaString(node.valueOf("./description")) + "';\n";
301          resultVector += "queueTypesVector[" + i + "][4]='" + StringFormat.toSafeJavaString(node.valueOf("./lastChangedDate")) + "';\n";
302          resultVector += "queueTypesVector[" + i + "][5]='" + StringFormat.toSafeJavaString(node.valueOf("./latChangedById")) + "';\n";
303          resultVector += "queueTypesVector[" + i + "][6]='" + StringFormat.toSafeJavaString(node.valueOf("./lastChangedByName")) + "';\n";
304          resultVector += "queueTypesVector[" + i + "][7]='N';\n";
305        }
306      }
307      catch(Exception e) {
308        resultVector += "/* " + e.getClass().getName() + " " + e.getMessage() + "*/";
309      }
310      finally {
311        return resultVector;
312      }
313    }
314  
315  
316    public String getRulesArray(String rules) {
317      DOMParser docParser = new DOMParser();
318      Field f;
319      boolean fieldFound;
320      int fieldPosition;
321      int i = 0;
322      XMLDocument inxml;
323      XMLNode node;
324      NodeList nodeList;
325      Element root;
326      String rulesArray = "";
327      ByteArrayInputStream stream = null;
328  
329      try {
330        stream = new ByteArrayInputStream(rules.getBytes());
331        docParser.setValidationMode(false);
332        docParser.parse(stream);
333        inxml = docParser.getDocument();
334        root = inxml.getDocumentElement();
335        nodeList = root.getChildNodes();
336        rulesArray += "rules = new Object();\n";
337        for(i = 0; i < nodeList.getLength(); i++) {
338          node = (XMLNode)nodeList.item(i);
339          fieldPosition = getFieldPosition(node);
340          rulesArray += "rules[" + i + "] = new rule(";
341          rulesArray += "'" + node.valueOf("./test") + "',";
342          rulesArray += "'" + fieldPosition + "',";
343          rulesArray += "'" + operatorPosition(node.valueOf("./operator")) + "',";
344          rulesArray += "'" + StringFormat.toSafeJavaString(node.valueOf("./value")) + "',";
345          rulesArray += "'" + connectorPosition(node.valueOf("./connector")) + "');\n";
346        }
347      }
348      catch(Exception e) {
349        rulesArray += "/* Exception: " + e.getClass().getName() + " " + e.getMessage() + " */";
350      }
351      rulesArray += "numRules = " + i + ";\n";
352      rulesArray += "\n";
353      return rulesArray;
354    }
355  
356  
357    public String getSelectedFields(String user) throws XMLParseException, XSLException, SAXException, IOException {
358      String array = "selectedFields = new Object();\n";
359      DOMParser docParser = new DOMParser();
360      String fieldName;
361      int fieldPosition;
362      XMLDocument inxml;
363      XMLNode node;
364      NodeList nodeList;
365      Element root;
366      ByteArrayInputStream stream = null;
367      int i = 0;
368  
369      try {
370        stream = new ByteArrayInputStream(user.getBytes());
371        docParser.setValidationMode(false);
372        docParser.parse(stream);
373        inxml = docParser.getDocument();
374        root = inxml.getDocumentElement();
375        nodeList = root.getChildNodes();
376        int fieldsPosition = -1;
377        for(i = 0; i < nodeList.getLength(); i++) {
378          node = (XMLNode)nodeList.item(i);
379          fieldPosition = getFieldPosition(node);
380          array += "selectedFields[" + i + "] = " + fieldPosition + ";\n";
381        }
382      }
383      catch(Exception e) {
384      }
385      array += "numSelectedFields = " + i + ";\n";
386      array += "\n";
387      return array;
388    }
389  
390  
391    public String getSelectedUsersArray(String user) throws XMLParseException, XSLException, SAXException, IOException {
392      DOMParser docParser = new DOMParser();
393      int i = 0;
394      XMLDocument inxml;
395      XMLNode node;
396      NodeList nodeList;
397      Element root;
398      ByteArrayInputStream stream = null;
399      String userArray = "selectedUsers = new Object();\n";
400  
401      try {
402        stream = new ByteArrayInputStream(user.getBytes());
403        docParser.setValidationMode(false);
404        docParser.parse(stream);
405        inxml = docParser.getDocument();
406        root = inxml.getDocumentElement();
407        nodeList = root.getChildNodes();
408        for(i = 0; i < nodeList.getLength(); i++) {
409          node = (XMLNode)nodeList.item(i);
410          userArray += "selectedUsers[" + i + "] = " + userPosition(node.valueOf("./userid")) + ";\n";
411        }
412      }
413      catch(Exception e) {
414      }
415      userArray += "numSelectedUsers = " + i + ";\n";
416      userArray += "\n";
417      return userArray;
418    }
419  
420  
421    public String getSortArray(String sort) throws XMLParseException, XSLException, SAXException, IOException {
422      DOMParser docParser = new DOMParser();
423      String fieldName;
424      int fieldPosition;
425      int i = 0;
426      XMLDocument inxml;
427      XMLNode node;
428      NodeList nodeList;
429      Element root;
430      String sortArray = "sorts = new Object();\n";
431      ByteArrayInputStream stream = null;
432  
433      try {
434        stream = new ByteArrayInputStream(sort.getBytes());
435        docParser.setValidationMode(false);
436        docParser.parse(stream);
437        inxml = docParser.getDocument();
438        root = inxml.getDocumentElement();
439        nodeList = root.getChildNodes();
440        for(i = 0; i < nodeList.getLength(); i++) {
441          node = (XMLNode)nodeList.item(i);
442          fieldPosition = getFieldPosition(node);
443          sortArray += "sorts[" + i + "] = new sort('" + fieldPosition + "',";
444          sortArray += "'" + node.valueOf("./direction") + "');\n";
445        }
446      }
447      catch(Exception e) {
448      }
449      sortArray += "numSort = " + i + ";\n";
450      sortArray += "\n";
451      return sortArray;
452    }
453  
454  
455    private void init() throws InstantbankException {
456      DOMParser docParser = new DOMParser();
457      XMLDocument doc;
458      Field f;
459      XMLNode node;
460      NodeList nodeList;
461      Element root;
462      ByteArrayInputStream stream = null;
463      String xmlString;
464  
465      try {
466        fieldsTable = new Hashtable();
467        QueueServicesHome home = (QueueServicesHome)ServiceLocator.instance().createEJB("QueueServicesHome", QueueServicesHome.class, false);
468        queueServices = home.create();
469        xmlString = queueServices.getFields(companyId, objectType);
470        stream = new ByteArrayInputStream(xmlString.getBytes());
471        docParser.setValidationMode(false);
472        docParser.parse(stream);
473        doc = docParser.getDocument();
474        root = doc.getDocumentElement();
475        nodeList = root.getChildNodes();
476        for(int i = 0; i < nodeList.getLength(); i++) {
477          node = (XMLNode)nodeList.item(i);
478          f = new Field();
479          f.tableName = node.valueOf("./tablename");
480          f.tableAlias = node.valueOf("./alias");
481          f.name = node.valueOf("./name");
482          f.displayName = node.valueOf("./display");
483          f.type = node.valueOf("./type");
484          f.menu = node.valueOf("./menu");
485          fieldsTable.put(new Integer(i), f);
486        }
487      }
488      catch(Exception e) {
489        throw new InstantbankException(e, "212001", "Failed to initialize FieldsTransformer");
490      }
491    }
492  
493  
494    private void initUsers() throws InstantbankException {
495      DOMParser docParser = new DOMParser();
496      XMLDocument inxml;
497      GroupItem item;
498      int nlLength;
499      XMLNode node;
500      NodeList nodeList;
501      NodeList nodeListGroup;
502      NodeList nodeListUser;
503      Element root;
504      ByteArrayInputStream stream = null;
505      String xmlString = "";
506  
507      vectorPosition = 0;
508      try {
509        groupsUserTable = new Hashtable();
510        SecurityServicesHome home = (SecurityServicesHome)ServiceLocator.instance().createEJB("SecurityServicesHome", SecurityServicesHome.class, false);
511        SecurityServices securityServices = home.create();
512        xmlString = securityServices.getGroupsUsers(companyId);
513        stream = new ByteArrayInputStream(xmlString.getBytes());
514        docParser.setValidationMode(false);
515        docParser.parse(stream);
516        inxml = docParser.getDocument();
517        root = inxml.getDocumentElement();
518        nodeList = root.getChildNodes();
519        nlLength = nodeList.getLength();
520        for(int i = 0; i < nlLength; i++) {
521          node = (XMLNode)nodeList.item(i);
522          item = new GroupItem("0", 0, node.valueOf("./name"));
523          groupsUserTable.put(new Integer(vectorPosition), item);
524          nodeListUser = node.selectNodes("./ChildUsers");
525          vectorPosition++;
526          if(nodeListUser.getLength() > 0) {
527            setChildrenUser(node, 0, 0);
528          }
529          nodeListGroup = node.selectNodes("./ChildGroups");
530          if(nodeListGroup.getLength() > 0) {
531            setChildrenGroupd(node, 0, 0);
532          }
533        }
534      }
535      catch(Exception e) {
536      }
537    }
538  
539  
540    private String operatorPosition(String value) {
541      if(value.equals("=")) {
542        return "0";
543      }
544      if(value.equals("BETWEEN")) {
545        return "1";
546      }
547      if(value.equals("<>")) {
548        return "2";
549      }
550      if(value.equals(">")) {
551        return "3";
552      }
553      if(value.equals(">=")) {
554        return "4";
555      }
556      if(value.equals("<")) {
557        return "5";
558      }
559      if(value.equals("<=")) {
560        return "6";
561      }
562      return "0";
563    }
564  
565  
566    private void setChildrenUser(XMLNode parent, int position, int hierarchy) throws XSLException {
567      XMLNode childNode;
568      GroupItem item;
569      NodeList nlChild;
570      int nlChildLen;
571      NodeList nlChildren;
572  
573      nlChildren = parent.selectNodes("./ChildUsers/User");
574      nlChildLen = nlChildren.getLength();
575      int k = 0;
576      hierarchy = hierarchy + 1;
577      for(k = 0; k < nlChildLen; k++) {
578        childNode = (XMLNode)nlChildren.item(k);
579        item = new GroupItem(childNode.valueOf("./id"), hierarchy, childNode.valueOf("./name"));
580        groupsUserTable.put(new Integer(vectorPosition), item);
581        vectorPosition++;
582      }
583    }
584  
585  
586    public void setChildrenGroupd(XMLNode parent, int position, int hierarchy) throws XSLException {
587      XMLNode childNode;
588      GroupItem item;
589      NodeList nlChild;
590      int nlChildLen;
591      NodeList nlChildren;
592      NodeList nlChildUser;
593  
594      nlChildren = parent.selectNodes("./ChildGroups/Group");
595      nlChildLen = nlChildren.getLength();
596      int k = 0;
597      hierarchy = hierarchy + 1;
598      for(k = 0; k < nlChildLen; k++) {
599        childNode = (XMLNode)nlChildren.item(k);
600        item = new GroupItem("0", hierarchy, childNode.valueOf("./name"));
601        groupsUserTable.put(new Integer(vectorPosition), item);
602        vectorPosition++;
603        nlChildUser = childNode.selectNodes("./ChildUsers");
604        if(nlChildUser.getLength() > 0) {
605          setChildrenUser(childNode, 0, hierarchy);
606        }
607        nlChild = childNode.selectNodes("./ChildGroups");
608        if(nlChild.getLength() > 0) {
609          setChildrenGroupd(childNode, 0, hierarchy);
610        }
611      }
612    }
613  
614  
615    private String userPosition(String id) {
616      GroupItem item;
617  
618      for(int i = 0; i < groupsUserTable.size(); i++) {
619        item = (GroupItem)groupsUserTable.get(new Integer(i));
620        if(item.id.equals(id)) {
621          return "" + i;
622        }
623      }
624      return "-1";
625    }
626  }
627  
628