Subversion Repositories SmartDukaan

Rev

Rev 745 | 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/";
                
        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 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");
//      }
        
}