Subversion Repositories SmartDukaan

Rev

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