Subversion Repositories SmartDukaan

Rev

Rev 1705 | Rev 2227 | 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.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.generateXMLList();
        }
        
        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 = 0;
                        
                        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 == 0){
                                        itemPrice = item.getSellingPrice();
                                }else 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);
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");

                        
                        irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + itemPrice + "</ProductPrice>");
                        
                        irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");          
                }
        }
        
        /**
         * Generate the xml list of all the active phones and store in a file
         * @throws Exception
         */
        public void generateXMLList() 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 + "phonecurry.xml";
                DBUtils.store(productDataXML, productXMLFilename);
        }
}