Subversion Repositories SmartDukaan

Rev

Rev 832 | Rev 1044 | 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
 
762 rajveer 6
import in.shop2020.model.v1.user.Sex;
555 chandransh 7
import in.shop2020.model.v1.user.User;
762 rajveer 8
import in.shop2020.model.v1.user.UserContextException;
507 rajveer 9
import in.shop2020.serving.utils.Utils;
10
import in.shop2020.thrift.clients.UserContextServiceClient;
11
 
12
import java.io.UnsupportedEncodingException;
13
import java.util.HashMap;
14
import java.util.Map;
15
 
16
import org.apache.juli.logging.Log;
17
import org.apache.juli.logging.LogFactory;
832 rajveer 18
import org.apache.log4j.Logger;
507 rajveer 19
import org.apache.struts2.convention.annotation.Result;
20
import org.apache.struts2.convention.annotation.Results;
762 rajveer 21
import org.apache.thrift.TException;
507 rajveer 22
 
23
/**
24
 * @author rajveer
25
 *
26
 */
27
 
595 rajveer 28
@Results({
29
    @Result(name="success", type="redirectAction", 
650 rajveer 30
    		params = {"actionName" , "personal-details"}),
31
    @Result(name="redirect", type="redirectAction", 
32
       		params = {"actionName" , "login"})
33
 
595 rajveer 34
})
507 rajveer 35
 
595 rajveer 36
public class PersonalDetailsController extends BaseController {
37
 
507 rajveer 38
 
650 rajveer 39
	private static final long serialVersionUID = 1L;
40
 
832 rajveer 41
	private static Logger log = Logger.getLogger(Class.class);
507 rajveer 42
 
595 rajveer 43
	private Map<String,String> velocityParams = new HashMap<String, String>();
507 rajveer 44
 
517 rajveer 45
	public PersonalDetailsController() {
507 rajveer 46
		super();
47
	}
48
 
49
    // POST /logindetails
50
    public String create() {
51
    	log.info("PersonaldetailsController.create");
52
 
53
		if(this.userinfo.isLoggedIn()){
595 rajveer 54
			String name = this.request.getParameter("txtName");
620 rajveer 55
			String dateOfBirth = this.request.getParameter("txtDateOfBirth");
595 rajveer 56
			String sex = this.request.getParameter("sex");
57
			String communicationEmail = this.request.getParameter("txtCommEmail");
58
			String subscribeNewsletter = this.request.getParameter("subscribe");
59
			String phone = this.request.getParameter("txtPhone");
60
 
762 rajveer 61
			if(UpdatePersonalDetails(userinfo.getUserId(), name, phone, dateOfBirth, sex,communicationEmail,subscribeNewsletter))
595 rajveer 62
			{	
63
				addActionMessage("Your personal details updated");
507 rajveer 64
				return "success";
65
			}
66
    	}
595 rajveer 67
		addActionError("Unable to update your personal details");
68
		return "success";
507 rajveer 69
    }
70
 
71
 
595 rajveer 72
    public String index() throws UnsupportedEncodingException {
507 rajveer 73
    	log.info("this.request=" + this.request);
74
 
75
    	if(this.userinfo.isLoggedIn()){
595 rajveer 76
    		htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());
77
    		htmlSnippets.put("PERSONAL_DETAILS", pageLoader.getPersonalDetailsHtml(userinfo.getUserId()));
650 rajveer 78
    		return "index";
507 rajveer 79
    	}else{
650 rajveer 80
    		return "redirect";
507 rajveer 81
    	}
82
 
83
    }
84
 
762 rajveer 85
	private boolean UpdatePersonalDetails(long userId,  String name,  String phone, String dateOfBirth, String sex, String communicationEmail,
86
			String subscribeNewsletter) {
87
 
88
		try {
89
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
90
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
91
 
92
 
93
			User user = userClient.getUserById(userId);		
94
			user.setDateOfBirth(dateOfBirth);
95
			user.setName(name);
96
			user.setCommunicationEmail(communicationEmail);
97
			user.setSex(Sex.findByValue(Integer.parseInt(sex)));
98
			user.setMobileNumber(phone);
99
			userClient.updateUser(user);
100
 
101
			return true;
102
		} catch (UserContextException e) {
103
			e.printStackTrace();
104
		} catch (TException e) {
105
			e.printStackTrace();
106
		} catch (Exception e) {
107
			e.printStackTrace();
108
		}
109
		return false;
110
	}
111
 
112
 
113
 
595 rajveer 114
 
115
    public Map<String, String> getVelocityParams(){
116
    	UserContextServiceClient userContextServiceClient = null;
117
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
118
		try{
119
			User user = null;
120
			userContextServiceClient = new UserContextServiceClient();
121
			userClient = userContextServiceClient.getClient();
122
			user = userClient.getUserById(userinfo.getUserId());
123
 
124
			velocityParams.put("email", user.getCommunicationEmail());
125
			velocityParams.put("sex", user.getSex().getValue()+"");
126
			velocityParams.put("name", user.getName());
127
			velocityParams.put("phone", user.getMobileNumber());
128
			velocityParams.put("dateOfBirth", user.getDateOfBirth());
129
		}catch (Exception e){
130
			e.printStackTrace();
131
		}
132
		return velocityParams;
133
	}
134
 
650 rajveer 135
    public String getMyaccountHeaderSnippet(){
507 rajveer 136
		return htmlSnippets.get("MYACCOUNT_HEADER");
137
	}
138
 
139
	public String getPersonalDetailsSnippet(){
140
		return htmlSnippets.get("PERSONAL_DETAILS");
141
	}
142
 
143
}