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
 
1314 rajveer 515
 
64 naveen 516
 
1314 rajveer 517
			String subCategory = defs.getCategory(categoryID).getLabel();
518
			String  mainCategory = defs.getCategory(categoryID).getParentCategory().getLabel();
88 naveen 519
 
1314 rajveer 520
			String brand = expEntity.getBrand();
88 naveen 521
 
64 naveen 522
 
88 naveen 523
 
1314 rajveer 524
			//Create zero slide
525
			Slide zeroSlide = createZeroSlide(brand, itemPrice, mainCategory, subCategory);
526
			ExpandedSlide expandedZeroSlide = new ExpandedSlide(zeroSlide);
527
			expSlides.add(expandedZeroSlide);
528
 
529
			for(ExpandedSlide expSlide: expSlides){
530
				System.out.println(expSlide.getSlideDefinitionID());
531
				if(slideFacets.containsKey(expSlide.getSlideDefinitionID())){
532
					List<FacetDefinition> facets = slideFacets.get(expSlide.getSlideDefinitionID());
533
					for(FacetDefinition facet: facets){
534
						for(FacetSlideDefinition facetSlides: facet.getFacetSlideDefinitions()){
535
							if(facetSlides.getSlideDefinitionID() != expSlide.getSlideDefinitionID()){continue;}
536
							ExpandedFacetSlideDefinition expFacetSlideDef = new ExpandedFacetSlideDefinition(facetSlides) ;
537
							for(ExpandedFeature expFeature: expSlide.getExpandedFeatures()){
538
								if(defs.needsNormalization(expFeature.getFeatureDefinitionID())) {
539
									Utils.info("needsNormalization feature=" + expFeature.getFeatureDefinitionID());
540
									//expFeature = this.normalize(expFeature);
541
								}
542
							}
543
							entityXMLSnippets.add(processFacet(facet, expSlide, expFacetSlideDef, facet.getID()));
544
						}
545
					}
546
				}
346 rajveer 547
			}
1314 rajveer 548
 
549
			// Collect PROPERTIES
550
			String propertiesXMLSnippetsStr = this.getPropertiesXMLSnippet(expEntity, new ArrayList<Long>(), 2);
551
			entityXMLSnippets.add(propertiesXMLSnippetsStr);
552
			entityXMLSnippets.add(this.xmlIndentation[1] + "</Entity>");
553
			System.out.println(entityXMLSnippets);
554
			irDataXMLSnippets.addAll(entityXMLSnippets);
555
 
346 rajveer 556
		}
557
 
1314 rajveer 558
		irDataXMLSnippets.add("</IRData>");
346 rajveer 559
 
1314 rajveer 560
		String irDataXML = StringUtils.join(irDataXMLSnippets, "\n");
561
		Utils.info(irDataXML);
64 naveen 562
 
1314 rajveer 563
		// Write it to file
564
		String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
565
		DBUtils.store(irDataXML, irDataFilename);
566
	}
567
 
568
	private Slide createZeroSlide(String brand, double price, String mainCategory, String subCategory) {
569
		Slide zeroSlide = new Slide(130000);
570
		List<Feature> zeroSlideFeatures = new ArrayList<Feature>();
91 naveen 571
 
1314 rajveer 572
		Feature brandFeature = new Feature(120080);
573
		Bullet brandBullet = new Bullet(new PrimitiveDataObject(brand));
574
		List<Bullet> brandBullets = new ArrayList<Bullet>();
575
		brandBullets.add(brandBullet);
576
		brandFeature.setBullets(brandBullets);
577
		zeroSlideFeatures.add(brandFeature);
91 naveen 578
 
1314 rajveer 579
		Feature priceFeature = new Feature(120128);
580
		Bullet priceBullet = new Bullet(new PrimitiveDataObject((new Double(price)).toString()));
581
		List<Bullet> priceBullets = new ArrayList<Bullet>();
582
		priceBullets.add(priceBullet);
583
		priceFeature.setBullets(priceBullets);
584
		zeroSlideFeatures.add(priceFeature);
91 naveen 585
 
1314 rajveer 586
		Feature mainCategoryFeature = new Feature(120123);
587
		Bullet mainCategoryBullet = new Bullet(new PrimitiveDataObject(mainCategory));
588
		List<Bullet> mainCategoryBullets = new ArrayList<Bullet>();
589
		mainCategoryBullets.add(mainCategoryBullet);
590
		mainCategoryFeature.setBullets(mainCategoryBullets);
591
		zeroSlideFeatures.add(mainCategoryFeature);
64 naveen 592
 
1314 rajveer 593
		Feature subCategoryFeature = new Feature(120124);
594
		Bullet subCategoryBullet = new Bullet(new PrimitiveDataObject(subCategory));
595
		List<Bullet> subCategoryBullets = new ArrayList<Bullet>();
596
		subCategoryBullets.add(subCategoryBullet);
597
		subCategoryFeature.setBullets(subCategoryBullets);
598
		zeroSlideFeatures.add(subCategoryFeature);
599
 
600
		zeroSlide.setFeatures(zeroSlideFeatures);
601
		return zeroSlide;
64 naveen 602
	}
1314 rajveer 603
 
102 naveen 604
 
605
	/**
606
	 * 
607
	 * @param feature
608
	 * @return Feature
609
	 * @throws Exception 
610
	 */
1314 rajveer 611
	private ExpandedFeature normalize(ExpandedFeature expFeature) throws Exception {
102 naveen 612
		BulletDefinition bulletDef = 
613
			expFeature.getFeatureDefinition().getBulletDefinition();
614
 
615
		ExpandedBulletDefinition expBulletDef = new ExpandedBulletDefinition(
616
				bulletDef);
617
 
618
		if(expBulletDef.isPrimitive()) {
619
			PrimitiveNormalizationJythonWrapper jy = 
620
				new PrimitiveNormalizationJythonWrapper();
621
 
622
			jy.setExpandedFeature(expFeature);
623
 
624
			jy.excuteRule();
625
 
626
			String newValue = jy.getNewValue();
627
			long newUnitID = jy.getNewUnitID();
628
 
629
			List<Bullet> newBullets = new ArrayList<Bullet>();
630
			Bullet newBullet = new Bullet(new PrimitiveDataObject(newValue));
631
			newBullet.setUnitID(newUnitID);
632
 
633
			newBullets.add(newBullet);
634
 
1314 rajveer 635
			expFeature.setBullets(newBullets);
102 naveen 636
		}
637
		else {
638
			Utils.severe("Normalization not defined for non-primitives");
639
		}
640
 
1314 rajveer 641
		return expFeature;
102 naveen 642
	}
643
 
67 naveen 644
	/**
645
	 * 
646
	 * @param expEntity
647
	 * @param facetFeatureIDs
648
	 * @param indent
649
	 * @return
650
	 */
651
	private String getPropertiesXMLSnippet(ExpandedEntity expEntity, 
652
			List<Long> facetFeatureIDs, int indent) {
653
 
654
		List<String> xmlSnippet = new ArrayList<String>();
655
 
656
		// Collect all free-form content here
657
		List<String> ffc = new ArrayList<String>();
658
 
659
		// Features
660
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
661
 
662
		for(ExpandedSlide expSlide : expSlides) {
1314 rajveer 663
			if(expSlide.getSlideDefinitionID() == 13000){
664
				continue;
665
			}
67 naveen 666
			List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
667
 
668
			if(expSlide.getFreeformContent() != null) {
199 naveen 669
				ffc.add(expSlide.getFreeformContent().getFreeformText());
67 naveen 670
			}
671
 
672
			if(expFeatures == null) {
673
				continue;
674
			}
675
 
676
			for(ExpandedFeature expFeature : expFeatures) {
677
				Long featureDefID = 
678
					new Long(expFeature.getFeatureDefinitionID());
679
 
680
				// FFC at feature level
681
				if(expFeature.getFreeformContent() != null) {
199 naveen 682
					ffc.add(expFeature.getFreeformContent().getFreeformText());
67 naveen 683
				}
684
 
685
				// Exclude those who are already covered as facets
686
				if(facetFeatureIDs.contains(featureDefID)) {
687
					continue;
688
				}
689
 
690
				List<ExpandedBullet> expBullets = 
691
					expFeature.getExpandedBullets();
692
 
693
				if(expBullets == null) {
694
					continue;
695
				}
696
 
697
				//<Property Label="">
698
				xmlSnippet.add(this.xmlIndentation[indent] + 
83 naveen 699
						"<Property ID=\"" + expFeature.getFeatureDefinitionID() 
700
						+ "\" Label=\"" + StringEscapeUtils.escapeXml(
67 naveen 701
								expFeature.getFeatureDefinition().getLabel()) + 
702
						"\">");
703
 
704
				//<Value></Value>
705
				for(ExpandedBullet bullet : expBullets) {
706
 
707
					// FFC at bullet level
708
					if(bullet.getFreeformContent() != null) {
199 naveen 709
						ffc.add(bullet.getFreeformContent().getFreeformText());
67 naveen 710
					}
711
 
712
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
713
							StringEscapeUtils.escapeXml(bullet.getValue()) + 
714
							"</Value>");
715
				}
716
 
717
				//</Property>
718
				xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
719
			}
720
		}
721
 
722
		// FFC as Label="Free-form Content"
723
		if(!ffc.isEmpty()) {
724
			xmlSnippet.add(this.xmlIndentation[indent] + 
108 naveen 725
					"<Property ID=\"000000\" Label=\"Free-form Content\">");
67 naveen 726
 
727
			for(String f : ffc) {
728
				if(f != null) {
729
					f = StringEscapeUtils.escapeXml(f);
730
 
731
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
732
						f + "</Value>");
733
				}
734
			}
735
 
736
			xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
737
		}
738
 
739
		// Children slides
740
		// TODO
741
 
742
		return StringUtils.join(xmlSnippet, "\n");
743
	}
64 naveen 744
 
62 naveen 745
}