Rev 2561 | Rev 3929 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.util;import in.shop2020.model.v1.catalog.Item;import in.shop2020.utils.CategoryManager;import java.io.File;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.commons.lang.StringUtils;/*** Command line utility to generate xml file for partners** @author rajveer**/public class ProductListGenerator {private String[] xmlIndentation = {"", " ", " ", " "," "};public Map<Long, List<Item>> entityIdItemMap;CategoryManager catm = CategoryManager.getCategoryManager();public ProductListGenerator(Map<Long, List<Item>> entityIdItemMap) throws Exception {this.entityIdItemMap = entityIdItemMap;}/*** Get the url of the entity* @param expEntity* @return url*/private String getProductURL(long entityId, Item item){long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();if(rootCategoryId == Utils.ROOT_CATAGOEY){rootCategoryId = item.getCategory();}String url = "http://www.saholic.com/" + catm.getCategoryLabel(rootCategoryId).toLowerCase().replace(' ', '-') + "/";String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')+ "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-')+ "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')+ "-" + entityId;productUrl = productUrl.replaceAll("/", "-");url = url + productUrl;url = url.replaceAll("-+", "-");return url;}/*** Get the minimum price of the items* @param items* @return*/private double getMinPrice(List<Item> items){double minPrice = Double.MAX_VALUE;for(Item item: items){if(minPrice > item.getSellingPrice()){minPrice = item.getSellingPrice();}}return minPrice;}/*** Check whether product is mobile or not* @param item* @return*/private boolean isMobile(Item item){long parentCategoryId = CategoryManager.getCategoryManager().getCategory(item.getCategory()).getParent_category_id();if(parentCategoryId == Utils.MOBILE_ACCESSORIES_CATEGORY){return false;}return true;}/**** @param item* @return*/private String getProductTitle(Item item){String title = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "")+ ((item.getModelName() != null) ? item.getModelName().trim() + " " : "")+ (( item.getModelNumber() != null ) ? item.getModelNumber().trim() : "" );title = title.replaceAll(" ", " ");return title;}/*** get xml feed for partner sites* @param irDataXMLSnippets* @throws Exception*/private void getProductsXML(ArrayList<String> irDataXMLSnippets) throws Exception{for(Long entityId: entityIdItemMap.keySet()){List<Item> items = entityIdItemMap.get(entityId);Item firstItem = items.get(0);if(!isMobile(firstItem)){continue;}irDataXMLSnippets.add(this.xmlIndentation[1] + "<products>");irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductSKU>" + entityId + "</ProductSKU>");String title = getProductTitle(firstItem);irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductName>" + title + "</ProductName>");String url = getProductURL(entityId, firstItem);String imageUrl = "http://static" + ((entityId+1)%3) + ".saholic.com" + File.separator + "images" + File.separator + entityId + File.separator + "icon.jpg";irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductURL>" + url + "</ProductURL>");irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductPrice>" + getMinPrice(items) + "</ProductPrice>");irDataXMLSnippets.add(this.xmlIndentation[2] + "<ProductImageURL>" + imageUrl + "</ProductImageURL>");irDataXMLSnippets.add(this.xmlIndentation[1] + "</products>");}}/*** Generate the xml list of all the active phones and store in a file* @throws Exception*/public void generateProductsListXML() throws Exception {ArrayList<String> productXMLSnippets = new ArrayList<String>();productXMLSnippets.add("<saholic.com>");getProductsXML(productXMLSnippets);productXMLSnippets.add("</saholic.com>");String productDataXML = StringUtils.join(productXMLSnippets, "\n");Utils.info(productDataXML);// Write it to fileString productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "saholicmobilephones.xml";DBUtils.store(productDataXML, productXMLFilename);}/*** get xml feed for partner sites* @param irDataXMLSnippets* @throws Exception*/private void getProductsJavascript(StringBuffer sb) throws Exception{boolean isFirst = true;for(Long entityId: entityIdItemMap.keySet()){List<Item> items = entityIdItemMap.get(entityId);Item firstItem = items.get(0);if(!isMobile(firstItem)){continue;}String title = getProductTitle(firstItem);if(isFirst){sb.append("\"" + title + "\":" + entityId);isFirst = false;}else{sb.append(",\"" + title + "\":" + entityId);}}}/*** Generate the javascript list of all the active phones and store in a file* @throws Exception*/public void generateProductListJavascript() throws Exception {StringBuffer productsJavascript = new StringBuffer();productsJavascript.append("var productIdNames={");getProductsJavascript(productsJavascript);productsJavascript.append("};");// Write it to fileString productXMLFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "saholicmobilephones.js";DBUtils.store(productsJavascript.toString(), productXMLFilename);}}