Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.shoppingcart.Cart;
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
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.controllers.BaseController;
import in.shop2020.serving.pages.PageContentKeys;
import in.shop2020.serving.pages.PageEnum;
import in.shop2020.serving.pages.PageManager;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.ShoppingCartClient;
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.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
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;
        
        public ShippingController(){
                super();
                pageManager = PageManager.getPageManager();     
        }
        
         // GET /shipping
         public HttpHeaders index() {
        long userId = 0;
        boolean isSessionId = true;
                Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
                
                if(userinfo.isLoggedIn()){
                        userId = userinfo.getUserId();
                        isSessionId = false;
                }else {
                        userId = userinfo.getSessionId();
                        isSessionId = true;
                }
                
                
                params.put(PageContentKeys.CUSTOMER_ID, userId+"");
                params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
                params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
                params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");

                htmlSnippets = pageManager.getPageContents(PageEnum.SHIPPING_PAGE, params);
                
        return new DefaultHttpHeaders("index").disableCaching();
         }

        // POST /entity
        public HttpHeaders create(){
                UserContextServiceClient userContextServiceClient;
                ShoppingCartClient shoppingCartClient;

        printParams();

                String action = this.request.getParameter("action");
                if(action == null){
                        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 {
                                        userContextServiceClient = new UserContextServiceClient();
                                        Client userClient = userContextServiceClient.getClient();
                                        userClient.addAddressForUser(address, userinfo.getUserId(), (new Date()).getTime(), false);
                                        List<Address> addresses = userClient.getPrimaryInfo(userinfo.getUserId(), userinfo.isSessionId()).getAddresses();
                                        long maxAddressId = 0;
                                        for(Address tempaddress: addresses){
                                                if(tempaddress.getId() > maxAddressId){
                                                        maxAddressId = tempaddress.getId();
                                                }
                                        }
                                        this.addressId = maxAddressId;
                                        // to set the address in cart
                                        shoppingCartClient = new ShoppingCartClient();
                                        in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                                        cartClient.addAddressToCart(userinfo.getCartId(), maxAddressId);
        
                                } catch (Exception e) {
                                        e.printStackTrace();
                                        return new DefaultHttpHeaders("failure");
                                }
                                
                                return new DefaultHttpHeaders("success");
                        }else{
                                return new DefaultHttpHeaders("failure");
                        }
                }
                if(action.equals("change")){
                        try {
                                shoppingCartClient = new ShoppingCartClient();
                                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                                long addressId = Long.parseLong(this.request.getParameter("addressid"));
                                cartClient.addAddressToCart(userinfo.getCartId(), addressId);
                        } catch (TException e) {
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                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;
        }
}