Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.utils;

import in.shop2020.config.ConfigException;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.Order;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.Line;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.config.ConfigClient;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class Utils {
    private static Logger logger = Logger.getLogger(Utils.class);
    
        public static final String EXPORT_ENTITIES_PATH = getExportPath();
        public static final long ROOT_CATEGORY = 10000;
        public static final long MOBILE_PHONES_CATEGORY = 10001;
        public static final long MOBILE_ACCESSORIES_CATEGORY = 10011;
        public static final long TABLETS_CATEGORY = 10010;
    
        private static String getExportPath(){
                String exportPath=null;
                ConfigClient client = ConfigClient.getClient();
                try{
                        exportPath = client.get("export_entities_path");
                }catch(ConfigException ce){
                        logger.error("Unable to read export path from the config client: ", ce);
                        logger.warn("Setting the default export path");
                        exportPath = "/var/lib/tomcat6/webapps/export/html/entities/";
                }
                return exportPath;
        }
        
        public static double getItemPrice(long itemId){
                CatalogClient catalogServiceClient = null;
                Client client = null;
                Double itemPrice = 0.0;
                try {
                        catalogServiceClient = new CatalogClient();
                        client = catalogServiceClient.getClient();
                        Item item = client.getItem(itemId);
                        itemPrice = item.getSellingPrice();
                        
                } catch(Exception e){
                        logger.error("Unable to get the item price because of:", e);
                }
                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 boolean validatePin(String pincode) {
                int pin;
                try {
                        pin = Integer.parseInt(pincode);
                }
                catch (NumberFormatException e) {
                        return false;
                }
                
                if (pin < 100000 || pin > 999999) {
                        return false;
                }
                return true;
        }

        public static boolean validatePhone(String phone) {
                long iPhone;
                try {
                iPhone = Long.parseLong(phone);
                }
                catch (NumberFormatException e) {
                        return false;
                }
                
                if (iPhone < 1000000000l || iPhone > 9999999999l) {
                        return false;
                }
                return true;
        }
        
        public static String getItemIdStringInCart(Cart cart) {
            List<String> itemIds = new ArrayList<String>();
        for (Line line : cart.getLines()) {
            itemIds.add(Long.toString(line.getItemId()));
        }
        return StringUtils.join(itemIds, '_');
    }
        
        public static String getItemIdStringFromOrders(List<Order> orders) {
        List<String> itemIds = new ArrayList<String>();
        if (orders != null) {
            for (Order order : orders) {
                for (LineItem lItem : order.getLineitems()) {
                    itemIds.add(Long.toString(lItem.getItem_id()));
                }
            }
        }
        return StringUtils.join(itemIds, '_');
    }

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