1    package com.instantbank.collections.commonQueuing.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.IOException;
12   import java.io.OutputStreamWriter;
13   import java.io.PrintWriter;
14   import javax.ejb.FinderException;
15   import com.instantbank.collections.commonQueuing.ejb.QueueServices;
16   import com.instantbank.collections.commonQueuing.ejb.QueueServicesHome;
17   import com.instantbank.collections.util.FilterChain;
18   import com.instantbank.collections.util.InstantbankException;
19   import com.instantbank.collections.util.ServiceLocator;
20   import com.instantbank.collections.util.StringFormat;
21   
22   public class DownloadController extends HttpServlet {
23     private String debug = "";
24     private QueueServices services;
25     private HttpSession session;
26   
27   
28     public void init(ServletConfig config) throws ServletException {
29       super.init(config);
30       try {
31         QueueServicesHome home = (QueueServicesHome)ServiceLocator.instance().createEJB("QueueServicesHome", QueueServicesHome.class, false);
32         services = home.create();
33       }
34       catch(Exception e) {}
35     }
36   
37   
38     public void dispatch(HttpServletRequest request, HttpServletResponse response, String action) throws ServletException, IOException, FinderException {
39       String useCase = "";
40   
41       if(action.equals("preview")) {
42         useCase = "PreviewDownload";
43       }
44       else if(action.equals("generate")) {
45         useCase = "GenerateDownloadAdHoc";
46       }
47       else if(action.equals("distribute")) {
48         useCase = "DistributeDownloadAdHoc";
49       }
50       ServletContext sc = getServletConfig().getServletContext();
51       RequestDispatcher rd = sc.getRequestDispatcher("/commonQueuing_web/CommonQueuingDispatcher.jsp?useCase=" + useCase);
52       rd.forward(request, response);
53     }
54   
55   
56     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57       doPost(request, response);
58     }
59   
60   
61     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62       String action = "";
63       com.instantbank.collections.util.FilterChain chain;
64   
65       try {
66         action = request.getParameter("action");
67         session = request.getSession(false);
68         session.setAttribute("hasException", "0");
69         try {
70           chain = new com.instantbank.collections.util.FilterChain(true, getServletConfig().getServletContext());
71           if(chain.processFilter(request, response)) {
72             return;
73           }
74           if(action.equals("initialize")) {
75             initialize(request, response);
76           }
77           else if(action.equals("getList")) {
78             getList(request, response);
79           }
80           else if(action.equals("save")) {
81             save(request, response);
82           }
83           else if(action.equals("preview")) {
84             dispatch(request, response, action);
85           }
86           else if(action.equals("generate")) {
87             dispatch(request, response, action);
88           }
89           else if(action.equals("distribute")) {
90             dispatch(request, response, action);
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 ServletException, InstantbankException, IOException, FinderException {
112      String responseXml = "";
113  
114      String category = request.getParameter("category");
115      Long queueType = new Long(request.getParameter("typeId"));
116      String queueName = request.getParameter("name");
117      Long rowNum = new Long(request.getParameter("rownum"));
118      Long companyId = (Long)session.getAttribute("companyId");
119      String objectType = (String)request.getParameter("objectType");
120      responseXml = services.getDownloads(companyId, objectType, category, queueType, queueName, rowNum.longValue(), 10);
121      responseXml = StringFormat.cleanXml(responseXml);
122      response.setContentType("text/html");
123      OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
124      PrintWriter out = new PrintWriter(osw);
125      out.println("<script>");
126      out.print("top.xml='");
127      out.print(responseXml);
128      out.println("';");
129      out.println("top.showDownloads();");
130      out.println("</script>");
131      out.close();
132    }
133  
134  
135    private void initialize(HttpServletRequest request, HttpServletResponse response) throws ServletException, InstantbankException, IOException, FinderException {
136      Long companyId = (Long)session.getAttribute("companyId");
137      Long downloadId;
138      String xml = null;
139  
140      downloadId = new Long(request.getParameter("downloadId"));
141      if(downloadId.longValue() == 0) {
142        xml = services.newDownload();
143      }
144      else {
145        xml = services.getDownload(downloadId, companyId);
146      }
147      request.setAttribute("data", xml);
148      ServletContext sc = getServletConfig().getServletContext();
149      RequestDispatcher rd = sc.getRequestDispatcher("/commonQueuing_web/DownloadView.jsp?useCase=SetUpDownload&title=Set Up Downloads");
150      rd.forward(request, response);
151    }
152  
153  
154    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, FinderException {
155      Long companyId = (Long)session.getAttribute("companyId");
156      String data = request.getParameter("data");
157      Long downloadId = new Long(0);
158      String objectType = (String)request.getParameter("objectType");
159      Long userId = (Long)session.getAttribute("userId");
160      Long validDownloadId = new Long(0);
161      String xml = "";
162  
163      try {
164        downloadId = services.saveDownload(companyId, userId, data);
165        xml = services.getDownload(downloadId, companyId);
166        request.setAttribute("data", xml);
167        validDownloadId = downloadId;
168      }
169      catch(Exception e) {
170        session.setAttribute("hasException", "1");
171        session.setAttribute("Exception", e);
172        request.setAttribute("data", data);
173      }
174      finally {
175        ServletContext sc = getServletConfig().getServletContext();
176        RequestDispatcher rd = sc.getRequestDispatcher("/commonQueuing_web/DownloadView.jsp?useCase=SetUpDownload&title=Set Up Downloads");
177        request.setAttribute("Reset", "DownloadController?action=initialize&objectType=" + objectType + "&downloadId=" + validDownloadId);
178        rd.forward(request, response);
179      }
180    }
181  
182  
183    public String getServletInfo() {
184      return "com.instantbank.collections.commonQueuing.web.DownloadController Information";
185    }
186  }
187  
188