Subversion Repositories SmartDukaan

Rev

Rev 3185 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.datalogger.EventType;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.AddressType;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.utils.DataLogger;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

@InterceptorRefs({
    @InterceptorRef("myDefault"),
    @InterceptorRef("login")
})

@Results({
    @Result(name="redirect", type="redirectAction", 
                params = {"actionName" , "address"})
})
public class AddressController extends BaseController{
        
        private static final long serialVersionUID = 1L;
        private static Logger log = Logger.getLogger(Class.class);
        
        private String errorMsg = "";
        
        private String name;
        private String line1;
        private String line2;
        private String city;
        private String state;
        private String pincode;
        private String phone;
        private String country;
        
        private String action;
        private String isDefault;
        
        public AddressController(){
                super();
        }
        
         // GET /address
         public String index() {
                log.info("Check error msgs");
                Collection<String> errorMsgs = getActionErrors();
                if(errorMsgs !=null && !errorMsgs.isEmpty()){
                        for (String errMsg : errorMsgs) { 
                            this.errorMsg += errMsg + "<br/>";
                        }
                }
                log.info(this.errorMsg);
                htmlSnippets.put("MYACCOUNT_HEADER",pageLoader.getMyaccountHeaderHtml());
                htmlSnippets.put("SHIPPING_ADDRESS_DETAILS",pageLoader.getShippingAddressDetailsHtml(userinfo.getUserId(), this.errorMsg));
                return "index";
         }

        // POST /address
        public String create(){

                if(userinfo.isLoggedIn()){
                        UserClient userContextServiceClient;
                        try {
                                userContextServiceClient = new UserClient();
                                Client userClient = userContextServiceClient.getClient();
                                
                                if(action != null){
                                        if(action.equals("add")){
                                                boolean invalidInput = false;
                                                if (!Utils.validatePin(this.pincode)) {
                                                        addActionError("Invalid pincode.");
                                                        invalidInput = true;
                                                }
                                                if (!Utils.validatePhone(this.phone)) {
                                                        addActionError("Invalid phone number.");
                                                        invalidInput = true;
                                                }
                                                if (this.line1.isEmpty()) {
                                                        addActionError("Address line1 is empty.");
                                                        invalidInput = true;
                                                }
                                                if (this.city.isEmpty()) {
                                                        addActionError("City name is empty.");
                                                        invalidInput = true;
                                                }
                                                if (this.state.isEmpty()) {
                                                        addActionError("State name is empty.");
                                                        invalidInput = true;
                                                }
                                                if (invalidInput) {
                                                        return "redirect";
                                                }
                                                Address address = new Address();
                                                address.setName(this.name);
                                                address.setLine1(this.line1);
                                                address.setLine2(this.line2);
                                                address.setCity(this.city);
                                                address.setState(this.state);
                                                address.setPin(this.pincode);
                                                address.setPhone(this.phone);
                                                address.setCountry(this.country);
                                                address.setEnabled(true);
                                                address.setType(AddressType.HOME);
                                                if(isDefault.equals("true")){
                                                        address.setId(userClient.addAddressForUser(userinfo.getUserId(), address, true));
                                                        userinfo.setPincode(this.pincode);
                                                }else{
                                                    address.setId(userClient.addAddressForUser(userinfo.getUserId(), address, false));
                                                }
                        DataLogger.logData(EventType.ADD_ADDRESS, getSessionId(),
                                userinfo.getUserId(), userinfo.getEmail(),
                                Long.toString(address.getId()),
                                address.getName(), address.getCity(),
                                address.getPin(), address.getPhone());
                                                addActionMessage("Address added successfully.");
                                                return "redirect";
                                        }
                                        
                                        if(action.equals("delete")){
                                                Long addressId = Long.parseLong(this.request.getParameter("addressid"));
                                                userClient.removeAddressForUser(userinfo.getUserId(), addressId);
                        DataLogger.logData(EventType.DELETE_ADDRESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), addressId.toString());
                                                addActionMessage("Address deleted successfully.");
                                                return "success";       
                                        }
                                        
                                        if(action.equals("setdefault")){
                                                Long addressId = Long.parseLong(this.request.getParameter("addressid"));
                                                userClient.setDefaultAddress(userinfo.getUserId(), addressId);
                                                DataLogger.logData(EventType.SET_DEFAULT_ADDRESS, getSessionId(), userinfo.getUserId(), userinfo.getEmail(), addressId.toString());
                                                //FIXME update pincode
                                                addActionMessage("Address set default successfully.");
                                                return "success";       
                                        }
                                }
                                
                        } catch (Exception e) {
                                log.error("Unable to update address. The underlying stack trace follows:", e);
                                addActionError("Unable to update address.");
                                return"redirect";
                        }
                        
                        return "redirect";
                }else{
                        return "failure";
                }
        }

        public String getMyaccountHeaderSnippet(){
                return htmlSnippets.get("MYACCOUNT_HEADER");
        }
        
        public String getShippingAddressDetailsSnippet(){
                return htmlSnippets.get("SHIPPING_ADDRESS_DETAILS");
        }
        
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getLine1() {
                return line1;
        }

        public void setLine1(String line1) {
                this.line1 = line1;
        }

        public String getLine2() {
                return line2;
        }

        public void setLine2(String line2) {
                this.line2 = line2;
        }

        public String getCity() {
                return city;
        }

        public void setCity(String city) {
                this.city = city;
        }

        public String getState() {
                return state;
        }

        public void setState(String state) {
                this.state = state;
        }

        public String getPincode() {
                return pincode;
        }

        public void setPincode(String pincode) {
                this.pincode = pincode;
        }

        public String getCountry() {
                return country;
        }

        public void setCountry(String country) {
                this.country = country;
        }
        
        public String getPhone() {
                return phone;
        }

        public void setPhone(String phone) {
                this.phone = phone;
        }
        
        public String getAction() {
                return action;
        }

        public void setAction(String action) {
                this.action = action;
        }

        public String getDefault() {
                return isDefault;
        }

        public void setDefault(String isDefault) {
                this.isDefault = isDefault;
        }
}