1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.emarket.web.controllers.order;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import javax.servlet.http.HttpSession;
26
27 import net.sf.emarket.account.service.AccountNotActiveException;
28 import net.sf.emarket.account.service.InSufficientCashBalanceException;
29 import net.sf.emarket.account.service.NotEnoughPositionsException;
30 import net.sf.emarket.order.domain.Order;
31 import net.sf.emarket.order.service.IOrderManagerService;
32 import net.sf.emarket.order.service.InstrumentCannotTradeException;
33 import net.sf.emarket.order.service.WrongLimitPriceExeption;
34 import net.sf.emarket.web.Session;
35 import net.sf.emarket.web.Views;
36 import net.sf.emarket.web.controllers.Errors;
37 import net.sf.emarket.web.controllers.user.Error;
38
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.validation.BindException;
41 import org.springframework.web.HttpSessionRequiredException;
42 import org.springframework.web.bind.ServletRequestDataBinder;
43 import org.springframework.web.servlet.ModelAndView;
44 import org.springframework.web.servlet.mvc.SimpleFormController;
45 import org.springframework.web.servlet.view.RedirectView;
46
47 import java.util.Map;
48
49 public class PreviewOrderPageController extends SimpleFormController {
50
51 private IOrderManagerService orderMgr = null;
52
53 @Autowired
54 public void setOrderManager(IOrderManagerService mgr) {
55 orderMgr = mgr;
56 }
57
58 protected Object formBackingObject(HttpServletRequest request)
59 throws Exception {
60 Order order = (Order) request.getSession().getAttribute(Session.PLACED_ORDER);
61 return order;
62 }
63
64 @Override
65 public ModelAndView handleRequestInternal(HttpServletRequest request,
66 HttpServletResponse response) throws Exception {
67
68
69 if (isFormSubmission(request)) {
70
71
72 try {
73 Object command = getCommand(request);
74 ServletRequestDataBinder binder = bindAndValidate(request,command);
75 BindException errors = new BindException(binder.getBindingResult());
76 return processFormSubmission(request, response, command, errors);
77 } catch (HttpSessionRequiredException ex) {
78
79
80 if (logger.isDebugEnabled()) {
81 logger.debug("Invalid submit detected: " + ex.getMessage());
82 }
83 return handleInvalidSubmit(request, response);
84 }
85
86 } else {
87
88
89 HttpSession session = request.getSession();
90 if (!Session.isAuthenticated(session)) {
91
92 session.setAttribute(Views.NEXT_VIEW, Session
93 .getURIWithQueryString(request));
94
95 return new ModelAndView(new RedirectView(Views.LOGIN_VIEW));
96
97 } else {
98
99
100
101
102 ModelAndView formMv = showForm(request, getErrorsForNewForm(request), getFormView());
103
104 Order order = (Order) request.getSession().getAttribute(Session.PLACED_ORDER);
105 formMv.addObject("PREVIEW_ORDER", order);
106
107 return formMv;
108 }
109 }
110 }
111
112 public ModelAndView onSubmit(HttpServletRequest request,
113 HttpServletResponse response, Object command, BindException errors)
114 throws ServletException {
115
116 Order order = (Order) command;
117
118
119 HttpSession session = request.getSession();
120
121
122 if (!Session.isAuthenticated(session)) {
123 errors.reject(Error.USER_NOT_LOGGED_IN, Error.getMessage(Error.USER_NOT_LOGGED_IN));
124 session.setAttribute(Views.NEXT_VIEW, Views.ORDER_CREATE_VIEW);
125 return new ModelAndView(new RedirectView(Views.LOGIN_VIEW));
126 }
127
128
129 String id = (String) session.getAttribute(Session.ID);
130
131 if (id == null || id.equals("")) {
132 errors.reject(Error.USER_NOT_LOGGED_IN, Error.getMessage(Error.USER_NOT_LOGGED_IN));
133 session.setAttribute(Views.NEXT_VIEW, Views.ORDER_CREATE_VIEW);
134 return new ModelAndView(new RedirectView(Views.LOGIN_VIEW));
135 }
136
137
138 try {
139 orderMgr.placeOrder(order);
140
141
142 } catch (NotEnoughPositionsException e) {
143 e.printStackTrace();
144 errors.reject( Errors.NOT_ENOUGH_POSITIONS, Errors.getErrorMessage(Errors.NOT_ENOUGH_POSITIONS) );
145
146
147 return new ModelAndView(getFormView() );
148 } catch (InstrumentCannotTradeException e) {
149 e.printStackTrace();
150 errors.reject( Errors.ORDER_PREVIEW_ERROR, Errors.getErrorMessage(Errors.SYMBOL_CANNOT_TRADE) );
151 return new ModelAndView(getFormView() );
152 } catch (AccountNotActiveException e) {
153 e.printStackTrace();
154 errors.reject( Errors.ACCOUNT_NOT_ACTIVE, Errors.getErrorMessage(Errors.ACCOUNT_NOT_ACTIVE) );
155
156
157 return new ModelAndView(getFormView() );
158 } catch (InSufficientCashBalanceException e) {
159 errors.reject( Errors.INSUFFICIENT_CASH_BALANCE, Errors.getErrorMessage(Errors.INSUFFICIENT_CASH_BALANCE) );
160
161
162 return new ModelAndView(getFormView() );
163 } catch (WrongLimitPriceExeption e) {
164 e.printStackTrace();
165 errors.reject( Errors.INVALID_LIMIT_PRICE , Errors.getErrorMessage( Errors.INVALID_LIMIT_PRICE));
166 return new ModelAndView( getFormView() );
167 }
168
169 return new ModelAndView(new RedirectView(getSuccessView()));
170 }
171
172 }