Subversion Repositories SmartDukaan

Rev

Rev 5155 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.util;

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 java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class ProductIndexGenerator {
        private String[] indentation = {"", "    ", "        ", "            ","                "};
        private List<Entity> entities;
        private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
        private Map<String, List<String>> brandSynonyms = new HashMap<String, List<String>>();
        
        
        public ProductIndexGenerator(List<Entity> entities)     {
                Map<String, String> brSynonyms = new HashMap<String, String>(); 
                try {
                        brSynonyms =  CreationUtils.getSynonyms().get("brand");
                        for(String brand : brSynonyms.keySet()){
                                List<String> brandSyns = Arrays.asList((brand + "," + StringUtils.defaultString(brSynonyms.get(brand))).split(","));
                                brandSynonyms.put(brand, brandSyns);
                        }
                } catch (Exception e) {
                        // TODO: handle exception
                }
                this.entities = entities;
        }
        
        private String getProductURL(Entity entity){
                in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(entity.getCategoryID()).getParentCategory();
                
                String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
                String productUrl = ((entity.getBrand() != null) ? entity.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
        + "-" + ((entity.getModelName() != null) ? entity.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
            + "-" + ((entity.getModelNumber() != null ) ? entity.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
        + "-" + entity.getID();
                productUrl = productUrl.replaceAll("/", "-");
                url = url + productUrl;
                url = url.replaceAll("-+", "-");
                return url;
        }
        
        public void generate()  {
                StringBuilder sb = new StringBuilder();
                
                for (Entity entity: entities)   {
                        String brand = entity.getBrand();
                        try{
                        String url = getProductURL(entity);

                        
                        Slide slide = entity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID);
                        List<String> modelNameSyn = new ArrayList<String>();
                        modelNameSyn.add(entity.getModelName());
                        List<String> modelNumberSyn = new ArrayList<String>();
                        modelNumberSyn.add(entity.getModelNumber());
                        
                        for(Feature feature: slide.getFeatures())       {
                                
                                if(feature.getFeatureDefinitionID() == 120156)  {
                                        List<Bullet> bullets =  feature.getBullets();
                                        
                                        if(bullets != null)     {
                                                for(Bullet bullet: bullets){
                                                        PrimitiveDataObject pdo = (PrimitiveDataObject) bullet.getDataObject();
                                                        modelNameSyn.add(pdo.getValue());
                                                }
                                        }
                                } else if(feature.getFeatureDefinitionID() == 120157)   {
                                        List<Bullet> bullets =  feature.getBullets();
                                        
                                        if(bullets != null)     {
                                                for(Bullet bullet: bullets)     {
                                                        PrimitiveDataObject pdo = (PrimitiveDataObject)bullet.getDataObject();
                                                        modelNumberSyn.add(pdo.getValue());
                                                }
                                        }
                                }
                        }
                        for(String br : getBrandSynonyms(brand)) {
                                for (String mName : modelNameSyn){
                                        for (String mNumber : modelNumberSyn){
                                                sb.append("<div>\n");
                                                sb.append(indentation[1] + "<a href='" + url + "'>");
                                                sb.append(br + " " + mName + " " + mNumber);
                                                sb.append("</div>\n");
                                                sb.append("</a>\n");
                                        }
                                }
                        }
                        }catch(Exception e){
                                e.printStackTrace();
                        }
                }
                String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "productindex.html";
                
                try     {
                        DBUtils.store(sb.toString(), indexFilename);
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        
        private List<String> getBrandSynonyms(String brand) {
                if(!brandSynonyms.containsKey(brand)){
                        List<String> list = new ArrayList<String>();
                        list.add(brand);
                        brandSynonyms.put(brand, list);
                }
                return brandSynonyms.get(brand);
        }
        
        public static void main(String[] args) {
                try {
                        List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
                        ProductIndexGenerator generator = new ProductIndexGenerator(entities);
                        generator.generate();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}