Subversion Repositories SmartDukaan

Rev

Rev 569 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.AddressType;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserContextService;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.serving.pages.PageContentKeys;
import in.shop2020.serving.pages.PageEnum;
import in.shop2020.serving.pages.PageManager;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.util.*;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.thrift.TException;

public class ShippingController extends BaseController implements ParameterAware{
        
        private static final long serialVersionUID = 1L;
        private static Log log = LogFactory.getLog(ShippingController.class);
        Map<String, String[]> reqparams = null;
        
        private Map<String,String> htmlSnippets;
        private PageManager pageManager = null;
        private long addressId = 0;
        private String errorMsg = "";
        
        public ShippingController(){
                super();
                pageManager = PageManager.getPageManager();     
        }
        
         // GET /shipping
         public HttpHeaders index() {
        long userId = userinfo.getUserId();
        boolean isLoggedIn = userinfo.isLoggedIn();
        
        
        try {
                        UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
                        User user = userClient.getUserById(userId);
                        log.info("The default address id of this user is: " + user.getDefaultAddressId());
                        if(user.getDefaultAddressId() > 0)
                                userClient.addAddressToCart(userinfo.getCartId(), user.getDefaultAddressId());
                        if(!userClient.validateCart(userinfo.getCartId()))
                                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();
                }
        
                Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
                
                params.put(PageContentKeys.CUSTOMER_ID, userId+"");
                params.put(PageContentKeys.IS_LOGGED_IN, isLoggedIn+"");
                params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
                params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
                params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
                params.put(PageContentKeys.ERROR_MSG, errorMsg);
                htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_PAGE, params);
                
        return new DefaultHttpHeaders("index").disableCaching();
         }

        // POST /entity
        public HttpHeaders create(){
                UserContextServiceClient userContextServiceClient = null;
                in.shop2020.model.v1.user.UserContextService.Client userClient = null;
                
        printParams();

                String action = this.request.getParameter("action");
                if(action == null){
                        return new DefaultHttpHeaders("failure");
                }
                
                try {
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                } catch (Exception e) {
                        return new DefaultHttpHeaders("failure");
                }
                
                if(action.equals("addnew")){
                        
                        Address address = new Address();
                        address.setName(this.request.getParameter("customername"));
                        address.setLine1(this.request.getParameter("line1"));
                        address.setLine2(this.request.getParameter("line2"));
                        address.setCity(this.request.getParameter("city"));
                        address.setState(this.request.getParameter("state"));
                        address.setPin(this.request.getParameter("pincode"));
                        address.setPhone(this.request.getParameter("mobilenumber"));
                        address.setCountry(this.request.getParameter("country"));
                        address.setEnabled(true);
                        address.setType(AddressType.HOME);
                        
                        if(userinfo.isLoggedIn()){
                                try {
                                        this.addressId = userClient.addAddressForUser(userinfo.getUserId(), address, false);
                                        // to set the address in cart
                                        userClient.addAddressToCart(userinfo.getCartId(), this.addressId);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                        return new DefaultHttpHeaders("failure");
                                }
                                
                                return new DefaultHttpHeaders("success");
                        }else{
                                return new DefaultHttpHeaders("failure");
                        }
                }
                if(action.equals("change")){
                        try {
                                long addressId = Long.parseLong(this.request.getParameter("addressid"));
                                userClient.addAddressToCart(userinfo.getCartId(), addressId);
                        } catch (TException e) {
                                e.printStackTrace();
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                        return new DefaultHttpHeaders("success");
                }
                return new DefaultHttpHeaders("failure");
        }

    public void printParams(){
        for(String param : reqparams.keySet()) {
                log.info("param name is " + param);
                log.info("param first is " + reqparams.get(param)[0]);
        }
        log.info(this.reqparams);
    }

        @Override
        public void setParameters(Map<String, String[]> reqmap) {
                log.info("setParameters:" + reqmap);
                
                this.reqparams = reqmap;
        }

    public String getHeaderSnippet(){
                return htmlSnippets.get("HEADER");
        }
        
        public String getMainMenuSnippet(){
                return htmlSnippets.get("MAIN_MENU");
        }
        
        public String getSearchBarSnippet(){
                return htmlSnippets.get("SEARCH_BAR");
        }
                        
        
        public String getCustomerServiceSnippet(){
                return htmlSnippets.get("CUSTOMER_SERVICE");
        }
        
        public String getShippingHeaderSnippet(){
                return htmlSnippets.get("SHIPPING_HEADER");
        }
        
        public String getShippingDetailsSnippet(){
                return htmlSnippets.get("SHIPPING_DETAILS");
        }
        
        public String getFooterSnippet(){
                return htmlSnippets.get("FOOTER");
        }
        
        public String getJsFileSnippet(){
                return htmlSnippets.get("JS_FILES");
        }
        
        public String getCssFileSnippet(){
                return htmlSnippets.get("CSS_FILES");
        }

        public long getAddressId(){
                return this.addressId;
        }
        
        public String getErrorMsg(){
                return this.errorMsg;
        }
}