1    package com.instantbank.collections.creditInfo.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.NodeList;
11   import oracle.xml.parser.v2.DOMParser;
12   import oracle.xml.parser.v2.XMLDocument;
13   import oracle.xml.parser.v2.XMLNode;
14   import com.instantbank.collections.util.DataAccess;
15   import com.instantbank.collections.util.InstantbankException;
16   import com.instantbank.collections.util.StringFormat;
17   import com.instantbank.collections.util.UniqueIDGenerator;
18   import com.instantbank.collections.util.XMLDataAccess;
19   
20   public class CreditInfoServicesBean
21       implements SessionBean {
22     private EJBContext context;
23   
24   
25     public CreditInfoServicesBean() { }
26   
27   
28     public void ejbCreate() throws CreateException {
29       // TODO:  Add custom implementation.
30     }
31   
32   
33     public void ejbActivate() { }
34   
35   
36     public void ejbPassivate() { }
37   
38   
39     public void ejbRemove() { }
40   
41   
42     public String getStatusCategoriesValues(Long companyId) throws InstantbankException {
43       String categoryId;
44       int categoryNumber;
45       String categoryXmlString = "";
46       String query = "";
47       String StringCategories = "";
48       String StringCategory = "";
49       String StringXmlValues;
50       XMLDataAccess xda = null;
51       XMLDocument xmlCategories;
52       XMLDocument xmlValues;
53   
54       try {
55         xda = new XMLDataAccess("database.properties");
56         xda.connect();
57         StringWriter sw = new StringWriter();
58         PrintWriter pw = new PrintWriter(sw);
59         query = " SELECT    stc_id Id," +
60           "           stc_number CNumber," +
61           "           stc_name Name," +
62           "           stc_interface_field_number InterfaceFieldNo," +
63           "           to_char(stc_last_changed_date,'mm-dd-yyyy') ChangedDate," +
64           "           user_userid ChangedBy" +
65           " FROM      status_categories, users" +
66           " WHERE     (stc_last_changed_by (+)= user_id ) and" +
67           "           (stc_cmp_id = " + companyId.toString() + ")";
68   
69         xmlCategories = xda.makeXMLSelect(query, "Categories", "Category");
70         if(!xmlCategories.valueOf("/Categories/Category/id").equals(new String(""))) {
71           xmlCategories.print(pw);
72           StringCategories = sw.toString();
73           StringCategories = StringCategories.substring(0, (StringCategories.indexOf("<Categories>") + 12));
74           if(xmlCategories.getDocumentElement().hasChildNodes()) {
75             categoryNumber = xmlCategories.selectNodes("/Categories/Category").getLength();
76   
77             for(int i = 0; i < categoryNumber; i++) {
78               XMLNode category = (XMLNode)xmlCategories.selectNodes("/Categories/Category").item(i);
79               categoryId = "";
80               for(int j = 0; j < category.getChildNodes().getLength(); j++) {
81                 String NodeName = category.getChildNodes().item(j).getNodeName();
82                 if(NodeName.equals("id")) {
83                   categoryId = category.getChildNodes().item(j).getFirstChild().getNodeValue();
84                 }
85               }
86               query = " SELECT    stv_id Id," +
87                 "           stv_name Name," +
88                 "           stv_interface_field_value InterfaceFieldNo" +
89                 " FROM      status_values" +
90                 " WHERE     stv_stc_id = " + categoryId;
91   
92               StringXmlValues = StringFormat.cleanXml(xda.getXml(query, "Values", "Value"));
93               sw = new StringWriter();
94               pw = new PrintWriter(sw);
95               category.print(pw);
96               StringCategory = sw.toString();
97               StringCategory = StringCategory.substring(0, StringCategory.indexOf("</Category>"));
98               StringCategory += StringXmlValues;
99               StringCategory += "</Category>";
100              StringCategories += StringCategory;
101            }
102          }
103          StringCategories += "</Categories>";
104        }
105        else {
106          StringCategories = "";
107        }
108        return StringCategories;
109      }
110      catch(Exception e) {
111        this.context.setRollbackOnly();
112        throw new InstantbankException(e, "311051", "Failed to get status categories");
113      }
114      finally {
115        try {
116          if(xda != null) {
117            xda.disconnect();
118          }
119        }
120        catch(Exception e) {
121        }
122      }
123    }
124  
125  
126    public void saveStatusCategoriesValues(String data, Long companyId, Long userId) throws InstantbankException {
127      XMLNode category;
128      DataAccess dataAccess = null;
129      DOMParser docParser;
130      String id;
131      String interfaceFieldNo;
132      String name;
133      String number;
134      long pk;
135      String query;
136      ByteArrayInputStream stream;
137      String valueId;
138      String valueInterfaceFieldNo;
139      String valueName;
140      int valueNum = 0;
141      XMLDocument xmlDoc;
142  
143      xmlDoc = null;
144      try {
145        dataAccess = new DataAccess();
146        dataAccess.connect();
147        stream = new ByteArrayInputStream(data.getBytes());
148        docParser = new DOMParser();
149        docParser.setValidationMode(false);
150        docParser.parse(stream);
151        xmlDoc = docParser.getDocument();
152        if(xmlDoc.getDocumentElement().hasChildNodes()) {
153          int categoryNumber = xmlDoc.selectNodes("/Categories/Category").getLength();
154          NodeList category1 = xmlDoc.selectNodes("/Categories/Category");
155          for(int i = 0; i < categoryNumber; i++) {
156            category = (XMLNode)xmlDoc.selectNodes("/Categories/Category").item(i);
157            id = category.valueOf("./Id");
158            name = category.valueOf("./Name");
159            number = category.valueOf("./Number");
160            interfaceFieldNo = category.valueOf("./InterfaceFieldNo");
161            if(id.equals("0")) {
162              pk = UniqueIDGenerator.instance().getNextId();
163              query = "  INSERT INTO STATUS_CATEGORIES (" +
164                "     stc_id," +
165                "     stc_cmp_id," +
166                "     stc_number," +
167                "     stc_name," +
168                "     stc_interface_field_number," +
169                "     stc_last_changed_date," +
170                "     stc_last_changed_by" +
171                " ) VALUES (" +
172                (new Long(pk)).toString() + ", " +
173                companyId.toString() + ", " +
174                "'" + number + "', " +
175                "'" + StringFormat.toSafeOracleString(name) + "', " +
176                "'" + interfaceFieldNo + "', " +
177                " SYSDATE, " +
178                userId.toString() + ")";
179              dataAccess.makeInsert(query);
180            }
181            else {
182              Long pk1 = new Long(id);
183              pk = pk1.longValue();
184              query = "UPDATE   status_categories " +
185                "SET      stc_name = '" + StringFormat.toSafeOracleString(name) + "'," +
186                "         stc_interface_field_number = '" + interfaceFieldNo + "'," +
187                "         stc_last_changed_date = SYSDATE ," +
188                "         stc_last_changed_by = " + userId.toString() +
189                " WHERE    (stc_id = " + id + ") AND (stc_cmp_id = " + companyId.toString() + ")";
190              dataAccess.makeUpdate(query);
191            }
192            NodeList values1 = null;
193            if(category1.item(i).hasChildNodes()) {
194              for(int g = 0; g < category1.item(i).getChildNodes().getLength(); g++) {
195                if(category1.item(i).getChildNodes().item(g).getNodeName().equals("Values")) {
196                  values1 = category1.item(i).getChildNodes().item(g).getChildNodes();
197                  valueNum = values1.getLength();
198                }
199              }
200            }
201            for(int k = 0; k < valueNum; k++) {
202              XMLNode value = (XMLNode)values1.item(k);
203              valueId = value.valueOf("./Id");
204              valueName = value.valueOf("./Name");
205              valueInterfaceFieldNo = value.valueOf("./InterfaceFieldNo");
206              if(valueId.equals("0")) {
207                query = "  INSERT INTO STATUS_VALUES (" +
208                  "     stv_id," +
209                  "     stv_stc_id," +
210                  "     stv_name," +
211                  "     stv_interface_field_value," +
212                  "     stv_last_changed_date," +
213                  "     stv_last_changed_by" +
214                  " ) VALUES (" +
215                  (new Long(UniqueIDGenerator.instance().getNextId())).toString() + ", " +
216                  "'" + (new Long(pk)).toString() + "', " +
217                  "'" + StringFormat.toSafeOracleString(valueName) + "', " +
218                  "'" + valueInterfaceFieldNo + "', " +
219                  " SYSDATE, " +
220                  userId.toString() + ")";
221                dataAccess.makeInsert(query);
222              }
223              else {
224                query = "UPDATE   status_values " +
225                  "SET      stv_name = '" + StringFormat.toSafeOracleString(valueName) + "'," +
226                  "         stv_interface_field_value = '" + valueInterfaceFieldNo + "'," +
227                  "         stv_last_changed_date = SYSDATE ," +
228                  "         stv_last_changed_by = " + userId.toString() +
229                  " WHERE    (stv_id = " + valueId + ") AND (stv_stc_id = " + (new Long(pk)).toString() + ")";
230                dataAccess.makeUpdate(query);
231              }
232            }
233          }
234        }
235      }
236      catch(Exception e) {
237        this.context.setRollbackOnly();
238        throw new InstantbankException(e, "311052", "Failed to save status values");
239      }
240      finally {
241        try {
242          if(dataAccess != null) {
243            dataAccess.disconnect();
244          }
245        }
246        catch(Exception e) {
247        }
248      }
249    }
250  
251  
252    public void setSessionContext(SessionContext ctx) {
253      this.context = ctx;
254    }
255  }
256  
257