Subversion Repositories SmartDukaan

Rev

Rev 4218 | 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.Media;
import in.shop2020.metamodel.core.Slide;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.FeatureDefinition;
import in.shop2020.metamodel.definitions.SlideDefinition;
import in.shop2020.metamodel.definitions.SlideFeatureDefinition;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedBullet;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.metamodel.util.ExpandedFeature;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.ui.util.NewVUI;


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

import org.apache.commons.lang.StringUtils;
import org.apache.thrift.transport.TTransportException;

public class FeatureValueExtractor {
        List<Long> featureIds = new ArrayList<Long>();
        Map<Long, List<String>> imageLabels = new HashMap<Long, List<String>>(); 
        StringBuffer imageNames = new StringBuffer();
        static CatalogClient cl;
        
        static {
                try {
                        cl = new CatalogClient();
                } catch (TTransportException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public Map<Long, String> extractFeatureValuesForEntity(Entity entity) throws Exception{
                Map<Long, String> featureIdValuesMap = new HashMap<Long, String>();
                
                for(Slide slide: entity.getSlides()){
                        if(slide.hasChildrenSlides()){
                                for(Slide sl: slide.getChildrenSlides()){
                                        if(sl.getFeatures()!=null){
                                                slide.getFeatures().addAll(sl.getFeatures());
                                        }
                                        if(sl.hasChildrenSlides()){
                                                for(Slide sl2: sl.getChildrenSlides()){
                                                        if(sl2.getFeatures()!=null){
                                                                slide.getFeatures().addAll(sl2.getFeatures());
                                                        }
                                                }
                                        }
                                }
                                
                        }
                        if(slide.getFeatures() == null){
                                continue;
                        }
                        for(Feature feature: slide.getFeatures()){
                                if(featureIds.contains(feature.getFeatureDefinitionID())){
                                        String value = "";
                                        ExpandedFeature expFeature = new ExpandedFeature(feature);
                                        if(expFeature.getExpandedBullets()!=null){
                                                int count = 0;
                                                for(ExpandedBullet expBullet: expFeature.getExpandedBullets()){
                                                        if(count==0){
                                                                value = value + expBullet.displayText();
                                                        }else{
                                                                value = value + " -- " + expBullet.displayText();
                                                        }
                                                        count++;
                                                }
                                        }
                                        featureIdValuesMap.put(feature.getFeatureDefinitionID(), value);
                                }
                        }
                }
                return featureIdValuesMap;
        }
        
        public StringBuffer printFeatureValues() throws Exception{
                StringBuffer featureValuesString = new StringBuffer();
                featureValuesString.append("Category Name" + "\t");
                featureValuesString.append("Entity Name" + "\t");
                featureValuesString.append("EntityID" + "\t");
                featureValuesString.append("ImageURL" + "\t");
                
                for(Long featureId : featureIds){
                        featureValuesString.append(Catalog.getInstance().getDefinitionsContainer().getFeatureDefinition(featureId).getLabel() + "\t");
                }
                featureValuesString.append("\n");
                
                for(Entity entity: CreationUtils.getEntities().values()){
                        if(entity == null || entity.getSlides() == null || Catalog.getInstance().getDefinitionsContainer().getCategory(entity.getCategoryID()).getParentCategory().getID() != 10001){
                                continue;
                        }
                        featureValuesString.append(Catalog.getInstance().getDefinitionsContainer().getCategory(entity.getCategoryID()).getLabel() + "\t");
                        featureValuesString.append(entity.getBrand()+ " " + entity.getModelName() + " " + entity.getModelNumber() + "\t");
                        featureValuesString.append(entity.getID() + "\t");
                        featureValuesString.append(getImageUrl(entity) + "\t");
                        //Just to get map
                        getAllImageUrls(entity);
                        Map<Long, String> fvalues = extractFeatureValuesForEntity(entity);
                        for(Long featureId : featureIds){
                                if(fvalues.get(featureId)!=null){
                                        featureValuesString.append(fvalues.get(featureId).trim() + "\t");
                                }else{
                                        featureValuesString.append("\t");
                                }
                        }
                        featureValuesString.append("\n");
                }
                return featureValuesString;
        }
        
        private String getImageUrl(Entity entity) throws Exception{
                ExpandedEntity expEntity = new ExpandedEntity(entity);
                long defaultImageCreationTime = EntityUtils.getCreationTimeFromSummarySlide(expEntity, "default");
                return "http://static0.saholic.com/images/media" + File.separator + entity.getID() + File.separator  + NewVUI.computeNewFileName(EntityUtils.getMediaPrefix(expEntity), "default.jpg", String.valueOf(defaultImageCreationTime));
        }
        
        private void getAllImageUrls(Entity entity) throws Exception{
                if(entity.getID() == 1000147){
                        System.out.println("N*");
                }
                ExpandedEntity expEntity = new ExpandedEntity(entity);
                Map<String, Media> medias = expEntity.getAllMedias();
                for(Item item :cl.getClient().getItemsByCatalogId(expEntity.getID())){
                        String url = "";
                        String color = item.getColor();
                        if(color==null){
                                color = "jsfhdjsfdhjfdfhdjffh";
                        }
                        String parts[] = color.split("(-|&|\\s|\\+|and|NA|\\.)+");
                        url = "http://static0.saholic.com/images/media" 
                                + File.separator + entity.getID() 
                                + File.separator  + NewVUI.computeNewFileName(EntityUtils.getMediaPrefix(expEntity), 
                                                "default.jpg", 
                                                String.valueOf(EntityUtils.getCreationTimeFromSummarySlide(expEntity, "default")));
                        for(String part: parts){
                                if(part.length() < 3){
                                        continue;
                                }
                                for(String label: expEntity.getAllImageLabels()){
                                        if(!StringUtils.containsIgnoreCase(label, part.trim())){
                                                url = "http://static0.saholic.com/images/media" 
                                                        + File.separator + entity.getID() 
                                                        + File.separator  + NewVUI.computeNewFileName(EntityUtils.getMediaPrefix(expEntity), 
                                                                        "default.jpg", 
                                                                        String.valueOf(EntityUtils.getCreationTimeFromSummarySlide(expEntity, "default")));
                                                continue;
                                        }else{
                                                url = "http://static0.saholic.com/images/media" + File.separator + entity.getID() + File.separator  + NewVUI.computeNewFileName(EntityUtils.getMediaPrefix(expEntity), medias.get(label).getFileName(), String.valueOf(medias.get(label).getCreationTime().getTime()));
                                                break;
                                        }
                                }       
                        }
                        imageNames.append(item.getId() + "\t" + url + "\n");
                }
                
                
        }
        
        public static void main(String[] args) throws Exception{

            FeatureValueExtractor extractor = new FeatureValueExtractor();
                DefinitionsContainer dfc = Catalog.getInstance().getDefinitionsContainer();
                List<SlideDefinition> slideDefs = dfc.getSlideDefinitions(10002);
                for(SlideDefinition slideDef: slideDefs){
                    for(SlideFeatureDefinition sfDef: slideDef.getSlideFeatureDefinitions()){
                        long fid = sfDef.getFeatureDefintionID();
                        if(!extractor.featureIds.contains(fid)){
                            extractor.featureIds.add(fid);
                        }
                    }
                    if(slideDef.hasChildren()){
                        for(long slideid: slideDef.getChildrenSlideDefinitionIDs()){
                            for(SlideFeatureDefinition sfDef: dfc.getSlideDefinition(slideid).getSlideFeatureDefinitions()){
                                long fid = sfDef.getFeatureDefintionID();
                                if(!extractor.featureIds.contains(fid)){
                                    extractor.featureIds.add(fid);
                                }        
                            }
                            if(dfc.getSlideDefinition(slideid).hasChildren()){
                                for(long slideid2: dfc.getSlideDefinition(slideid).getChildrenSlideDefinitionIDs()){
                                    for(SlideFeatureDefinition sfDef: dfc.getSlideDefinition(slideid2).getSlideFeatureDefinitions()){
                                        long fid = sfDef.getFeatureDefintionID();
                                        if(!extractor.featureIds.contains(fid)){
                                            extractor.featureIds.add(fid);
                                        }        
                                    }
                                }
                            }
                        }
                        
                    }
                }
                
                for(long fid: Catalog.getInstance().getDefinitionsContainer().getFeatureDefinitionIDs(10002)){
                    if(!extractor.featureIds.contains(fid)){
                        extractor.featureIds.add(fid);
                    }
                }
                for(long fid: Catalog.getInstance().getDefinitionsContainer().getFeatureDefinitionIDs(10003)){
            if(!extractor.featureIds.contains(fid)){
                extractor.featureIds.add(fid);
            }
        }
                for(long fid: Catalog.getInstance().getDefinitionsContainer().getFeatureDefinitionIDs(10004)){
            if(!extractor.featureIds.contains(fid)){
                extractor.featureIds.add(fid);
            }
        }
                for(long fid: Catalog.getInstance().getDefinitionsContainer().getFeatureDefinitionIDs(10005)){
            if(!extractor.featureIds.contains(fid)){
                extractor.featureIds.add(fid);
            }
        }
                
        /*      
            extractor.featureIds.add(120001L);
                extractor.featureIds.add(120002L);
                extractor.featureIds.add(120003L);
                extractor.featureIds.add(120008L);
                extractor.featureIds.add(120005L);
                extractor.featureIds.add(120006L);
                extractor.featureIds.add(120007L);
                extractor.featureIds.add(120009L);
                extractor.featureIds.add(120010L);
                extractor.featureIds.add(120082L);
                extractor.featureIds.add(120048L);
                extractor.featureIds.add(120049L);
                extractor.featureIds.add(120127L);
                extractor.featureIds.add(120011L);
                extractor.featureIds.add(120014L);
                extractor.featureIds.add(120015L);
                extractor.featureIds.add(120016L);
                extractor.featureIds.add(120017L);
                extractor.featureIds.add(120018L);
                extractor.featureIds.add(120019L);
                extractor.featureIds.add(120021L);
                extractor.featureIds.add(120023L);
                extractor.featureIds.add(120051L);
                extractor.featureIds.add(120022L);
                extractor.featureIds.add(120052L);
                extractor.featureIds.add(120020L);
                extractor.featureIds.add(120024L);
                extractor.featureIds.add(120025L);
                extractor.featureIds.add(120053L);
                extractor.featureIds.add(120026L);
                extractor.featureIds.add(120027L);
                extractor.featureIds.add(120028L);
                extractor.featureIds.add(120056L);
                extractor.featureIds.add(120058L);
                extractor.featureIds.add(120029L);
                extractor.featureIds.add(120030L);
                extractor.featureIds.add(120031L);
                extractor.featureIds.add(120032L);
                extractor.featureIds.add(120033L);
                extractor.featureIds.add(120072L);
                extractor.featureIds.add(120073L);
                extractor.featureIds.add(120076L);
                extractor.featureIds.add(120043L);
                extractor.featureIds.add(120044L);
                extractor.featureIds.add(120045L);
                extractor.featureIds.add(120122L);
                extractor.featureIds.add(120046L);
*/
                StringBuffer featureValuesString = extractor.printFeatureValues();
                File f = new File("/home/rajveer/Desktop/cms/featurevalues.txt");
                FileWriter writer = new FileWriter(f);
                writer.write(featureValuesString.toString());
                writer.close();
                
                File f1 = new File("/home/rajveer/Desktop/cms/imagenames.txt");
                FileWriter writer1 = new FileWriter(f1);
                writer1.write(extractor.imageNames.toString());
                writer1.close();
        
                /*
                String color = "rajveer  . this";
                String parts[] = color.split("(-|&|\\s|\\+|and|NA|\\.)+");
                //String parts[] = color.split("-|\\s|&|.|NA|\\+|and");
                for(String part: parts){
                        System.out.println(part);
                }
                */
        }
}