View Javadoc

1   /*
2    *  
3    *  author nambi sankaran
4    *  copyright (C) 2009 nambi sankaran.
5    *
6    *  This program is free software: you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation, either version 3 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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  		// Form submission or new form to show?
69  		if (isFormSubmission(request)) {
70  			// Fetch form object from HTTP session, bind, validate, process
71  			// submission.
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  				// Cannot submit a session form if no form object is in the
79  				// session.
80  				if (logger.isDebugEnabled()) {
81  					logger.debug("Invalid submit detected: " + ex.getMessage());
82  				}
83  				return handleInvalidSubmit(request, response);
84  			}
85  
86  		} else {
87  
88  			// check whether the user is already logged in
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  				// New form to show: render form view.
99  				//showForm(request, errors, getFormView(), controlModel);
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 		// check whether the user is logged in
119 		HttpSession session = request.getSession();
120 
121 		// throw error if user is not logged in
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 		// get the user id from Session
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 		// place the order
138 		try {
139 				orderMgr.placeOrder(order);
140 				
141 				// TODO:  show proper errors in order preview in case of errors
142 			} catch (NotEnoughPositionsException e) {
143 				e.printStackTrace();
144 				errors.reject( Errors.NOT_ENOUGH_POSITIONS, Errors.getErrorMessage(Errors.NOT_ENOUGH_POSITIONS) );
145 //				Map model = errors.getModel();
146 //				model.putAll(createRefData());
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 //			Map model = errors.getModel();
156 //			model.putAll(createRefData());
157 			return new ModelAndView(getFormView()  );					
158 		} catch (InSufficientCashBalanceException e) {
159 			errors.reject( Errors.INSUFFICIENT_CASH_BALANCE, Errors.getErrorMessage(Errors.INSUFFICIENT_CASH_BALANCE) );
160 //			Map model = errors.getModel();
161 //			model.putAll(createRefData());
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 }