Subversion Repositories SmartDukaan

Rev

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