Subversion Repositories SmartDukaan

Rev

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