Subversion Repositories SmartDukaan

Rev

Rev 1475 | Rev 2306 | Go to most recent revision | Blame | Compare with Previous | 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 in.shop2020.config.ConfigException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.thrift.clients.config.ConfigClient;

public class Utils {
        public static final String EXPORT_ENTITIES_PATH = getExportPath();
        
        private static String getExportPath(){
                String exportPath=null;
                ConfigClient client = ConfigClient.getClient();
                try{
                        exportPath = client.get("export_entities_path");
                }catch(ConfigException ce){
                        ce.printStackTrace();
                        exportPath = "/var/lib/tomcat6/webapps/export/html/entities/";
                }
                return exportPath;
        }
        
        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 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 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");
//      }
        
}