1 package com.instantbank.collections.creditInfo.web;
2
3 import javax.servlet.RequestDispatcher;
4 import javax.servlet.ServletConfig;
5 import javax.servlet.ServletContext;
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11 import java.io.ByteArrayInputStream;
12 import java.io.IOException;
13 import java.io.PrintWriter;
14 import java.io.StringWriter;
15 import oracle.xml.parser.v2.DOMParser;
16 import oracle.xml.parser.v2.XMLDocument;
17 import oracle.xml.parser.v2.XMLElement;
18 import org.xml.sax.SAXException;
19 import com.instantbank.collections.creditInfo.ejb.AccountInfoServices;
20 import com.instantbank.collections.creditInfo.ejb.AccountInfoServicesHome;
21 import com.instantbank.collections.util.FilterChain;
22 import com.instantbank.collections.util.InstantbankException;
23 import com.instantbank.collections.util.ServiceLocator;
24 import com.instantbank.collections.util.XMLUtils;
25
26 public class AccountMaintenanceController extends HttpServlet {
27 private AccountInfoServices AccServices;
28 private Long companyId;
29 private String debug;
30 HttpSession session;
31 private String typeSel;
32 private Long userId;
33
34
35 private String createMaintenanceXML(String collateral, String misc, String agrmId) throws SAXException, ServletException, IOException, InstantbankException {
36 XMLDocument docColl;
37 XMLDocument docMisc;
38 XMLElement element;
39 StringWriter sw;
40 PrintWriter pw;
41 String xml;
42 String xmlAgrId;
43 String xmlAgrCode;
44
45 docColl = parseInfo(collateral);
46 docMisc = parseInfo(misc);
47 element = (XMLElement)docColl.getDocumentElement();
48 sw = new StringWriter();
49 pw = new PrintWriter(sw);
50 element.print(pw);
51 xml = sw.toString();
52 element = (XMLElement)docMisc.getDocumentElement();
53 sw = new StringWriter();
54 pw = new PrintWriter(sw);
55 element.print(pw);
56 xmlAgrId = makeXMLAttributes("agrmid", agrmId);
57 xml = xmlAgrId + xml + sw.toString();
58 xml = makeXMLNode("Maintenance", xml, true);
59 return xml;
60 }
61
62
63 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
64 doPost(request, response);
65 }
66
67
68 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69 String action;
70 com.instantbank.collections.util.FilterChain chain;
71 try {
72 debug = "set action";
73 action = request.getParameter("action");
74 try {
75
76 chain = new com.instantbank.collections.util.FilterChain(true, getServletConfig().getServletContext());
77 if(chain.processFilter(request, response)) {
78 return;
79 }
80 if(action.equals("initialize")) {
81 initialize(request, response);
82 }
83 else if(action.equals("save")) {
84 save(request, response);
85 }
86 else {
87 throw new InstantbankException("242001", "Action " + action + " not supported");
88 }
89 }
90 catch(Exception e) {
91 throw new InstantbankException(e, "242002", "Failed to execute controller action " + action);
92 }
93 }
94 catch(InstantbankException e) {
95 session.setAttribute("Exception", e);
96 response.sendRedirect("../main_web/ControllerError.jsp?showTechnical=0");
97 }
98 }
99
100
101 public String getServletInfo() {
102 return "com.instantbank.collections.creditInfo.web.AccountMaintenanceController Information";
103 }
104
105
106 public void init(ServletConfig config) throws ServletException {
107 super.init(config);
108 try {
109 AccountInfoServicesHome Home = (AccountInfoServicesHome)
110 ServiceLocator.instance().createEJB("AccountInfoServicesHome", AccountInfoServicesHome.class, false);
111 AccServices = Home.create();
112 }
113 catch(Exception e) {
114 throw new ServletException(e);
115 }
116 }
117
118
119 private void initialize(HttpServletRequest request, HttpServletResponse response) throws SAXException, ServletException, IOException, InstantbankException {
120 Long agrmId;
121 String collateral;
122 String data;
123 String misc;
124 HttpSession session;
125
126 session = request.getSession();
127 companyId = (Long)session.getAttribute("companyId");
128 agrmId = new Long(request.getAttribute("agrmId").toString());
129
130 collateral = AccServices.getCollateralInfo(companyId, agrmId);
131 misc = AccServices.getAgreementDetails(companyId, agrmId, false, false, false, true);
132 data = createMaintenanceXML(collateral, misc, agrmId.toString());
133 ServletContext sc = getServletConfig().getServletContext();
134 request.setAttribute("data", data);
135 RequestDispatcher rd = sc.getRequestDispatcher("/creditInfo_web/AccountMaintenanceView.jsp");
136 rd.forward(request, response);
137 }
138
139
140 private String makeXMLAttributes(String attributeNode, String valueNode) throws ServletException, IOException, InstantbankException {
141 char Ch3 = '/';
142 String XMLtmp;
143
144 XMLtmp = "<" + attributeNode + ">" + valueNode + "<" + Ch3 + attributeNode + ">\n";
145 return XMLtmp;
146 }
147
148
149 private String makeXMLElement(String element, String XMLAttributes) throws ServletException, IOException, InstantbankException {
150 char Ch3 = '/';
151 String XMLtmp;
152
153 XMLtmp = "<" + element + ">\n" + XMLAttributes +
154 "<" + Ch3 + element + ">\n";
155 return XMLtmp;
156 }
157
158
159 private String makeXMLNode(String nameNode, String XMLElement, boolean withHeader) throws ServletException, IOException, InstantbankException {
160 char Ch1 = '"';
161 char Ch3 = '/';
162 String XMLtmp;
163
164 if(withHeader) {
165 XMLtmp = XMLUtils.xmlHeader() + "\n";
166 }
167 else {
168 XMLtmp = "";
169 }
170 XMLtmp = XMLtmp + "<" + nameNode + "> \n" + XMLElement + "<" + Ch3 + nameNode + ">\n";
171 return XMLtmp;
172 }
173
174
175 private XMLDocument parseInfo(String data) throws SAXException, ServletException, IOException, InstantbankException {
176 DOMParser docParser = new DOMParser();
177 ByteArrayInputStream stream;
178 XMLDocument xmlDoc;
179
180 xmlDoc = null;
181 stream = new ByteArrayInputStream(data.getBytes());
182 docParser.setValidationMode(false);
183 docParser.parse(stream);
184 xmlDoc = docParser.getDocument();
185 return xmlDoc;
186 }
187
188
189 private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException {
190 Long agrmId;
191 String data;
192 String mainData;
193 String wrkiId;
194
195 session = request.getSession();
196 companyId = (Long)session.getAttribute("companyId");
197 userId = (Long)session.getAttribute("userId");
198 agrmId = new Long((String)request.getParameter("agrmId"));
199 data = (String)request.getParameter("data");
200 mainData = request.getParameter("mainData");
201 wrkiId = request.getParameter("wrki_id");
202 request.setAttribute("agrmId", (String)request.getParameter("agrmId"));
203 AccServices.saveMaintenanceData(data, companyId, userId);
204 response.sendRedirect("/Instantbank/collectionsActivities_web/ReviewAccountController?action=changeTab&mainData=" + mainData + "&tab=Maintenance&agrmId=" + agrmId + "&wrki_id=" + wrkiId);
205 }
206 }
207
208