Subversion Repositories SmartDukaan

Rev

Rev 5367 | Rev 6656 | 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>>();
5078 amit.gupta 61
	private static Map<String, Map<String, String>> synonymMap = null;
2171 rajveer 62
 
5078 amit.gupta 63
	static {
64
		try {
65
			synonymMap = CreationUtils.getSynonyms();
66
		}catch(Exception e){
67
			e.printStackTrace();
68
		}
69
	}
70
 
2367 rajveer 71
	List<Entity> entities;
2171 rajveer 72
 
73
	/**
74
	 * @param args
75
	 * @throws Exception 
76
	 */
77
	public static void main(String[] args) throws Exception {
78
		/*
79
		NewIR ir = new NewIR();
80
		ir.exportIRData();
81
		ir.transformIrDataXMLtoSolrXML();
82
		ir.exportIRMetaData();
4056 rajveer 83
		ir.transformIrMetaDataXMLtoSolrSchemaXML();
84
		ir.transformIrMetaDataXMLtoSolrCatchAllXML();
2171 rajveer 85
		*/
86
	}
87
 
88
	/**
89
	 * 
90
	 * @param entityIdItemMap
91
	 */
2367 rajveer 92
	public NewIR(List<Entity> entities){
93
	    this.entities = entities;
2171 rajveer 94
	}
95
 
96
 
97
	/**
98
	 * 
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
	 * 
125
	 * @throws Exception
126
	 */
2367 rajveer 127
	public void transformIrDataXMLtoSolrXML(long catalogId) throws Exception {
128
		String irDataFilename = Utils.EXPORT_PATH + "xml/intermediate/" + catalogId +  "_irdata.xml";
129
		String irSolrDataFilename = Utils.EXPORT_PATH + "xml/final/" +  catalogId +  "_irdata_solr.xml";
2171 rajveer 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
	 */
4056 rajveer 144
	public void transformIrMetaDataXMLtoSolrSchemaXML() throws Exception {
2367 rajveer 145
		String irDataFilename = Utils.EXPORT_PATH  + "xml/intermediate/" + "irmetadata.xml";
146
		String irSolrDataFilename = Utils.EXPORT_PATH + "xml/final/" + "irmetadata_solrschema.xml";
2171 rajveer 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
	 */
4056 rajveer 162
	public void transformIrMetaDataXMLtoSolrCatchAllXML() throws Exception {
163
		String irDataFilename = Utils.EXPORT_PATH  + "xml/intermediate/" + "irmetadata.xml";
164
		String irSolrDataFilename = Utils.EXPORT_PATH + "xml/final/" + "irmetadata_catchall.xml";
165
		String irXslFilename = "src/xsl/irmetadata_catchall.xsl";
166
		System.out.println(irSolrDataFilename);
167
		File solrFile = new File(irSolrDataFilename);
168
		if(!solrFile.exists()){
169
			solrFile.createNewFile();
170
		}
171
		xsltTransformation(irDataFilename, irXslFilename, irSolrDataFilename);
172
	}
173
 
174
	/**
175
	 * 
176
	 * @throws Exception
177
	 */
2171 rajveer 178
	public void exportIRMetaData() throws Exception {
179
		DefinitionsContainer defs = 
180
			Catalog.getInstance().getDefinitionsContainer();
181
 
182
		// <IRMetaData>
183
		List<String> xmlSnippets = new ArrayList<String>();
184
		xmlSnippets.add("<IRMetaData>");
185
		xmlSnippets.add("\t<Facets>");
186
 
187
		IRMetaDataJythonWrapper jy = new IRMetaDataJythonWrapper();
188
 
189
		// Iterate over all facet definitions
190
		Map<Long, FacetDefinition> facetDefs = defs.getFacetDefinitions();
191
		for(FacetDefinition facetDef : facetDefs.values()) {
192
 
193
			jy.reset();
194
			jy.initialize();
195
 
196
			ExpandedFacetDefinition expFacetDef = 
197
				new ExpandedFacetDefinition(facetDef);
198
 
199
			jy.setExpandedFacetDefinition(expFacetDef);
200
 
201
			jy.executeRule();
202
 
203
			String facetXMLSnip = jy.getXMLSnippet();
204
			Utils.info("facetXMLSnip=" + facetXMLSnip);
205
 
206
			xmlSnippets.add(facetXMLSnip);
207
 
208
		}
209
		xmlSnippets.add("\t</Facets>");
210
 
211
 
212
 
213
		xmlSnippets.add("\n\t<Properties>");
214
 
215
		// Iterate over all feature definitions
216
		Map<Long, FeatureDefinition> featureDefs = defs.getFeatureDefinitions();
217
		for(FeatureDefinition featureDef : featureDefs.values()) {
218
			String propertyXMLSnip = this.getPropertyXMLSnippet(featureDef);
219
			Utils.info("propertyXMLSnip=" + propertyXMLSnip);
220
 
221
			xmlSnippets.add(propertyXMLSnip);
222
		}
223
 
224
		xmlSnippets.add("\t</Properties>");
225
 
226
		xmlSnippets.add("\n\t<Categories>");
227
 
228
		// Iterate over all categories
229
		/*
230
		Category rootCategory = defs.getCategory(Catalog.getInstance().getRootCategory().getID());
231
		List<Category> children = rootCategory.getChildrenCategory();
232
		for (Category child : children) {
233
 
234
			String categoryXMLSnip = this.getCategoryXMLSnippet(child, 2);
235
			Utils.info("categoryXMLSnip=" + categoryXMLSnip);
236
 
237
			xmlSnippets.add(categoryXMLSnip);
238
		}
239
		*/
240
 
241
		Category rootCategory = defs.getCategory(Catalog.getInstance().getRootCategory().getID());
242
		String categoryXMLSnip = this.getCategoryXMLSnippet(rootCategory, 2);
243
		Utils.info("categoryXMLSnip=" + categoryXMLSnip);
244
 
245
 
246
		xmlSnippets.add("\t</Categories>");
247
 
248
		// </IRMetaData>
249
		xmlSnippets.add("</IRMetaData>");
250
 
251
		String irMetaDataXML = StringUtils.join(xmlSnippets, "\n");
252
		Utils.info(irMetaDataXML);
253
 
254
		// Write it to file
2367 rajveer 255
		String irMetaDataFilename = Utils.EXPORT_PATH +  "xml/intermediate/" + "irmetadata.xml";
2171 rajveer 256
		DBUtils.store(irMetaDataXML, irMetaDataFilename);
257
	}
258
 
259
	/**
260
	 * 
261
	 * @param category
262
	 * @return
263
	 * @throws Exception 
264
	 */
265
	private String getCategoryXMLSnippet(Category category, int indent) 
266
		throws Exception {
267
 
268
		DefinitionsContainer defs = 
269
			Catalog.getInstance().getDefinitionsContainer();
270
 
271
		String xmlSnip = this.xmlTabIndentation[indent] + "<Category";
272
 
273
		xmlSnip += " ID=\"" + category.getID() + "\"";
274
		xmlSnip += " label=\"" + category.getLabel() + "\"";
275
		xmlSnip += ">\n";
276
 
277
		List<Category> children = category.getChildrenCategory();
278
 
279
		if(children != null) {
280
			for(Category child : children) {
281
				xmlSnip += this.getCategoryXMLSnippet(child, indent+1);
282
			}
283
		}
284
		else {
285
			// Only leaf category will have entities
286
			// Facet IDs
287
			xmlSnip += this.xmlTabIndentation[indent+1] + "<Facets>\n";
288
 
289
			List<Long> facetDefIDs = 
290
				defs.getFacetDefinitionIDs(category.getID());
291
			//Utils.info("facetDefIDs=" + facetDefIDs);
292
 
293
 
294
 
295
			if( facetDefIDs != null && !facetDefIDs.isEmpty() ){
296
					for(Long facetDefID : facetDefIDs) {
297
					xmlSnip += this.xmlTabIndentation[indent+2] + 
298
						"<FacetID>" + facetDefID + "</FacetID>\n";
299
				}
300
			}
301
 
302
 
303
			xmlSnip += this.xmlTabIndentation[indent+1] + "</Facets>\n\n";
304
 
305
			// Feature IDs
306
			xmlSnip += this.xmlTabIndentation[indent+1] + "<Properties>\n";
307
 
308
			List<Long> featureDefIDs = 
309
				defs.getFeatureDefinitionIDs(category.getID());
310
			//Utils.info("featureDefIDs=" + featureDefIDs);
311
 
312
			for(Long featureDefID : featureDefIDs) {
313
				xmlSnip += this.xmlTabIndentation[indent+2] + 
314
					"<FeatureID>" + featureDefID + "</FeatureID>\n";
315
			}
316
 
317
			xmlSnip += this.xmlTabIndentation[indent+1] + "</Properties>\n";
318
		}
319
 
320
		xmlSnip += this.xmlTabIndentation[indent] + "</Category>\n";
321
 
322
		return xmlSnip;
323
	}
324
 
325
	/**
326
	 * 
327
	 * @param featureDef
328
	 * @return
329
	 * @throws Exception 
330
	 */
331
	private String getPropertyXMLSnippet(FeatureDefinition featureDef) 
332
		throws Exception {
333
		String xmlSnip = "\t\t<Property";
334
 
335
		xmlSnip += " ID=\"" + featureDef.getID() + "\"";
336
		xmlSnip += " label=\"" + featureDef.getLabel() + "\"";
337
 
338
		ExpandedFeatureDefinition expFeatureDef = 
339
			new ExpandedFeatureDefinition(featureDef);
340
 
341
		ExpandedBulletDefinition expBulletDef = 
342
			expFeatureDef.getExpandedBulletDefinition();
343
 
344
		String datatype = "string";
345
		if(expBulletDef.isComposite() || expBulletDef.isEnumerated()) {
346
			datatype = "string";
347
		}
348
		else {
349
			DatatypeDefinition datatypeDef = 
350
				expBulletDef.getDatatypeDefinition();
351
 
352
			datatype = datatypeDef.getName();
353
 
354
			// REVISIT
355
			if(datatype.equals("hours_mins") || datatype.equals("days_hours") ||
356
					datatype.equals("hours_mins") || 
357
					datatype.equals("days_hours")) {
358
				datatype = "string";
359
			}
360
		}
361
		xmlSnip += " datatype=\"" + datatype + "\"";
362
 
363
		String multivalue = "false";
364
		if (expBulletDef.isMultivalue()) {
365
			multivalue = "true";
366
		}
367
		xmlSnip += " isMultivalue=\"" + multivalue + "\"";
368
 
369
		xmlSnip += "/>";
370
		return xmlSnip;
371
	}
372
 
373
 
374
 
375
	private String processFacet(FacetDefinition facet, ExpandedSlide expSlide, ExpandedFacetSlideDefinition expFacetSlideDef, Long facetDefID) throws Exception {
376
 
377
		IRDataJythonWrapper jw = new IRDataJythonWrapper();
378
 
379
		jw.setExpandedSlide(expSlide);
380
		jw.setExpandedFacetSlideDefinition(expFacetSlideDef);
381
 
382
		// Execute Python script
383
		jw.executeRule();
384
 
385
		//FIXME
386
		// Normalize
387
//		if(defs.needsNormalization(feature.getFeatureDefinitionID())) {
388
//			Utils.info("needsNormalization feature=" + feature.getFeatureDefinitionID());
389
//			
390
//			feature = this.normalize(feature);
391
//		}
392
 
393
 
394
 
395
		List<Object> values = (List<Object>)jw.getValues();
396
		Utils.info("values=" + values);
397
 
398
		// Append to facet values
399
 
400
		Utils.info("facetDefID=" + facetDefID);
401
 
402
		List<String> facetValues = this.facetIDFacetValues.get(
403
				new Long(facetDefID));
404
 
405
		if(facetValues == null) {
406
			facetValues = new ArrayList<String>();
407
			this.facetIDFacetValues.put(new Long(facetDefID), facetValues);
408
		}
409
 
410
		if(values != null) {
411
			for(Object value : values) {
412
				String strValue = value.toString();
413
 
414
				if(!facetValues.contains(strValue)) {
415
					facetValues.add(strValue);
416
				}
417
			}
418
		}
419
 
420
		ExpandedFacetDefinition expFacet = new ExpandedFacetDefinition(facet);
421
		// Parse returned Python list
422
		String facetXMLSnip = null;
423
		if(values != null) {
424
			facetXMLSnip = this.getFacetXMLSnippet(expFacet, values, 2);
425
		}
426
 
427
		Utils.info("0 facetXMLSnip=" + facetXMLSnip);
428
		return facetXMLSnip;
429
	}
430
 
431
 
432
 
433
	/**
434
	 * 
435
	 * @param expFacetDef
436
	 * @param values
437
	 * @param indent
438
	 * @return String XML Snippet
439
	 */
440
	private String getFacetXMLSnippet(ExpandedFacetDefinition expFacetDef,	List<Object> values, int indent) {
441
 
442
		// <Facet Label="Form Factor">
443
		List<String> xmlSnippet = new ArrayList<String>();
444
		String target = expFacetDef.getTarget();
445
 
446
		xmlSnippet.add(this.xmlIndentation[indent] + "<Facet ID=\"" + expFacetDef.getID() + "\" Label=\"" + target + "\">");
447
 
448
		//<Value>Candybar</Value>
449
		for(Object value : values) {
450
			xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + value.toString() + "</Value>");
451
		}
452
 
453
		//</Facet>
454
		xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");
455
 
456
		return StringUtils.join(xmlSnippet, "\n");
457
	}
458
 
459
	/**
460
	 * @throws Exception 
461
	 * 
462
	 */
463
	public void exportIRData() throws Exception {
464
		DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
465
		Map<Long, List<FacetDefinition>> slideFacets = defs.getSlideFacetDefinitions();
466
 
2367 rajveer 467
		for(Entity entity: entities){
468
			// <IRData>
469
			List<String> irDataXMLSnippets = new ArrayList<String>();
470
			irDataXMLSnippets.add("<IRData>");
471
		    long entityID = entity.getID();
472
 
473
		    //Setting minPrice zero because irdatarule requires float. It wont accept string 
474
		    double minPrice = 0;
2171 rajveer 475
			ExpandedEntity expEntity = new ExpandedEntity(entity);
476
			long categoryID = expEntity.getCategoryID();
477
			List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
478
 
479
			List<String> entityXMLSnippets = new ArrayList<String>();
480
			entityXMLSnippets.add(this.xmlIndentation[1] + "<Entity ID=\""+ entityID +"\">");
481
 
482
			String parentCategory = expEntity.getCategory().getLabel();
483
			entityXMLSnippets.add(this.xmlIndentation[2] + "<Category>" + StringEscapeUtils.escapeXml(parentCategory) + "</Category>");
484
 
485
 
486
			String title = StringEscapeUtils.escapeXml(expEntity.getBrand()) + " " + StringEscapeUtils.escapeXml(expEntity.getModelName()) + 
487
				((expEntity.getModelNumber() != null) ? (" " +	StringEscapeUtils.escapeXml(expEntity.getModelNumber())): "");
488
 
489
			entityXMLSnippets.add(this.xmlIndentation[2] +	"<Title>" + StringEscapeUtils.escapeXml(title) + "</Title>");
490
 
5367 rajveer 491
			//Boost titles for the mobile phones, laptops and tablets only
492
			if(expEntity.getCategory().getParentCategory().isHasAccessories() || expEntity.getCategory().getParentCategory().getID() == Utils.TABLETS_CATEGORY || expEntity.getCategory().getParentCategory().getID() == Utils.LAPTOPS_CATEGORY){
2171 rajveer 493
			    entityXMLSnippets.add(this.xmlIndentation[2] +   "<Boost>" + 5 + "</Boost>");
494
			}else{
495
			    entityXMLSnippets.add(this.xmlIndentation[2] +   "<Boost>" + 0 + "</Boost>");
496
			}
497
 
498
 
499
			String subCategory = defs.getCategory(categoryID).getLabel();
500
			String  mainCategory = defs.getCategory(categoryID).getParentCategory().getLabel();
501
 
2500 rajveer 502
			long mainCatId = defs.getCategory(categoryID).getParentCategory().getID();
503
			if(mainCatId == Utils.ROOT_CATAGOEY){
504
				mainCategory = subCategory;
505
			}
506
 
2171 rajveer 507
			String brand = expEntity.getBrand();
508
 
509
 
510
 
511
			//Create zero slide
6602 amit.gupta 512
			Slide zeroSlide = createZeroSlide(brand, minPrice, mainCategory, subCategory, "In Stock");
2171 rajveer 513
			ExpandedSlide expandedZeroSlide = new ExpandedSlide(zeroSlide);
514
			expSlides.add(expandedZeroSlide);
515
 
516
			for(ExpandedSlide expSlide: expSlides){
517
				System.out.println(expSlide.getSlideDefinitionID());
518
				if(slideFacets.containsKey(expSlide.getSlideDefinitionID())){
519
					List<FacetDefinition> facets = slideFacets.get(expSlide.getSlideDefinitionID());
520
					for(FacetDefinition facet: facets){
521
						for(FacetSlideDefinition facetSlides: facet.getFacetSlideDefinitions()){
522
							if(facetSlides.getSlideDefinitionID() != expSlide.getSlideDefinitionID()){continue;}
523
							ExpandedFacetSlideDefinition expFacetSlideDef = new ExpandedFacetSlideDefinition(facetSlides) ;
524
							for(ExpandedFeature expFeature: expSlide.getExpandedFeatures()){
525
								if(defs.needsNormalization(expFeature.getFeatureDefinitionID())) {
526
									Utils.info("needsNormalization feature=" + expFeature.getFeatureDefinitionID());
527
									//expFeature = this.normalize(expFeature);
528
								}
529
							}
530
							entityXMLSnippets.add(processFacet(facet, expSlide, expFacetSlideDef, facet.getID()));
531
						}
532
					}
533
				}
534
			}
535
 
536
			// Collect PROPERTIES
5074 amit.gupta 537
			List<Long>facetFeatureIds = new ArrayList<Long>();
538
			facetFeatureIds.add(Utils.BRAND_FEATURE_DEFINITION_ID);
539
			facetFeatureIds.add(Utils.MAIN_CAT_FEATURE_DEFINITION_ID);
540
			facetFeatureIds.add(Utils.SUB_CAT_FEATURE_DEFINITION_ID);
541
			facetFeatureIds.add(Utils.PRICE_FEATURE_DEFINITION_ID);
542
			String propertiesXMLSnippetsStr = this.getPropertiesXMLSnippet(expEntity, facetFeatureIds, 2);
2171 rajveer 543
			entityXMLSnippets.add(propertiesXMLSnippetsStr);
544
			entityXMLSnippets.add(this.xmlIndentation[1] + "</Entity>");
545
			System.out.println(entityXMLSnippets);
546
			irDataXMLSnippets.addAll(entityXMLSnippets);
2367 rajveer 547
			irDataXMLSnippets.add("</IRData>");
548
			String irDataXML = StringUtils.join(irDataXMLSnippets, "\n");
549
			Utils.info(irDataXML);
550
 
551
			// Write it to file
552
			String irDataFilename = Utils.EXPORT_PATH + "xml/intermediate/" + entityID + "_irdata.xml";
553
			DBUtils.store(irDataXML, irDataFilename);
554
			transformIrDataXMLtoSolrXML(entityID);
2171 rajveer 555
		}
556
 
557
 
558
 
2367 rajveer 559
 
2171 rajveer 560
	}
2488 rajveer 561
 
6602 amit.gupta 562
	private Slide createZeroSlide(String brand, double price, String mainCategory, String subCategory, String availability) {
2488 rajveer 563
		Slide zeroSlide = new Slide(Utils.ZERO_SLIDE_DEFINITION_ID);
2171 rajveer 564
		List<Feature> zeroSlideFeatures = new ArrayList<Feature>();
565
 
2488 rajveer 566
		Feature brandFeature = new Feature(Utils.BRAND_FEATURE_DEFINITION_ID);
2171 rajveer 567
		Bullet brandBullet = new Bullet(new PrimitiveDataObject(brand));
568
		List<Bullet> brandBullets = new ArrayList<Bullet>();
569
		brandBullets.add(brandBullet);
570
		brandFeature.setBullets(brandBullets);
571
		zeroSlideFeatures.add(brandFeature);
572
 
6602 amit.gupta 573
 
574
		Feature availabilityFeature = new Feature(Utils.AVAILABILITY_FEATURE_DEFINITION_ID);
575
		Bullet availabilityBullet = new Bullet(new PrimitiveDataObject(availability));
576
		List<Bullet> availabilityBullets = new ArrayList<Bullet>();
577
		brandBullets.add(availabilityBullet);
578
		brandFeature.setBullets(availabilityBullets);
579
		zeroSlideFeatures.add(availabilityFeature);
580
 
2171 rajveer 581
		Feature priceFeature = new Feature(120128);
582
		Bullet priceBullet = new Bullet(new PrimitiveDataObject((new Double(price)).toString()));
583
		List<Bullet> priceBullets = new ArrayList<Bullet>();
584
		priceBullets.add(priceBullet);
585
		priceFeature.setBullets(priceBullets);
586
		zeroSlideFeatures.add(priceFeature);
587
 
588
		Feature mainCategoryFeature = new Feature(120123);
589
		Bullet mainCategoryBullet = new Bullet(new PrimitiveDataObject(mainCategory));
590
		List<Bullet> mainCategoryBullets = new ArrayList<Bullet>();
591
		mainCategoryBullets.add(mainCategoryBullet);
592
		mainCategoryFeature.setBullets(mainCategoryBullets);
593
		zeroSlideFeatures.add(mainCategoryFeature);
594
 
595
		Feature subCategoryFeature = new Feature(120124);
596
		Bullet subCategoryBullet = new Bullet(new PrimitiveDataObject(subCategory));
597
		List<Bullet> subCategoryBullets = new ArrayList<Bullet>();
598
		subCategoryBullets.add(subCategoryBullet);
599
		subCategoryFeature.setBullets(subCategoryBullets);
600
		zeroSlideFeatures.add(subCategoryFeature);
601
 
5074 amit.gupta 602
 
603
		try {
5078 amit.gupta 604
 
5074 amit.gupta 605
			String brandSynonyms = synonymMap.get("brand").get(brand);
606
			String subCategorySynonyms = synonymMap.get("subcategory").get(subCategory);
607
 
608
			if(brandSynonyms != null){
609
				Feature brandSynonymFeature = new Feature(Utils.BRAND_SYNONYMS_FEATURE_DEFINITION_ID);
610
				List<Bullet> brandSynonymBullets = new ArrayList<Bullet>();
611
				for(String brSyn : brandSynonyms.split(",")){
612
					Bullet brandSynonymBullet = new Bullet(new PrimitiveDataObject(brSyn));
613
					brandSynonymBullets.add(brandSynonymBullet);
614
				}
615
				brandSynonymFeature.setBullets(brandSynonymBullets);
616
				zeroSlideFeatures.add(brandSynonymFeature);
617
			}
618
 
619
			if(subCategorySynonyms != null){
620
				Feature subCategorySynonymFeature = new Feature(Utils.SUB_CATEGORY_SYNONYMS_FEATURE_DEFINITION_ID);
621
				List<Bullet> subCategorySynonymBullets = new ArrayList<Bullet>();
622
				for(String subCatSyn : subCategorySynonyms.split(",")){
623
					Bullet subCategorySynonymBullet = new Bullet(new PrimitiveDataObject(subCatSyn));
624
					subCategorySynonymBullets.add(subCategorySynonymBullet);
625
				}
626
				subCategorySynonymFeature.setBullets(subCategorySynonymBullets);
627
				zeroSlideFeatures.add(subCategorySynonymFeature);
628
			}
629
		} catch (Exception e) {
630
			e.printStackTrace();
631
		}
632
 
633
 
634
 
2171 rajveer 635
		zeroSlide.setFeatures(zeroSlideFeatures);
636
		return zeroSlide;
637
	}
638
 
639
 
640
	/**
641
	 * 
642
	 * @param feature
643
	 * @return Feature
644
	 * @throws Exception 
645
	 */
646
	private ExpandedFeature normalize(ExpandedFeature expFeature) throws Exception {
647
		BulletDefinition bulletDef = 
648
			expFeature.getFeatureDefinition().getBulletDefinition();
649
 
650
		ExpandedBulletDefinition expBulletDef = new ExpandedBulletDefinition(
651
				bulletDef);
652
 
653
		if(expBulletDef.isPrimitive()) {
654
			PrimitiveNormalizationJythonWrapper jy = 
655
				new PrimitiveNormalizationJythonWrapper();
656
 
657
			jy.setExpandedFeature(expFeature);
658
 
659
			jy.excuteRule();
660
 
661
			String newValue = jy.getNewValue();
662
			long newUnitID = jy.getNewUnitID();
663
 
664
			List<Bullet> newBullets = new ArrayList<Bullet>();
665
			Bullet newBullet = new Bullet(new PrimitiveDataObject(newValue));
666
			newBullet.setUnitID(newUnitID);
667
 
668
			newBullets.add(newBullet);
669
 
670
			expFeature.setBullets(newBullets);
671
		}
672
		else {
673
			Utils.severe("Normalization not defined for non-primitives");
674
		}
675
 
676
		return expFeature;
677
	}
678
 
679
	/**
680
	 * 
681
	 * @param expEntity
682
	 * @param facetFeatureIDs
683
	 * @param indent
684
	 * @return
685
	 */
686
	private String getPropertiesXMLSnippet(ExpandedEntity expEntity, 
687
			List<Long> facetFeatureIDs, int indent) {
688
 
689
		List<String> xmlSnippet = new ArrayList<String>();
690
 
691
		// Collect all free-form content here
692
		List<String> ffc = new ArrayList<String>();
693
 
694
		// Features
695
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
696
 
697
		for(ExpandedSlide expSlide : expSlides) {
5074 amit.gupta 698
 
2171 rajveer 699
			List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
700
 
701
			if(expSlide.getFreeformContent() != null) {
702
				ffc.add(expSlide.getFreeformContent().getFreeformText());
703
			}
704
 
705
			if(expFeatures == null) {
706
				continue;
707
			}
708
 
709
			for(ExpandedFeature expFeature : expFeatures) {
710
				Long featureDefID = 
711
					new Long(expFeature.getFeatureDefinitionID());
712
 
713
				// FFC at feature level
714
				if(expFeature.getFreeformContent() != null) {
715
					ffc.add(expFeature.getFreeformContent().getFreeformText());
716
				}
717
 
718
				// Exclude those who are already covered as facets
5019 amit.gupta 719
				// and the ones that are marked false for indexing
720
				if(facetFeatureIDs.contains(featureDefID) || !expFeature.getFeatureDefinition().isIndexed()) {
2171 rajveer 721
					continue;
722
				}
723
 
5019 amit.gupta 724
				if(!expFeature.getFeatureDefinition().isIndexed()){
725
					System.out.println("Hello");
726
				}
2171 rajveer 727
				List<ExpandedBullet> expBullets = 
728
					expFeature.getExpandedBullets();
729
 
730
				if(expBullets == null) {
731
					continue;
732
				}
733
 
734
				//<Property Label="">
735
				xmlSnippet.add(this.xmlIndentation[indent] + 
736
						"<Property ID=\"" + expFeature.getFeatureDefinitionID() 
737
						+ "\" Label=\"" + StringEscapeUtils.escapeXml(
738
								expFeature.getFeatureDefinition().getLabel()) + 
739
						"\">");
740
 
741
				//<Value></Value>
742
				for(ExpandedBullet bullet : expBullets) {
4270 rajveer 743
					if(bullet.getValue().trim().equalsIgnoreCase("")){
744
						continue;
745
					}
2171 rajveer 746
					// FFC at bullet level
747
					if(bullet.getFreeformContent() != null) {
748
						ffc.add(bullet.getFreeformContent().getFreeformText());
749
					}
750
 
751
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
752
							StringEscapeUtils.escapeXml(bullet.getValue()) + 
753
							"</Value>");
754
				}
755
 
756
				//</Property>
757
				xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
758
			}
759
		}
760
 
761
		// FFC as Label="Free-form Content"
762
		if(!ffc.isEmpty()) {
763
			xmlSnippet.add(this.xmlIndentation[indent] + 
764
					"<Property ID=\"000000\" Label=\"Free-form Content\">");
765
 
766
			for(String f : ffc) {
767
				if(f != null) {
768
					f = StringEscapeUtils.escapeXml(f);
769
 
770
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
771
						f + "</Value>");
772
				}
773
			}
774
 
775
			xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
776
		}
777
 
778
		// Children slides
779
		// TODO
5074 amit.gupta 780
 
2171 rajveer 781
		return StringUtils.join(xmlSnippet, "\n");
782
	}
783
 
784
}