Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
62 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
102 naveen 6
import in.shop2020.metamodel.core.Bullet;
63 naveen 7
import in.shop2020.metamodel.core.Entity;
1363 rajveer 8
import in.shop2020.metamodel.core.EntityState;
9
import in.shop2020.metamodel.core.EntityStatus;
63 naveen 10
import in.shop2020.metamodel.core.Feature;
102 naveen 11
import in.shop2020.metamodel.core.PrimitiveDataObject;
63 naveen 12
import in.shop2020.metamodel.core.Slide;
102 naveen 13
import in.shop2020.metamodel.definitions.BulletDefinition;
63 naveen 14
import in.shop2020.metamodel.definitions.Catalog;
64 naveen 15
import in.shop2020.metamodel.definitions.Category;
83 naveen 16
import in.shop2020.metamodel.definitions.DatatypeDefinition;
63 naveen 17
import in.shop2020.metamodel.definitions.DefinitionsContainer;
82 naveen 18
import in.shop2020.metamodel.definitions.FacetDefinition;
1314 rajveer 19
import in.shop2020.metamodel.definitions.FacetSlideDefinition;
63 naveen 20
import in.shop2020.metamodel.definitions.FeatureDefinition;
1050 rajveer 21
import in.shop2020.metamodel.util.CreationUtils;
67 naveen 22
import in.shop2020.metamodel.util.ExpandedBullet;
83 naveen 23
import in.shop2020.metamodel.util.ExpandedBulletDefinition;
64 naveen 24
import in.shop2020.metamodel.util.ExpandedEntity;
82 naveen 25
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
1314 rajveer 26
import in.shop2020.metamodel.util.ExpandedFacetSlideDefinition;
65 naveen 27
import in.shop2020.metamodel.util.ExpandedFeature;
83 naveen 28
import in.shop2020.metamodel.util.ExpandedFeatureDefinition;
65 naveen 29
import in.shop2020.metamodel.util.ExpandedSlide;
451 rajveer 30
import in.shop2020.model.v1.catalog.Item;
897 rajveer 31
import in.shop2020.model.v1.catalog.status;
451 rajveer 32
import in.shop2020.thrift.clients.CatalogServiceClient;
63 naveen 33
 
341 rajveer 34
import java.io.File;
35
import java.io.FileOutputStream;
81 naveen 36
import java.util.ArrayList;
897 rajveer 37
import java.util.Date;
91 naveen 38
import java.util.HashMap;
81 naveen 39
import java.util.List;
82 naveen 40
import java.util.Map;
81 naveen 41
 
341 rajveer 42
import javax.xml.transform.TransformerFactory;
43
import javax.xml.transform.Transformer;
44
import javax.xml.transform.stream.StreamResult;
45
import javax.xml.transform.stream.StreamSource;
46
 
81 naveen 47
import org.apache.commons.lang.StringEscapeUtils;
48
import org.apache.commons.lang.StringUtils;
49
 
341 rajveer 50
 
62 naveen 51
/**
52
 * Command line utility to convert IR Definitions into IR Data and IR Meta-data
53
 * 
1314 rajveer 54
 * @author rajveer
62 naveen 55
 *
56
 */
57
public class IR {
63 naveen 58
 
62 naveen 59
	/**
64 naveen 60
	 * Level - 4, 8, 12, 16
61
	 */
1314 rajveer 62
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
83 naveen 63
 
1314 rajveer 64
	private String[] xmlTabIndentation = {"", "\t", "\t\t", "\t\t\t", "\t\t\t\t", "\t\t\t\t\t", "\t\t\t\t\t\t"};
91 naveen 65
 
1314 rajveer 66
	private Map<Long, List<String>> facetIDFacetValues = new HashMap<Long, List<String>>();
733 rajveer 67
 
68
	CatalogServiceClient catalogServiceClient = null;
69
	in.shop2020.model.v1.catalog.InventoryService.Client client = null;
64 naveen 70
 
71
	/**
62 naveen 72
	 * @param args
63 naveen 73
	 * @throws Exception 
62 naveen 74
	 */
63 naveen 75
	public static void main(String[] args) throws Exception {
62 naveen 76
 
1314 rajveer 77
		IR ir = new IR();
78
		ir.exportIRData();
79
		ir.transformIrDataXMLtoSolrXML();
80
		ir.exportIRMetaData();
81
		ir.transformIrMetaDataXMLSolrSchemaXML();
81 naveen 82
 
62 naveen 83
	}
63 naveen 84
 
85
	/**
86
	 * 
87
	 * @param categoryID
1314 rajveer 88
	 * @return 
733 rajveer 89
	 * @throws Exception 
63 naveen 90
	 */
1314 rajveer 91
	public IR() throws Exception {
733 rajveer 92
		catalogServiceClient = new CatalogServiceClient();
93
		client = catalogServiceClient.getClient();
63 naveen 94
	}
95
 
341 rajveer 96
 
63 naveen 97
	/**
81 naveen 98
	 * 
341 rajveer 99
	 * @param inXMLFilename
100
	 * @param xslFilename
101
	 * @param outXMLFilename
102
	 * @throws Exception 
103
	 */
104
 
105
	public void xsltTransformation(String inXMLFilename, String xslFilename, String outXMLFilename) throws Exception{
106
		// Use the static TransformerFactory.newInstance() method to instantiate 
107
		  // a TransformerFactory. The javax.xml.transform.TransformerFactory 
108
		  // system property setting determines the actual class to instantiate --
109
		  // org.apache.xalan.transformer.TransformerImpl.
110
		TransformerFactory tFactory = TransformerFactory.newInstance();
111
 
112
			// Use the TransformerFactory to instantiate a Transformer that will work with  
113
			// the stylesheet you specify. This method call also processes the stylesheet
114
		  // into a compiled Templates object.
115
		Transformer transformer = tFactory.newTransformer(new StreamSource(xslFilename));
116
 
117
			// Use the Transformer to apply the associated Templates object to an XML document
118
			// (foo.xml) and write the output to a file (foo.out).
119
		transformer.transform(new StreamSource(inXMLFilename), new StreamResult(new FileOutputStream(outXMLFilename)));
120
 
121
	}
122
 
123
	/**
124
	 * 
81 naveen 125
	 * @throws Exception
126
	 */
341 rajveer 127
	public void transformIrDataXMLtoSolrXML() throws Exception {
128
		String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
129
		String irSolrDataFilename = Utils.EXPORT_SOLR_PATH + "irdata_solr.xml";
130
		String irXslFilename = "src/xsl/irdata_solrdata.xsl";
131
		System.out.println(irSolrDataFilename);
132
		File solrFile = new File(irSolrDataFilename);
133
		if(!solrFile.exists()){
134
			solrFile.createNewFile();
135
		}
136
		xsltTransformation(irDataFilename, irXslFilename, irSolrDataFilename);
137
	}
138
 
139
 
140
	/**
141
	 * 
142
	 * @throws Exception
143
	 */
144
	public void transformIrMetaDataXMLSolrSchemaXML() throws Exception {
145
		String irDataFilename = Utils.EXPORT_IR_PATH + "irmetadata.xml";
146
		String irSolrDataFilename = Utils.EXPORT_SOLR_PATH + "irmetadata_solrschema.xml";
147
		String irXslFilename = "src/xsl/irmetadata_solrschema.xsl";
148
		System.out.println(irSolrDataFilename);
149
		File solrFile = new File(irSolrDataFilename);
150
		if(!solrFile.exists()){
151
			solrFile.createNewFile();
152
		}
153
		xsltTransformation(irDataFilename, irXslFilename, irSolrDataFilename);
154
	}
155
 
156
 
157
 
158
	/**
159
	 * 
160
	 * @throws Exception
161
	 */
81 naveen 162
	public void exportIRMetaData() throws Exception {
163
		DefinitionsContainer defs = 
164
			Catalog.getInstance().getDefinitionsContainer();
82 naveen 165
 
81 naveen 166
		// <IRMetaData>
82 naveen 167
		List<String> xmlSnippets = new ArrayList<String>();
168
		xmlSnippets.add("<IRMetaData>");
83 naveen 169
		xmlSnippets.add("\t<Facets>");
81 naveen 170
 
82 naveen 171
		IRMetaDataJythonWrapper jy = new IRMetaDataJythonWrapper();
81 naveen 172
 
173
		// Iterate over all facet definitions
82 naveen 174
		Map<Long, FacetDefinition> facetDefs = defs.getFacetDefinitions();
175
		for(FacetDefinition facetDef : facetDefs.values()) {
176
 
177
			jy.reset();
178
			jy.initialize();
179
 
180
			ExpandedFacetDefinition expFacetDef = 
181
				new ExpandedFacetDefinition(facetDef);
182
 
183
			jy.setExpandedFacetDefinition(expFacetDef);
184
 
1314 rajveer 185
			jy.setPossibleValues(facetIDFacetValues.get(facetDef.getID()));
186
 
82 naveen 187
			jy.executeRule();
188
 
189
			String facetXMLSnip = jy.getXMLSnippet();
190
			Utils.info("facetXMLSnip=" + facetXMLSnip);
191
 
192
			xmlSnippets.add(facetXMLSnip);
83 naveen 193
 
82 naveen 194
		}
83 naveen 195
		xmlSnippets.add("\t</Facets>");
81 naveen 196
 
1314 rajveer 197
 
83 naveen 198
 
199
		xmlSnippets.add("\n\t<Properties>");
200
 
81 naveen 201
		// Iterate over all feature definitions
83 naveen 202
		Map<Long, FeatureDefinition> featureDefs = defs.getFeatureDefinitions();
203
		for(FeatureDefinition featureDef : featureDefs.values()) {
204
			String propertyXMLSnip = this.getPropertyXMLSnippet(featureDef);
205
			Utils.info("propertyXMLSnip=" + propertyXMLSnip);
206
 
207
			xmlSnippets.add(propertyXMLSnip);
208
		}
81 naveen 209
 
83 naveen 210
		xmlSnippets.add("\t</Properties>");
211
 
212
		xmlSnippets.add("\n\t<Categories>");
213
 
214
		// Iterate over all categories
580 rajveer 215
		/*
1050 rajveer 216
		Category rootCategory = defs.getCategory(Catalog.getInstance().getRootCategory().getID());
83 naveen 217
		List<Category> children = rootCategory.getChildrenCategory();
218
		for (Category child : children) {
516 rajveer 219
 
83 naveen 220
			String categoryXMLSnip = this.getCategoryXMLSnippet(child, 2);
221
			Utils.info("categoryXMLSnip=" + categoryXMLSnip);
222
 
223
			xmlSnippets.add(categoryXMLSnip);
224
		}
580 rajveer 225
		*/
226
 
1050 rajveer 227
		Category rootCategory = defs.getCategory(Catalog.getInstance().getRootCategory().getID());
580 rajveer 228
		String categoryXMLSnip = this.getCategoryXMLSnippet(rootCategory, 2);
229
		Utils.info("categoryXMLSnip=" + categoryXMLSnip);
230
 
231
		xmlSnippets.add(categoryXMLSnip);
232
 
233
 
83 naveen 234
		xmlSnippets.add("\t</Categories>");
235
 
81 naveen 236
		// </IRMetaData>
82 naveen 237
		xmlSnippets.add("</IRMetaData>");
81 naveen 238
 
82 naveen 239
		String irMetaDataXML = StringUtils.join(xmlSnippets, "\n");
81 naveen 240
		Utils.info(irMetaDataXML);
241
 
242
		// Write it to file
243
		String irMetaDataFilename = Utils.EXPORT_IR_PATH + "irmetadata.xml";
244
		DBUtils.store(irMetaDataXML, irMetaDataFilename);
245
	}
246
 
247
	/**
83 naveen 248
	 * 
249
	 * @param category
250
	 * @return
63 naveen 251
	 * @throws Exception 
83 naveen 252
	 */
253
	private String getCategoryXMLSnippet(Category category, int indent) 
254
		throws Exception {
255
 
256
		DefinitionsContainer defs = 
257
			Catalog.getInstance().getDefinitionsContainer();
258
 
259
		String xmlSnip = this.xmlTabIndentation[indent] + "<Category";
260
 
261
		xmlSnip += " ID=\"" + category.getID() + "\"";
262
		xmlSnip += " label=\"" + category.getLabel() + "\"";
263
		xmlSnip += ">\n";
264
 
265
		List<Category> children = category.getChildrenCategory();
266
 
267
		if(children != null) {
268
			for(Category child : children) {
269
				xmlSnip += this.getCategoryXMLSnippet(child, indent+1);
270
			}
271
		}
272
		else {
273
			// Only leaf category will have entities
274
			// Facet IDs
275
			xmlSnip += this.xmlTabIndentation[indent+1] + "<Facets>\n";
276
 
277
			List<Long> facetDefIDs = 
278
				defs.getFacetDefinitionIDs(category.getID());
1229 rajveer 279
			//Utils.info("facetDefIDs=" + facetDefIDs);
83 naveen 280
 
516 rajveer 281
 
282
 
283
			if( facetDefIDs != null && !facetDefIDs.isEmpty() ){
284
					for(Long facetDefID : facetDefIDs) {
285
					xmlSnip += this.xmlTabIndentation[indent+2] + 
286
						"<FacetID>" + facetDefID + "</FacetID>\n";
287
				}
83 naveen 288
			}
289
 
516 rajveer 290
 
83 naveen 291
			xmlSnip += this.xmlTabIndentation[indent+1] + "</Facets>\n\n";
292
 
293
			// Feature IDs
294
			xmlSnip += this.xmlTabIndentation[indent+1] + "<Properties>\n";
295
 
296
			List<Long> featureDefIDs = 
297
				defs.getFeatureDefinitionIDs(category.getID());
1229 rajveer 298
			//Utils.info("featureDefIDs=" + featureDefIDs);
83 naveen 299
 
300
			for(Long featureDefID : featureDefIDs) {
301
				xmlSnip += this.xmlTabIndentation[indent+2] + 
302
					"<FeatureID>" + featureDefID + "</FeatureID>\n";
303
			}
304
 
305
			xmlSnip += this.xmlTabIndentation[indent+1] + "</Properties>\n";
306
		}
307
 
308
		xmlSnip += this.xmlTabIndentation[indent] + "</Category>\n";
309
 
310
		return xmlSnip;
311
	}
312
 
313
	/**
63 naveen 314
	 * 
83 naveen 315
	 * @param featureDef
316
	 * @return
317
	 * @throws Exception 
63 naveen 318
	 */
83 naveen 319
	private String getPropertyXMLSnippet(FeatureDefinition featureDef) 
320
		throws Exception {
321
		String xmlSnip = "\t\t<Property";
322
 
323
		xmlSnip += " ID=\"" + featureDef.getID() + "\"";
324
		xmlSnip += " label=\"" + featureDef.getLabel() + "\"";
325
 
326
		ExpandedFeatureDefinition expFeatureDef = 
327
			new ExpandedFeatureDefinition(featureDef);
328
 
329
		ExpandedBulletDefinition expBulletDef = 
330
			expFeatureDef.getExpandedBulletDefinition();
331
 
332
		String datatype = "string";
333
		if(expBulletDef.isComposite() || expBulletDef.isEnumerated()) {
334
			datatype = "string";
335
		}
336
		else {
337
			DatatypeDefinition datatypeDef = 
338
				expBulletDef.getDatatypeDefinition();
339
 
340
			datatype = datatypeDef.getName();
341
 
342
			// REVISIT
343
			if(datatype.equals("hours_mins") || datatype.equals("days_hours") ||
344
					datatype.equals("hours_mins") || 
345
					datatype.equals("days_hours")) {
346
				datatype = "string";
347
			}
348
		}
349
		xmlSnip += " datatype=\"" + datatype + "\"";
350
 
351
		String multivalue = "false";
352
		if (expBulletDef.isMultivalue()) {
353
			multivalue = "true";
354
		}
355
		xmlSnip += " isMultivalue=\"" + multivalue + "\"";
356
 
357
		xmlSnip += "/>";
358
		return xmlSnip;
359
	}
1314 rajveer 360
 
83 naveen 361
 
1314 rajveer 362
 
363
	private String processFacet(FacetDefinition facet, ExpandedSlide expSlide, ExpandedFacetSlideDefinition expFacetSlideDef, Long facetDefID) throws Exception {
364
 
365
		IRDataJythonWrapper jw = new IRDataJythonWrapper();
63 naveen 366
 
1314 rajveer 367
		jw.setExpandedSlide(expSlide);
368
		jw.setExpandedFacetSlideDefinition(expFacetSlideDef);
733 rajveer 369
 
1314 rajveer 370
		// Execute Python script
371
		jw.executeRule();
63 naveen 372
 
1314 rajveer 373
		//FIXME
374
		// Normalize
375
//		if(defs.needsNormalization(feature.getFeatureDefinitionID())) {
376
//			Utils.info("needsNormalization feature=" + feature.getFeatureDefinitionID());
377
//			
378
//			feature = this.normalize(feature);
379
//		}
63 naveen 380
 
516 rajveer 381
 
64 naveen 382
 
1314 rajveer 383
		List<Object> values = (List<Object>)jw.getValues();
384
		Utils.info("values=" + values);
64 naveen 385
 
1314 rajveer 386
		// Append to facet values
387
 
388
		Utils.info("facetDefID=" + facetDefID);
91 naveen 389
 
1314 rajveer 390
		List<String> facetValues = this.facetIDFacetValues.get(
391
				new Long(facetDefID));
91 naveen 392
 
1314 rajveer 393
		if(facetValues == null) {
394
			facetValues = new ArrayList<String>();
395
			this.facetIDFacetValues.put(new Long(facetDefID), facetValues);
396
		}
88 naveen 397
 
1314 rajveer 398
		if(values != null) {
399
			for(Object value : values) {
400
				String strValue = value.toString();
88 naveen 401
 
1314 rajveer 402
				if(!facetValues.contains(strValue)) {
403
					facetValues.add(strValue);
88 naveen 404
				}
405
			}
406
		}
407
 
1314 rajveer 408
		ExpandedFacetDefinition expFacet = new ExpandedFacetDefinition(facet);
409
		// Parse returned Python list
410
		String facetXMLSnip = null;
411
		if(values != null) {
412
			facetXMLSnip = this.getFacetXMLSnippet(expFacet, values, 2);
88 naveen 413
		}
414
 
1314 rajveer 415
		Utils.info("0 facetXMLSnip=" + facetXMLSnip);
416
		return facetXMLSnip;
88 naveen 417
	}
1314 rajveer 418
 
88 naveen 419
 
1314 rajveer 420
 
88 naveen 421
	/**
422
	 * 
1314 rajveer 423
	 * @param expFacetDef
424
	 * @param values
425
	 * @param indent
426
	 * @return String XML Snippet
88 naveen 427
	 */
1314 rajveer 428
	private String getFacetXMLSnippet(ExpandedFacetDefinition expFacetDef,	List<Object> values, int indent) {
88 naveen 429
 
1314 rajveer 430
		// <Facet Label="Form Factor">
431
		List<String> xmlSnippet = new ArrayList<String>();
432
		String target = expFacetDef.getTarget();
88 naveen 433
 
1314 rajveer 434
		xmlSnippet.add(this.xmlIndentation[indent] + "<Facet ID=\"" + expFacetDef.getID() + "\" Label=\"" + target + "\">");
88 naveen 435
 
1314 rajveer 436
		//<Value>Candybar</Value>
437
		for(Object value : values) {
438
			xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + value.toString() + "</Value>");
88 naveen 439
		}
440
 
1314 rajveer 441
		//</Facet>
442
		xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");
88 naveen 443
 
1314 rajveer 444
		return StringUtils.join(xmlSnippet, "\n");
88 naveen 445
	}
1314 rajveer 446
 
88 naveen 447
	/**
1314 rajveer 448
	 * @throws Exception 
88 naveen 449
	 * 
450
	 */
1314 rajveer 451
	public void exportIRData() throws Exception {
452
		DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
453
		Map<Long, List<FacetDefinition>> slideFacets = defs.getSlideFacetDefinitions();
88 naveen 454
 
1314 rajveer 455
		// <IRData>
456
		List<String> irDataXMLSnippets = new ArrayList<String>();
457
		irDataXMLSnippets.add("<IRData>");
458
 
459
		Map<Long, Entity> entities =  CreationUtils.getEntities();
460
		for(Entity entity: entities.values()){
1363 rajveer 461
 
462
			EntityState state = CreationUtils.getEntityState(entity.getID());
463
			if(state.getStatus() != EntityStatus.READY){
464
				continue;
465
			}
466
 
467
 
1314 rajveer 468
			double itemPrice = 0;
469
			long entityID = entity.getID();
1363 rajveer 470
 
1314 rajveer 471
			List<Item> items = client.getItemsByCatalogId(entity.getID());
472
			if(items == null){
473
				continue;
88 naveen 474
			}
475
 
1314 rajveer 476
			Date todate = new Date();
477
			Boolean hasActiveItem = false;
478
			for(Item item: items){
1412 rajveer 479
				if(item.getItemStatus() == status.ACTIVE && todate.getTime() > item.getStartDate()){
480
						hasActiveItem = true;
88 naveen 481
				}
1412 rajveer 482
 
1314 rajveer 483
				if(itemPrice == 0){
484
					itemPrice = item.getSellingPrice();
485
				}else if(itemPrice>item.getSellingPrice()){
486
					itemPrice = item.getSellingPrice();
88 naveen 487
				}
488
			}
1314 rajveer 489
			if(!hasActiveItem){
897 rajveer 490
				continue;
491
			}
1314 rajveer 492
			ExpandedEntity expEntity = new ExpandedEntity(entity);
493
			long categoryID = expEntity.getCategoryID();
494
			if(categoryID == -1){
495
				continue;
638 rajveer 496
			}
516 rajveer 497
 
1314 rajveer 498
 
64 naveen 499
 
1314 rajveer 500
			List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
501
			if(expSlides==null){continue;}
64 naveen 502
 
1314 rajveer 503
			List<String> entityXMLSnippets = new ArrayList<String>();
504
			entityXMLSnippets.add(this.xmlIndentation[1] + "<Entity ID=\""+ entityID +"\">");
64 naveen 505
 
1314 rajveer 506
			String parentCategory = expEntity.getCategory().getLabel();
507
			entityXMLSnippets.add(this.xmlIndentation[2] + "<Category>" + StringEscapeUtils.escapeXml(parentCategory) + "</Category>");
90 naveen 508
 
1314 rajveer 509
 
510
			String title = StringEscapeUtils.escapeXml(expEntity.getBrand()) + " " + StringEscapeUtils.escapeXml(expEntity.getModelName()) + 
511
				((expEntity.getModelNumber() != null) ? (" " +	StringEscapeUtils.escapeXml(expEntity.getModelNumber())): "");
346 rajveer 512
 
1314 rajveer 513
			entityXMLSnippets.add(this.xmlIndentation[2] +	"<Title>" + StringEscapeUtils.escapeXml(title) + "</Title>");
64 naveen 514
 
1914 rajveer 515
			//Boost titles for the mobile phones
516
			if(expEntity.getCategory().getParentCategory().getID() == 10001){
517
			    entityXMLSnippets.add(this.xmlIndentation[2] +   "<Boost>" + 5 + "</Boost>");
518
			}else{
519
			    entityXMLSnippets.add(this.xmlIndentation[2] +   "<Boost>" + 0 + "</Boost>");
520
			}
1314 rajveer 521
 
64 naveen 522
 
1314 rajveer 523
			String subCategory = defs.getCategory(categoryID).getLabel();
524
			String  mainCategory = defs.getCategory(categoryID).getParentCategory().getLabel();
88 naveen 525
 
1314 rajveer 526
			String brand = expEntity.getBrand();
88 naveen 527
 
64 naveen 528
 
88 naveen 529
 
1314 rajveer 530
			//Create zero slide
531
			Slide zeroSlide = createZeroSlide(brand, itemPrice, mainCategory, subCategory);
532
			ExpandedSlide expandedZeroSlide = new ExpandedSlide(zeroSlide);
533
			expSlides.add(expandedZeroSlide);
534
 
535
			for(ExpandedSlide expSlide: expSlides){
536
				System.out.println(expSlide.getSlideDefinitionID());
537
				if(slideFacets.containsKey(expSlide.getSlideDefinitionID())){
538
					List<FacetDefinition> facets = slideFacets.get(expSlide.getSlideDefinitionID());
539
					for(FacetDefinition facet: facets){
540
						for(FacetSlideDefinition facetSlides: facet.getFacetSlideDefinitions()){
541
							if(facetSlides.getSlideDefinitionID() != expSlide.getSlideDefinitionID()){continue;}
542
							ExpandedFacetSlideDefinition expFacetSlideDef = new ExpandedFacetSlideDefinition(facetSlides) ;
543
							for(ExpandedFeature expFeature: expSlide.getExpandedFeatures()){
544
								if(defs.needsNormalization(expFeature.getFeatureDefinitionID())) {
545
									Utils.info("needsNormalization feature=" + expFeature.getFeatureDefinitionID());
546
									//expFeature = this.normalize(expFeature);
547
								}
548
							}
549
							entityXMLSnippets.add(processFacet(facet, expSlide, expFacetSlideDef, facet.getID()));
550
						}
551
					}
552
				}
346 rajveer 553
			}
1314 rajveer 554
 
555
			// Collect PROPERTIES
556
			String propertiesXMLSnippetsStr = this.getPropertiesXMLSnippet(expEntity, new ArrayList<Long>(), 2);
557
			entityXMLSnippets.add(propertiesXMLSnippetsStr);
558
			entityXMLSnippets.add(this.xmlIndentation[1] + "</Entity>");
559
			System.out.println(entityXMLSnippets);
560
			irDataXMLSnippets.addAll(entityXMLSnippets);
561
 
346 rajveer 562
		}
563
 
1314 rajveer 564
		irDataXMLSnippets.add("</IRData>");
346 rajveer 565
 
1314 rajveer 566
		String irDataXML = StringUtils.join(irDataXMLSnippets, "\n");
567
		Utils.info(irDataXML);
64 naveen 568
 
1314 rajveer 569
		// Write it to file
570
		String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
571
		DBUtils.store(irDataXML, irDataFilename);
572
	}
573
 
574
	private Slide createZeroSlide(String brand, double price, String mainCategory, String subCategory) {
575
		Slide zeroSlide = new Slide(130000);
576
		List<Feature> zeroSlideFeatures = new ArrayList<Feature>();
91 naveen 577
 
1314 rajveer 578
		Feature brandFeature = new Feature(120080);
579
		Bullet brandBullet = new Bullet(new PrimitiveDataObject(brand));
580
		List<Bullet> brandBullets = new ArrayList<Bullet>();
581
		brandBullets.add(brandBullet);
582
		brandFeature.setBullets(brandBullets);
583
		zeroSlideFeatures.add(brandFeature);
91 naveen 584
 
1314 rajveer 585
		Feature priceFeature = new Feature(120128);
586
		Bullet priceBullet = new Bullet(new PrimitiveDataObject((new Double(price)).toString()));
587
		List<Bullet> priceBullets = new ArrayList<Bullet>();
588
		priceBullets.add(priceBullet);
589
		priceFeature.setBullets(priceBullets);
590
		zeroSlideFeatures.add(priceFeature);
91 naveen 591
 
1314 rajveer 592
		Feature mainCategoryFeature = new Feature(120123);
593
		Bullet mainCategoryBullet = new Bullet(new PrimitiveDataObject(mainCategory));
594
		List<Bullet> mainCategoryBullets = new ArrayList<Bullet>();
595
		mainCategoryBullets.add(mainCategoryBullet);
596
		mainCategoryFeature.setBullets(mainCategoryBullets);
597
		zeroSlideFeatures.add(mainCategoryFeature);
64 naveen 598
 
1314 rajveer 599
		Feature subCategoryFeature = new Feature(120124);
600
		Bullet subCategoryBullet = new Bullet(new PrimitiveDataObject(subCategory));
601
		List<Bullet> subCategoryBullets = new ArrayList<Bullet>();
602
		subCategoryBullets.add(subCategoryBullet);
603
		subCategoryFeature.setBullets(subCategoryBullets);
604
		zeroSlideFeatures.add(subCategoryFeature);
605
 
606
		zeroSlide.setFeatures(zeroSlideFeatures);
607
		return zeroSlide;
64 naveen 608
	}
1314 rajveer 609
 
102 naveen 610
 
611
	/**
612
	 * 
613
	 * @param feature
614
	 * @return Feature
615
	 * @throws Exception 
616
	 */
1314 rajveer 617
	private ExpandedFeature normalize(ExpandedFeature expFeature) throws Exception {
102 naveen 618
		BulletDefinition bulletDef = 
619
			expFeature.getFeatureDefinition().getBulletDefinition();
620
 
621
		ExpandedBulletDefinition expBulletDef = new ExpandedBulletDefinition(
622
				bulletDef);
623
 
624
		if(expBulletDef.isPrimitive()) {
625
			PrimitiveNormalizationJythonWrapper jy = 
626
				new PrimitiveNormalizationJythonWrapper();
627
 
628
			jy.setExpandedFeature(expFeature);
629
 
630
			jy.excuteRule();
631
 
632
			String newValue = jy.getNewValue();
633
			long newUnitID = jy.getNewUnitID();
634
 
635
			List<Bullet> newBullets = new ArrayList<Bullet>();
636
			Bullet newBullet = new Bullet(new PrimitiveDataObject(newValue));
637
			newBullet.setUnitID(newUnitID);
638
 
639
			newBullets.add(newBullet);
640
 
1314 rajveer 641
			expFeature.setBullets(newBullets);
102 naveen 642
		}
643
		else {
644
			Utils.severe("Normalization not defined for non-primitives");
645
		}
646
 
1314 rajveer 647
		return expFeature;
102 naveen 648
	}
649
 
67 naveen 650
	/**
651
	 * 
652
	 * @param expEntity
653
	 * @param facetFeatureIDs
654
	 * @param indent
655
	 * @return
656
	 */
657
	private String getPropertiesXMLSnippet(ExpandedEntity expEntity, 
658
			List<Long> facetFeatureIDs, int indent) {
659
 
660
		List<String> xmlSnippet = new ArrayList<String>();
661
 
662
		// Collect all free-form content here
663
		List<String> ffc = new ArrayList<String>();
664
 
665
		// Features
666
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
667
 
668
		for(ExpandedSlide expSlide : expSlides) {
1314 rajveer 669
			if(expSlide.getSlideDefinitionID() == 13000){
670
				continue;
671
			}
67 naveen 672
			List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
673
 
674
			if(expSlide.getFreeformContent() != null) {
199 naveen 675
				ffc.add(expSlide.getFreeformContent().getFreeformText());
67 naveen 676
			}
677
 
678
			if(expFeatures == null) {
679
				continue;
680
			}
681
 
682
			for(ExpandedFeature expFeature : expFeatures) {
683
				Long featureDefID = 
684
					new Long(expFeature.getFeatureDefinitionID());
685
 
686
				// FFC at feature level
687
				if(expFeature.getFreeformContent() != null) {
199 naveen 688
					ffc.add(expFeature.getFreeformContent().getFreeformText());
67 naveen 689
				}
690
 
691
				// Exclude those who are already covered as facets
692
				if(facetFeatureIDs.contains(featureDefID)) {
693
					continue;
694
				}
695
 
696
				List<ExpandedBullet> expBullets = 
697
					expFeature.getExpandedBullets();
698
 
699
				if(expBullets == null) {
700
					continue;
701
				}
702
 
703
				//<Property Label="">
704
				xmlSnippet.add(this.xmlIndentation[indent] + 
83 naveen 705
						"<Property ID=\"" + expFeature.getFeatureDefinitionID() 
706
						+ "\" Label=\"" + StringEscapeUtils.escapeXml(
67 naveen 707
								expFeature.getFeatureDefinition().getLabel()) + 
708
						"\">");
709
 
710
				//<Value></Value>
711
				for(ExpandedBullet bullet : expBullets) {
712
 
713
					// FFC at bullet level
714
					if(bullet.getFreeformContent() != null) {
199 naveen 715
						ffc.add(bullet.getFreeformContent().getFreeformText());
67 naveen 716
					}
717
 
718
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
719
							StringEscapeUtils.escapeXml(bullet.getValue()) + 
720
							"</Value>");
721
				}
722
 
723
				//</Property>
724
				xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
725
			}
726
		}
727
 
728
		// FFC as Label="Free-form Content"
729
		if(!ffc.isEmpty()) {
730
			xmlSnippet.add(this.xmlIndentation[indent] + 
108 naveen 731
					"<Property ID=\"000000\" Label=\"Free-form Content\">");
67 naveen 732
 
733
			for(String f : ffc) {
734
				if(f != null) {
735
					f = StringEscapeUtils.escapeXml(f);
736
 
737
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
738
						f + "</Value>");
739
				}
740
			}
741
 
742
			xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
743
		}
744
 
745
		// Children slides
746
		// TODO
747
 
748
		return StringUtils.join(xmlSnippet, "\n");
749
	}
64 naveen 750
 
62 naveen 751
}