View Javadoc

1   /*
2    *  RegisterUserFormController
3    *
4    *  author snambi
5    *  copyright (C) 2009 nambi sankaran.
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  
22  package net.sf.emarket.web.controllers.user;
23  
24  import java.util.Date;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.logging.Logger;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  
32  import net.sf.emarket.account.domain.Account;
33  import net.sf.emarket.account.service.IAccountManagerService;
34  import net.sf.emarket.user.domain.User;
35  import net.sf.emarket.user.service.IUserManagerService;
36  import net.sf.emarket.user.service.UserAlreadyExistsException;
37  import net.sf.emarket.user.service.UserDataNotCompleteException;
38  
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.validation.BindException;
41  import org.springframework.validation.Validator;
42  import org.springframework.web.servlet.ModelAndView;
43  import org.springframework.web.servlet.mvc.SimpleFormController;
44  import org.springframework.web.servlet.view.RedirectView;
45  
46  /***
47   * RegisterUserFormController Description.
48   *
49   * @author : nambi
50   */
51  public class RegisterUserController extends SimpleFormController {
52  
53      private static Logger logger = Logger.getLogger(RegisterUserController.class.getName());
54      
55      private IUserManagerService userManager;
56      private IAccountManagerService accountMgr;
57      private Validator validator;
58  
59  
60      @Autowired
61      public void setUserManager(IUserManagerService mgr){
62          userManager = mgr;
63      }
64      
65      @Autowired
66      public void setAccountManager( IAccountManagerService mgr){
67      	accountMgr = mgr;
68      }
69  
70  
71      protected Object formBackingObject(HttpServletRequest request) throws Exception {
72      	RegisterUser registerUser = new RegisterUser();
73          System.out.println("calling formBackingObject");
74          return registerUser;
75      }
76  
77  
78      protected Map referenceData(HttpServletRequest request) throws Exception {
79      	Map<Object, Object> dataMap = new HashMap<Object, Object>();
80      	dataMap.put("time", new Date());
81      	return dataMap;
82      }
83  
84  
85      @Override
86      public ModelAndView onSubmit(Object command, BindException errors) throws ServletException {
87  
88          RegisterUser u  = (RegisterUser) command;
89  
90          User user = new User();
91          user.setId( u.getId());
92          user.setFirstName( u.getFirstName());
93          user.setPassword( u.getPassword() );
94  
95          // Call userManager to register the user
96          try {
97              userManager.addUser(user);
98          } catch (UserAlreadyExistsException e) {
99              e.printStackTrace();
100             logger.info(user.getId() + " already exists " + e.getMessage());
101 
102             Map map = errors.getModel();
103             map.put("time", "current time is great");
104 
105             errors.reject("10001","emarket Id already exists");
106             logger.info("form view "+ getFormView() );
107             return new ModelAndView( getFormView() , map);
108 
109         } catch (UserDataNotCompleteException e) {
110             e.printStackTrace();
111             logger.info("form view "+ getFormView() );
112 
113             errors.reject("10001", e.getMessage());
114             Map map = errors.getModel();
115             map.put("time", "current time is great");
116             return new ModelAndView( getFormView(), map );
117         }
118 
119         Account account = addAccount(user);
120 /*        transferCash(account);
121         transferPositions( account );*/
122 
123         //logger.fine("returing model and view");
124         logger.fine("success view "+ getSuccessView() );
125 
126         return new ModelAndView(new RedirectView(getSuccessView()));
127     }
128 
129 
130     private Account  addAccount( User user ){
131 
132         // create account, once user creation succeeds
133         // Call AccountMgr to create the account
134         Account account = new Account();
135 
136         account.setId(user.getId());
137         account.setType(Account.TYPE_CASH);
138         //TODO:  remove hardcoded currency
139         account.setCurrency( "INR");
140         //account.setCountry("India");
141 
142         accountMgr.addAccount(account);
143 
144         return account;
145     }
146 
147 /*    
148     private void transferCash( Account account ){
149         
150         Date date = new Date();
151         Timestamp createdTime = new Timestamp(date.getTime());
152 
153         // transfer 1000 from system account
154         CashTransfer transfer = new CashTransfer();
155         transfer.setAmount(1000);
156         transfer.setSenderAcctId("1000");
157         transfer.setReceiverAcctId(account.getAcctId());
158         transfer.setCreatedTime(createdTime);
159 
160         // transfer 1000 stocks from each of the STOCKS
161         try {
162             accountMgr.tranferCash(transfer);
163         } catch (NotEnoughCashException e) {
164             e.printStackTrace();
165         } catch (NotValidAccountException e) {
166             e.printStackTrace();
167         }
168     }
169 
170     private void transferPositions( Account  account ){
171 
172         // Add 1000 stcoks of each share
173  
174         AcctPositionTransfer blrtransfer = new AcctPositionTransfer();
175         blrtransfer.setSenderAcctId("1000");
176         blrtransfer.setReceiverAcctId( account.getAcctId());
177         blrtransfer.setSymbol("BLR");
178         blrtransfer.setQuantity(1000);
179         blrtransfer.setPrice(10.0f);
180         blrtransfer.setType( AcctPosition.TYPE_LONG);
181 
182         AcctPositionTransfer chntransfer = new AcctPositionTransfer();
183         chntransfer.setSenderAcctId("1000");
184         chntransfer.setReceiverAcctId( account.getAcctId());
185         chntransfer.setSymbol("CHN");
186         chntransfer.setQuantity(1000);
187         chntransfer.setPrice(10.0f);
188         chntransfer.setType( AcctPosition.TYPE_LONG);
189 
190         AcctPositionTransfer dlhtransfer = new AcctPositionTransfer();
191         dlhtransfer.setSenderAcctId("1000");
192         dlhtransfer.setReceiverAcctId( account.getAcctId());
193         dlhtransfer.setSymbol("DLH");
194         dlhtransfer.setQuantity(1000);
195         dlhtransfer.setPrice(10.0f);
196         dlhtransfer.setType( AcctPosition.TYPE_LONG);
197 
198         AcctPositionTransfer hydtransfer = new AcctPositionTransfer();
199         hydtransfer.setSenderAcctId("1000");
200         hydtransfer.setReceiverAcctId( account.getAcctId());
201         hydtransfer.setSymbol("HYD");
202         hydtransfer.setQuantity(1000);
203         hydtransfer.setPrice(10.0f);
204         hydtransfer.setType( AcctPosition.TYPE_LONG);
205 
206         AcctPositionTransfer koltransfer = new AcctPositionTransfer();
207         koltransfer.setSenderAcctId("1000");
208         koltransfer.setReceiverAcctId( account.getAcctId());
209         koltransfer.setSymbol("KOL");
210         koltransfer.setQuantity(1000);
211         koltransfer.setPrice(10.0f);
212         koltransfer.setType( AcctPosition.TYPE_LONG);
213 
214         AcctPositionTransfer mumtransfer = new AcctPositionTransfer();
215         mumtransfer.setSenderAcctId("1000");
216         mumtransfer.setReceiverAcctId( account.getAcctId());
217         mumtransfer.setSymbol("MUM");
218         mumtransfer.setQuantity(1000);
219         mumtransfer.setPrice(10.0f);
220         mumtransfer.setType( AcctPosition.TYPE_LONG);
221 
222         AcctPositionTransfer puntransfer = new AcctPositionTransfer();
223         puntransfer.setSenderAcctId("1000");
224         puntransfer.setReceiverAcctId( account.getAcctId());
225         puntransfer.setSymbol("PUN");
226         puntransfer.setQuantity(1000);
227         puntransfer.setPrice(10.0f);
228         puntransfer.setType( AcctPosition.TYPE_LONG);
229 
230         AcctPositionTransfer rjdtransfer = new AcctPositionTransfer();
231         rjdtransfer.setSenderAcctId("1000");
232         rjdtransfer.setReceiverAcctId( account.getAcctId());
233         rjdtransfer.setSymbol("RJD");
234         rjdtransfer.setQuantity(1000);
235         rjdtransfer.setPrice(10.0f);
236         rjdtransfer.setType( AcctPosition.TYPE_LONG);
237 
238         try {
239             accountMgr.transferPosition(blrtransfer);
240             accountMgr.transferPosition(chntransfer);
241             accountMgr.transferPosition(dlhtransfer);
242             accountMgr.transferPosition(hydtransfer);
243             accountMgr.transferPosition(koltransfer);
244             accountMgr.transferPosition(mumtransfer);
245             accountMgr.transferPosition(puntransfer);
246             accountMgr.transferPosition(rjdtransfer);
247         } catch (NotEnoughPositionsException e) {
248             e.printStackTrace();
249         } catch (NotValidAccountException e) {
250             e.printStackTrace(); 
251         }
252     }
253 */
254 
255 }