Subversion Repositories SmartDukaan

Rev

Rev 3378 | Rev 4453 | 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;
3830 chandransh 8
import in.shop2020.model.v1.user.Cart;
555 chandransh 9
import in.shop2020.model.v1.user.Sex;
3378 vikas 10
import in.shop2020.model.v1.user.TrackLogType;
555 chandransh 11
import in.shop2020.model.v1.user.User;
1868 vikas 12
import in.shop2020.serving.interceptors.TrackingInterceptor;
815 rajveer 13
import in.shop2020.serving.utils.DesEncrypter;
3126 rajveer 14
import in.shop2020.thrift.clients.UserClient;
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{
3830 chandransh 69
    	String email, password, userName;
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
 
741 rajveer 75
    	if(userName == null ){
76
    		addActionError("Please enter name of user.");
77
    		isValid = false;
78
        }
79
    	if(email == null ){
80
    		addActionError("Please enter valid email.");
81
    		isValid = false;
82
        }
83
    	if(password == null ){
84
    		addActionError("Please enter password.");
85
    		isValid = false;
86
        }
507 rajveer 87
 
741 rajveer 88
    	if(!isValid){
3830 chandransh 89
    	    DataLogger.logData(EventType.REGISTER_DATA_INCOMPLETE, getSessionId(), userinfo.getUserId(), email, userName, email);
741 rajveer 90
    		return isValid;
91
    	}
92
 
3126 rajveer 93
		UserClient userContextServiceClient = new UserClient();
555 chandransh 94
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
507 rajveer 95
 
96
		if(userClient.userExists(email)){
786 rajveer 97
			addActionError("User already exists with this email id.");
3830 chandransh 98
			DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, getSessionId(), userinfo.getUserId(), email, userName, email);
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);
3830 chandransh 107
		user.setCommunicationEmail(email);
2021 vikas 108
		Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
109
        if (sourceCookie != null) {
2817 vikas 110
            DesEncrypter des = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
2021 vikas 111
            String sourceCookieVal = des.decrypt(sourceCookie.getValue());
112
            user.setSource(sourceCookieVal);
113
        }
2817 vikas 114
 
115
        Cookie sourceTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_TIME_COOKIE);
116
        long sourceTime = 0;
117
        if (sourceTimeCookie != null) {
118
            try {
119
                sourceTime = Long.parseLong(sourceTimeCookie.getValue());
120
            }
121
            catch (Exception e) {
122
                log.warn("Unable to parse session src time cookie.");
123
            }
124
            user.setSourceStartTime(sourceTime);
125
        }
555 chandransh 126
 
127
		user.setSex(Sex.WONT_SAY);
3830 chandransh 128
 
507 rajveer 129
 
555 chandransh 130
		user = userClient.createUser(user);
131
		long userId = user.getUserId();
132
		userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
793 rajveer 133
		String pincode = userClient.getDefaultPincode(user.getUserId());
134
 
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.
1625 rajveer 140
		if(userinfo.getUserId() != -1){
1623 rajveer 141
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
142
 
2982 rajveer 143
			List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
144
			if(items != null){
145
				for(Long itemId: items){
146
					userClient.updateBrowseHistory(user.getUserId(), itemId);
1623 rajveer 147
				}
148
			}
2982 rajveer 149
 
150
			items = userClient.getMyResearchItems(userinfo.getUserId());
151
			if(items != null){
152
				for(Long itemId: items){
153
					userClient.updateMyResearch(user.getUserId(), itemId);
1625 rajveer 154
				}
155
			}
1623 rajveer 156
		}
157
 
1625 rajveer 158
		userinfo.setUserId(userId);
159
		userinfo.setNameOfUser(userName);
160
		userinfo.setEmail(email);
161
		userinfo.setLoggedIn(true);
162
		userinfo.setPincode(pincode);
555 chandransh 163
		userinfo.setCartId(user.getActiveCartId());
3830 chandransh 164
		Cart cart = userClient.getCart(userinfo.getCartId());
165
		userinfo.setTotalItems(cart.getLinesSize());
166
		userinfo.setTotalAmount(cart.getTotalPrice());
167
 
1999 vikas 168
        if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
169
            long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
3378 vikas 170
            userClient.addTrackLog(affId, userId, TrackLogType.NEW_REGISTRATION, "",email, (new Date()).getTime());
1868 vikas 171
        }
3830 chandransh 172
        DataLogger.logData(EventType.REGISTER_SUCCESS, getSessionId(), userinfo.getUserId(), email, userName, email);
555 chandransh 173
 
174
		return true;
507 rajveer 175
    }
176
 
177
	public String getRegistrationHeaderSnippet(){
178
		return htmlSnippets.get("REGISTRATION_HEADER");
179
	}
180
 
181
	public String getRegistrationFormSnippet(){
182
		return htmlSnippets.get("REGISTRATION_FORM");
183
	}
1776 varun.gupt 184
	public String getRedirectUrl() {
185
		return redirectUrl;
186
	}
507 rajveer 187
 
1776 varun.gupt 188
	public void setRedirectUrl(String redirectUrl) {
189
		this.redirectUrl = redirectUrl;
190
	}
191
}