summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sampleapps/bigbank/webclient/src/main/java/bigbank/webclient/ui/FormServlet.java
blob: bad6d370b0e6ac9276614db2205a0a78af921262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package bigbank.webclient.ui;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osoa.sca.CurrentModuleContext;
import org.osoa.sca.ModuleContext;

import bigbank.webclient.services.profile.ProfileService;

import com.bigbank.account.AccountFactory;
import com.bigbank.account.AccountService;
import com.bigbank.account.CustomerProfileData;
import com.bigbank.account.StockSummary;

public class FormServlet extends HttpServlet {

    // private ServletContext mContext;
    // public void init(ServletConfig pCfg) throws ServletException {
    // mContext = pCfg.getServletContext();
    // }

    public void doPost(HttpServletRequest pReq, HttpServletResponse pResp) throws ServletException {

        try {
            final String action = pReq.getParameter("action");
            ModuleContext moduleContext = CurrentModuleContext.getContext();
            AccountService accountServices = (AccountService) moduleContext.locateService("AccountServiceComponent");
            if (accountServices == null) {
                throw new ServletException("AccountServiceComponent");
            }
            ProfileService profileServices = null;
            if (!"createAccount".equals(action)) {
                profileServices = (ProfileService) moduleContext.locateService("ProfileServiceComponent");
                if (profileServices == null) {
                    throw new ServletException("ProfileServiceComponent not found.");
                }
                if (!profileServices.isLoggedIn()) {
                    throw new ServletException("User id '" + profileServices.getId() + "' not logged on.");
                }
            }

            if ("createAccount".equals(action)) {
                createAccount(pReq, pResp, accountServices);
            } else if ("account".equals(action)) {
                accountTransaction(pReq, pResp, accountServices);
            } else if ("stockPurchase".equals(action)) {
                stockPurchase(pReq, pResp, profileServices, accountServices);
            } else if ("stockSale".equals(action)) {
                stockSale(pReq, pResp, profileServices, accountServices);
            } else {
                throw new IllegalArgumentException("Unknown action in Form servlet '" + action + "'.");
            }
            // mContext.getRequestDispatcher("summary.jsp").forward(pReq, pResp);
            pResp.sendRedirect("summary.jsp");
        } catch (ServletException e) {
            e.printStackTrace();
            throw e;

        } catch (Exception e) {

            throw new ServletException(e);
        }

    }

    private void stockSale(HttpServletRequest req, HttpServletResponse resp, ProfileService profileServices, AccountService accountServices)
            throws ServletException {
        try {
            if (!"cancel".equals(req.getParameter("cancel"))) {

                int quantity = Integer.parseInt(req.getParameter("quantity"));
                int purchaseLotNumber = Integer.parseInt(req.getParameter("purchaseLotNumber"));
                accountServices.sellStock(purchaseLotNumber, quantity);
            }

        } catch (Exception e) {

            throw new ServletException("stockSale " + e.getMessage(), e);
        }

    }

    private void stockPurchase(HttpServletRequest req, HttpServletResponse resp, ProfileService profileServices, AccountService accountServices)
            throws ServletException {
        try {
            if (!"cancel".equals(req.getParameter("cancel"))) {

                String symbol = req.getParameter("symbol").trim().toUpperCase();
                int quantity = Integer.parseInt(req.getParameter("quantity"));
                StockSummary stockSummry = AccountFactory.INSTANCE.createStockSummary();
                stockSummry.setSymbol(symbol);
                stockSummry.setQuantity(quantity);
                accountServices.purchaseStock(profileServices.getId(), stockSummry);
            }
        } catch (Exception e) {
            throw new ServletException("stockPurchase " + e.getMessage(), e);
        }
    }

    private void accountTransaction(HttpServletRequest req, HttpServletResponse resp, AccountService accountServices) throws ServletException {
        try {
            if (!"cancel".equals(req.getParameter("cancel"))) {
                String account = req.getParameter("account");
                String amount = req.getParameter("Amount");
                if ("deposit".equals(req.getParameter("actionType")))
                    accountServices.deposit(account, Float.parseFloat(amount));
                else
                    accountServices.withdraw(account, Float.parseFloat(amount));
            }
        } catch (Exception e) {
            throw new ServletException("accountTransaction " + e.getMessage(), e);
        }

    }

    private void createAccount(HttpServletRequest pReq, HttpServletResponse pResp, AccountService accountServices) throws ServletException {
        try {
            CustomerProfileData customerProfileData = AccountFactory.INSTANCE.createCustomerProfileData();
            customerProfileData.setFirstName(pReq.getParameter("firstName"));
            customerProfileData.setLastName(pReq.getParameter("lastName"));
            customerProfileData.setAddress(pReq.getParameter("address"));
            customerProfileData.setEmail(pReq.getParameter("email"));
            customerProfileData.setLoginID(pReq.getParameter("loginID"));
            customerProfileData.setPassword(pReq.getParameter("password"));

            CustomerProfileData resp = accountServices.createAccount(customerProfileData, "savings".equals(pReq.getParameter("savings")), "checkings"
                    .equals(pReq.getParameter("checkings")));
            LoginServlet.login(resp.getLoginID(), resp.getPassword());

        } catch (IOException e) {
            throw new ServletException(e);
        }

    }
}