Subversion Repositories SmartDukaan

Rev

Rev 5155 | 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.Slide;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.Category;
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 CompatibleAccessoriesIndexGenerator {

        private String[] indentation = {"", "    ", "        ", "            ","                "};
        private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
        List<Entity> entities;
        private Map<Long, Long> categorySlides = new HashMap<Long, Long>();
        private Map<Long, String> categoryLabels = new HashMap<Long, String>();

        private Map<Long, List<String>> categorySynonyms = new HashMap<Long, List<String>>();
        private Map<String, List<String>> brandSynonyms = new HashMap<String, List<String>>();
        
        public CompatibleAccessoriesIndexGenerator(List<Entity> entities) throws Exception      {
                
                categorySlides.put((long) 10014, (long) 130067);
                categorySlides.put((long) 10018, (long) 130073);
                
                categoryLabels.put((long) 10014, "Battery");
                categoryLabels.put((long) 10018, "Carrying Case");

                Map<String, String> brSynonyms = new HashMap<String, String>(); 
                Map<String, String> catSynonyms = new HashMap<String, String>();
                
                
                try {
                        brSynonyms =  CreationUtils.getSynonyms().get("brand");
                        catSynonyms =  CreationUtils.getSynonyms().get("subcategory");
                        
                        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
                }
                catSynonyms.put("Battery", StringUtils.defaultString("Battery," + StringUtils.defaultString(catSynonyms.get("Battery"))));
                catSynonyms.put("Carrying Case", StringUtils.defaultString("Carrying Case," + StringUtils.defaultString(catSynonyms.get("Carrying Case"))));
                
                categorySynonyms.put((long) 10014, Arrays.asList(catSynonyms.get("Battery").split(",")));
                categorySynonyms.put((long) 10018, Arrays.asList(catSynonyms.get("Carrying Case").split(",")));
                this.entities = entities;
        }
        
        public void generate()  {
                StringBuilder sb = new StringBuilder();
                
                for(Entity entity: entities)    {
                        
                        long categoryId = entity.getCategoryID();
                        String url = getProductURL(entity);
                        
                        if (url != null && categorySlides.keySet().contains(categoryId))        {
                                
                                Slide compatibilitySlide = entity.getSlide(categorySlides.get(categoryId));
                                
                                try {
                                        String text = compatibilitySlide.getFreeformContent().getFreeformText();
                                        
                                        if(text.indexOf("<ul>") > -1)   continue;
                                        for(String name: getHandsetNames(text)) {
                                                for(String categoryLabel : getCategorySynonyms(categoryId)){
                                                        sb.append(indentation[1] + "<div>\n");
                                                        sb.append(indentation[2] + "<a href='" + url + "'>" + name + " " + categoryLabel + "</a>\n");
                                                        sb.append(indentation[1] + "</div>\n");
                                                }
                                        }
                                        
                                } catch (NullPointerException e) {
                                        // TODO: handle exception
                                }
                        }
                }
                
                String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "compatible-accessories-index.html";
                
                try     {
                        DBUtils.store(sb.toString(), indexFilename);
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        
        private String getProductURL(Entity entity)     {

                Category category = defContainer.getCategory(entity.getCategoryID());
                try {
                        Category parentCategory = category.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;
                        
                } catch (NullPointerException e) {
                        return null;
                }
        }
        
        private List<String> getHandsetNames(String rawText)    {
                
                List<String> names = new ArrayList<String>();
                
                for (String namesWithSameBrand: rawText.trim().split("\n"))     {
                        boolean isFirst = true;
                        List<String> brandSynonyms = null;
                        
                        for(String name: namesWithSameBrand.trim().split(","))  {
                                name = name.trim();
                                String [] brandName;
                                if(isFirst)     {
                                        brandName = name.split(" ", 2);
                                        if(brandName.length==1){
                                                continue;
                                        }
                                        if(brandName[1].startsWith("Ericsson")) {
                                                brandName[0] = "Sony Ericsson";
                                                brandName[1] = brandName[1].substring(9);
                                        }
                                        brandSynonyms = getBrandSynonyms(brandName[0]);
                                        name = brandName[1];
                                        isFirst = false;
                                } 
                                for(String brand : brandSynonyms){
                                        names.add(brand + " " + name);
                                }
                                
                        }
                }
                return names;
        }
        
        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);
        }

        private List<String> getCategorySynonyms(Long categoryId) {
                return categorySynonyms.get(categoryId);
        }
        
        

        public static void main(String[] args) {
                try {
                        List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
                        CompatibleAccessoriesIndexGenerator generator = new CompatibleAccessoriesIndexGenerator(entities);
                        generator.generate();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}