Rev 7860 | Rev 9218 | 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.CatalogService.Client;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.Source;import in.shop2020.model.v1.catalog.SourceItemPricing;import in.shop2020.model.v1.catalog.status;import in.shop2020.model.v1.order.EmiScheme;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.TransactionClient;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.Locale;import java.util.Map;import java.util.Properties;import java.util.TreeMap;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 {static Map<Long, EmiScheme> EMI_SCHEMES = new TreeMap<Long, EmiScheme>();static {try {TransactionClient transactionClient = new TransactionClient("support_transaction_service_server_host", "transaction_service_server_port");in.shop2020.model.v1.order.TransactionService.Client trxClient = transactionClient.getClient();List<EmiScheme> emiSchemes = trxClient.getAvailableEmiSchemes();for (EmiScheme emiScheme : emiSchemes){if(!EMI_SCHEMES.containsKey(emiScheme.getMinAmount())) {EMI_SCHEMES.put(emiScheme.getMinAmount(), emiScheme);} else {EmiScheme emiSchemeOld = EMI_SCHEMES.get(emiScheme.getMinAmount());if(Double.compare((1 + (emiSchemeOld.getChargeValue()/100))/emiSchemeOld.getTenure(), (1 + (emiScheme.getChargeValue()/100))/emiScheme.getTenure()) > 0){EMI_SCHEMES.put(emiScheme.getMinAmount(), emiScheme);}}}} catch (Exception e) {e.printStackTrace();}}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, String availabilityString) 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);s = s.replaceAll("<field name=\"F_50028\">.*</field>", availabilityString);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;String offerDetailText = null;String offerDetailLink = null;String afterArrival = null;String showPrice = "TRUE";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 (CatalogServiceException 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);if(item.getItemStatus().equals(status.COMING_SOON)){afterArrival = "after arrival";if(!item.isShowSellingPrice()){showPrice = "FALSE";}}String bestDealsText = item.getBestDealText();if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null && status.ACTIVE.equals(item.getItemStatus())){offerText = bestDealsText;String bestDealsDetailsText = item.getBestDealsDetailsText();if( bestDealsDetailsText != null && bestDealsDetailsText.length() > 0){offerDetailText = bestDealsDetailsText;}String bestDealsDetailsLink = item.getBestDealsDetailsLink();if( bestDealsDetailsLink != null && bestDealsDetailsLink.length() > 0){offerDetailLink = bestDealsDetailsLink;}}}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.contains("store") ? "saholic.com":domain);VelocityContext context = new VelocityContext();context.put("domain", domain.contains("store") ? "saholic.com":domain);context.put("itemDetails", itemDetails);context.put("minPriceItem", minPriceItem);context.put("staticurl", staticurl);context.put("OFFER_TEXT", offerText);context.put("OFFER_DETAIL_TEXT", offerDetailText);context.put("OFFER_DETAIL_LINK", offerDetailLink);context.put("AFTER_ARRIVAL", afterArrival);context.put("SHOW_PRICE", domain.contains("store")?"FALSE":showPrice);context.put("IS_STORE", domain.contains("store")?"TRUE":"FALSE");context.put("EMI", getEMI(items.get(0).getSellingPrice()));List<String> filenames = new ArrayList<String>();if(!domain.contains("store")){filenames.add("WidgetSnippet");filenames.add("ProductPropertiesSnippet");filenames.add("MyResearchSnippet");filenames.add("AfterSales");filenames.add("SearchSnippet");filenames.add("CategorySnippet");filenames.add("HomeSnippet");filenames.add("ProductDetail");filenames.add("SlideGuide");}else {filenames.add("store/" + catalogId + "/SearchSnippet");filenames.add("store/" + catalogId + "/CategorySnippet");filenames.add("store/" + catalogId + "/HomeSnippet");filenames.add("store/" + catalogId + "/ProductDetail");filenames.add("store/" + catalogId + "/SlideGuide");}Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(items.get(0).getCategory());if(category!=null && !domain.contains("store")){Category parentCategory = category.getParentCategory();if(parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY) {filenames.add("PhonesIOwnSnippet");}if(parentCategory.isHasAccessories()){filenames.add("RelatedAccessories");}filenames.add("CompareProductSnippet");filenames.add("SlideNamesSnippet");filenames.add("ComparisonSnippet");if(category.isComparable()) {filenames.add("MostComparedProducts");filenames.add("CompareProductSummarySnippet");filenames.add("MostComparedSnippet");}}getHtmlFromVelocity(filenames,exportPath,context,catalogId);return minPriceItem.getSellingPrice();}private static String getEMI(double sellingPrice) {String returnString = null;EmiScheme finalEmi = null;for (EmiScheme emiScheme : EMI_SCHEMES.values() ){if(emiScheme.getMinAmount() <= sellingPrice) {if(finalEmi == null){finalEmi = emiScheme;} else if(Double.compare((1 + (finalEmi.getChargeValue()/100))/finalEmi.getTenure(), (1 + (emiScheme.getChargeValue()/100))/emiScheme.getTenure()) > 0){finalEmi = emiScheme;}}}if(finalEmi != null) {returnString = String.format(Locale.getDefault(),"%.0f",sellingPrice * (1 + (finalEmi.getChargeValue()/100))/finalEmi.getTenure());}return returnString;}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){if(filename.contains("store/")){Template template = Velocity.getTemplate(filename + ".vm", "UTF-8");BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator + filename.split(""+ catalogId +"/")[1] +".html")));template.merge(context, writer);writer.flush();writer.close();} else {Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm", "UTF-8");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();}}}