1    package com.instantbank.collections.commonQueuing.web;
2    
3    import java.io.IOException;
4    import java.io.OutputStreamWriter;
5    import java.io.PrintWriter;
6    import java.rmi.RemoteException;
7    import javax.ejb.FinderException;
8    import javax.servlet.RequestDispatcher;
9    import javax.servlet.ServletConfig;
10   import javax.servlet.ServletContext;
11   import javax.servlet.ServletException;
12   import javax.servlet.http.HttpServlet;
13   import javax.servlet.http.HttpServletRequest;
14   import javax.servlet.http.HttpServletResponse;
15   import javax.servlet.http.HttpSession;
16   import com.instantbank.collections.commonQueuing.ejb.QueueServices;
17   import com.instantbank.collections.commonQueuing.ejb.QueueServicesHome;
18   import com.instantbank.collections.util.FilterChain;
19   import com.instantbank.collections.util.InstantbankException;
20   import com.instantbank.collections.util.ServiceLocator;
21   import com.instantbank.collections.util.StringFormat;
22   
23   public class SetUpClassesController extends HttpServlet {
24     private String debug = "";
25     private QueueServices services;
26     private HttpSession session;
27   
28   
29     /**
30      * Initialize global variables
31      *
32      * @param config Description of the Parameter
33      * @throws ServletException Description of the Exception
34      */
35     public void init(ServletConfig config) throws ServletException {
36       super.init(config);
37       try {
38         QueueServicesHome home = (QueueServicesHome)ServiceLocator.instance().createEJB("QueueServicesHome", QueueServicesHome.class, false);
39         services = home.create();
40       }
41       catch(Exception e) {
42       }
43     }
44   
45   
46     /**
47      * Process the HTTP Get request
48      *
49      * @param request Description of the Parameter
50      * @param response Description of the Parameter
51      * @throws ServletException Description of the Exception
52      * @throws IOException Description of the Exception
53      */
54     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
55       doPost(request, response);
56     }
57   
58   
59     /**
60      * Process the HTTP Post request
61      *
62      * @param request Description of the Parameter
63      * @param response Description of the Parameter
64      * @throws ServletException Description of the Exception
65      * @throws IOException Description of the Exception
66      */
67     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
68       String action = "";
69       com.instantbank.collections.util.FilterChain chain;
70   
71       try {
72         action = request.getParameter("action");
73         session = request.getSession(false);
74         session.setAttribute("hasException", "0");
75         try {
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("openSelectClass")) {
84             openSelectClass(request, response);
85           }
86           else if(action.equals("getList")) {
87             getList(request, response);
88           }
89           else if(action.equals("save")) {
90             saveClass(request, response);
91           }
92           else {
93             throw new InstantbankException("112001", "Action " + action + " not supported");
94           }
95         }
96         catch(Exception e) {
97           throw new InstantbankException(e, "112002", "Failed to execute controller action " + action + " (" + debug + ")");
98         }
99       }
100      catch(InstantbankException e) {
101        session.setAttribute("hasException", "1");
102        session.setAttribute("Exception", e);
103        if(response.isCommitted()) {
104          return;
105        }
106        response.sendRedirect("../main_web/ControllerError.jsp?showTechnical=0");
107      }
108    }
109  
110  
111    public void getList(HttpServletRequest request, HttpServletResponse response) throws InstantbankException, IOException, RemoteException, ServletException {
112      String responseXml = "";
113  
114      OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
115      PrintWriter out = new PrintWriter(osw);
116      try {
117        String className = request.getParameter("name");
118        Long rowNum = new Long(request.getParameter("rownum"));
119        Long companyId = (Long)session.getAttribute("companyId");
120        String objectType = (String)request.getParameter("objectType");
121        responseXml = StringFormat.cleanXml(services.getClasses(companyId, objectType, className, rowNum.longValue(), 10));
122        response.setContentType("text/html");
123        out.println("<script>");
124        out.print("top.xml='");
125        out.print(responseXml);
126        out.println("';");
127        out.println("top.showClasses();");
128        out.println("</script>");
129        out.close();
130      }
131      catch(Exception e) {
132        out.println("<script>");
133        out.print("alert('" + e.getMessage() + "';");
134        out.println("</script>");
135        out.close();
136      }
137    }
138  
139  
140    private void initialize(HttpServletRequest request, HttpServletResponse response) throws ServletException, InstantbankException, IOException, RemoteException {
141      Long companyId = (Long)session.getAttribute("companyId");
142      Long classId = new Long(request.getParameter("classId"));
143      String xml = "";
144  
145      debug = "Reading Data";
146      if(classId.equals(new Long(0))) {
147        xml = services.newClass();
148      }
149      else {
150        xml = services.getClass(classId, companyId);
151      }
152      request.setAttribute("data", xml);
153      ServletContext sc = getServletConfig().getServletContext();
154      debug = "Creating Dispatcher";
155      RequestDispatcher rd = sc.getRequestDispatcher("/commonQueuing_web/ClassView.jsp?useCase=SetUpClasses&title=Set Up Classes");
156      debug = "Forwarding";
157      rd.forward(request, response);
158    }
159  
160  
161    public void openSelectClass(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
162      ServletContext sc = getServletConfig().getServletContext();
163      RequestDispatcher rd = sc.getRequestDispatcher("/commonQueuing_web/SelectClassListView.jsp?useCase=SetUpClasses&title=Select a Class");
164      rd.forward(request, response);
165    }
166  
167  
168    public void saveClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, InstantbankException, IOException, FinderException {
169      Long classId;
170      Long companyId = (Long)session.getAttribute("companyId");
171      String data = request.getParameter("data");
172      String objectType = (String)request.getParameter("objectType");
173      Long userId = (Long)session.getAttribute("userId");
174      Long validClassId;
175      String xml = "";
176  
177      validClassId = new Long(0);
178      try {
179        classId = services.saveClass(companyId, userId, data);
180        xml = services.getClass(classId, companyId);
181        request.setAttribute("data", xml);
182        validClassId = classId;
183      }
184      catch(Exception e) {
185        session.setAttribute("hasException", "1");
186        session.setAttribute("Exception", e);
187        request.setAttribute("data", data);
188      }
189      finally {
190        ServletContext sc = getServletConfig().getServletContext();
191        RequestDispatcher rd = sc.getRequestDispatcher("/commonQueuing_web/ClassView.jsp?useCase=SetUpClasses&title=Set Up Classes");
192        request.setAttribute("Reset", "SetUpClassesController?action=initialize&objectType=" + objectType + "&classId=" + validClassId);
193        rd.forward(request, response);
194      }
195    }
196  
197  
198    /**
199     * Get Servlet information
200     *
201     * @return java.lang.String
202     */
203    public String getServletInfo() {
204      return "com.instantbank.collections.commonQueuing.web.SetUpClassesController Information";
205    }
206  }
207  
208