Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.util;

import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.EntityState;
import in.shop2020.metamodel.core.EntityStatus;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.status;
import in.shop2020.thrift.clients.CatalogServiceClient;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;


/**
 * Command line utility to generate xml file for partners
 * 
 * @author rajveer
 *
 */
public class ProductListGenerator {
        
        private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
        CatalogServiceClient catalogServiceClient = null;
        in.shop2020.model.v1.catalog.InventoryService.Client client = null;
        public static long MOBILE_PHONES = 10001;
        
        
        public static void main(String[] args) throws Exception {

                /*
                String[] options = new String[] {"domain", "partner"};
                String[] values = new String[] {"saholic.com", "phonecurry"};

                String usage = "Usage: ProductListGenerator ["+ StringUtils.join(options, "|") +"] ["+ StringUtils.join(values, "|") + "] ";
                
                if(args.length < 1) {
                        System.out.println(usage);
                        System.exit(-1);
                }
                String inputCommand = args[0];
                String inputID = null;
                if(args.length == 2){
                        inputID = args[1];
                }
                */
                ProductListGenerator generator = new ProductListGenerator();
                generator.generateProductsListXML();
                generator.generateProductListJavascript();
        }
        
        public ProductListGenerator() throws Exception {
                catalogServiceClient = new CatalogServiceClient();
                client = catalogServiceClient.getClient();
        }
        
        /**
         * 
         * @param expEntity
         * @return url
         */
        private String getEntityURL(ExpandedEntity expEntity){
                String url = "http://www.saholic.com/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";
                String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')
        + "-" + expEntity.getModelName().toLowerCase().replace(' ', '-') 
            + "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')
        + "-" + expEntity.getID();
                productUrl = productUrl.replaceAll("/", "-");
                url = url + productUrl;
                url = url.replaceAll("--", "-");
                return url;
        }

        /**
         * get xml feed for partner sites
         * @param irDataXMLSnippets
         * @throws Exception
         */
        private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{      
                Map<Long, Entity> entities =  CreationUtils.getEntities();
                for(Entity entity: entities.values()){
                        EntityState state = CreationUtils.getEntityState(entity.getID());
                        
                        ExpandedEntity expEntity = new ExpandedEntity(entity);
                        if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
                                continue;
                        }
                        if(state.getStatus() != EntityStatus.READY){
                                continue;
                        }
                        
                        double itemPrice = Double.MAX_VALUE;
                        
                        List<Item> items = client.getItemsByCatalogId(entity.getID());
                        if(items == null){
                                continue;
                        }
                        
                        Date todate = new Date();
                        Boolean hasActiveItem = false;
                        for(Item item: items){
                                if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
                                                hasActiveItem = true;
                                                if(itemPrice > item.getSellingPrice()){
                                                        itemPrice = item.getSellingPrice();
                                                }
                                }
                        }
                        if(!hasActiveItem){
                                continue;
                        }
                        
                        long categoryID = expEntity.getCategoryID();
                        if(categoryID == -1){
                                continue;
                        }
                        irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");   
                        
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + expEntity.getID() + "</ProductSKU>");
                        
                        String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");

                        String url = getEntityURL(expEntity);
                        
                        String imageUrl = "http://static" + ((expEntity.getID()+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + expEntity.getID() + File.separator + "icon.jpg";    
                        
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");

                        
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
                        
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");
                        
                        irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");          
                }
        }
        
        /**
         * Generate the xml list of all the active phones and store in a file
         * @throws Exception
         */
        public void generateProductsListXML() throws Exception {
                ArrayList<String> productXMLSnippets = new ArrayList<String>();
                productXMLSnippets.add("<saholic.com>");
                
                getProductsXML(productXMLSnippets);
                
                productXMLSnippets.add("</saholic.com>");
                
                String productDataXML = StringUtils.join(productXMLSnippets, "\n");
                
                Utils.info(productDataXML);
                
                // Write it to file
                String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";
                DBUtils.store(productDataXML, productXMLFilename);
        }




        /**
         * get xml feed for partner sites
         * @param irDataXMLSnippets
         * @throws Exception
         */
        private void getProductsJavascript(StringBuffer sb) throws Exception{
                boolean isFirst = true;
                Map<Long, Entity> entities =  CreationUtils.getEntities();
                for(Entity entity: entities.values()){
                        EntityState state = CreationUtils.getEntityState(entity.getID());
                        
                        ExpandedEntity expEntity = new ExpandedEntity(entity);
                        if(expEntity.getCategoryID() == -1 || expEntity.getCategory().getParentCategory().getID() != MOBILE_PHONES){
                                continue;
                        }
                        if(state.getStatus() != EntityStatus.READY){
                                continue;
                        }
                        
                        List<Item> items = client.getItemsByCatalogId(entity.getID());
                        if(items == null){
                                continue;
                        }
                        
                        Date todate = new Date();
                        Boolean hasActiveItem = false;
                        for(Item item: items){
                                if((item.getItemStatus() == status.ACTIVE || item.getItemStatus() == status.PAUSED) && todate.getTime() > item.getStartDate()){
                                                hasActiveItem = true;
                                }
                        }
                        if(!hasActiveItem){
                                continue;
                        }
                        
                        long categoryID = expEntity.getCategoryID();
                        if(categoryID == -1){
                                continue;
                        }
                        String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();
                        if(isFirst){
                                sb.append("\"" + title + "\":" + entity.getID());
                                isFirst = false;
                        }else{
                                sb.append(",\"" + title + "\":" + entity.getID());
                        }
                        
                }
        }
        
        
        /**
         * Generate the javascript list of all the active phones and store in a file
         * @throws Exception
         */
        public void generateProductListJavascript() throws Exception {
                StringBuffer productsJavascript = new StringBuffer();
                
                productsJavascript.append("var productIdNames={");
                
                getProductsJavascript(productsJavascript);
                
                productsJavascript.append("};");
                
                // Write it to file
                String productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";
                DBUtils.store(productsJavascript.toString(), productXMLFilename);
        }

}