Subversion Repositories SmartDukaan

Rev

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