Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import in.shop2020.model.v1.catalog.InventoryServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.shoppingcart.Cart;
import in.shop2020.model.v1.shoppingcart.Line;
import in.shop2020.model.v1.shoppingcart.LineStatus;
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.UserContext;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserPrimaryInfo;
import in.shop2020.model.v1.widgets.WidgetException;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.ShoppingCartClient;
import in.shop2020.thrift.clients.UserContextServiceClient;
import in.shop2020.thrift.clients.WidgetServiceClient;

import org.apache.thrift.TException;

public class Utils {
        private static UserContextServiceClient userContextServiceClient;
        private static ShoppingCartClient shoppingCartClient;
        private static CatalogServiceClient catalogServiceClient;
        private static WidgetServiceClient widgetServiceClient;
        
        
        static {
                try {
                        userContextServiceClient = new UserContextServiceClient();
                        shoppingCartClient = new ShoppingCartClient();
                        catalogServiceClient = new CatalogServiceClient();
                        widgetServiceClient = new WidgetServiceClient();
                } 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.getCart(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 boolean deleteItemFromCart(long catalogItemId, long userId, boolean isSessionId){
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                long cartId = -1;
                
                try {
                        
                        cartId = cartClient.createCart(userId, isSessionId);
                        cartClient.deleteItemFromCart(cartId, catalogItemId);
                        //cartClient.changeItemStatus(cartId, catalogItemId, LineStatus.LINE_DELETED);
                        return true;    
                } 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 false;
        }

        public static boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                try {
                        cartClient.changeQuantity(cartId, itemId, quantity);
                        return true;
                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return false;
        }
        
        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("");
                catalogClient.getItem(itemId).getSellingPrice()
                */
                return "100";
                // 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() + "   ");
                        orderDetails.append("Item Name : " + Utils.getItemName(line.getItemId()) + "   ");
                        orderDetails.append("Item Quantity : " + line.getQuantity() + "   ");
                }
                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 " test ";
        }

        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 " test ";
        }
        
        public static String getItemName(long itemId){
                //TODO          write function to get item name given item id
                return "test";
        }


        public static String getNameOfUser(long userId) {
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                String name = "";
                
                try {
                        name = userClient.getPrimaryInfo(userId, false).getFirstName();
                } catch (UserContextException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return name; 
        }
        
        public static double getPaymentAmount(long cartId){
                in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
                double totalAmount = 0;
                
                Cart cart;
                try {
                        cart = cartClient.getCart(cartId);
                
                        List<Line> lineItems = cart.getLines(); 
                
                        for (Line line : lineItems) {
                                long productId = line.getItemId();
                                totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPriceByCatalogId(productId);
                        }

                } catch (ShoppingCartException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                return totalAmount;
        }
        
        public static double getItemPriceByCatalogId(long productId){
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                Double itemPrice = 0.0;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        Item item = client.getItemByCatalogId(productId);
                        /*
                        Map<Long,Double> priceMap = item.getPrice();
                        if(priceMap == null || priceMap.isEmpty()){
                                System.out.println("Price Not Found");
                        }else{
                                for(Entry<Long, Double> e: priceMap.entrySet()){
                                        itemPrice = e.getValue();
                                }
                        }
                        */
                }
                catch(Exception e){
                        e.printStackTrace();
                }
                return itemPrice;
        }


        public static void deleteFromMyResearch(long userId, long itemId) {
                in.shop2020.model.v1.widgets.WidgetService.Client widgetClient = widgetServiceClient.getClient();
                try {
                        widgetClient.deleteItemFromMyResearch(userId, itemId);
                } catch (WidgetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
        }


        public static boolean ChangePassword(long userId, String email, String oldPassword,
                        String newPassword) {
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                
                try {
                        userClient.updatePassword(userId, newPassword);
                        return true;
                } catch (UserContextException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return false;
        }


        public static boolean UpdatePersonalDetails(long userId,  String name, int month,
                        int day, int year, String sex, String communicationEmail,
                        String subscribeNewsletter) {
                
                in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                
                try {
                        UserContext context = userClient.getContextFromId(userId, false);
                        UserPrimaryInfo primaryInfo = context.getPrimaryInfo();
                        long dateOfBirth = (new Date(year, month, day)).getTime();
                        
                        primaryInfo.setDateOfBirth(dateOfBirth);
                        primaryInfo.setFirstName(name);
                        context.setPrimaryInfo(primaryInfo);
                        userClient.createContext(context, true);
                        return true;
                } catch (UserContextException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (TException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return false;
        }
        
}