Subversion Repositories SmartDukaan

Rev

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