1    package com.instantbank.collections.documents.ejb;
2    
3    import java.io.ByteArrayInputStream;
4    import java.io.PrintWriter;
5    import java.io.StringWriter;
6    import javax.ejb.CreateException;
7    import javax.ejb.EJBContext;
8    import javax.ejb.SessionBean;
9    import javax.ejb.SessionContext;
10   import org.w3c.dom.Element;
11   import org.w3c.dom.Node;
12   import org.w3c.dom.NodeList;
13   import oracle.xml.parser.v2.DOMParser;
14   import oracle.xml.parser.v2.XMLDocument;
15   import oracle.xml.parser.v2.XMLNode;
16   import com.instantbank.collections.util.DataAccess;
17   import com.instantbank.collections.util.InstantbankException;
18   import com.instantbank.collections.util.StringFormat;
19   import com.instantbank.collections.util.UniqueIDGenerator;
20   import com.instantbank.collections.util.XMLDataAccess;
21   
22   public class DocumentServicesBean
23       implements SessionBean {
24     private EJBContext context;
25   
26   
27     public DocumentServicesBean() { }
28   
29   
30     public void ejbCreate() throws CreateException {
31       // TODO:  Add custom implementation.
32     }
33   
34   
35     public void ejbActivate() { }
36   
37   
38     public void ejbPassivate() { }
39   
40   
41     public void ejbRemove() { }
42   
43   
44     public String getDocuments(Long companyId) throws InstantbankException {
45       XMLDataAccess da = null;
46       Node domNode;
47       XMLDocument doc;
48       XMLNode document;
49       String documentId;
50       NodeList documents;
51       XMLNode root;
52       String sql;
53   
54       try {
55         da = new XMLDataAccess("");
56         da.connect();
57         sql = "SELECT ";
58         sql += "doc_id id,";
59         sql += "doc_number docnumber,";
60         sql += "doc_type type,";
61         sql += "doc_name name,";
62         sql += "doc_output_name output,";
63         sql += "doc_template_name template,";
64         sql += "doc_security_level securitylevel,";
65         sql += "doc_filename filename,";
66         sql += "doc_dateformat dateformat,";
67         sql += "doc_timeformat timeformat,";
68         sql += "doc_extension extension,";
69         sql += "doc_frequency frequency,";
70         sql += "doc_priority priority,";
71         sql += "doc_trigger doctrigger,";
72         sql += "doc_status_flag status,";
73         sql += "doc_generate generate,";
74         sql += "user_userid lastchangedby,";
75         sql += "TO_CHAR(doc_last_changed_date,'MM/DD/YYYY HH24:MI') lastchangeddate ";
76         sql += "FROM ";
77         sql += "documents,";
78         sql += "users ";
79         sql += "WHERE ";
80         sql += "(doc_cmp_id = " + companyId + ") AND ";
81         sql += "(doc_last_changed_by = user_id)";
82   
83         doc = da.makeXMLSelect(sql, "Documents", "Document");
84         root = (XMLNode)doc.getDocumentElement();
85         documents = root.selectNodes("/Documents/Document");
86         for(int i = 0; i < documents.getLength(); i++) {
87           document = (XMLNode)documents.item(i);
88           documentId = document.valueOf("./id");
89   
90           sql = "SELECT ";
91           sql += "docl_cmpl_id id ";
92           sql += "FROM ";
93           sql += "document_locations ";
94           sql += "WHERE ";
95           sql += "docl_doc_id = " + documentId;
96           doc = da.makeXMLSelect(sql, "Locations", "Location", (Node)document);
97   
98           sql = "SELECT ";
99           sql += "docc_copy_id id ";
100          sql += "FROM ";
101          sql += "document_copies ";
102          sql += "WHERE ";
103          sql += "docc_doc_id = " + documentId;
104          doc = da.makeXMLSelect(sql, "Copies", "Copy", (Node)document);
105        }
106        root = (XMLNode)doc.getDocumentElement();
107        StringWriter sw = new StringWriter();
108        PrintWriter pw = new PrintWriter(sw);
109        root.print(pw);
110        return sw.toString();
111      }
112      catch(Exception e) {
113        setRollbackOnly();
114        throw new InstantbankException(e, "231001", "Failed to retrieve documents");
115      }
116      finally {
117        try {
118          if(da != null) {
119            da.disconnect();
120          }
121        }
122        catch(Exception e) {
123        }
124      }
125    }
126  
127  
128    public String getRules(Long companyId, String documentId) throws InstantbankException {
129      XMLDataAccess da = null;
130      XMLDocument doc;
131      XMLNode document;
132      XMLNode root;
133      String sql;
134  
135      try {
136        da = new XMLDataAccess("");
137        da.connect();
138        sql = "SELECT	";
139        sql += "doc_description description,";
140        sql += "docwr_sequence sequence,";
141        sql += "docwr_test test,";
142        sql += "docwr_table_name tablename,";
143        sql += "docwr_table_alias tablealias,";
144        sql += "docwr_field_name fieldname,";
145        sql += "docwr_field_type fieldtype,";
146        sql += "docwr_operator operator,";
147        sql += "docwr_value value,";
148        sql += "docwr_value_type valuetype,";
149        sql += "docwr_connector connector ";
150        sql += "FROM	";
151        sql += "DOCUMENTS_WHERE_RULES,DOCUMENTS ";
152        sql += "WHERE ";
153        sql += "docwr_doc_id =" + documentId + " and docwr_doc_id = doc_id";
154        sql += " and doc_cmp_id =" + companyId + " and doc_status_flag=1";
155        doc = da.makeXMLSelect(sql, "RulesList", "Rule");
156        root = (XMLNode)doc.getDocumentElement();
157        StringWriter sw = new StringWriter();
158        PrintWriter pw = new PrintWriter(sw);
159        root.print(pw);
160        return sw.toString();
161      }
162      catch(Exception e) {
163        setRollbackOnly();
164        throw new InstantbankException(e, "231001", "Failed to retrieve documents");
165      }
166      finally {
167        try {
168          if(da != null) {
169            da.disconnect();
170          }
171        }
172        catch(Exception e) {
173        }
174      }
175    }
176  
177  
178    public String getTrigger() throws InstantbankException {
179      XMLDataAccess da = null;
180      XMLDocument doc;
181      XMLNode document;
182      XMLNode root;
183      String sql;
184  
185      try {
186        da = new XMLDataAccess("");
187        da.connect();
188        sql = "SELECT	";
189        sql += "RPT_TRIGGER listtrigger ";
190        sql += " FROM	";
191        sql += "REPORT_TRIGGER ";
192        sql += " ORDER BY RPT_TRIGGER";
193        doc = da.makeXMLSelect(sql, "TriggerList", "Trigger");
194        root = (XMLNode)doc.getDocumentElement();
195        StringWriter sw = new StringWriter();
196        PrintWriter pw = new PrintWriter(sw);
197        root.print(pw);
198        return sw.toString();
199      }
200      catch(Exception e) {
201        setRollbackOnly();
202        throw new InstantbankException(e, "2310003", "Failed to retrieve Triggers");
203      }
204      finally {
205        try {
206          if(da != null) {
207            da.disconnect();
208          }
209        }
210        catch(Exception e) {
211        }
212      }
213    }
214  
215  
216  
217    public void saveDocument(String xml, Long companyId, Long userId) throws InstantbankException {
218      XMLNode childNode;
219      long docIdPk;
220      DataAccess da = null;
221      XMLDocument doc;
222      DOMParser docParser;
223      String Id;
224      XMLNode node;
225      NodeList nodeList;
226      String sql;
227      ByteArrayInputStream stream;
228  
229      try {
230        da = new DataAccess();
231        da.connect();
232        docParser = new DOMParser();
233        stream = new ByteArrayInputStream(xml.getBytes());
234        docParser.setValidationMode(false);
235        docParser.parse(stream);
236        doc = docParser.getDocument();
237        node = (XMLNode)doc.getDocumentElement();
238        String docId = node.valueOf("/Document/id");
239        if(!docId.equals("0")) {
240          sql = "UPDATE DOCUMENTS SET ";
241          sql += "DOC_NUMBER =  '" + node.valueOf("/Document/docnumber") + "',";
242          sql += "DOC_NAME = '" + node.valueOf("/Document/name") + "',";
243          sql += "DOC_TYPE = '" + node.valueOf("/Document/type") + "',";
244          sql += "DOC_OUTPUT_NAME = '" + node.valueOf("/Document/output") + "',";
245          sql += "DOC_TEMPLATE_NAME = '" + node.valueOf("/Document/template") + "',";
246          sql += "DOC_FILENAME = '" + node.valueOf("/Document/filename") + "',";
247          sql += "DOC_DATEFORMAT = '" + node.valueOf("/Document/dateformat") + "',";
248          sql += "DOC_TIMEFORMAT = '" + node.valueOf("/Document/timeformat") + "',";
249          sql += "DOC_SECURITY_LEVEL ='" + node.valueOf("/Document/securitylevel") + "',";
250          sql += "DOC_EXTENSION = '" + node.valueOf("/Document/extension") + "',";
251          sql += "DOC_FREQUENCY = '" + node.valueOf("/Document/frequency") + "',";
252          sql += "DOC_PRIORITY = '" + node.valueOf("/Document/priority") + "',";
253          sql += "DOC_TRIGGER = '" + node.valueOf("/Document/doctrigger") + "',";
254          sql += "DOC_STATUS_FLAG = " + node.valueOf("/Document/status") + ",";
255          sql += "DOC_GENERATE = '" + node.valueOf("/Document/generate") + "',";
256          sql += "DOC_LAST_CHANGED_BY = " + userId + ",";
257          sql += "DOC_LAST_CHANGED_DATE = SYSDATE ";
258          sql += "WHERE ";
259          sql += "DOC_ID = " + node.valueOf("/Document/id");
260          sql += " AND DOC_CMP_ID = " + companyId;
261          da.makeUpdate(sql);
262          sql = "DELETE FROM DOCUMENT_LOCATIONS ";
263          sql += "WHERE DOCL_DOC_ID=" + node.valueOf("/Document/id");
264          da.makeDelete(sql);
265          nodeList = node.selectNodes("/Document/Locations/Location");
266          for(int i = 0; i < nodeList.getLength(); i++) {
267            childNode = (XMLNode)nodeList.item(i);
268            Id = childNode.valueOf("./id");
269            sql = "INSERT INTO DOCUMENT_LOCATIONS ";
270            sql += "(DOCL_DOC_ID,DOCL_CMPL_ID";
271            sql += ")VALUES(";
272            sql += node.valueOf("/Document/id") + ",";
273            sql += Id + ")";
274            da.makeInsert(sql);
275          }
276          sql = "DELETE FROM DOCUMENT_COPIES ";
277          sql += "WHERE DOCC_DOC_ID=" + node.valueOf("/Document/id");
278          da.makeDelete(sql);
279          nodeList = node.selectNodes("/Document/Copies/Copy");
280          for(int i = 0; i < nodeList.getLength(); i++) {
281            childNode = (XMLNode)nodeList.item(i);
282            Id = childNode.valueOf("./id");
283            sql = "INSERT INTO DOCUMENT_COPIES ";
284            sql += "(DOCC_DOC_ID,DOCC_COPY_ID";
285            sql += ")VALUES(";
286            sql += node.valueOf("/Document/id") + ",";
287            sql += Id + ")";
288            da.makeInsert(sql);
289          }
290        }
291        else {
292          docIdPk = UniqueIDGenerator.instance().getNextId();
293          sql = "INSERT INTO DOCUMENTS (";
294          sql += "DOC_ID ,";
295          sql += "DOC_NUMBER, ";
296          sql += "DOC_NAME, ";
297          sql += "DOC_TYPE, ";
298          sql += "DOC_OUTPUT_NAME, ";
299          sql += "DOC_TEMPLATE_NAME , ";
300          sql += "DOC_SECURITY_LEVEL, ";
301          sql += "DOC_FILENAME ,";
302          sql += "DOC_DATEFORMAT ,";
303          sql += "DOC_TIMEFORMAT ,";
304          sql += "DOC_EXTENSION ,";
305          sql += "DOC_FREQUENCY ,";
306          sql += "DOC_PRIORITY ,";
307          sql += "DOC_TRIGGER ,";
308          sql += "DOC_STATUS_FLAG ,";
309          sql += "DOC_GENERATE ,";
310          sql += "DOC_CMP_ID ,";
311          sql += "DOC_LAST_CHANGED_BY ,";
312          sql += "DOC_LAST_CHANGED_DATE ";
313          sql += ")VALUES (";
314          sql += docIdPk + ",";
315          sql += "'" + node.valueOf("/Document/docnumber") + "',";
316          sql += "'" + node.valueOf("/Document/name") + "',";
317          sql += "'" + node.valueOf("/Document/type") + "',";
318          sql += "'" + node.valueOf("/Document/output") + "',";
319          sql += "'" + node.valueOf("/Document/template") + "',";
320          sql += "'" + node.valueOf("/Document/securitylevel") + "',";
321          sql += "'" + node.valueOf("/Document/filename") + "',";
322          sql += "'" + node.valueOf("/Document/dateformat") + "',";
323          sql += "'" + node.valueOf("/Document/timeformat") + "',";
324          sql += "'" + node.valueOf("/Document/extension") + "',";
325          sql += "'" + node.valueOf("/Document/frequency") + "',";
326          sql += "'" + node.valueOf("/Document/priority") + "',";
327          sql += "'" + node.valueOf("/Document/doctrigger") + "',";
328          sql += "'" + node.valueOf("/Document/status") + "',";
329          sql += "'" + node.valueOf("/Document/generate") + "',";
330          sql += companyId + ",";
331          sql += userId + ",";
332          sql += "SYSDATE)";
333          da.makeInsert(sql);
334          nodeList = node.selectNodes("/Document/Locations/Location");
335          for(int i = 0; i < nodeList.getLength(); i++) {
336            childNode = (XMLNode)nodeList.item(i);
337            Id = childNode.valueOf("./id");
338            sql = "INSERT INTO DOCUMENT_LOCATIONS ";
339            sql += "(DOCL_DOC_ID,DOCL_CMPL_ID";
340            sql += ")VALUES(";
341            sql += docIdPk + ",";
342            sql += Id + ")";
343            da.makeInsert(sql);
344          }
345          nodeList = node.selectNodes("/Document/Copies/Copy");
346          for(int i = 0; i < nodeList.getLength(); i++) {
347            childNode = (XMLNode)nodeList.item(i);
348            Id = childNode.valueOf("./id");
349            sql = "INSERT INTO DOCUMENT_COPIES ";
350            sql += "(DOCC_DOC_ID,DOCC_COPY_ID";
351            sql += ")VALUES(";
352            sql += docIdPk + ",";
353            sql += Id + ")";
354            da.makeInsert(sql);
355          }
356        }
357      }
358      catch(Exception e) {
359        setRollbackOnly();
360        throw new InstantbankException(e, "231002", "Failed to update document into the database");
361      }
362      finally {
363        try {
364          if(da != null) {
365            da.disconnect();
366          }
367        }
368        catch(Exception e) {
369        }
370      }
371    }
372  
373  
374    public void saveRules(Long companyId, String xmlRules) throws InstantbankException {
375      XMLNode childNode;
376      DataAccess da = null;
377      DOMParser docParser = new DOMParser();
378      XMLDocument doc;
379      XMLNode node;
380      NodeList rulesList;
381      Element root;
382      String sql = "";
383      ByteArrayInputStream stream = null;
384      String test;
385      String tablename;
386      String docId;
387      String description;
388  
389      try {
390        da = new DataAccess();
391        da.connect();
392        stream = new ByteArrayInputStream(xmlRules.getBytes());
393        docParser.setValidationMode(false);
394        docParser.parse(stream);
395        doc = docParser.getDocument();
396        node = (XMLNode)doc.getDocumentElement();
397        docId = node.valueOf("./Id");
398        description = node.valueOf("./description");
399        rulesList = node.selectNodes("/Document/RulesList/Rule");
400  
401        sql = "Delete DOCUMENTS_WHERE_RULES where DOCWR_DOC_ID=" + docId;
402        da.makeDelete(sql);
403        for(int i = 0; i < rulesList.getLength(); i++) {
404          childNode = (XMLNode)rulesList.item(i);
405          sql = "INSERT into DOCUMENTS_WHERE_RULES(";
406          sql += "DOCWR_DOC_ID,";
407          sql += "DOCWR_SEQUENCE,";
408          sql += "DOCWR_TEST,";
409          sql += "DOCWR_TABLE_NAME,";
410          sql += "DOCWR_TABLE_ALIAS,";
411          sql += "DOCWR_FIELD_NAME,";
412          sql += "DOCWR_FIELD_TYPE,";
413          sql += "DOCWR_OPERATOR,";
414          sql += "DOCWR_VALUE,";
415          sql += "DOCWR_VALUE_TYPE,";
416          sql += "DOCWR_CONNECTOR";
417          sql += ") VALUES (";
418          sql += docId + ",";
419          sql += (i + 1) + ",";
420          sql += "'" + childNode.valueOf("./test") + "',";
421          sql += "'" + childNode.valueOf("./tablename") + "',";
422          sql += "'" + childNode.valueOf("./tablealias") + "',";
423          sql += "'" + childNode.valueOf("./fieldname") + "',";
424          sql += "'" + childNode.valueOf("./fieldtype") + "',";
425          sql += "'" + childNode.valueOf("./operator") + "',";
426          sql += "'" + StringFormat.toSafeOracleString(childNode.valueOf("./value")) + "',";
427          sql += "'" + childNode.valueOf("./valuetype") + "',";
428          sql += "'" + childNode.valueOf("./connector") + "'";
429          sql += ")";
430          da.makeInsert(sql);
431        }
432        sql = "UPDATE DOCUMENTS SET DOC_DESCRIPTION = '" + description + "'";
433        sql += " WHERE DOC_ID=" + docId + " AND DOC_CMP_ID=" + companyId;
434        da.makeUpdate(sql);
435      }
436      catch(Exception e) {
437        setRollbackOnly();
438        throw new InstantbankException(e, "211042", "Failed to save queue rules");
439      }
440      finally {
441        try {
442          if(da != null) {
443            da.disconnect();
444          }
445        }
446        catch(Exception e) {
447        }
448      }
449    }
450  
451  
452  
453    private void setRollbackOnly() {
454      try {
455        this.context.setRollbackOnly();
456      }
457      catch(Exception ne) {
458      }
459    }
460  
461  
462    public void setSessionContext(SessionContext ctx) {
463      this.context = ctx;
464    }
465  }
466  
467