Subversion Repositories SmartDukaan

Rev

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