Rev 2949 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.serving.controllers;import in.shop2020.model.v1.user.Sex;import in.shop2020.model.v1.user.User;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.thrift.clients.UserClient;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.thrift.TException;/*** @author rajveer**/@Results({@Result(name="success", type="redirectAction",params = {"actionName" , "personal-details"}),@Result(name="redirect", type="redirectAction",params = {"actionName" , "login"})})public class PersonalDetailsController extends BaseController {private static final long serialVersionUID = 1L;private static Logger log = Logger.getLogger(Class.class);private Map<String,String> velocityParams = new HashMap<String, String>();public PersonalDetailsController() {super();}// POST /logindetailspublic String create() {log.info("PersonaldetailsController.create");if(this.userinfo.isLoggedIn()){String name = this.request.getParameter("txtName");String dateOfBirth = this.request.getParameter("txtDateOfBirth");String sex = this.request.getParameter("sex");String communicationEmail = this.request.getParameter("txtCommEmail");String subscribeNewsletter = this.request.getParameter("subscribe");String phone = this.request.getParameter("txtPhone");if(updatePersonalDetails(userinfo.getUserId(), name, phone, dateOfBirth, sex,communicationEmail,subscribeNewsletter)){addActionMessage("Your personal details updated");return "success";}}addActionError("Unable to update your personal details");return "success";}public String index() throws UnsupportedEncodingException {log.info("this.request=" + this.request);if(this.userinfo.isLoggedIn()){htmlSnippets.put("MYACCOUNT_HEADER", pageLoader.getMyaccountHeaderHtml());htmlSnippets.put("PERSONAL_DETAILS", pageLoader.getPersonalDetailsHtml(userinfo.getUserId()));return "index";}else{return "redirect";}}private boolean updatePersonalDetails(long userId, String name, String phone, String dateOfBirth, String sex, String communicationEmail,String subscribeNewsletter) {try {UserClient userContextServiceClient = new UserClient();in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();User user = userClient.getUserById(userId);user.setDateOfBirth(dateOfBirth);user.setName(name);user.setCommunicationEmail(communicationEmail);try {user.setSex(Sex.findByValue(Integer.parseInt(sex)));} catch (NumberFormatException e) {log.warn("Unable to parse the value of sex", e);}user.setMobileNumber(phone);userClient.updateUser(user);return true;} catch (UserContextException e) {log.error("Unable to get or update the personal details:", e);} catch (TException e) {log.error("Unable to get or update the personal details:", e);} catch (Exception e) {log.error("Unable to get or update the personal details:", e);}return false;}public Map<String, String> getVelocityParams(){UserClient userContextServiceClient = null;in.shop2020.model.v1.user.UserContextService.Client userClient = null;try{User user = null;userContextServiceClient = new UserClient();userClient = userContextServiceClient.getClient();user = userClient.getUserById(userinfo.getUserId());velocityParams.put("email", user.getCommunicationEmail());velocityParams.put("sex", user.getSex().getValue()+"");velocityParams.put("name", user.getName());velocityParams.put("phone", user.getMobileNumber());velocityParams.put("dateOfBirth", user.getDateOfBirth());}catch (Exception e){log.error("Unable to get user from the user context service", e);}return velocityParams;}public String getMyaccountHeaderSnippet(){return htmlSnippets.get("MYACCOUNT_HEADER");}public String getPersonalDetailsSnippet(){return htmlSnippets.get("PERSONAL_DETAILS");}}