Subversion Repositories SmartDukaan

Rev

Rev 2547 | Rev 2733 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.ui.util;

import in.shop2020.model.v1.catalog.InventoryService.Client;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.thrift.clients.CatalogServiceClient;
import in.shop2020.util.Utils;
import in.shop2020.utils.CategoryManager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;


/**
 * utility class to generated all html stuff from java objects
 * Driver class to merge Java data objects with VTL script. 
 * 
 * @author rajveer
 *
 */
public class PriceInsertor {

        public void insertPriceInSolrData(long catalogId, double price) throws IOException{
                String irSolrFilename = Utils.EXPORT_PATH + "xml/final/" + catalogId + "_irdata_solr.xml";
                String finalFile = Utils.EXPORT_PATH + "solr/" + catalogId + "_irdata_solr.xml";
                File f = new File(irSolrFilename);
                String s = FileUtils.readFileToString(f);
                s = s.replaceAll("<field name=\"F_50002\">.*</field>", "<field name=\"F_50002\">"+price+"</field>");
                File f1 = new File(finalFile);
                FileUtils.writeStringToFile(f1, s);
        }
        
        public void insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath){
                List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
                Item minPriceItem = null;
                for(Item item: items){
                        Map<String, String> itemDetail = new HashMap<String, String>();
                        boolean showmrp = true;
                        if (item.getMrp() <= item.getSellingPrice()) {
                                showmrp = false;
                        }
                        if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
                                minPriceItem = item;
                        }
                        double sellingPrice = item.getSellingPrice();
                        double mrp = item.getMrp();
                        double saving = Math.round((mrp-sellingPrice)/mrp*100);
                        itemDetail.put("ITEM_ID", ((int)item.getId())+"");      
                        itemDetail.put("SP", ((int)item.getSellingPrice())+"");
                        itemDetail.put("MRP", ((int)item.getMrp())+"");
                        itemDetail.put("SAVING", ((int)saving)+"");
                        itemDetail.put("COLOR", item.getColor());
                        itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
                        itemDetail.put("SHOWMRP", showmrp +"");
                        itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
                        itemDetails.add(itemDetail);
                }
                
                File exportDir = new File(exportPath + catalogId);
                if(!exportDir.exists()) {
                        exportDir.mkdir();
                }

                // This URL is used to ensure that images such as icon and thumbnail for
                // a particular entity are always downloaded from the same location.
                String staticurl = "http://static" + catalogId%3 + "." + domain;
                
                
                VelocityContext context = new VelocityContext();
                context.put("itemDetails", itemDetails);
                context.put("minPriceItem", minPriceItem);
                context.put("domain", domain);
                context.put("staticurl", staticurl);
        
                List<String> filenames = new ArrayList<String>();
                filenames.add("ProductDetail");
                filenames.add("WidgetSnippet");
                filenames.add("HomeSnippet");
                filenames.add("SearchSnippet");
                filenames.add("CategorySnippet");
                filenames.add("SlideGuide");
                filenames.add("ProductPropertiesSnippet");
                if(CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory()).getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY){
                        filenames.add("PhonesIOwnSnippet");
                        filenames.add("CompareProductSnippet");
                        filenames.add("ComparisonSnippet");
                        filenames.add("CompareProductSummarySnippet");
                        filenames.add("SlideNamesSnippet");
                        filenames.add("RelatedAccessories");
                }
                getHtmlFromVelocity(filenames,exportPath,context,catalogId);
        }
        
        public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
                try {
                        Properties p = new Properties();
                        p.setProperty("resource.loader", "file");
                        p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
                        p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
                        Velocity.init(p);
                        for(String filename: filenames){
                                Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
                                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + catalogId + File.separator +filename+".html")));
                                template.merge(context, writer);
                                writer.flush();
                                writer.close(); 
                        }
                }catch (ResourceNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (ParseErrorException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static void main(String[] args) throws Exception{
                PriceInsertor generator = new PriceInsertor();
                CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
                Client client = catalogServiceClient.getClient();
                List<Item> items = client.getItemsByCatalogId(1001210);
                List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
                Item minPriceItem = null;
                for(Item item: items){
                        Map<String, String> itemDetail = new HashMap<String, String>();
                        boolean showmrp = true;
                        if (item.getMrp() <= item.getSellingPrice()) {
                                showmrp = false;
                        }
                        if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
                                minPriceItem = item;
                        }
                        double sellingPrice = item.getSellingPrice();
                        double mrp = item.getMrp();
                        double saving = Math.round((mrp-sellingPrice)/mrp*100);
                        itemDetail.put("ITEM_ID", ((int)item.getId())+"");      
                        itemDetail.put("SP", ((int)item.getSellingPrice())+"");
                        itemDetail.put("MRP", ((int)item.getMrp())+"");
                        itemDetail.put("SAVING", ((int)saving)+"");
                        itemDetail.put("COLOR", item.getColor());
                        itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
                        itemDetail.put("SHOWMRP", showmrp +"");
                        itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
                        itemDetails.add(itemDetail);
                }
                
                String staticurl = "http://static" + 1001210%3 + "." + "saholic.com";
                VelocityContext context = new VelocityContext();
                context.put("itemDetails", itemDetails);
                context.put("minPriceItem", minPriceItem);
                context.put("domain", "saholic.com");
                context.put("staticurl", staticurl);
                
                List<String> filenames = new ArrayList<String>();
                filenames.add("ProductDetail");
                filenames.add("WidgetSnippet");
                filenames.add("HomeSnippet");
                filenames.add("SearchSnippet");
                filenames.add("CategorySnippet");
                filenames.add("SlideGuide");
                filenames.add("PhonesIOwnSnippet");
                filenames.add("ProductPropertiesSnippet");
                if(CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory()).getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY){
                        filenames.add("CompareProductSnippet");
                        filenames.add("ComparisonSnippet");
                        filenames.add("CompareProductSummarySnippet");
                        filenames.add("SlideNamesSnippet");
                        filenames.add("RelatedAccessories");
                }               
                generator.getHtmlFromVelocity(filenames,Utils.EXPORT_ENTITIES_PATH_SAHOLIC,context,1001210);
        }
}