KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > web > Dispatcher


1 /*
2  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28
29 package com.sun.ebank.web;
30
31 import net.sf.jasperreports.ebank.ReportBean;
32
33 import javax.servlet.*;
34 import javax.servlet.http.*;
35 import com.sun.ebank.util.Debug;
36 import java.util.*;
37 import java.math.BigDecimal JavaDoc;
38
39
40 public class Dispatcher extends HttpServlet {
41     public void doGet(HttpServletRequest request, HttpServletResponse response) {
42         HttpSession session = request.getSession();
43
44         BeanManager beanManager =
45             (BeanManager) session.getAttribute("beanManager");
46
47         if (beanManager == null) {
48             Debug.print("Creating bean manager.");
49             beanManager = new BeanManager();
50             session.setAttribute("beanManager", beanManager);
51         }
52
53         String JavaDoc selectedScreen = request.getServletPath();
54         Debug.print(selectedScreen);
55
56         CustomerBean customerBean = new CustomerBean();
57         customerBean.setBeanManager(beanManager);
58         request.setAttribute("customerBean", customerBean);
59
60         if (selectedScreen.equals("/accountHist")) {
61             AccountHistoryBean accountHistoryBean = new AccountHistoryBean();
62             request.setAttribute("accountHistoryBean", accountHistoryBean);
63
64             try {
65                 accountHistoryBean.setBeanManager(beanManager);
66                 accountHistoryBean.setCustomerBean(customerBean);
67                 accountHistoryBean.setAccountId(request.getParameter(
68                         "accountId"));
69                 accountHistoryBean.setSortOption(Integer.parseInt(
70                         request.getParameter("sortOption")));
71                 accountHistoryBean.setActivityView(Integer.parseInt(
72                         request.getParameter("activityView")));
73                 accountHistoryBean.setDate(Integer.parseInt(
74                         request.getParameter("date")));
75                 accountHistoryBean.setYear(Integer.parseInt(
76                         request.getParameter("year")));
77                 accountHistoryBean.setSinceDay(Integer.parseInt(
78                         request.getParameter("sinceDay")));
79                 accountHistoryBean.setSinceMonth(Integer.parseInt(
80                         request.getParameter("sinceMonth")));
81                 accountHistoryBean.setBeginDay(Integer.parseInt(
82                         request.getParameter("beginDay")));
83                 accountHistoryBean.setBeginMonth(Integer.parseInt(
84                         request.getParameter("beginMonth")));
85                 accountHistoryBean.setEndDay(Integer.parseInt(
86                         request.getParameter("endDay")));
87                 accountHistoryBean.setEndMonth(Integer.parseInt(
88                         request.getParameter("endMonth")));
89             } catch (NumberFormatException JavaDoc e) {
90             }
91
92             accountHistoryBean.doTx();
93         }
94         else if (selectedScreen.equals("/accountHistJR")) {
95             ReportBean reportBean =
96                 (ReportBean) session.getAttribute("reportBean");
97
98             if (reportBean == null) {
99                 reportBean = new ReportBean();
100                 session.setAttribute("reportBean", reportBean);
101             }
102
103             reportBean.setServletContext(getServletContext());
104             reportBean.setLocale(request.getLocale());
105
106             AccountHistoryBean accountHistoryBean = new AccountHistoryBean();
107             reportBean.setAccountHistoryBean(accountHistoryBean);
108             reportBean.setCustomerBean(customerBean);
109
110             try {
111                 accountHistoryBean.setBeanManager(beanManager);
112                 accountHistoryBean.setCustomerBean(customerBean);
113                 accountHistoryBean.setAccountId(request.getParameter(
114                         "accountId"));
115                 accountHistoryBean.setSortOption(Integer.parseInt(
116                         request.getParameter("sortOption")));
117                 accountHistoryBean.setActivityView(Integer.parseInt(
118                         request.getParameter("activityView")));
119                 accountHistoryBean.setDate(Integer.parseInt(
120                         request.getParameter("date")));
121                 accountHistoryBean.setYear(Integer.parseInt(
122                         request.getParameter("year")));
123                 accountHistoryBean.setSinceDay(Integer.parseInt(
124                         request.getParameter("sinceDay")));
125                 accountHistoryBean.setSinceMonth(Integer.parseInt(
126                         request.getParameter("sinceMonth")));
127                 accountHistoryBean.setBeginDay(Integer.parseInt(
128                         request.getParameter("beginDay")));
129                 accountHistoryBean.setBeginMonth(Integer.parseInt(
130                         request.getParameter("beginMonth")));
131                 accountHistoryBean.setEndDay(Integer.parseInt(
132                         request.getParameter("endDay")));
133                 accountHistoryBean.setEndMonth(Integer.parseInt(
134                         request.getParameter("endMonth")));
135
136                 reportBean.setDisplayOption(Integer.parseInt(
137                         request.getParameter("displayOption")));
138             } catch (NumberFormatException JavaDoc e) {
139             }
140
141             reportBean.fillReport();
142         }
143
144         try {
145             Debug.print("Forwarding to template.");
146             request.getRequestDispatcher("/template/template.jsp")
147                    .forward(request, response);
148         } catch (Exception JavaDoc ex) {
149             ex.printStackTrace();
150         }
151     }
152
153     public void doPost(HttpServletRequest request, HttpServletResponse response) {
154         HttpSession session = request.getSession();
155         ResourceBundle messages =
156             (ResourceBundle) session.getAttribute("messages");
157
158         if (messages == null) {
159             Locale locale = request.getLocale();
160             Debug.print(locale.getDisplayName());
161             messages = ResourceBundle.getBundle("WebMessages", locale);
162
163             if (messages == null) {
164                 Debug.print("Didn't locate resource bundle.");
165             }
166
167             session.setAttribute("messages", messages);
168         }
169
170         String JavaDoc selectedScreen = request.getServletPath();
171         Debug.print(selectedScreen);
172
173         BeanManager beanManager =
174             (BeanManager) session.getAttribute("beanManager");
175
176         if (beanManager == null) {
177             beanManager = new BeanManager();
178             session.setAttribute("beanManager", beanManager);
179         }
180
181         CustomerBean customerBean = new CustomerBean();
182         customerBean.setBeanManager(beanManager);
183         request.setAttribute("customerBean", customerBean);
184
185         if (selectedScreen.equals("/accountHist")) {
186             AccountHistoryBean accountHistoryBean = new AccountHistoryBean();
187             request.setAttribute("accountHistoryBean", accountHistoryBean);
188
189             try {
190                 accountHistoryBean.setBeanManager(beanManager);
191                 accountHistoryBean.setCustomerBean(customerBean);
192                 accountHistoryBean.setAccountId(request.getParameter(
193                         "accountId"));
194                 accountHistoryBean.setSortOption(Integer.parseInt(
195                         request.getParameter("sortOption")));
196                 accountHistoryBean.setActivityView(Integer.parseInt(
197                         request.getParameter("activityView")));
198                 accountHistoryBean.setYear(Integer.parseInt(
199                         request.getParameter("year")));
200                 accountHistoryBean.setDate(Integer.parseInt(
201                         request.getParameter("date")));
202                 accountHistoryBean.setSinceDay(Integer.parseInt(
203                         request.getParameter("sinceDay")));
204                 accountHistoryBean.setSinceMonth(Integer.parseInt(
205                         request.getParameter("sinceMonth")));
206                 accountHistoryBean.setBeginDay(Integer.parseInt(
207                         request.getParameter("beginDay")));
208                 accountHistoryBean.setBeginMonth(Integer.parseInt(
209                         request.getParameter("beginMonth")));
210                 accountHistoryBean.setEndDay(Integer.parseInt(
211                         request.getParameter("endDay")));
212                 accountHistoryBean.setEndMonth(Integer.parseInt(
213                         request.getParameter("endMonth")));
214             } catch (NumberFormatException JavaDoc e) {
215                 // Not possible
216
}
217
218             accountHistoryBean.doTx();
219         } else if (selectedScreen.equals("/accountHistJR")) {
220             ReportBean reportBean =
221                 (ReportBean) session.getAttribute("reportBean");
222
223             if (reportBean == null) {
224                 reportBean = new ReportBean();
225                 session.setAttribute("reportBean", reportBean);
226             }
227
228             reportBean.setServletContext(getServletContext());
229             reportBean.setLocale(request.getLocale());
230
231             if (request.getParameter("btnFirst") != null) {
232                 reportBean.setFirstPage();
233             } else if (request.getParameter("btnPrevious") != null) {
234                 reportBean.setPreviousPage();
235             } else if (request.getParameter("btnNext") != null) {
236                 reportBean.setNextPage();
237             } else if (request.getParameter("btnLast") != null) {
238                 reportBean.setLastPage();
239             } else {
240                 AccountHistoryBean accountHistoryBean = new AccountHistoryBean();
241                 reportBean.setAccountHistoryBean(accountHistoryBean);
242
243                 try {
244                     accountHistoryBean.setBeanManager(beanManager);
245                     accountHistoryBean.setCustomerBean(customerBean);
246                     accountHistoryBean.setAccountId(request.getParameter(
247                             "accountId"));
248                     accountHistoryBean.setSortOption(Integer.parseInt(
249                             request.getParameter("sortOption")));
250                     accountHistoryBean.setActivityView(Integer.parseInt(
251                             request.getParameter("activityView")));
252                     accountHistoryBean.setYear(Integer.parseInt(
253                             request.getParameter("year")));
254                     accountHistoryBean.setDate(Integer.parseInt(
255                             request.getParameter("date")));
256                     accountHistoryBean.setSinceDay(Integer.parseInt(
257                             request.getParameter("sinceDay")));
258                     accountHistoryBean.setSinceMonth(Integer.parseInt(
259                             request.getParameter("sinceMonth")));
260                     accountHistoryBean.setBeginDay(Integer.parseInt(
261                             request.getParameter("beginDay")));
262                     accountHistoryBean.setBeginMonth(Integer.parseInt(
263                             request.getParameter("beginMonth")));
264                     accountHistoryBean.setEndDay(Integer.parseInt(
265                             request.getParameter("endDay")));
266                     accountHistoryBean.setEndMonth(Integer.parseInt(
267                             request.getParameter("endMonth")));
268
269                     reportBean.setDisplayOption(Integer.parseInt(
270                             request.getParameter("displayOption")));
271                 } catch (NumberFormatException JavaDoc e) {
272                     // Not possible
273
}
274
275                 reportBean.fillReport();
276             }
277         } else if (selectedScreen.equals("/transferAck")) {
278             String JavaDoc fromAccountId = request.getParameter("fromAccountId");
279             String JavaDoc toAccountId = request.getParameter("toAccountId");
280
281             if ((fromAccountId == null) || (toAccountId == null)) {
282                 request.setAttribute("errorMessage",
283                     messages.getString("AccountError"));
284
285                 try {
286                     request.getRequestDispatcher("/error.jsp")
287                            .forward(request, response);
288                 } catch (Exception JavaDoc ex) {
289                 }
290             } else {
291                 TransferBean transferBean = new TransferBean();
292                 request.setAttribute("transferBean", transferBean);
293
294                 try {
295                     transferBean.setMessages(messages);
296                     transferBean.setBeanManager(beanManager);
297                     transferBean.setFromAccountId(fromAccountId);
298                     transferBean.setToAccountId(toAccountId);
299                     transferBean.setTransferAmount((new BigDecimal JavaDoc(
300                             request.getParameter("transferAmount"))).setScale(2));
301
302                     String JavaDoc errorMessage = transferBean.doTx();
303
304                     if (errorMessage != null) {
305                         request.setAttribute("errorMessage", errorMessage);
306
307                         try {
308                             request.getRequestDispatcher("/error.jsp")
309                                    .forward(request, response);
310                         } catch (Exception JavaDoc ex) {
311                         }
312                     }
313                 } catch (NumberFormatException JavaDoc e) {
314                     request.setAttribute("errorMessage",
315                         messages.getString("AmountError"));
316
317                     try {
318                         request.getRequestDispatcher("/error.jsp")
319                                .forward(request, response);
320                     } catch (Exception JavaDoc ex) {
321                     }
322                 }
323             }
324         } else if (selectedScreen.equals("/atmAck")) {
325             ATMBean atmBean = new ATMBean();
326             request.setAttribute("atmBean", atmBean);
327
328             try {
329                 customerBean.setBeanManager(beanManager);
330                 atmBean.setMessages(messages);
331                 atmBean.setBeanManager(beanManager);
332                 atmBean.setCustomerBean(customerBean);
333                 atmBean.setAccountId(request.getParameter("accountId"));
334                 atmBean.setAmount((new BigDecimal JavaDoc(request.getParameter("amount"))).setScale(
335                         2));
336                 atmBean.setOperation(Integer.parseInt(request.getParameter(
337                             "operation")));
338
339                 String JavaDoc errorMessage = atmBean.doTx();
340
341                 if (errorMessage != null) {
342                     request.setAttribute("errorMessage", errorMessage);
343
344                     try {
345                         request.getRequestDispatcher("/error.jsp")
346                                .forward(request, response);
347                     } catch (Exception JavaDoc ex) {
348                     }
349                 }
350             } catch (NumberFormatException JavaDoc e) {
351                 request.setAttribute("errorMessage",
352                     messages.getString("AmountError"));
353
354                 try {
355                     request.getRequestDispatcher("/error.jsp")
356                            .forward(request, response);
357                 } catch (Exception JavaDoc ex) {
358                 }
359             }
360         }
361
362         try {
363             request.getRequestDispatcher("/template/template.jsp")
364                    .forward(request, response);
365         } catch (Exception JavaDoc ex) {
366         }
367     }
368 }
369
Popular Tags