Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.order.Transaction;
import in.shop2020.model.v1.user.Cart;

import in.shop2020.model.v1.user.Line;
import in.shop2020.model.v1.user.Sex;
import in.shop2020.model.v1.user.ShoppingCartException;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.WidgetException;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.TransactionServiceClient;
import in.shop2020.thrift.clients.UserContextServiceClient;

import org.apache.thrift.TException;

public class Utils {
        //FIXME: Read this path from the config server
        public static final String EXPORT_ENTITIES_PATH =       "/var/lib/tomcat6/webapps/export/html/entities/";
        /*
        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){ 
                boolean isNotLoggedIn = true;
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        isNotLoggedIn = userClient.getUserById(userId).isIsAnonymous();
                } catch (UserContextException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return !isNotLoggedIn;
        }

        
        public static String getEmailId(long userId){
                String email = " ";
                
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        if(userClient.getUserById(userId).getEmail() != null){
                                email = userClient.getUserById(userId).getEmail();
                        }
                } catch (UserContextException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return email; 
        }

        public static int getNumberOfItemsInCart(long cartId) {
                 

                int numberOfItems = 0;
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        numberOfItems = userClient.getCart(cartId).getLinesSize();
                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return numberOfItems;
        }
        
        
        public static long getCartId(long userId) {
                long cartId = 0;
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        cartId = userClient.getCurrentCart(userId).getId();
                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return cartId;
        }

        
        public static long addItemToCart(long catalogItemId, long userId, boolean isSessionId){
                long cartId = -1;
                
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        
                        cartId = userClient.createCart(userId);
                        userClient.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 cartId, long catalogItemId, long userId, boolean isSessionId){
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        userClient.deleteItemFromCart(cartId, catalogItemId);
                        return true;    
                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                
                return false;
        }

        public static boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        userClient.changeQuantity(cartId, itemId, quantity);
                        return true;
                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return false;
        }
        
        
        
        public static String getOrderDetails(long txnId){
                Transaction transaction;
                StringBuilder orderDetails = new StringBuilder();
                try {
                        TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
                        in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();

                        transaction = txnClient.getTransaction(txnId);
                        
                        for (Order order : transaction.getOrders()) {
                                for(LineItem line: order.getLineitems()){
                                        if(line.getBrand() != null){
                                                orderDetails.append(line.getBrand());
                                        }
                                        if(line.getModel_name() != null){
                                                orderDetails.append(line.getModel_name()); 
                                        }
                                        if(line.getModel_number() != null){
                                                orderDetails.append(line.getModel_number());
                                        }
                                        if(line.getColor() != null){
                                                orderDetails.append(line.getColor());
                                        }
                                }
                                
                        }
                        
                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return orderDetails.toString() + " ";
        }
        
        public static String getContactNumber(long txnId){
                Transaction transaction;
                StringBuilder orderDetails = new StringBuilder();
                try {
                        TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
                        in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();

                        transaction = txnClient.getTransaction(txnId);
                        for (Order order : transaction.getOrders()) {
                                if(order.getCustomer_mobilenumber() != null){
                                        orderDetails.append(order.getCustomer_mobilenumber()) ; 
                                }
                                
                        }
                        
                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                
                return orderDetails.toString() + " ";
        }

        public static String getBillingAddress(long txnId){             
        Transaction transaction;
        StringBuilder orderDetails = new StringBuilder();
        try {
                TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
                in.shop2020.model.v1.order.TransactionService.Client txnClient = transactionServiceClient.getClient();

                transaction = txnClient.getTransaction(txnId);
                
                for (Order order : transaction.getOrders()) {
                        if(order.getCustomer_name()!=null){
                                orderDetails.append(order.getCustomer_name());
                        }
                        if(order.getCustomer_address()!=null){
                                orderDetails.append(order.getCustomer_address());
                        }
                        if(order.getCustomer_city()!=null){
                                orderDetails.append(order.getCustomer_city());
                        }
                        if(order.getCustomer_state()!=null){
                                orderDetails.append(order.getCustomer_state());
                        }
                        if(order.getCustomer_pincode()!=null){
                                orderDetails.append(order.getCustomer_pincode());
                        }
                }
                
        } catch (ShoppingCartException e) {
                e.printStackTrace();
        } catch (TException e) {
                e.printStackTrace();
        } catch (Exception e) {
                e.printStackTrace();
        }
        
        return orderDetails.toString() + " ";

        }
        
        
        public static String getNameOfUser(long userId) {
                String name = " ";
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        
                        name = userClient.getUserById(userId).getName();
                } catch (UserContextException 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 name; 
        }
        
        public static double getPaymentAmount(long cartId){
                double totalAmount = 0;
                
                Cart cart;
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        cart = userClient.getCart(cartId);
                
                        List<Line> lineItems = cart.getLines(); 
                
                        for (Line line : lineItems) {
                                long productId = line.getItemId();
                                totalAmount =  totalAmount + line.getQuantity() * Utils.getItemPrice(productId);
                        }

                } catch (ShoppingCartException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }

                return totalAmount;
        }
        
        public static double getItemPrice(long itemId){
                CatalogServiceClient catalogServiceClient = null;
                Client client = null;
                Double itemPrice = 0.0;
                try {
                        catalogServiceClient = new CatalogServiceClient();
                        client = catalogServiceClient.getClient();
                        Item item = client.getItem(itemId);
                        itemPrice = item.getSellingPrice();
                        
                }
                catch(Exception e){
                        e.printStackTrace();
                }
                return itemPrice;
        }


        public static void deleteFromMyResearch(long userId, long itemId) {
                
                try {
                        UserContextServiceClient userServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userServiceClient.getClient();
                        userClient.deleteItemFromMyResearch(userId, itemId);
                } catch (WidgetException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                
        }


        public static boolean ChangePassword(long userId, String email, String oldPassword,
                        String newPassword) {

                
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        return userClient.updatePassword(userId,oldPassword, newPassword);
                } catch (UserContextException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return false;
        }


        public static boolean UpdatePersonalDetails(long userId,  String name,  String phone, String dateOfBirth, String sex, String communicationEmail,
                        String subscribeNewsletter) {
                
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        
                        User user = userClient.getUserById(userId);             
                        user.setDateOfBirth(dateOfBirth);
                        user.setName(name);
                        user.setCommunicationEmail(communicationEmail);
                        user.setSex(Sex.findByValue(Integer.parseInt(sex)));
                        user.setMobileNumber(phone);
                        userClient.updateUser(user);
                        
                        return true;
                } catch (UserContextException e) {
                        e.printStackTrace();
                } catch (TException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return false;
        }


        public static void mergeCarts(long fromCartId, long toCartId){
                try {
                        UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
                        userClient.mergeCart(fromCartId, toCartId);
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
        }
        
        public static void storeCategories(Object obj) throws Exception {
        
                ByteArrayOutputStream bStream = new ByteArrayOutputStream();
                ObjectOutputStream oStream = new ObjectOutputStream( bStream );
                oStream.writeObject ( obj );
                
                byte[] byteVal = bStream. toByteArray();
                
                CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
                Client client = catalogServiceClient.getClient();
                
                client.putCategoryObject(byteVal);
        }
        
        public static Object getCategories() throws Exception {
                CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
                Client client = catalogServiceClient.getClient();
                
                byte[] byteVal  = client.getCategoryObject();
                
                Object obj = null;
                ObjectInputStream in = null;
                try {
                        ByteArrayInputStream bStream = new ByteArrayInputStream(byteVal);
                        in = new ObjectInputStream(bStream);
                        obj = in.readObject();
                }
                finally {
                        if(in != null) {
                                in.close();
                        }
                }
                return obj;
        }
        
//      public static void main(String args[]) throws Exception{
//              // to store categories
//              Map<Long, Category> categories = new HashMap<Long, Category>();
//              Map<Long, in.shop2020.metamodel.definitions.Category> cmsCategories = Catalog.getInstance().getDefinitionsContainer().getCategories();
//              for(in.shop2020.metamodel.definitions.Category cmsCategory: cmsCategories.values()){
//                      Category category = new Category(cmsCategory.getID());
//                      category.setLabel(cmsCategory.getLabel());
//                      if(cmsCategory.getParentCategory()!=null){
//                              category.setParentCategoryId(cmsCategory.getParentCategory().getID());
//                      }
//                      if(cmsCategory.getChildrenCategory()!=null){
//                              for(in.shop2020.metamodel.definitions.Category childCategory: cmsCategory.getChildrenCategory()){
//                                      category.addChild(childCategory.getID());
//                              }
//                      }
//                      categories.put(cmsCategory.getID(), category);
//              }
//              Utils.storeCategories(categories);
//              // to get categories
//              //Map<Long, Category> 
//              categories = (Map<Long, Category>)Utils.getCategories();
//              System.out.println("hello");
//      }
        
}