Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.util;

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

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

import in.shop2020.metamodel.core.Bullet;
import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.Feature;
import in.shop2020.metamodel.core.PrimitiveDataObject;
import in.shop2020.metamodel.core.Slide;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.status;
import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.thrift.clients.CatalogClient;

public class MobileSiteDataXMLGenerator {
        
        private List<Entity> entities;
        private List<Item> items;
        private Map<Long, Item> entityItemMap = new HashMap<Long, Item>();
        private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
        
        private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
    private CatalogClient csc;
    private Client client;
        
        public MobileSiteDataXMLGenerator(List<Entity> entities) throws Exception {
                this.entities = entities;
        csc = new CatalogClient();
        client = csc.getClient();

        items = client.getAllItemsByStatus(status.ACTIVE);
        items.addAll(client.getAllItemsByStatus(status.COMING_SOON));
        items.addAll(client.getAllItemsByStatus(status.PAUSED));
        
        for (Item item: items)  {
                if(!entityItemMap.containsKey(item.getCatalogItemId())) {
                        
                        entityItemMap.put(item.getCatalogItemId(), item);
                }
        }
        }
        
        void generate() throws Exception        {
        List<String> productXMLSnippets = new ArrayList<String>();
                productXMLSnippets.add("<Products>");
                
                for (Entity entity: entities)   {
                        try     {
                                long categoryId = defContainer.getCategory(entity.getCategoryID()).getParentCategory().getID();
                                
                                if(categoryId != Utils.MOBILE_PHONES_CATAGORY && categoryId != Utils.TABLETS_CATEGORY && categoryId != Utils.LAPTOPS_CATEGORY)  {
                                        continue;
                                }
                        } catch (NullPointerException e) {
                                continue;
                        }
                        
                        String description = "";
                        Slide slide = entity.getSlide(130054);
                        
                        if (slide == null)      continue;
                        
                        List<Feature> features = slide.getFeatures();
                        
                        if (features == null)   continue;
                        
                        for(Feature feature: slide.getFeatures())       {
                        
                                if (feature.getFeatureDefinitionID() == 120081) {
                        List<Bullet> bullets = feature.getBullets();
                        
                        if (bullets == null)    continue;
                        
                        int counter = 0;
                        
                        for (Bullet bullet : bullets) {
                                
                            PrimitiveDataObject dataObject = (PrimitiveDataObject) bullet.getDataObject();
                            
                            description += dataObject.getValue();
                            if(counter < 2)     description += ", ";
                            counter ++;
                        }
                    }
                        }
                        if (! entityItemMap.containsKey(entity.getID()))        continue;
                        
                        Item item = entityItemMap.get(entity.getID());
                        String productType = "";
                        String url = getProductURL(entity.getID(), item);
                        String imageUrl = "http://www.saholic.com/images/website" + File.separator + entity.getID() + File.separator + "thumbnail.jpg";
                        
                        if (entity.getCategoryID() == 10006)    {
                                productType = "Mobile Phone";
                        
                        } else if (entity.getCategoryID() == 10010)     {
                                productType = "Tablet";
                                
                        } else if (entity.getCategoryID() == 10050)     {
                                productType = "Laptop";
                        }
                        productXMLSnippets.add(this.xmlIndentation[1] + "<Product>");   
                        
                        productXMLSnippets.add(this.xmlIndentation[2] + "<ID>" + entity.getID() + "</ID>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<Type>" + productType + "</Type>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<Brand>" + item.getBrand() + "</Brand>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<ModelName>" + item.getModelName() + "</ModelName>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<ModelNumber>" + item.getModelNumber() + "</ModelNumber>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<URL>" + url + "</URL>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<ImageURL>" + imageUrl + "</ImageURL>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<ShortDesc>" + StringEscapeUtils.escapeXml(description) + "</ShortDesc>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<MRP>" + item.getMrp() + "</MRP>");
                        productXMLSnippets.add(this.xmlIndentation[2] + "<SellingPrice>" + (int)item.getSellingPrice() + "</SellingPrice>");
                        
                        productXMLSnippets.add(this.xmlIndentation[1] + "</Product>");
                }
                productXMLSnippets.add("</Products>");
                String productDataXML = StringUtils.join(productXMLSnippets, "\n");
                
                Utils.info(productDataXML);
                
                // Write it to file
                String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "msitedata.xml";
                DBUtils.store(productDataXML, productXMLFilename);
        }
        
        private String getProductURL(long entityId, Item item){
                //long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();
                in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
                
                String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
                String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
            + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
        + "-" + entityId;
                productUrl = productUrl.replaceAll("/", "-");
                url = url + productUrl;
                url = url.replaceAll("-+", "-");
                return url;
        }
        
        public static void main(String[] args) {
                try {
                        List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
                        MobileSiteDataXMLGenerator generator = new MobileSiteDataXMLGenerator(entities);
                        generator.generate();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}