Subversion Repositories SmartDukaan

Rev

Rev 11706 | 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                productXMLSnippets.add("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
                
                for (Entity entity: entities)   {
                        try     {
                                long categoryId = defContainer.getCategory(entity.getCategoryID()).getParentCategory().getID();
                        } catch (NullPointerException e) {
                                continue;
                        }
                        if (! entityItemMap.containsKey(entity.getID()))        continue;
                        
                        Item item = entityItemMap.get(entity.getID());
                        String url = getProductURL(entity.getID(), item);
                        productXMLSnippets.add(this.xmlIndentation[2] + "<url><loc>" + url + "</loc></url>");
                }
                productXMLSnippets.add("</urlset>");
                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://m.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();
                }
        }
}