Rev 5117 | Rev 5566 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.util;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.Category;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.util.CreationUtils;public class CompatibleAccessoriesIndexGenerator {private String[] indentation = {"", " ", " ", " "," "};private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();List<Entity> entities;private Map<Long, Long> categorySlides = new HashMap<Long, Long>();private Map<Long, String> categoryLabels = new HashMap<Long, String>();public CompatibleAccessoriesIndexGenerator(List<Entity> entities) throws Exception {categorySlides.put((long) 10014, (long) 130067);categorySlides.put((long) 10018, (long) 130073);categoryLabels.put((long) 10014, "Battery");categoryLabels.put((long) 10018, "Carrying Case");this.entities = entities;}public void generate() {StringBuilder sb = new StringBuilder();for(Entity entity: entities) {long categoryId = entity.getCategoryID();String url = getProductURL(entity);if (url != null && categorySlides.keySet().contains(categoryId)) {Slide compatibilitySlide = entity.getSlide(categorySlides.get(categoryId));try {String text = compatibilitySlide.getFreeformContent().getFreeformText();if(text.indexOf("<ul>") > -1) continue;for(String name: getHandsetNames(text)) {sb.append(indentation[1] + "<div>\n");sb.append(indentation[2] + "<a href='" + url + "'>" + name + " " + categoryLabels.get(categoryId)+ "</a>\n");sb.append(indentation[1] + "</div>\n");}} catch (NullPointerException e) {// TODO: handle exception}}}String indexFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "compatible-accessories-index.html";try {DBUtils.store(sb.toString(), indexFilename);} catch (Exception e) {e.printStackTrace();}}private String getProductURL(Entity entity) {Category category = defContainer.getCategory(entity.getCategoryID());try {Category parentCategory = category.getParentCategory();String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";String productUrl = ((entity.getBrand() != null) ? entity.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')+ "-" + ((entity.getModelName() != null) ? entity.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-')+ "-" + ((entity.getModelNumber() != null ) ? entity.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')+ "-" + entity.getID();productUrl = productUrl.replaceAll("/", "-");url = url + productUrl;url = url.replaceAll("-+", "-");return url;} catch (NullPointerException e) {return null;}}private List<String> getHandsetNames(String rawText) {List<String> names = new ArrayList<String>();for (String namesWithSameBrand: rawText.trim().split("\n")) {boolean isFirst = true;String brand = "";for(String name: namesWithSameBrand.trim().split(",")) {name = name.trim();if(isFirst) {names.add(name);brand = name.split(" ")[0];isFirst = false;} else {names.add(brand + " " + name);}}}return names;}public static void main(String[] args) {try {List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());CompatibleAccessoriesIndexGenerator generator = new CompatibleAccessoriesIndexGenerator(entities);generator.generate();} catch (Exception e) {e.printStackTrace();}}}