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", 
741 rajveer 28
    		params = {"actionName" , "home"}),
29
    @Result(name="failure", type="redirectAction", 
30
    		params = {"actionName" , "register"})
31
 
550 rajveer 32
})
507 rajveer 33
public class RegisterController extends BaseController{
650 rajveer 34
 
35
	private static final long serialVersionUID = 1L;
507 rajveer 36
	private static Log log = LogFactory.getLog(RegisterController.class);
37
 
38
	public RegisterController(){
39
		super();
40
	}
41
 
42
 
650 rajveer 43
    public String index() throws SecurityException, IOException {
44
    	htmlSnippets.put("REGISTRATION_HEADER",pageLoader.getRegistrationHeaderHtml());
45
    	htmlSnippets.put("REGISTRATION_FORM",pageLoader.getRegistrationFormHtml());
46
    	return "index";
507 rajveer 47
    }
48
 
49
    public String create() throws SecurityException, Exception {
50
 
51
    	if(registerUser())
52
    		return "success";
53
    	else
54
    		return "failure";
55
    }
56
 
57
    public boolean registerUser() throws Exception{
569 rajveer 58
    	String email, password, userName, mobileNumber, communicationEmail, sex, dateOfBirth;
741 rajveer 59
    	boolean isValid = true;
555 chandransh 60
    	userName =  this.request.getParameter("nameOfUser");
507 rajveer 61
    	email = this.request.getParameter("email"); 
62
    	password = this.request.getParameter("password");
550 rajveer 63
 
64
    	mobileNumber = this.request.getParameter("mobileNumber");
65
    	communicationEmail = this.request.getParameter("communicationEmail");
66
 
569 rajveer 67
    	dateOfBirth = this.request.getParameter("dateOfBirth");
507 rajveer 68
    	sex =  this.request.getParameter("sex");
741 rajveer 69
 
70
    	if(userName == null ){
71
    		addActionError("Please enter name of user.");
72
    		isValid = false;
73
        }
74
    	if(email == null ){
75
    		addActionError("Please enter valid email.");
76
    		isValid = false;
77
        }
78
    	if(password == null ){
79
    		addActionError("Please enter password.");
80
    		isValid = false;
81
        }
82
    	if(communicationEmail == null ){
83
    		addActionError("Please enter Communication email.");
84
    		isValid = false;
85
        }
507 rajveer 86
 
741 rajveer 87
    	if(!isValid){
88
    		return isValid;
89
    	}
90
 
555 chandransh 91
		UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
92
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
507 rajveer 93
 
94
		if(userClient.userExists(email)){
555 chandransh 95
			log.error("User Exists!!!!");
507 rajveer 96
			return false;
555 chandransh 97
		}
98
 
99
		User user = new User();
100
		user.setName(userName);
101
		user.setEmail(email);
102
		user.setPassword(password);
103
		user.setCommunicationEmail(communicationEmail);
569 rajveer 104
		user.setMobileNumber(mobileNumber);
105
		user.setDateOfBirth(dateOfBirth);
555 chandransh 106
 
107
 
108
		user.setSex(Sex.WONT_SAY);
109
		if (sex != null) {
110
			try {
111
				user.setSex(Sex.findByValue(Integer.parseInt(sex)));
112
			} catch (NumberFormatException nfe) {
113
				log.error("Unusual value for sex. Leaving it marked as won't say.");
507 rajveer 114
			}
555 chandransh 115
		}
507 rajveer 116
 
117
 
555 chandransh 118
		user = userClient.createUser(user);
119
		long userId = user.getUserId();
120
		userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
121
 
122
		userinfo.setUserId(userId);
123
		userinfo.setNameOfUser(userName);
124
		userinfo.setEmail(email);
125
		userinfo.setLoggedIn(true);
126
 
127
		// TODO: setTotalItems shouldn't be a method on userinfo. This allows
128
		// for potentially updating the item count wrongly. The method setCartId
129
		// should update the item count as well. Also, there can be a method
130
		// called refreshItemCount() that automatically updates the number of
131
		// items currently in the cart.
132
		userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
133
		userinfo.setCartId(user.getActiveCartId());
134
		userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
135
 
136
		return true;
507 rajveer 137
    }
138
 
139
	public String getRegistrationHeaderSnippet(){
140
		return htmlSnippets.get("REGISTRATION_HEADER");
141
	}
142
 
143
	public String getRegistrationFormSnippet(){
144
		return htmlSnippets.get("REGISTRATION_FORM");
145
	}
146
 
147
}