Subversion Repositories SmartDukaan

Rev

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