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