Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import in.shop2020.model.v1.catalog.InventoryServiceException;
import in.shop2020.model.v1.shoppingcart.Line;
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.ShoppingCartClient;
import in.shop2020.thrift.clients.UserContextServiceClient;

import org.apache.thrift.TException;

public class Utils {
        private static UserContextServiceClient userContextServiceClient;
        private static ShoppingCartClient shoppingCartClient;
        private static CatalogServiceClient catalogServiceClient;

        
        static {
                try {
                        userContextServiceClient = new UserContextServiceClient();
                        shoppingCartClient = new ShoppingCartClient();
                        catalogServiceClient = new CatalogServiceClient();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static boolean isUserLoggedIn(long userId){
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                boolean isLoggedin = false;
                try {
                        isLoggedin = userClient.getState(userId, false).isIsLoggedIn();
                } catch (UserContextException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return isLoggedin;
        }

        
        public static String getEmailId(long userId){
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                String email = "";
                
                try {
                        email = userClient.getPrimaryInfo(userId, false).getEmail();
                } catch (UserContextException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return email; 
        }

        public static int getNumberOfItemsInCart(long cartId) {
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                int numberOfItems = 0;
                try {
                        numberOfItems = cartClient.getShadowCart(cartId).getLinesSize();
                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return numberOfItems;
        }
        
        
        public static long getCartId(long userId) {
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                long cartId = 0;
                try {
                        cartId = cartClient.getCurrentCart(userId, false).getId();
                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return cartId;
        }

        
        public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                long cartId = -1;
                
                try {
//                      userContextServiceClient = new UserContextServiceClient();
//                      userClient = userContextServiceClient.getClient();
//
//                      shoppingCartClient = new ShoppingCartClient();
//                      cartClient = shoppingCartClient.getClient();
                        
                        cartId = cartClient.createCart(userId, isSessionId);
                        cartClient.addItemToCart(cartId, catalogItemId, 1);
        
                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return cartId;
                //if user is logged in create  new cart
                //if( userClient.getState(userId, false).isIsLoggedIn()){
        }

        
        public static String getItemPrice(long itemId) throws InventoryServiceException, TException{
                in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = catalogServiceClient.getClient();
                Map priceMap = catalogClient.getItem(itemId).getPrice();
                return (String)priceMap.get("");
                // TODO to implement this function correctly
        }
        
        
        public static String getOrderDetails(long cartId){
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                List<Line> lineItems = null;
                StringBuilder orderDetails = new StringBuilder();
                try {
                        lineItems = cartClient.getCart(cartId).getLines();
                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                for (Line line : lineItems) {
                        orderDetails.append("Item Id : " + line.getItemId() + "\n");
                        orderDetails.append("Item Name : " + Utils.getItemName(line.getItemId()) + "\n");
                        orderDetails.append("Item Quantity : " + line.getQuantity() + "\n");
                }
                return orderDetails.toString();
        }
        
        public static String getContactNumber(long cartId){
//              in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
//              in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                
//              userClient.getPrimaryInfo(userId, false).getAddresses()
//              cartClient.getCart(cartId).getAddressId()
//TODO          write function to get address from addressId
                return "";
        }

        public static String getBillingAddress(long cartId){
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                //TODO          write function to get shipping and billing address
                return "";
        }
        
        public static String getItemName(long itemId){
                //TODO          write function to get item name given item id
                return "";
        }
        
}