Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
 
555 chandransh 7
import in.shop2020.model.v1.user.Sex;
8
import in.shop2020.model.v1.user.User;
9
import in.shop2020.serving.utils.Utils;
507 rajveer 10
import in.shop2020.thrift.clients.UserContextServiceClient;
11
 
12
import java.io.IOException;
13
import java.util.Date;
14
 
15
import org.apache.juli.logging.Log;
16
import org.apache.juli.logging.LogFactory;
17
import org.apache.struts2.convention.annotation.Result;
18
import org.apache.struts2.convention.annotation.Results;
19
 
20
/**
21
 * 
22
 * @author rajveer
23
 *
24
 */
25
 
550 rajveer 26
@Results({
27
    @Result(name="success", type="redirectAction", 
28
    		params = {"actionName" , "home"})
29
})
507 rajveer 30
public class RegisterController extends BaseController{
650 rajveer 31
 
32
	private static final long serialVersionUID = 1L;
507 rajveer 33
	private static Log log = LogFactory.getLog(RegisterController.class);
34
 
35
	public RegisterController(){
36
		super();
37
	}
38
 
39
 
650 rajveer 40
    public String index() throws SecurityException, IOException {
41
    	htmlSnippets.put("REGISTRATION_HEADER",pageLoader.getRegistrationHeaderHtml());
42
    	htmlSnippets.put("REGISTRATION_FORM",pageLoader.getRegistrationFormHtml());
43
    	return "index";
507 rajveer 44
    }
45
 
46
    public String create() throws SecurityException, Exception {
47
 
48
    	if(registerUser())
49
    		return "success";
50
    	else
51
    		return "failure";
52
    }
53
 
54
    public boolean registerUser() throws Exception{
569 rajveer 55
    	String email, password, userName, mobileNumber, communicationEmail, sex, dateOfBirth;
507 rajveer 56
 
555 chandransh 57
    	userName =  this.request.getParameter("nameOfUser");
507 rajveer 58
    	email = this.request.getParameter("email"); 
59
    	password = this.request.getParameter("password");
550 rajveer 60
 
61
    	mobileNumber = this.request.getParameter("mobileNumber");
62
    	communicationEmail = this.request.getParameter("communicationEmail");
63
 
569 rajveer 64
    	dateOfBirth = this.request.getParameter("dateOfBirth");
507 rajveer 65
    	sex =  this.request.getParameter("sex");
66
 
555 chandransh 67
		UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
68
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
507 rajveer 69
 
70
		if(userClient.userExists(email)){
555 chandransh 71
			log.error("User Exists!!!!");
507 rajveer 72
			return false;
555 chandransh 73
		}
74
 
75
		User user = new User();
76
		user.setName(userName);
77
		user.setEmail(email);
78
		user.setPassword(password);
79
		user.setCommunicationEmail(communicationEmail);
569 rajveer 80
		user.setMobileNumber(mobileNumber);
81
		user.setDateOfBirth(dateOfBirth);
555 chandransh 82
 
83
 
84
		user.setSex(Sex.WONT_SAY);
85
		if (sex != null) {
86
			try {
87
				user.setSex(Sex.findByValue(Integer.parseInt(sex)));
88
			} catch (NumberFormatException nfe) {
89
				log.error("Unusual value for sex. Leaving it marked as won't say.");
507 rajveer 90
			}
555 chandransh 91
		}
507 rajveer 92
 
93
 
555 chandransh 94
		user = userClient.createUser(user);
95
		long userId = user.getUserId();
96
		userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
97
 
98
		userinfo.setUserId(userId);
99
		userinfo.setNameOfUser(userName);
100
		userinfo.setEmail(email);
101
		userinfo.setLoggedIn(true);
102
 
103
		// TODO: setTotalItems shouldn't be a method on userinfo. This allows
104
		// for potentially updating the item count wrongly. The method setCartId
105
		// should update the item count as well. Also, there can be a method
106
		// called refreshItemCount() that automatically updates the number of
107
		// items currently in the cart.
108
		userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
109
		userinfo.setCartId(user.getActiveCartId());
110
		userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
111
 
112
		return true;
507 rajveer 113
    }
114
 
115
	public String getRegistrationHeaderSnippet(){
116
		return htmlSnippets.get("REGISTRATION_HEADER");
117
	}
118
 
119
	public String getRegistrationFormSnippet(){
120
		return htmlSnippets.get("REGISTRATION_FORM");
121
	}
122
 
123
}