Subversion Repositories SmartDukaan

Rev

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