Rev 4802 | Rev 5233 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.ui.util;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.Category;import in.shop2020.model.v1.catalog.InventoryService.Client;import in.shop2020.model.v1.catalog.InventoryServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.Source;import in.shop2020.model.v1.catalog.SourceItemPricing;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.util.Utils;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.thrift.TException;import org.apache.thrift.transport.TTransportException;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 {CatalogClient csc;Client client;public PriceInsertor() throws TTransportException{csc = new CatalogClient();client = csc.getClient();}public void copySolrSchemaFiles() throws IOException{String source = Utils.EXPORT_PATH + "xml/final/irmetadata_solrschema.xml";String destination = Utils.EXPORT_PATH + "solr/irmetadata_solrschema.xml";FileUtils.copyFile(new File(source), new File(destination));source = Utils.EXPORT_PATH + "xml/final/irmetadata_catchall.xml";destination = Utils.EXPORT_PATH + "solr/irmetadata_catchall.xml";FileUtils.copyFile(new File(source), new File(destination));}public void insertPriceInSolrData(long catalogId, String priceString) 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>", priceString);File f1 = new File(finalFile);FileUtils.writeStringToFile(f1, s);}/***** @param items* @param catalogId* @param domain* @param exportPath* @param source* @return*/public double insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath, Source source){List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();String offerText = null;Item minPriceItem = null;for(Item item: items){Map<String, String> itemDetail = new HashMap<String, String>();boolean showmrp = true;double sellingPrice = item.getSellingPrice();double mrp = item.getMrp();if(source != null){try {SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());sellingPrice = sip.getSellingPrice();mrp = sip.getMrp();} catch (TException e) {} catch (InventoryServiceException e) {}}if (mrp <= sellingPrice) {showmrp = false;}if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){minPriceItem = item;}double saving = Math.round((mrp-sellingPrice)/mrp*100);itemDetail.put("ITEM_ID", ((int)item.getId())+"");itemDetail.put("SP", ((int)sellingPrice)+"");itemDetail.put("MRP", ((int)mrp)+"");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 bestDealsText = item.getBestDealText();if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null){offerText = bestDealsText;}}if(source == null){exportPath = exportPath + catalogId;}else{exportPath = exportPath + catalogId + File.separator + source.getId();}File exportDir = new File(exportPath);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);context.put("OFFER_TEXT", offerText);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");filenames.add("MyResearchSnippet");filenames.add("AfterSales");Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(items.get(0).getCategory());if(category!=null){Category parentCategory = category.getParentCategory();if(parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY) {filenames.add("PhonesIOwnSnippet");}if(parentCategory.isHasAccessories()){filenames.add("RelatedAccessories");}if(parentCategory.isComparable()) {filenames.add("MostComparedProducts");filenames.add("CompareProductSnippet");filenames.add("ComparisonSnippet");filenames.add("CompareProductSummarySnippet");filenames.add("SlideNamesSnippet");}}getHtmlFromVelocity(filenames,exportPath,context,catalogId);return minPriceItem.getSellingPrice();}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 + File.separator +filename+".html")));template.merge(context, writer);writer.flush();writer.close();}}catch (ResourceNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ParseErrorException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}