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