Rev 2198 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.ui.util;import in.shop2020.metamodel.core.Bullet;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.EntityState;import in.shop2020.metamodel.core.EntityStatus;import in.shop2020.metamodel.core.Feature;import in.shop2020.metamodel.core.PrimitiveDataObject;import in.shop2020.metamodel.definitions.CMPBucketDefinition;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.definitions.SlideDefinition;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.metamodel.util.ExpandedSlide;import in.shop2020.model.v1.catalog.InventoryService.Client;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.status;import in.shop2020.thrift.clients.CatalogServiceClient;import in.shop2020.util.Utils;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.StringWriter;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import javax.imageio.ImageIO;import org.apache.commons.lang.StringUtils;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.exception.MethodInvocationException;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 NewVUI {CatalogServiceClient catalogServiceClient = null;Client client = null;String lastContentVersion;String contentVersion;StringBuffer problems = new StringBuffer();public NewVUI(Long contentVersion) throws Exception {catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();this.lastContentVersion = contentVersion.toString();this.contentVersion = (new Date()).getTime() +"" ;}/*** Usage: VUI [entities | entity] [Entity ID] {Template file name}** @param args*/public static void main(String[] args) throws Exception {String[] commands = new String[] {"entity", "entities", "entitiesall"};String[] datatypes = new String[] {"Entity ID"};String usage = "Usage: VUI ["+ StringUtils.join(commands, "|") +"] ["+ StringUtils.join(datatypes, "|") + "] ";if(args.length < 1) {System.out.println(usage);System.exit(-1);}String inputCommand = args[0];String inputID = null;if(args.length == 2){inputID = args[1];}Long lastGenerationTime = CreationUtils.getLastContentGenerationTime();if(lastGenerationTime==null){lastGenerationTime = new Long(0);}//If non incremental content needs to be generatedif(inputCommand.equals("entitiesall") || inputCommand.equals("entity")) {lastGenerationTime = new Long(0);}NewVUI vui = new NewVUI(lastGenerationTime);long catalogId = 0;if(inputCommand.equals("entity")) {try {catalogId = Long.parseLong(inputID);// vui.generateHtmlForOneEntity(CreationUtils.getEntity(catalogId), "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC);// vui.generateHtmlForOneEntity(CreationUtils.getEntity(catalogId), "shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020);// vui.generateHtmlForOneEntity(CreationUtils.getEntity(catalogId), "localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST);}catch (NumberFormatException nfe) {System.out.println("Invalid ID - " + inputID);System.exit(-1);}}if(inputCommand.equals("entities") || inputCommand.equals("entitiesall")) {vui.generateHtmlForAllEntities("saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC);vui.generateHtmlForAllEntities("shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020);vui.generateHtmlForAllEntities("localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST);}Velocity.init("src/main/java/in/shop2020/ui/util/velocity.properties");CreationUtils.storeLastContentGenerationTime((new Date()).getTime());}/*** Generates content for the specified entity embedding links to the* specified domain name.** The method updates the member variable problems in any of the following* four cases:* <ol>* <li>The entity is not ready.* <li>The category has not been updated yet. (Set to -1).* <li>There are no items in the catalog corresponding to this entity.* <li>There are no active or content-complete items in the catalog* corresponding to this entity.* <li>Neither the items have been updated nor the content has been updated.* </ol>** @param entity* - Entity for which the content has to be generated.* @param domain* - The domain name to be used to serve static content.* @param exportPath* - Local file system path where content has to be generated.* @return -True if content is generated successfully, False otherwise.* @throws Exception*/public boolean generateHtmlForOneEntity(Entity entity, String domain, String exportPath, List<Item> items) throws Exception{String mediaPath = Utils.EXPORT_MEDIA_PATH + entity.getID();deleteOldResources(mediaPath, exportPath + entity.getID());ExpandedEntity expEntity = new ExpandedEntity(entity);long catalogId = expEntity.getID();String templateFile;//Create new directoryFile exportDir = new File(exportPath + catalogId);if(!exportDir.exists()) {exportDir.mkdir();}/** To delete*/// 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" + entity.getID()%3 + "." + domain;templateFile= Utils.VTL_SRC_PATH + "product_summary.vm";getProductSummaryHtml(expEntity, items, templateFile, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_widget.vm";getEntityWidgetSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_homepage.vm";getEntityHomeSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_searchpage.vm";getEntitySearchSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_categorypage.vm";getEntityCategorySnippetHtml(expEntity, items, templateFile, staticurl, exportPath);templateFile= Utils.VTL_SRC_PATH + "slideguide_img_video.vm";getEntitySlideGuideHtml(expEntity, templateFile, domain, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_phones_i_own.vm";getEntityPhonesIOwnSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);if(expEntity.getCategory().getParentCategory().getID() == Utils.MOBILE_PHONES_CATAGOEY){templateFile= Utils.VTL_SRC_PATH + "slideguide_for_comparison.vm";getEntitySnippetForComparison(expEntity, templateFile, domain, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_comparisonpage.vm";getEntityCompareSnippetHtml(expEntity, items, templateFile, staticurl, exportPath);templateFile= Utils.VTL_SRC_PATH + "entity_snippet_for_comparisonpage_summary.vm";getEntityCompareSummarySnippetHtml(expEntity, items, templateFile, staticurl, exportPath);getSlidenamesSnippet(expEntity, exportPath);}getEntityTitleSnippetHtml(expEntity, exportPath);getEntityMetaDescriptionSnippetHtml(expEntity, exportPath);getEntityMetaKeywordSnippetHtml(expEntity, exportPath);generateImages(expEntity.getID(), getImagePrefix(expEntity));return true;}private boolean generateHtmlForAllEntities(String domain, String exportPath) throws Exception{Map<Long, Entity> entities = CreationUtils.getEntities();for(Entity entity: entities.values()){// generateHtmlForOneEntity(entity, domain, exportPath);}System.out.println(problems);return true;}public boolean deleteDir(File dir) {if (dir.isDirectory()) {String[] children = dir.list();for (int i=0; i<children.length; i++) {boolean success = deleteDir(new File(dir, children[i]));if (!success) {return false;}}}// The directory is now empty so delete itreturn dir.delete();}private void deleteOldResources(String mediaPath, String contentPath) {File f = new File(contentPath);if(f.exists()){deleteDir(f);}f = new File(mediaPath);if(f.exists()){deleteDir(f);}}private void generateImages(long catalogId, String imagePrefix) throws IOException {String globalImageDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator;String globalDefaultImagePath = globalImageDirPath + "default.jpg";String imageDirPath = globalImageDirPath + catalogId + File.separator;String defaultImagePath = imageDirPath + "default.jpg";/** Create the directory for this entity if it didn't exist.*/File f = new File(globalImageDirPath + catalogId);if (!f.exists()) {f.mkdir();}/** If the default image is not present for this entity, copy the global* default image.* TODO: This part will be moved to the Jython Script*/File f3 = new File(defaultImagePath);if (!f3.exists()) {copyFile(globalDefaultImagePath, defaultImagePath);}String exportPath = Utils.EXPORT_MEDIA_PATH + catalogId;/** Copying the generated content from db/media to export/media. This* will also insert a timestamp tag in the file names.*/File sourceFile = new File(globalImageDirPath + catalogId);File destinationFile = new File(exportPath);//copyDirectory(sourceFile, destinationFile, imagePrefix);/** Copy the thumbnail and the icon files. This is required so that we can display the* thumbnail on the cart page and icon on the facebook.*/// copyFile(imageDirPath + "thumbnail.jpg", exportPath + File.separator + "thumbnail.jpg");// copyFile(imageDirPath + "icon.jpg", exportPath + File.separator + "icon.jpg");}/*** Copies the contents of the input file into the output file. Creates the* output file if it doesn't exist already.** @param inputFile* File to be copied.* @param outputFile* File to be created.* @throws IOException*/private void copyFile(String inputFile, String outputFile) throws IOException {File sourceFile = new File(inputFile);File destinationFile = new File(outputFile);/*if (!destinationFile.exists())destinationFile.createNewFile();InputStream in = new FileInputStream(sourceFile);OutputStream out = new FileOutputStream(destinationFile);// Copy the bits from instream to outstreambyte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();*/}// If targetLocation does not exist, it will be created.public void copyDirectory(File sourceLocation , File targetLocation, String imagePrefix) throws IOException {if (sourceLocation.isDirectory()) {if (!targetLocation.exists()) {targetLocation.mkdir();}String[] children = sourceLocation.list();for (int i=0; i<children.length; i++) {copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]), imagePrefix);}} else {InputStream in = new FileInputStream(sourceLocation);String fileName = targetLocation.getName().split("\\.")[0];String fileExt = targetLocation.getName().split("\\.")[1];String newFileName = targetLocation.getParent() + File.separator + imagePrefix + "-" + fileName + "-" + this.contentVersion + "." + fileExt;//String fileName = targetLocationOutputStream out = new FileOutputStream(newFileName);// Copy the bits from instream to outstreambyte[] buf = new byte[1024];int len;while ((len = in.read(buf)) > 0) {out.write(buf, 0, len);}in.close();out.close();}}private void getEntityMetaDescriptionSnippetHtml(ExpandedEntity expEntity, String exportPath) {long catalogId = expEntity.getID();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String description = "Best Price " + expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber() + " ";if(expEntity.getCategory().getParentCategory().getID() == 10011) {description += expEntity.getCategory().getLabel() + " in India.";}else {description += ".";}description += " Experience n' buy online. FREE Next Day delivery."+ " Original product - Full manufacturer warranty. Comprehensive reviews.";description = description.replaceAll("--", "-");String exportFileName = exportPath + catalogId + File.separator + "MetaDescriptionSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));writer.write(description);writer.flush();writer.close();} catch (Exception e) {e.printStackTrace();}}private void getEntityMetaKeywordSnippetHtml(ExpandedEntity expEntity, String exportPath) {long catalogId = expEntity.getID();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String keywords = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + ", ";if(expEntity.getCategory().getParentCategory().getID() == 10001) {keywords += expEntity.getBrand() + " mobile phones, ";}if(expEntity.getCategory().getParentCategory().getID() == 10011) {keywords += "phone accessories, ";}keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " Price, ";keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " India, ";if(expEntity.getCategory().getParentCategory().getID() == 10001) {keywords += expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber() + " Review, ";}String exportFileName = exportPath + catalogId + File.separator + "MetaKeywordsSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));writer.write(keywords);writer.flush();writer.close();} catch (Exception e) {e.printStackTrace();}}private void getSlidenamesSnippet(ExpandedEntity expEntity, String exportPath) throws ResourceNotFoundException, ParseErrorException, Exception{long catalogId = expEntity.getID();StringBuilder slideNames = new StringBuilder();slideNames.append(expEntity.getBrand() + " " + expEntity.getModelNumber() + " " + expEntity.getModelName() + "\n");Map<Long, Double> slideScores = CreationUtils.getSlideComparisonScores(catalogId);for(ExpandedSlide expSlide: expEntity.getExpandedSlides()){if(expSlide.getSlideDefinitionID() == 130054){continue;}slideNames.append(expSlide.getSlideDefinition().getLabel() + "!!!");String bucketName = "None";if(Catalog.getInstance().getDefinitionsContainer().getComparisonBucketName(expEntity.getCategoryID(), expSlide.getSlideDefinitionID()) != null){bucketName = Catalog.getInstance().getDefinitionsContainer().getComparisonBucketName(expEntity.getCategoryID(), expSlide.getSlideDefinitionID());}slideNames.append(bucketName + "!!!");double score = 0;if(slideScores.get(expSlide.getSlideDefinitionID())!=null){score = slideScores.get(expSlide.getSlideDefinitionID());}DecimalFormat oneDForm = new DecimalFormat("#.#");score = Double.valueOf(oneDForm.format(score));slideNames.append(score + "\n");}String exportFileName = exportPath + catalogId + File.separator + "SlideNamesSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));writer.write(slideNames.toString());writer.flush();writer.close();}private void getEntitySnippetForComparison(ExpandedEntity expEntity, String templateFile, String domain, String exportPath) throws Exception {long catalogId = expEntity.getID();VelocityContext context = new VelocityContext();context.put("imagePrefix", getImagePrefix(expEntity));context.put("expentity", expEntity);context.put("domain", domain);context.put("contentVersion", this.contentVersion);String exportFileName = null;Template template = Velocity.getTemplate(templateFile);exportFileName = exportPath + catalogId + File.separator + "ComparisonSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));//Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");}private void getEntitySlideGuideHtml(ExpandedEntity expEntity, String templateFile, String domain, String exportPath) throws Exception {long catalogId = expEntity.getID();VelocityContext context = new VelocityContext();context.put("imagePrefix", getImagePrefix(expEntity));context.put("expentity", expEntity);context.put("domain", domain);context.put("contentVersion", this.contentVersion);context.put("defs", Catalog.getInstance().getDefinitionsContainer());String exportFileName = null;Template template = Velocity.getTemplate(templateFile);exportFileName = exportPath + catalogId + File.separator + "SlideGuide.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));//Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");}public void getProductSummaryHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String exportPath) {long catalogId = expEntity.getID();try {List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();for(Item item: items){Map<String, String> itemDetail = new HashMap<String, String>();double sellingPrice = item.getSellingPrice();double mrp = item.getMrp();boolean showmrp = true;if (mrp <= sellingPrice) {showmrp = false;}double saving = Math.round((mrp-sellingPrice)/mrp*100);itemDetail.put("ITEM_ID", ((int)item.getId())+"");itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");itemDetail.put("SP", ((int)item.getSellingPrice())+"");itemDetail.put("MRP", ((int)item.getMrp())+"");itemDetail.put("SAVING", ((int)saving)+"");itemDetail.put("COLOR", item.getColor());itemDetail.put("SHOWMRP", showmrp +"");itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");itemDetails.add(itemDetail);}String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();String categoryName = expEntity.getCategory().getLabel();String tagline = "";String warranty = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120084){PrimitiveDataObject dataObject= (PrimitiveDataObject)feature.getBullets().get(0).getDataObject();tagline = dataObject.getValue();}if(feature.getFeatureDefinitionID() == 120125){ExpandedFeature expFeature = new ExpandedFeature(feature);ExpandedBullet expBullet =expFeature.getExpandedBullets().get(0);if(expBullet != null){warranty = expBullet.getValue() + " "+ expBullet.getUnit().getShortForm();}}}long categoryId = expEntity.getCategory().getID();Map<String,String> params = new HashMap<String, String>();params.put("TITLE", title);params.put("CATEGORY_ID", ((int)categoryId)+"");params.put("CATEGORY_NAME", categoryName);params.put("CATEGORY_URL", categoryName.replaceAll(" ", "-").toLowerCase() + "/" + categoryId);params.put("CATALOG_ID", ((int)catalogId)+"");params.put("TAGLINE", tagline);params.put("WARRANTY", warranty);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "ProductDetail.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private String getEntityURL(ExpandedEntity expEntity){String url = "/" + expEntity.getCategory().getParentCategory().getLabel().toLowerCase().replace(' ', '-') + "/";String productUrl = expEntity.getBrand().toLowerCase().replace(' ', '-')+ "-" + expEntity.getModelName().toLowerCase().replace(' ', '-')+ "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-')+ "-" + expEntity.getID();productUrl = productUrl.replaceAll("/", "-");url = url + productUrl;url = url.replaceAll("--", "-");return url;}private String getImagePrefix(ExpandedEntity expEntity){String imagePrefix = expEntity.getBrand().toLowerCase().replace(' ', '-')+ "-" + expEntity.getModelName().toLowerCase().replace(' ', '-')+ "-" + expEntity.getModelNumber().toLowerCase().replace(' ', '-');imagePrefix = imagePrefix.replaceAll("/", "-");imagePrefix = imagePrefix.replaceAll("--", "-");return imagePrefix;}private void getEntityWidgetSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {long catalogId = expEntity.getID();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName() + " " + expEntity.getModelNumber();String imagePrefix = getImagePrefix(expEntity);String url = getEntityURL(expEntity);String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120089){PrimitiveDataObject dataObject= (PrimitiveDataObject)feature.getBullets().get(0).getDataObject();tinySnippet = dataObject.getValue();}}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>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}Map<String,String> params = new HashMap<String, String>();params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("TINY_SNIPPET", tinySnippet);params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "WidgetSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private void getEntityHomeSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {long catalogId = expEntity.getID();Map<String,String> params = new HashMap<String, String>();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();String url = getEntityURL(expEntity);String imagePrefix = getImagePrefix(expEntity);catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120081){List<Bullet> bullets = feature.getBullets();int count = 1;for(Bullet bullet: bullets){PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();tinySnippet = dataObject.getValue();params.put("SNIPPET_"+count++, tinySnippet);System.out.println("Tiny Snippets is " + dataObject.getValue());}}}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>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "HomeSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private void getEntityCategorySnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {Map<String,String> params = new HashMap<String, String>();long catalogId = expEntity.getID();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();String url = getEntityURL(expEntity);String imagePrefix = getImagePrefix(expEntity);String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120081){List<Bullet> bullets = feature.getBullets();int count = 1;for(Bullet bullet: bullets){PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();tinySnippet = dataObject.getValue();params.put("SNIPPET_"+count++, tinySnippet);System.out.println("Tiny Snippets is " + dataObject.getValue());}}}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>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "CategorySnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private void getEntityTitleSnippetHtml(ExpandedEntity expEntity, String exportPath) {long catalogId = expEntity.getID();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " "+ expEntity.getModelNumber();if(expEntity.getCategory().getParentCategory().getID() == 10001) {title += " | " + expEntity.getBrand() + " Mobile Phones";}if(expEntity.getCategory().getParentCategory().getID() == 10011) {title += " " + expEntity.getCategory().getLabel()+ " | " + expEntity.getBrand() + " Mobile Phone Accessories";}title += " | Saholic.com";String exportFileName = exportPath + catalogId + File.separator + "TitleSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));writer.write(title);writer.flush();writer.close();} catch (Exception e) {e.printStackTrace();}}private void getEntitySearchSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {long catalogId = expEntity.getID();Map<String,String> params = new HashMap<String, String>();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();String url = getEntityURL(expEntity);String imagePrefix = getImagePrefix(expEntity);catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120081){List<Bullet> bullets = feature.getBullets();int count = 1;for(Bullet bullet: bullets){PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();tinySnippet = dataObject.getValue();params.put("SNIPPET_"+count++, tinySnippet);System.out.println("Tiny Snippets is " + dataObject.getValue());}}}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>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "SearchSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private void getEntityCompareSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {long catalogId = expEntity.getID();Map<String,String> params = new HashMap<String, String>();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();String url = getEntityURL(expEntity);String imagePrefix = getImagePrefix(expEntity);catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120081){List<Bullet> bullets = feature.getBullets();int count = 1;for(Bullet bullet: bullets){PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();tinySnippet = dataObject.getValue();params.put("SNIPPET_"+count++, tinySnippet);System.out.println("Tiny Snippets is " + dataObject.getValue());}}}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>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "CompareProductSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private void getEntityCompareSummarySnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {long catalogId = expEntity.getID();Map<String,String> params = new HashMap<String, String>();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();String url = getEntityURL(expEntity);String imagePrefix = getImagePrefix(expEntity);catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120081){List<Bullet> bullets = feature.getBullets();int count = 1;for(Bullet bullet: bullets){PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();tinySnippet = dataObject.getValue();params.put("SNIPPET_"+count++, tinySnippet);System.out.println("Tiny Snippets is " + dataObject.getValue());}}}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>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "CompareProductSummarySnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}private void getEntityPhonesIOwnSnippetHtml(ExpandedEntity expEntity, List<Item> items, String templateFile, String staticurl, String exportPath) {long catalogId = expEntity.getID();Map<String,String> params = new HashMap<String, String>();try {expEntity = CreationUtils.getExpandedEntity(catalogId);String title = expEntity.getBrand() + " " + expEntity.getModelName()+ " " + expEntity.getModelNumber();String url = getEntityURL(expEntity);String imagePrefix = getImagePrefix(expEntity);catalogServiceClient = new CatalogServiceClient();client = catalogServiceClient.getClient();String tinySnippet = "";List<Feature> features = expEntity.getSlide(130054).getFeatures();for(Feature feature: features){if(feature.getFeatureDefinitionID() == 120081){List<Bullet> bullets = feature.getBullets();int count = 1;for(Bullet bullet: bullets){PrimitiveDataObject dataObject = (PrimitiveDataObject)bullet.getDataObject();tinySnippet = dataObject.getValue();params.put("SNIPPET_"+count++, tinySnippet);System.out.println("Tiny Snippets is " + dataObject.getValue());}}}List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();Item minPriceItem = null;Date todate = new Date();for(Item item: items){Map<String, String> itemDetail = new HashMap<String, String>();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());itemDetails.add(itemDetail);}boolean showmrp = true;if (minPriceItem.getMrp() <= minPriceItem.getSellingPrice()) {showmrp = false;}params.put("TITLE", title);params.put("URL", url);params.put("IMAGE_PREFIX", imagePrefix);params.put("SELLING_PRICE", ((int)minPriceItem.getSellingPrice())+"");params.put("MRP", ((int)minPriceItem.getMrp())+"");params.put("SHOWMRP", showmrp +"");params.put("CATALOG_ID", ((int)catalogId)+"");params.put("ITEM_ID", minPriceItem.getId()+"");params.put("staticurl", staticurl);params.put("contentVersion", contentVersion);VelocityContext context = new VelocityContext();context.put("itemDetails", itemDetails);context.put("params", params);String exportFileName = exportPath + catalogId + File.separator + "PhonesIOwnSnippet.html";File exportFile = new File(exportFileName);if(!exportFile.exists()) {exportFile.createNewFile();}BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile)));Template template = Velocity.getTemplate(templateFile);template.merge(context, writer);writer.flush();writer.close();Utils.info("Export Complete!");} catch (Exception e) {e.printStackTrace();}}public String getHtmlFromVelocity(String templateFile, VelocityContext context){try {Velocity.init("velocity/velocity.properties");Template template = Velocity.getTemplate(templateFile);if(template != null) {StringWriter writer = new StringWriter();template.merge(context, writer);writer.flush();writer.close();return writer.toString();}} catch (ResourceNotFoundException e) {e.printStackTrace();} catch (ParseErrorException e) {e.printStackTrace();} catch (MethodInvocationException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return null;}}