Subversion Repositories SmartDukaan

Rev

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