Subversion Repositories SmartDukaan

Rev

Rev 2419 | Rev 2817 | 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
 
2263 vikas 7
import in.shop2020.datalogger.EventType;
555 chandransh 8
import in.shop2020.model.v1.user.Sex;
9
import in.shop2020.model.v1.user.User;
1623 rajveer 10
import in.shop2020.model.v1.user.Widget;
11
import in.shop2020.model.v1.user.WidgetItem;
1868 vikas 12
import in.shop2020.serving.interceptors.TrackingInterceptor;
815 rajveer 13
import in.shop2020.serving.utils.DesEncrypter;
507 rajveer 14
import in.shop2020.thrift.clients.UserContextServiceClient;
2511 vikas 15
import in.shop2020.utils.DataLogger;
507 rajveer 16
 
17
import java.io.IOException;
18
import java.util.Date;
1623 rajveer 19
import java.util.List;
507 rajveer 20
 
2021 vikas 21
import javax.servlet.http.Cookie;
22
 
832 rajveer 23
import org.apache.log4j.Logger;
507 rajveer 24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Results;
26
 
27
/**
28
 * 
29
 * @author rajveer
30
 *
31
 */
32
 
550 rajveer 33
@Results({
1776 varun.gupt 34
    @Result(name = "success", type = "redirectAction", params = {"actionName" , "home"}),
1904 varun.gupt 35
    @Result(name = "failure", type = "redirectAction", params = {"actionName" , "login"}),
36
    @Result(name = "redirect-to-login", type = "redirectAction", params = {"actionName" , "login"}),
1776 varun.gupt 37
    @Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
550 rajveer 38
})
507 rajveer 39
public class RegisterController extends BaseController{
1904 varun.gupt 40
 
650 rajveer 41
	private static final long serialVersionUID = 1L;
832 rajveer 42
	private static Logger log = Logger.getLogger(Class.class);
815 rajveer 43
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
507 rajveer 44
 
1776 varun.gupt 45
	private String redirectUrl = null;
46
 
507 rajveer 47
	public RegisterController(){
48
		super();
49
	}
50
 
650 rajveer 51
    public String index() throws SecurityException, IOException {
925 rajveer 52
    	if(userinfo.isLoggedIn()){
53
    		return "success";
54
    	}
1904 varun.gupt 55
//    	htmlSnippets.put("REGISTRATION_HEADER",pageLoader.getRegistrationHeaderHtml());
56
//    	htmlSnippets.put("REGISTRATION_FORM",pageLoader.getRegistrationFormHtml());
57
    	return "redirect-to-login";
507 rajveer 58
    }
59
 
60
    public String create() throws SecurityException, Exception {
61
 
62
    	if(registerUser())
1776 varun.gupt 63
    		return "redirect";
507 rajveer 64
    	else
65
    		return "failure";
66
    }
67
 
68
    public boolean registerUser() throws Exception{
569 rajveer 69
    	String email, password, userName, mobileNumber, communicationEmail, sex, dateOfBirth;
741 rajveer 70
    	boolean isValid = true;
555 chandransh 71
    	userName =  this.request.getParameter("nameOfUser");
507 rajveer 72
    	email = this.request.getParameter("email"); 
762 rajveer 73
    	password = this.request.getParameter("txtPassword");
550 rajveer 74
 
75
    	mobileNumber = this.request.getParameter("mobileNumber");
76
    	communicationEmail = this.request.getParameter("communicationEmail");
77
 
569 rajveer 78
    	dateOfBirth = this.request.getParameter("dateOfBirth");
507 rajveer 79
    	sex =  this.request.getParameter("sex");
741 rajveer 80
 
81
    	if(userName == null ){
82
    		addActionError("Please enter name of user.");
83
    		isValid = false;
84
        }
85
    	if(email == null ){
86
    		addActionError("Please enter valid email.");
87
    		isValid = false;
88
        }
89
    	if(password == null ){
90
    		addActionError("Please enter password.");
91
    		isValid = false;
92
        }
93
    	if(communicationEmail == null ){
94
    		addActionError("Please enter Communication email.");
95
    		isValid = false;
96
        }
507 rajveer 97
 
741 rajveer 98
    	if(!isValid){
2419 vikas 99
    	    DataLogger.logData(EventType.REGISTER_DATA_INCOMPLETE, session.getId(), userinfo.getUserId(), email, userName, communicationEmail);
741 rajveer 100
    		return isValid;
101
    	}
102
 
555 chandransh 103
		UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
104
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
507 rajveer 105
 
106
		if(userClient.userExists(email)){
786 rajveer 107
			addActionError("User already exists with this email id.");
2419 vikas 108
			DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, session.getId(), userinfo.getUserId(), email, userName, communicationEmail);
507 rajveer 109
			return false;
555 chandransh 110
		}
111
 
112
		User user = new User();
113
		user.setName(userName);
114
		user.setEmail(email);
815 rajveer 115
		String encryptedPassword = desEncrypter.encrypt(password);
116
		user.setPassword(encryptedPassword);
555 chandransh 117
		user.setCommunicationEmail(communicationEmail);
569 rajveer 118
		user.setMobileNumber(mobileNumber);
119
		user.setDateOfBirth(dateOfBirth);
2021 vikas 120
		Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
121
        if (sourceCookie != null) {
122
            DesEncrypter des = new DesEncrypter("Saholic");
123
            String sourceCookieVal = des.decrypt(sourceCookie.getValue());
124
            user.setSource(sourceCookieVal);
125
        }
555 chandransh 126
 
127
		user.setSex(Sex.WONT_SAY);
128
		if (sex != null) {
129
			try {
130
				user.setSex(Sex.findByValue(Integer.parseInt(sex)));
131
			} catch (NumberFormatException nfe) {
132
				log.error("Unusual value for sex. Leaving it marked as won't say.");
507 rajveer 133
			}
555 chandransh 134
		}
507 rajveer 135
 
136
 
555 chandransh 137
		user = userClient.createUser(user);
138
		long userId = user.getUserId();
139
		userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
793 rajveer 140
		String pincode = userClient.getDefaultPincode(user.getUserId());
141
 
555 chandransh 142
		// TODO: setTotalItems shouldn't be a method on userinfo. This allows
143
		// for potentially updating the item count wrongly. The method setCartId
144
		// should update the item count as well. Also, there can be a method
145
		// called refreshItemCount() that automatically updates the number of
146
		// items currently in the cart.
1625 rajveer 147
		if(userinfo.getUserId() != -1){
1623 rajveer 148
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
149
 
1625 rajveer 150
			Widget browseHistory = userClient.getBrowseHistory(userinfo.getUserId());
1623 rajveer 151
			if(browseHistory != null){
152
				List<WidgetItem> items =  browseHistory.getItems();
153
				if(items != null){
154
					for(WidgetItem item: items){
155
						userClient.updateBrowseHistory(user.getUserId(), item.getItem_id());
156
					}
157
				}
158
			}
1625 rajveer 159
 
160
			Widget myResearch = userClient.getMyResearch(userinfo.getUserId());
161
			if(myResearch != null){
162
				List<WidgetItem> items =  browseHistory.getItems();
163
				if(items != null){
164
					for(WidgetItem item: items){
165
						userClient.updateMyResearch(user.getUserId(), item.getItem_id());
166
					}
167
				}
168
			}
1623 rajveer 169
		}
170
 
1625 rajveer 171
		userinfo.setUserId(userId);
172
		userinfo.setNameOfUser(userName);
173
		userinfo.setEmail(email);
174
		userinfo.setLoggedIn(true);
175
		userinfo.setPincode(pincode);
555 chandransh 176
		userinfo.setCartId(user.getActiveCartId());
762 rajveer 177
		int totalItems = userClient.getCart(userinfo.getCartId()).getLinesSize();
178
		userinfo.setTotalItems(totalItems);
1999 vikas 179
        if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
180
            long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
181
            userClient.addTrackLog(affId, userId, "new registration", "",email, (new Date()).getTime());
1868 vikas 182
        }
2419 vikas 183
        DataLogger.logData(EventType.REGISTER_SUCCESS, session.getId(), userinfo.getUserId(), email, userName, communicationEmail);
555 chandransh 184
 
185
		return true;
507 rajveer 186
    }
187
 
188
	public String getRegistrationHeaderSnippet(){
189
		return htmlSnippets.get("REGISTRATION_HEADER");
190
	}
191
 
192
	public String getRegistrationFormSnippet(){
193
		return htmlSnippets.get("REGISTRATION_FORM");
194
	}
1776 varun.gupt 195
	public String getRedirectUrl() {
196
		return redirectUrl;
197
	}
507 rajveer 198
 
1776 varun.gupt 199
	public void setRedirectUrl(String redirectUrl) {
200
		this.redirectUrl = redirectUrl;
201
	}
202
}