1 package com.instantbank.collections.companyInfo.web;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.OutputStreamWriter;
6 import java.io.PrintWriter;
7 import javax.servlet.RequestDispatcher;
8 import javax.servlet.ServletConfig;
9 import javax.servlet.ServletContext;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15 import oracle.xml.parser.v2.DOMParser;
16 import oracle.xml.parser.v2.XMLDocument;
17 import oracle.xml.parser.v2.XSLException;
18 import org.xml.sax.SAXException;
19 import com.instantbank.collections.basicInfo.ejb.BasicInfoServices;
20 import com.instantbank.collections.basicInfo.ejb.BasicInfoServicesHome;
21 import com.instantbank.collections.companyInfo.ejb.CompanyServices;
22 import com.instantbank.collections.companyInfo.ejb.CompanyServicesHome;
23 import com.instantbank.collections.security.ejb.SecurityProfilesServices;
24 import com.instantbank.collections.security.ejb.SecurityProfilesServicesHome;
25 import com.instantbank.collections.security.ejb.SecurityServices;
26 import com.instantbank.collections.security.ejb.SecurityServicesHome;
27 import com.instantbank.collections.util.FilterChain;
28 import com.instantbank.collections.util.InstantbankException;
29 import com.instantbank.collections.util.ServiceLocator;
30 import com.instantbank.collections.util.StringFormat;
31
32 public class CompanyMaintenanceController extends HttpServlet {
33 private BasicInfoServices basicServices;
34 private SecurityServices servicesSecurity;
35 private SecurityProfilesServices PrfServices;
36 private CompanyServices services;
37 private HttpSession session;
38
39
40 public void doGet(HttpServletRequest request, HttpServletResponse response) throws
41 ServletException, IOException {
42
43 doPost(request, response);
44 }
45
46
47 public void doPost(HttpServletRequest request, HttpServletResponse response) throws
48 ServletException, IOException {
49
50 String action;
51 com.instantbank.collections.util.FilterChain chain;
52
53 try {
54 session = request.getSession(false);
55 action = request.getParameter("action");
56 try {
57
58 chain = new com.instantbank.collections.util.FilterChain(true, getServletConfig().getServletContext());
59 if(chain.processFilter(request, response)) {
60 return;
61 }
62 if(action.equals("initialize")) {
63 initialize(request, response);
64 }
65 else if(action.equals("getStates")) {
66 getStates(request, response);
67 }
68 else if(action.equals("save")) {
69 save(request, response);
70 }
71 else if(action.equals("reset")) {
72 reset(request, response);
73 }
74 else {
75 throw new InstantbankException("122001", "Action " + action + " not supported");
76 }
77 }
78 catch(Exception e) {
79 throw new InstantbankException(e, "122002", "Failed to execute controller action " + action);
80 }
81 }
82 catch(InstantbankException e) {
83 session.setAttribute("Exception", e);
84 response.sendRedirect("../main_web/ControllerError.jsp?showTechnical=0");
85 }
86 }
87
88
89 private String getCompanyId(String xml) throws ServletException,
90 IOException, InstantbankException, SAXException, XSLException {
91
92 DOMParser docParser = new DOMParser();
93 String Id;
94 ByteArrayInputStream stream;
95 XMLDocument xmlDoc = null;
96
97 stream = new ByteArrayInputStream(xml.getBytes());
98 docParser.setValidationMode(false);
99 docParser.parse(stream);
100 xmlDoc = docParser.getDocument();
101 Id = xmlDoc.selectNodes("/Company/Id/text()").item(0).getNodeValue();
102 return Id;
103 }
104
105
106 public String getServletInfo() {
107 return "com.instantbank.collections.companyInfo.web.CompanyMaintenanceController Information";
108 }
109
110
111 private void getStates(HttpServletRequest request, HttpServletResponse response) throws
112 InstantbankException, ServletException, IOException {
113
114 String countryId = request.getParameter("countryId");
115 OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
116 String xml = null;
117
118 xml = basicServices.getStates(countryId);
119 PrintWriter out = new PrintWriter(osw);
120 response.setContentType("text/html");
121 out.println("<html>");
122 out.println("<script language=JavaScript>");
123 out.println("parent.statesXML='" + StringFormat.cleanXml(xml) + "';");
124 out.println("parent.loadStates();");
125 out.println("</script></html>");
126 out.close();
127 }
128
129
130 public void init(ServletConfig config) throws ServletException {
131 super.init(config);
132 try {
133 CompanyServicesHome home = (CompanyServicesHome)ServiceLocator.instance().createEJB("CompanyServicesHome", CompanyServicesHome.class, false);
134 services = home.create();
135 BasicInfoServicesHome basicHome = (BasicInfoServicesHome)ServiceLocator.instance().createEJB("BasicInfoServicesHome", BasicInfoServicesHome.class, false);
136 basicServices = basicHome.create();
137 SecurityServicesHome homeSecurity = (SecurityServicesHome)ServiceLocator.instance().createEJB("SecurityServicesHome", SecurityServicesHome.class, false);
138 servicesSecurity = homeSecurity.create();
139
140 SecurityProfilesServicesHome PrfHome = (SecurityProfilesServicesHome)ServiceLocator.instance().createEJB("SecurityProfilesServicesHome", SecurityProfilesServicesHome.class, false);
141 PrfServices = PrfHome.create();
142
143 }
144 catch(Exception e) {
145 throw new ServletException(e);
146 }
147 }
148
149
150 private void initialize(HttpServletRequest request, HttpServletResponse response) throws InstantbankException, ServletException, IOException {
151
152 Long companyId;
153 String xml;
154 String xmlSec = "";
155 Long sprfId;
156
157 companyId = (Long)session.getAttribute("companyId");
158 xml = services.getCompany(companyId.toString());
159
160 sprfId = PrfServices.getCompanyProfile(companyId);
161 if(!sprfId.equals(new Long("0"))) {
162 xmlSec = PrfServices.getProfiles(sprfId, new String("C"));
163 }
164 else {
165 xmlSec = PrfServices.newProfiles(companyId);
166 }
167
168 request.setAttribute("title", "Company Maintenance");
169 request.setAttribute("controller", "CompanyMaintenanceController");
170 request.setAttribute("data", xml);
171 request.setAttribute("dataSec", xmlSec);
172 request.setAttribute("prfId", sprfId.toString());
173 ServletContext sc = getServletConfig().getServletContext();
174 RequestDispatcher rd = sc.getRequestDispatcher("/companyInfo_web/CompanyView.jsp");
175 rd.forward(request, response);
176 }
177
178
179 private void reset(HttpServletRequest request, HttpServletResponse response) throws
180 InstantbankException, IOException {
181
182 Long companyId;
183 Long userId;
184 String xml;
185
186 companyId = (Long)session.getAttribute("companyId");
187 userId = (Long)session.getAttribute("userId");
188 servicesSecurity.resetAllPassword(companyId, userId);
189 }
190
191
192 private void save(HttpServletRequest request, HttpServletResponse response) throws
193 InstantbankException, ServletException, IOException, XSLException, SAXException {
194
195 Long userId;
196 Long sprfId;
197 String xml;
198 String xmlSec;
199 String companyId;
200 String companyreturn;
201
202 userId = (Long)session.getAttribute("userId");
203
204 xml = request.getParameter("data");
205 xmlSec = request.getParameter("dataSec");
206 companyId = getCompanyId(xml);
207
208 companyreturn = services.saveCompany(xml, userId.longValue());
209 PrfServices.saveDefaultProfile(xmlSec, new Long(companyId));
210 sprfId = PrfServices.getCompanyProfile(new Long(companyId));
211 xmlSec = PrfServices.getProfiles(sprfId, new String("C"));
212 xml = services.getCompany(companyreturn);
213
214 ServletContext sc = getServletConfig().getServletContext();
215 request.setAttribute("title", "Company Maintenance");
216 request.setAttribute("controller", "CompanyMaintenanceController");
217 request.setAttribute("data", xml);
218 request.setAttribute("dataSec", xmlSec);
219 request.setAttribute("prfId", sprfId.toString());
220 response.setContentType("text/html");
221 RequestDispatcher rd = sc.getRequestDispatcher("/companyInfo_web/CompanyView.jsp");
222 rd.forward(request, response);
223 }
224 }
225