Subversion Repositories SmartDukaan

Rev

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