Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.utils;

import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
import in.shop2020.model.v1.user.UserContextException;
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 in.shop2020.model.v1.user.UserContextService.Client userClient;

        private static ShoppingCartClient shoppingCartClient;
        private static in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient;

        
        static {
                try {
                        userContextServiceClient = new UserContextServiceClient();
                        userClient = userContextServiceClient.getClient();
                        
                        shoppingCartClient = new ShoppingCartClient();
                        cartClient = shoppingCartClient.getClient();
                        
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static boolean isUserLoggedIn(long userId){
                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){
                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) {
                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) {
                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){
                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()){
        }
}