Subversion Repositories SmartDukaan

Rev

Rev 5503 | Go to most recent revision | 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.Category;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedEntity;

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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MostComparedIndexGenerator {
        
        private String[] indentation = {"", "    ", "        ", "            ","                "};
        private Map<String, List<String>> brandSynonyms = new HashMap<String, List<String>>();
        private static Log log = LogFactory.getLog(MostComparedIndexGenerator.class);

        private List<Long> validEntityIds;


        public MostComparedIndexGenerator(List<Long> valiEntityIds) {
                Map<String, String> brSynonyms = new HashMap<String, String>();

                this.validEntityIds = valiEntityIds;
                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
                }
        }


        
        public void generate()  {
                StringBuilder sb = new StringBuilder();
                try {
                        sb.append(getAutoGeneratedComparisonLinks());
                } catch(Exception e){
                        log.error("Could not generate Autogenerated Comparison Links");
                        e.printStackTrace();
                }
                String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "most-compared-index.html";
                
                try     {
                        DBUtils.store(sb.toString(), indexFilename);
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public static void main(String[] args) {
                List<Long> list = new ArrayList<Long>();
                MostComparedIndexGenerator g = new MostComparedIndexGenerator(list);
                g.generate();
        }
        
        private StringBuffer getAutoGeneratedComparisonLinks() throws Exception {
                Map<Long, Map<Long, Long>> comparisonStats = null;
                StringBuffer sbProds = new StringBuffer();
                comparisonStats = CreationUtils.getComparisonStats();
                if(comparisonStats != null){
                        for(Map.Entry<Long, Map<Long, Long>> entry : comparisonStats.entrySet()){
                                Long entityId = entry.getKey();
                                Entity entity = CreationUtils.getEntity(entityId);
                                if(entity == null) {
                                        continue;
                                }
                                ExpandedEntity expandedEntity = new ExpandedEntity(entity);
                                Category category = expandedEntity.getCategory();
                                if(category == null){
                                        continue;
                                }
                                if(this.validEntityIds.contains(entityId) && category.getParentCategory().getID()==Utils.MOBILE_PHONES_CATAGORY) {
                                        Slide slide = expandedEntity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID);
                                        List<String> modelNameSyn = new ArrayList<String>();
                                        modelNameSyn.add(expandedEntity.getModelName());
                                        List<String> modelNumberSyn = new ArrayList<String>();
                                        modelNumberSyn.add(expandedEntity.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());
                                                                }
                                                        }
                                                }
                                        }

                                        String entityName = EntityUtils.getProductName(expandedEntity);
                                        String hyphendatedName = entityName.replaceAll(" +", "-").replaceAll("/+", "-").replaceAll("-+", "-").toLowerCase();
                                        sbProds.append(indentation[1] + "<div>\n");
                                        sbProds.append(indentation[2] + "<a href='/generated/compare-" + hyphendatedName 
                                                        +"?p1=" + entityId + "'>Compare " + entityName +"</a>\n");
                                        sbProds.append(indentation[1] + "</div>\n");
                                        StringBuffer sbProds1 = new StringBuffer();
                                        StringBuffer sbProds2 = new StringBuffer();
                                        for (Long anotherEntityId : entry.getValue().keySet()) {
                                                if(entityId.equals(anotherEntityId)){
                                                        continue;
                                                }
                                                Entity anotherEntity = CreationUtils.getEntity(anotherEntityId);
                                                if(anotherEntity == null) {
                                                        continue;
                                                }
                                                ExpandedEntity anotherExpandedEntity = new ExpandedEntity(anotherEntity);
                                                Category anotherCategory = anotherExpandedEntity.getCategory();
                                                if(anotherCategory == null){
                                                        continue;
                                                }

                                                if(this.validEntityIds.contains(anotherEntityId) && anotherCategory.getParentCategory().getID() == Utils.MOBILE_PHONES_CATAGORY){
                                                        Slide slide1 = anotherExpandedEntity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID);
                                                        List<String> modelNameSyn1 = new ArrayList<String>();
                                                        modelNameSyn1.add(anotherExpandedEntity.getModelName());
                                                        List<String> modelNumberSyn1 = new ArrayList<String>();
                                                        modelNumberSyn1.add(anotherExpandedEntity.getModelNumber());
                                                        
                                                        for(Feature feature: slide1.getFeatures())      {
                                                                
                                                                if(feature.getFeatureDefinitionID() == 120156)  {
                                                                        List<Bullet> bullets =  feature.getBullets();
                                                                        
                                                                        if(bullets != null)     {
                                                                                for(Bullet bullet: bullets){
                                                                                        PrimitiveDataObject pdo = (PrimitiveDataObject) bullet.getDataObject();
                                                                                        modelNameSyn1.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();
                                                                                        modelNumberSyn1.add(pdo.getValue());
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        String anotherEntityName = EntityUtils.getProductName(anotherExpandedEntity);
                                                        String anotherHyphenatedName = anotherEntityName.replaceAll(" +", "-").replaceAll("/+", "-").replaceAll("-+", "-").toLowerCase();
                                                        
                                                        sbProds1.append(indentation[1] + "<div>\n");
                                                        sbProds1.append(indentation[2] + "<a href='/compare-mobile-phones/" + hyphendatedName + "-vs-" + anotherHyphenatedName 
                                                                        +"?p1=" + entityId +"&p2="+ anotherEntityId+"'>" + entityName + " Vs "+ anotherEntityName + "</a>\n");
                                                        sbProds1.append(indentation[1] + "</div>\n");
                                                        sbProds2.append(indentation[1] + "<div>\n");
                                                        sbProds2.append(indentation[2] + "<a href='/generated/" + hyphendatedName + "-vs-" + anotherHyphenatedName 
                                                                        +"?p1=" + entityId +"&p2="+ anotherEntityId+" '>" + entityName + " Vs "+ anotherEntityName + "</a>\n");
                                                        sbProds2.append(indentation[1] + "</div>\n");

                                                        StringBuffer sb = new StringBuffer();
                                                        for(String br : getBrandSynonyms(expandedEntity.getBrand())){
                                                                for (String mName : modelNameSyn){
                                                                        for (String mNumber : modelNumberSyn){
                                                                                for(String br1 : getBrandSynonyms(anotherExpandedEntity.getBrand())) {
                                                                                        for (String mName1 : modelNameSyn1){
                                                                                                for (String mNumber1 : modelNumberSyn1){
                                                                                                        sb.append(indentation[1] + "<div>\n");
                                                                                                        sb.append(indentation[2] + "<a href='http://www.saholic.com/compare-mobile-phones/" + hyphendatedName + "-vs-" + anotherHyphenatedName 
                                                                                                                        +"?p1=" + entityId +"&p2="+ anotherEntityId+" '>" + br + " " + mName + " " + mNumber 
                                                                                                                        + " Vs "+ br1 + " " + mName1 + " " + mNumber1 + "</a>\n");
                                                                                                        sb.append(indentation[1] + "</div>\n");
                                                                                                }
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "most-compared-" + entityId + "-vs-" + anotherEntityId + ".html";
                                                        
                                                        try     {
                                                                DBUtils.store(sb.toString(), indexFilename);
                                                                
                                                        } catch (Exception e) {
                                                                e.printStackTrace();
                                                        }
                                                }
                                        }
                                        String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "most-compared-" + entityId + ".html";
                                        
                                        try     {
                                                DBUtils.store(sbProds1.append(sbProds2).toString(), indexFilename);
                                                
                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }
                                }
                        }
                }
                
                return sbProds;
                
        }
        

        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);
        }
}