Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import java.util.Collection;

import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.AddressType;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.UserContextServiceClient;


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;
import org.apache.thrift.TApplicationException;

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

@Results({
    @Result(name="redirect", type="redirectAction", 
                params = {"actionName" , "shipping"})
})
public class ShippingController extends BaseController{
        
        private static final long serialVersionUID = 1L;
        private static Logger log = Logger.getLogger(Class.class);
        
        private long addressId = 0;
        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;
        
        public ShippingController(){
                super();
        }
        
         // GET /shipping
         public String index() {
        long userId = userinfo.getUserId();
        boolean isLoggedIn = userinfo.isLoggedIn();
        long cartId = userinfo.getCartId();
        try {
                        UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
                        userClient.checkOut(cartId);
                        Cart cart = userClient.getCart(cartId);
                        long defaultAddressId = cart.getAddressId();
                        if (defaultAddressId == 0) {
                            defaultAddressId = userClient.getDefaultAddressId(userId);
                        }
                        log.info("The default address id of this user is: " + defaultAddressId);
                        if(defaultAddressId > 0)
                                userClient.addAddressToCart(cartId, defaultAddressId);
                        if(!userClient.validateCart(cartId))
                                errorMsg = "Your cart has been updated.";
                        
                } catch (Exception e) {
                        // This exception can be ignored for showing the cart. Not so
                        // innocent when this occurs at the time of checkout or when the
                        // user is proceeding to pay.
                        e.printStackTrace();
                }
        // in case this page is redirected from payment failure
                
                Collection<String> actionErrors = getActionErrors();
                if(actionErrors != null && !actionErrors.isEmpty()){
                        for (String str : actionErrors) {
                            errorMsg += "<BR/>" + str;
                        }
                }
                
                htmlSnippets.put("SHIPPING_HEADER", pageLoader.getShippingHeaderHtml());
                htmlSnippets.put("SHIPPING_DETAILS", pageLoader.getShippingDetailsHtml(userinfo.getCartId(), errorMsg));
                
        return "index";
         }

        // POST /entity
        public String create(){
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                
        String action = this.request.getParameter("action");
                if(action == null){
                        return "failure";
                }
                
                try {
                        userContextServiceClient = new UserContextServiceClient();
                        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) {
                                                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);
                                                long addressId = userClient.addAddressForUser(
                                                                userinfo.getUserId(), address, false);
                                                userClient.addAddressToCart(userinfo.getCartId(),
                                                                addressId);
                                                addActionMessage("Address added successfully.");
                                        }
                                        return "redirect";                              
                                }
                                
                                if(action.equals("change")){
                                        addressId = Long.parseLong(this.request.getParameter("addressid"));
                                        userClient.addAddressToCart(userinfo.getCartId(), addressId);
                                        return "success";
                                }
                        }
                } catch (Exception e) {
                        return "failure";
                }
                return null;
        }

        public String getShippingHeaderSnippet(){
                return htmlSnippets.get("SHIPPING_HEADER");
        }
        
        public String getShippingDetailsSnippet(){
                return htmlSnippets.get("SHIPPING_DETAILS");
        }
        
        public long getAddressId(){
                return this.addressId;
        }
        
        public String getErrorMsg(){
                return this.errorMsg;
        }
        
        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;
        }
}