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