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 {
47
    	htmlSnippets.put("REGISTRATION_HEADER",pageLoader.getRegistrationHeaderHtml());
48
    	htmlSnippets.put("REGISTRATION_FORM",pageLoader.getRegistrationFormHtml());
49
    	return "index";
507 rajveer 50
    }
51
 
52
    public String create() throws SecurityException, Exception {
53
 
54
    	if(registerUser())
55
    		return "success";
56
    	else
57
    		return "failure";
58
    }
59
 
60
    public boolean registerUser() throws Exception{
569 rajveer 61
    	String email, password, userName, mobileNumber, communicationEmail, sex, dateOfBirth;
741 rajveer 62
    	boolean isValid = true;
555 chandransh 63
    	userName =  this.request.getParameter("nameOfUser");
507 rajveer 64
    	email = this.request.getParameter("email"); 
762 rajveer 65
    	password = this.request.getParameter("txtPassword");
550 rajveer 66
 
67
    	mobileNumber = this.request.getParameter("mobileNumber");
68
    	communicationEmail = this.request.getParameter("communicationEmail");
69
 
569 rajveer 70
    	dateOfBirth = this.request.getParameter("dateOfBirth");
507 rajveer 71
    	sex =  this.request.getParameter("sex");
741 rajveer 72
 
73
    	if(userName == null ){
74
    		addActionError("Please enter name of user.");
75
    		isValid = false;
76
        }
77
    	if(email == null ){
78
    		addActionError("Please enter valid email.");
79
    		isValid = false;
80
        }
81
    	if(password == null ){
82
    		addActionError("Please enter password.");
83
    		isValid = false;
84
        }
85
    	if(communicationEmail == null ){
86
    		addActionError("Please enter Communication email.");
87
    		isValid = false;
88
        }
507 rajveer 89
 
741 rajveer 90
    	if(!isValid){
91
    		return isValid;
92
    	}
93
 
555 chandransh 94
		UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
95
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
507 rajveer 96
 
97
		if(userClient.userExists(email)){
786 rajveer 98
			addActionError("User already exists with this email id.");
507 rajveer 99
			return false;
555 chandransh 100
		}
101
 
102
		User user = new User();
103
		user.setName(userName);
104
		user.setEmail(email);
815 rajveer 105
		String encryptedPassword = desEncrypter.encrypt(password);
106
		user.setPassword(encryptedPassword);
555 chandransh 107
		user.setCommunicationEmail(communicationEmail);
569 rajveer 108
		user.setMobileNumber(mobileNumber);
109
		user.setDateOfBirth(dateOfBirth);
555 chandransh 110
 
111
 
112
		user.setSex(Sex.WONT_SAY);
113
		if (sex != null) {
114
			try {
115
				user.setSex(Sex.findByValue(Integer.parseInt(sex)));
116
			} catch (NumberFormatException nfe) {
117
				log.error("Unusual value for sex. Leaving it marked as won't say.");
507 rajveer 118
			}
555 chandransh 119
		}
507 rajveer 120
 
121
 
555 chandransh 122
		user = userClient.createUser(user);
123
		long userId = user.getUserId();
124
		userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
793 rajveer 125
		String pincode = userClient.getDefaultPincode(user.getUserId());
126
 
555 chandransh 127
		userinfo.setUserId(userId);
128
		userinfo.setNameOfUser(userName);
129
		userinfo.setEmail(email);
130
		userinfo.setLoggedIn(true);
793 rajveer 131
		userinfo.setPincode(pincode);
555 chandransh 132
		// TODO: setTotalItems shouldn't be a method on userinfo. This allows
133
		// for potentially updating the item count wrongly. The method setCartId
134
		// should update the item count as well. Also, there can be a method
135
		// called refreshItemCount() that automatically updates the number of
136
		// items currently in the cart.
137
		userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
138
		userinfo.setCartId(user.getActiveCartId());
762 rajveer 139
		int totalItems = userClient.getCart(userinfo.getCartId()).getLinesSize();
140
		userinfo.setTotalItems(totalItems);
555 chandransh 141
 
142
		return true;
507 rajveer 143
    }
144
 
145
	public String getRegistrationHeaderSnippet(){
146
		return htmlSnippets.get("REGISTRATION_HEADER");
147
	}
148
 
149
	public String getRegistrationFormSnippet(){
150
		return htmlSnippets.get("REGISTRATION_FORM");
151
	}
152
 
153
}