Rev 67 | Blame | Last modification | View Log | RSS feed
/****/package in.shop2020.util;import java.util.ArrayList;import java.util.List;import org.apache.commons.lang.StringEscapeUtils;import org.apache.commons.lang.StringUtils;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.Feature;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.definitions.EntityContainer;import in.shop2020.metamodel.definitions.FeatureDefinition;import in.shop2020.metamodel.definitions.SlideDefinition;import in.shop2020.metamodel.util.ExpandedBullet;import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;import in.shop2020.metamodel.util.ExpandedEntity;import in.shop2020.metamodel.util.ExpandedFacetDefinition;import in.shop2020.metamodel.util.ExpandedFeature;import in.shop2020.metamodel.util.ExpandedSlide;/*** Command line utility to convert IR Definitions into IR Data and IR Meta-data** @author naveen**/public class IR {/****/private long categoryID;/*** Level - 4, 8, 12, 16*/private String[] xmlIndentation = {"", " ", " ", " "," "};/*** @param args* @throws Exception*/public static void main(String[] args) throws Exception {String usage = "Usage: IR {Category ID}";if(args.length > 1) {System.out.println(usage);System.exit(-1);}long categoryID = 0L;if(args.length > 0) {try {categoryID = Long.parseLong(args[0]);}catch (NumberFormatException nfe) {System.out.println(usage);System.exit(-1);}}IR ir = new IR(categoryID);ir.exportIRData();}/**** @param categoryID*/public IR(long categoryID) {this.categoryID = categoryID;}/*** @throws Exception**/public void exportIRData() throws Exception {DefinitionsContainer defs =Catalog.getInstance().getDefinitionsContainer();EntityContainer ents =Catalog.getInstance().getEntityContainer();// <IRData>List<String> entityXMLSnippets = new ArrayList<String>();entityXMLSnippets.add("<IRData>");List<Category> categories = null;if(this.categoryID != 0L) {categories = new ArrayList<Category>();categories.add(defs.getCategory(this.categoryID));}else {// Get all categoriescategories = defs.getChildrenCategories(10001);}for(Category cat : categories) {long catID = cat.getID();// Get all facets for the categoryExpandedCategoryFacetDefinition expCategoryFacetDef =defs.getExpandedCategoryFacetDefinition(catID);List<ExpandedFacetDefinition> expFacetDefs =expCategoryFacetDef.getExpandedFacetDefinitions();// Get all entities for the categoryList<Entity> entities = ents.getEntities(catID);if(entities == null) {continue;}// For each entityfor(Entity entity : entities) {ExpandedEntity expEntity =ents.getExpandedEntity(entity.getID());List<String> facetXMLSnippets = new ArrayList<String>();// Collect FACETsList<Long> facetFeatureIDs = new ArrayList<Long>();// For each facet execute Rulefor(ExpandedFacetDefinition expFacetDef : expFacetDefs) {String facetXMLSnip =this.processFacet(expEntity, expFacetDef);if(!facetXMLSnip.isEmpty()) {facetXMLSnippets.add(facetXMLSnip);Utils.info("facetXMLSnip=" + facetXMLSnip);}// Collect features already covered as Facetif(expFacetDef.getFeatureDefinition() != null) {facetFeatureIDs.add(new Long(expFacetDef.getFeatureDefinitionID()));}}String facetXMLSnippetsStr =StringUtils.join(facetXMLSnippets, "\n");Utils.info("facetXMLSnippets=" + facetXMLSnippetsStr);// Collect PROPERTIESString propertiesXMLSnippetsStr =this.getPropertiesXMLSnippet(expEntity, facetFeatureIDs, 2);Utils.info(propertiesXMLSnippetsStr);String entityXMLSnip = this.getEntityXMLSnippet(expEntity,facetXMLSnippetsStr, propertiesXMLSnippetsStr, 1);Utils.info(entityXMLSnip);entityXMLSnippets.add(entityXMLSnip);}}// </IRData>entityXMLSnippets.add("</IRData>");String irDataXML = StringUtils.join(entityXMLSnippets, "\n");Utils.info(irDataXML);// Write it to fileString irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";DBUtils.store(irDataXML, irDataFilename);}/**** @param expEntity* @param expFacetDef* @return* @throws Exception*/@SuppressWarnings("unchecked")private String processFacet(ExpandedEntity expEntity,ExpandedFacetDefinition expFacetDef) throws Exception {EntityContainer ents =Catalog.getInstance().getEntityContainer();IRJythonWrapper jw = new IRJythonWrapper();jw.setExpandedEntity(expEntity);jw.setExpandedFacetDefinition(expFacetDef);// Set FeatureDefinitionFeatureDefinition featureDef =expFacetDef.getFeatureDefinition();if(featureDef != null) {jw.setFeatureDefinition(featureDef);// Set FeatureUtils.info("featureDef.getID()=" + featureDef.getID());Feature feature =ents.getFeature(expEntity.getID(), featureDef.getID());ExpandedFeature expFeature = new ExpandedFeature(feature);jw.setExpandedFeature(expFeature);}// Set SlideDefinitionSlideDefinition slideDef = expFacetDef.getSlideDefinition();if(slideDef != null) {jw.setSlideDefinition(slideDef);// Set SlideUtils.info("slideDef.getID()=" + slideDef.getID());Slide slide = ents.getSlide(expEntity.getID(), slideDef.getID());ExpandedSlide expSlide = new ExpandedSlide(slide);jw.setExpandedSlide(expSlide);}// Execute Python scriptjw.execIRDataRule();// Parse returned Python listList<Object> values = (List<Object>)jw.getValues();Utils.info("values=" + values);String facetXMLSnip = "";if(values != null) {// Get IR Data XML snippet for this entity and facetfacetXMLSnip = this.getFacetXMLSnippet(expFacetDef, values, 2);}Utils.info(facetXMLSnip);return facetXMLSnip;}/**** @param expEntity* @param facetFeatureIDs* @param indent* @return*/private String getPropertiesXMLSnippet(ExpandedEntity expEntity,List<Long> facetFeatureIDs, int indent) {List<String> xmlSnippet = new ArrayList<String>();// Collect all free-form content hereList<String> ffc = new ArrayList<String>();// FeaturesList<ExpandedSlide> expSlides = expEntity.getExpandedSlides();for(ExpandedSlide expSlide : expSlides) {List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();if(expSlide.getFreeformContent() != null) {ffc.add(expSlide.getFreeformContent().getContent());}if(expFeatures == null) {continue;}for(ExpandedFeature expFeature : expFeatures) {Long featureDefID =new Long(expFeature.getFeatureDefinitionID());// FFC at feature levelif(expFeature.getFreeformContent() != null) {ffc.add(expFeature.getFreeformContent().getContent());}// Exclude those who are already covered as facetsif(facetFeatureIDs.contains(featureDefID)) {continue;}List<ExpandedBullet> expBullets =expFeature.getExpandedBullets();if(expBullets == null) {continue;}//<Property Label="">xmlSnippet.add(this.xmlIndentation[indent] +"<Property Label=\"" + StringEscapeUtils.escapeXml(expFeature.getFeatureDefinition().getLabel()) +"\">");//<Value></Value>for(ExpandedBullet bullet : expBullets) {// FFC at bullet levelif(bullet.getFreeformContent() != null) {ffc.add(bullet.getFreeformContent().getContent());}xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +StringEscapeUtils.escapeXml(bullet.getValue()) +"</Value>");}//</Property>xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");}}// FFC as Label="Free-form Content"if(!ffc.isEmpty()) {xmlSnippet.add(this.xmlIndentation[indent] +"<Property Label=\"Free-form Content\">");for(String f : ffc) {if(f != null) {f = StringEscapeUtils.escapeXml(f);xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +f + "</Value>");}}xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");}// Children slides// TODOreturn StringUtils.join(xmlSnippet, "\n");}/**** @param expEntity* @param facetXMLSnippets* @param indent* @return*/private String getEntityXMLSnippet(ExpandedEntity expEntity,String facetXMLSnippets, String propertiesXMLSnippets, int indent) {//<Entity ID="40001">List<String> xmlSnippet = new ArrayList<String>();String entityID = new Long(expEntity.getID()).toString();xmlSnippet.add(this.xmlIndentation[indent] + "<Entity ID=\""+ entityID +"\">");//<Category>Business Phones</Category>String category = expEntity.getCategory().getLabel();xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Category>" +StringEscapeUtils.escapeXml(category) + "</Category>");//<Title>Nokia E71</Title>String title = StringEscapeUtils.escapeXml(expEntity.getBrand()) + " " +StringEscapeUtils.escapeXml(expEntity.getModelName()) +((expEntity.getModelNumber() != null) ?(" " +StringEscapeUtils.escapeXml(expEntity.getModelNumber())): "");xmlSnippet.add(this.xmlIndentation[indent + 1] +"<Title>" + StringEscapeUtils.escapeXml(title) + "</Title>");xmlSnippet.add(facetXMLSnippets);xmlSnippet.add(propertiesXMLSnippets);//</Entity>xmlSnippet.add(this.xmlIndentation[indent] + "</Entity>");return StringUtils.join(xmlSnippet, "\n");}/**** @param expFacetDef* @param values* @param indent* @return String XML Snippet*/private String getFacetXMLSnippet(ExpandedFacetDefinition expFacetDef,List<Object> values, int indent) {// <Facet Label="Form Factor">List<String> xmlSnippet = new ArrayList<String>();String target = expFacetDef.getTarget();xmlSnippet.add(this.xmlIndentation[indent] + "<Facet Label=\"" +target + "\">");//<Value>Candybar</Value>for(Object value : values) {xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +value.toString() + "</Value>");}//</Facet>xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");return StringUtils.join(xmlSnippet, "\n");}}