1 package com.instantbank.collections.letters.ejb;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import javax.ejb.CreateException;
6 import javax.ejb.EJBContext;
7 import javax.ejb.SessionBean;
8 import javax.ejb.SessionContext;
9 import oracle.xml.parser.v2.XMLDocument;
10 import oracle.xml.parser.v2.XMLNode;
11 import com.instantbank.collections.util.DataAccess;
12 import com.instantbank.collections.util.InstantbankException;
13 import com.instantbank.collections.util.UniqueIDGenerator;
14 import com.instantbank.collections.util.XMLDataAccess;
15
16
17 public class LetterServicesBean
18 implements SessionBean {
19 private EJBContext context;
20
21
22 public LetterServicesBean() { }
23
24
25 public void ejbCreate() throws CreateException {
26
27 }
28
29
30 public void ejbActivate() { }
31
32
33 public void ejbPassivate() { }
34
35
36 public void ejbRemove() { }
37
38
39 public String getLetters(Long companyId) throws InstantbankException {
40 XMLDataAccess da = null;
41 XMLDocument doc;
42 XMLNode root;
43 String sql;
44 String strLetters;
45
46 try {
47 da = new XMLDataAccess("");
48 da.connect();
49 sql = "SELECT let_id Id, let_name Name FROM letter_templates WHERE let_cmp_id = " + companyId.toString();
50 doc = da.makeXMLSelect(sql, "Letters", "Letter");
51 StringWriter sw = new StringWriter();
52 PrintWriter pw = new PrintWriter(sw);
53 doc.print(pw);
54 da.disconnect();
55 strLetters = sw.toString();
56 return strLetters;
57 }
58 catch(Exception e) {
59 setRollbackOnly();
60 throw new InstantbankException(e, "221001", "Failed to retrieve letter templates");
61 }
62 finally {
63 try {
64 if(da != null) {
65 da.disconnect();
66 }
67 }
68 catch(Exception e) {
69 }
70 }
71 }
72
73
74 public String getListByCompany(Long companyId, Long userId) throws InstantbankException {
75 String query;
76 XMLDataAccess xda = null;
77
78 try {
79 xda = new XMLDataAccess("");
80 xda.connect();
81 query = "SELECT ";
82 query += "let_id, ";
83 query += "let_code, ";
84 query += "let_name ";
85 query += "FROM ";
86 query += "letter_templates, ";
87 query += "users, ";
88 query += "collections_security_profiles, ";
89 query += "security_profiles ";
90 query += "WHERE ";
91 query += "let_cmp_id = " + companyId.toString() + " AND ";
92 query += "user_cmp_id = let_cmp_id AND ";
93 query += "user_id = " + userId.toString() + " AND ";
94 query += "user_sprf_id = sprf_id AND ";
95 query += "sprf_csp_id = csp_id AND ";
96 query += "csp_order_letter >= let_security_level ";
97 query += "ORDER BY let_name";
98 return xda.getXml(query, "letterTemplatesList", "letterTemplate");
99 }
100 catch(Exception e) {
101 setRollbackOnly();
102 throw new InstantbankException(e, "221002", "Failed to retrieve letter templates for the company");
103 }
104 finally {
105 try {
106 if(xda != null) {
107 xda.disconnect();
108 }
109 }
110 catch(Exception e) {
111 }
112 }
113 }
114
115
116 public void orderLetter(Long templateId, Long objectId, boolean immediate) throws InstantbankException {
117 DataAccess da = null;
118 String query;
119
120 try {
121 da = new DataAccess();
122 da.connect();
123 query = "INSERT INTO letters_queue (";
124 query += "lq_id,";
125 query += "lq_datestamp,";
126 query += "lq_let_id,";
127 query += "lq_request_type,";
128 query += "lq_object_id";
129 query += ") VALUES (";
130 query += (new Long(UniqueIDGenerator.instance().getNextId())).toString() + ",";
131 query += "SYSDATE, ";
132 query += templateId.toString() + ", ";
133 query += ((immediate) ? ("'I', ") : ("'B', "));
134 query += objectId.toString();
135 query += ")";
136 da.makeInsert(query);
137 }
138 catch(Exception e) {
139 setRollbackOnly();
140 throw new InstantbankException(e, "221003", "Failed to order letter");
141 }
142 finally {
143 try {
144 if(da != null) {
145 da.disconnect();
146 }
147 }
148 catch(Exception e) {
149 }
150 }
151 }
152
153
154 private void setRollbackOnly() {
155 try {
156 this.context.setRollbackOnly();
157 }
158 catch(Exception ne) {
159 }
160 }
161
162
163 public void setSessionContext(SessionContext ctx) {
164 this.context = ctx;
165 }
166 }
167
168