1 package com.instantbank.collections.companyInfo.web;
2
3 import java.io.IOException;
4 import java.io.OutputStreamWriter;
5 import javax.servlet.RequestDispatcher;
6 import javax.servlet.ServletConfig;
7 import javax.servlet.ServletContext;
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13 import com.instantbank.collections.companyInfo.ejb.CompanyServices;
14 import com.instantbank.collections.companyInfo.ejb.CompanyServicesHome;
15 import com.instantbank.collections.util.FilterChain;
16 import com.instantbank.collections.util.InstantbankException;
17 import com.instantbank.collections.util.ServiceLocator;
18 import com.instantbank.collections.util.StringFormat;
19
20 public class CalendarMaintenanceController extends HttpServlet {
21 private OutputStreamWriter osw;
22 private HttpSession session;
23 private CompanyServices services;
24
25
26 public void init(ServletConfig config) throws ServletException {
27 super.init(config);
28 try {
29 CompanyServicesHome home = (CompanyServicesHome)ServiceLocator.instance().createEJB("CompanyServicesHome", CompanyServicesHome.class, false);
30 services = home.create();
31 }
32 catch(Exception e) {
33 throw new ServletException(e);
34 }
35 }
36
37
38 private void changeYear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException {
39 Long companyId;
40 String change;
41 String dayArray;
42 String year;
43 String yearPos;
44
45 companyId = new Long(request.getParameter("company"));
46 System.out.println("company=" + companyId);
47 year = request.getParameter("year");
48 dayArray = request.getParameter("days");
49 yearPos = request.getParameter("yearPos");
50 change = request.getParameter("change");
51
52
53
54 showCalendar(request, response, companyId, (Integer.parseInt(year) + Integer.parseInt(change)), yearPos);
55 }
56
57
58 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
59 doPost(request, response);
60 }
61
62
63 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
64 String action;
65 com.instantbank.collections.util.FilterChain chain;
66 try {
67 action = request.getParameter("action");
68 session = request.getSession(false);
69 session.setAttribute("hasException", "0");
70 try {
71 chain = new com.instantbank.collections.util.FilterChain(true, getServletConfig().getServletContext());
72 if(chain.processFilter(request, response)) {
73 return;
74 }
75 if(action.equals("initialize")) {
76 initialize(request, response);
77 }
78 else if(action.equals("save")) {
79 save(request, response);
80 }
81 else if(action.equals("changeYear")) {
82 changeYear(request, response);
83 }
84 else if(action.equals("saveAs")) {
85 saveAs(request, response);
86 }
87 else {
88 throw new InstantbankException("122001", "Action " + action + " not supported");
89 }
90 }
91 catch(Exception e) {
92 throw new InstantbankException(e, "122002", "Failed to execute controller action " + action);
93 }
94 }
95 catch(InstantbankException e) {
96 session.setAttribute("hasException", "1");
97 session.setAttribute("Exception", e);
98 if(response.isCommitted()) {
99 return;
100 }
101 response.sendRedirect("../main_web/ControllerError.jsp?showTechnical=0");
102 }
103 }
104
105
106 public String getServletInfo() {
107 return "com.instantbank.collections.companyInfo.web.CalendarMaintenanceController Information";
108 }
109
110
111 private void initialize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException {
112 Long companyId;
113 int currentYear;
114
115 companyId = (Long)session.getAttribute("companyId");
116 currentYear = services.getDBYear();
117 showCalendar(request, response, companyId, currentYear, new String("C"));
118 }
119
120
121 private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException {
122 Long companyId;
123 String dayArray;
124 String year;
125 String yearPos;
126
127
128 companyId = new Long(request.getParameter("company"));
129 year = request.getParameter("year");
130 dayArray = request.getParameter("days");
131 yearPos = request.getParameter("yearPos");
132 services.saveCalendar(companyId, Integer.parseInt(year), dayArray);
133 showCalendar(request, response, companyId, Integer.parseInt(year), yearPos);
134 }
135
136
137 private void saveAs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException {
138 String change;
139 Long companyId;
140 String dayArray;
141 String year;
142 String yearPos;
143
144 companyId = new Long(request.getParameter("company"));
145 year = request.getParameter("year");
146 yearPos = request.getParameter("yearPos");
147 dayArray = request.getParameter("days");
148 services.saveCalendar(companyId, Integer.parseInt(year), dayArray);
149 showCalendar(request, response, companyId, Integer.parseInt(year), yearPos);
150 }
151
152
153 private void showCalendar(HttpServletRequest request, HttpServletResponse response, Long companyId, int currentYear, String yearPosition) throws ServletException, IOException, InstantbankException {
154 String xml;
155 String xmlCompanies;
156
157 xml = services.getCalendar(companyId, currentYear);
158 xmlCompanies = services.getCompanies();
159 ServletContext sc = getServletConfig().getServletContext();
160 request.setAttribute("data", StringFormat.cleanXml(xml));
161 System.out.println("xml=" + StringFormat.cleanXml(xml));
162 request.setAttribute("xmlCompanies", xmlCompanies);
163 request.setAttribute("yearPos", yearPosition);
164 request.setAttribute("currentYear", new String(new Integer(currentYear).toString()));
165 RequestDispatcher rd = sc.getRequestDispatcher("/companyInfo_web/CalendarView.jsp");
166 rd.forward(request, response);
167 }
168 }
169
170