Subversion Repositories SmartDukaan

Rev

Rev 4270 | Rev 5019 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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