Subversion Repositories SmartDukaan

Rev

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.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class ProductIndexGenerator {
        private String[] indentation = {"", "    ", "        ", "            ","                "};
        private List<Entity> entities;
        private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();

        public ProductIndexGenerator(List<Entity> entities)     {
                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();
                        String modelName = entity.getModelName();
                        String modelNumber = entity.getModelNumber();
                        String url = getProductURL(entity);

                        sb.append("<div>\n");
                        sb.append(indentation[1] + "<a href='" + url + "'>");
                        sb.append(indentation[2] + brand + " " + modelName + " " + modelNumber);
                        sb.append(indentation[1] + "</a>\n");
                        sb.append("</div>\n");
                        
                        Slide slide = entity.getSlide(Utils.SUMMARY_SLIDE_DEFINITION_ID);
                        
                        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();

                                                        sb.append("<div>\n");
                                                        sb.append(indentation[1] + "<a href='" + url + "'>");
                                                        sb.append(indentation[2] + brand + " " + pdo.getValue() + " " + modelNumber);
                                                        sb.append(indentation[1] + "</a>\n");
                                                        sb.append("</div>\n");
                                                }
                                        }
                                } else if(feature.getFeatureDefinitionID() == 120157)   {
                                        List<Bullet> bullets =  feature.getBullets();
                                        
                                        if(bullets != null)     {
                                                for(Bullet bullet: bullets)     {
                                                        PrimitiveDataObject pdo = (PrimitiveDataObject)bullet.getDataObject();

                                                        sb.append("<div>\n");
                                                        sb.append(indentation[1] + "<a href='" + url + "'>");
                                                        sb.append(indentation[2] + brand + " " + modelName + " " + pdo.getValue());
                                                        sb.append(indentation[1] + "</a>\n");
                                                        sb.append("</div>\n");
                                                }
                                        }
                                }
                        }
                }
                String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "productindex.html";
                
                try     {
                        DBUtils.store(sb.toString(), indexFilename);
                        
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}