Subversion Repositories SmartDukaan

Rev

Rev 5019 | Rev 5078 | 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
5074 amit.gupta 528
			List<Long>facetFeatureIds = new ArrayList<Long>();
529
			facetFeatureIds.add(Utils.BRAND_FEATURE_DEFINITION_ID);
530
			facetFeatureIds.add(Utils.MAIN_CAT_FEATURE_DEFINITION_ID);
531
			facetFeatureIds.add(Utils.SUB_CAT_FEATURE_DEFINITION_ID);
532
			facetFeatureIds.add(Utils.PRICE_FEATURE_DEFINITION_ID);
533
			String propertiesXMLSnippetsStr = this.getPropertiesXMLSnippet(expEntity, facetFeatureIds, 2);
2171 rajveer 534
			entityXMLSnippets.add(propertiesXMLSnippetsStr);
535
			entityXMLSnippets.add(this.xmlIndentation[1] + "</Entity>");
536
			System.out.println(entityXMLSnippets);
537
			irDataXMLSnippets.addAll(entityXMLSnippets);
2367 rajveer 538
			irDataXMLSnippets.add("</IRData>");
539
			String irDataXML = StringUtils.join(irDataXMLSnippets, "\n");
540
			Utils.info(irDataXML);
541
 
542
			// Write it to file
543
			String irDataFilename = Utils.EXPORT_PATH + "xml/intermediate/" + entityID + "_irdata.xml";
544
			DBUtils.store(irDataXML, irDataFilename);
545
			transformIrDataXMLtoSolrXML(entityID);
2171 rajveer 546
		}
547
 
548
 
549
 
2367 rajveer 550
 
2171 rajveer 551
	}
2488 rajveer 552
 
2171 rajveer 553
	private Slide createZeroSlide(String brand, double price, String mainCategory, String subCategory) {
2488 rajveer 554
		Slide zeroSlide = new Slide(Utils.ZERO_SLIDE_DEFINITION_ID);
2171 rajveer 555
		List<Feature> zeroSlideFeatures = new ArrayList<Feature>();
556
 
2488 rajveer 557
		Feature brandFeature = new Feature(Utils.BRAND_FEATURE_DEFINITION_ID);
2171 rajveer 558
		Bullet brandBullet = new Bullet(new PrimitiveDataObject(brand));
559
		List<Bullet> brandBullets = new ArrayList<Bullet>();
560
		brandBullets.add(brandBullet);
561
		brandFeature.setBullets(brandBullets);
562
		zeroSlideFeatures.add(brandFeature);
563
 
564
		Feature priceFeature = new Feature(120128);
565
		Bullet priceBullet = new Bullet(new PrimitiveDataObject((new Double(price)).toString()));
566
		List<Bullet> priceBullets = new ArrayList<Bullet>();
567
		priceBullets.add(priceBullet);
568
		priceFeature.setBullets(priceBullets);
569
		zeroSlideFeatures.add(priceFeature);
570
 
571
		Feature mainCategoryFeature = new Feature(120123);
572
		Bullet mainCategoryBullet = new Bullet(new PrimitiveDataObject(mainCategory));
573
		List<Bullet> mainCategoryBullets = new ArrayList<Bullet>();
574
		mainCategoryBullets.add(mainCategoryBullet);
575
		mainCategoryFeature.setBullets(mainCategoryBullets);
576
		zeroSlideFeatures.add(mainCategoryFeature);
577
 
578
		Feature subCategoryFeature = new Feature(120124);
579
		Bullet subCategoryBullet = new Bullet(new PrimitiveDataObject(subCategory));
580
		List<Bullet> subCategoryBullets = new ArrayList<Bullet>();
581
		subCategoryBullets.add(subCategoryBullet);
582
		subCategoryFeature.setBullets(subCategoryBullets);
583
		zeroSlideFeatures.add(subCategoryFeature);
584
 
5074 amit.gupta 585
 
586
		try {
587
			Map<String, Map<String, String>> synonymMap = CreationUtils.getSynonyms();
588
			String brandSynonyms = synonymMap.get("brand").get(brand);
589
			String subCategorySynonyms = synonymMap.get("subcategory").get(subCategory);
590
 
591
			if(brandSynonyms != null){
592
				Feature brandSynonymFeature = new Feature(Utils.BRAND_SYNONYMS_FEATURE_DEFINITION_ID);
593
				List<Bullet> brandSynonymBullets = new ArrayList<Bullet>();
594
				for(String brSyn : brandSynonyms.split(",")){
595
					Bullet brandSynonymBullet = new Bullet(new PrimitiveDataObject(brSyn));
596
					brandSynonymBullets.add(brandSynonymBullet);
597
				}
598
				brandSynonymFeature.setBullets(brandSynonymBullets);
599
				zeroSlideFeatures.add(brandSynonymFeature);
600
			}
601
 
602
			if(subCategorySynonyms != null){
603
				Feature subCategorySynonymFeature = new Feature(Utils.SUB_CATEGORY_SYNONYMS_FEATURE_DEFINITION_ID);
604
				List<Bullet> subCategorySynonymBullets = new ArrayList<Bullet>();
605
				for(String subCatSyn : subCategorySynonyms.split(",")){
606
					Bullet subCategorySynonymBullet = new Bullet(new PrimitiveDataObject(subCatSyn));
607
					subCategorySynonymBullets.add(subCategorySynonymBullet);
608
				}
609
				subCategorySynonymFeature.setBullets(subCategorySynonymBullets);
610
				zeroSlideFeatures.add(subCategorySynonymFeature);
611
			}
612
		} catch (Exception e) {
613
			e.printStackTrace();
614
		}
615
 
616
 
617
 
2171 rajveer 618
		zeroSlide.setFeatures(zeroSlideFeatures);
619
		return zeroSlide;
620
	}
621
 
622
 
623
	/**
624
	 * 
625
	 * @param feature
626
	 * @return Feature
627
	 * @throws Exception 
628
	 */
629
	private ExpandedFeature normalize(ExpandedFeature expFeature) throws Exception {
630
		BulletDefinition bulletDef = 
631
			expFeature.getFeatureDefinition().getBulletDefinition();
632
 
633
		ExpandedBulletDefinition expBulletDef = new ExpandedBulletDefinition(
634
				bulletDef);
635
 
636
		if(expBulletDef.isPrimitive()) {
637
			PrimitiveNormalizationJythonWrapper jy = 
638
				new PrimitiveNormalizationJythonWrapper();
639
 
640
			jy.setExpandedFeature(expFeature);
641
 
642
			jy.excuteRule();
643
 
644
			String newValue = jy.getNewValue();
645
			long newUnitID = jy.getNewUnitID();
646
 
647
			List<Bullet> newBullets = new ArrayList<Bullet>();
648
			Bullet newBullet = new Bullet(new PrimitiveDataObject(newValue));
649
			newBullet.setUnitID(newUnitID);
650
 
651
			newBullets.add(newBullet);
652
 
653
			expFeature.setBullets(newBullets);
654
		}
655
		else {
656
			Utils.severe("Normalization not defined for non-primitives");
657
		}
658
 
659
		return expFeature;
660
	}
661
 
662
	/**
663
	 * 
664
	 * @param expEntity
665
	 * @param facetFeatureIDs
666
	 * @param indent
667
	 * @return
668
	 */
669
	private String getPropertiesXMLSnippet(ExpandedEntity expEntity, 
670
			List<Long> facetFeatureIDs, int indent) {
671
 
672
		List<String> xmlSnippet = new ArrayList<String>();
673
 
674
		// Collect all free-form content here
675
		List<String> ffc = new ArrayList<String>();
676
 
677
		// Features
678
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
679
 
680
		for(ExpandedSlide expSlide : expSlides) {
5074 amit.gupta 681
 
2171 rajveer 682
			List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
683
 
684
			if(expSlide.getFreeformContent() != null) {
685
				ffc.add(expSlide.getFreeformContent().getFreeformText());
686
			}
687
 
688
			if(expFeatures == null) {
689
				continue;
690
			}
691
 
692
			for(ExpandedFeature expFeature : expFeatures) {
693
				Long featureDefID = 
694
					new Long(expFeature.getFeatureDefinitionID());
695
 
696
				// FFC at feature level
697
				if(expFeature.getFreeformContent() != null) {
698
					ffc.add(expFeature.getFreeformContent().getFreeformText());
699
				}
700
 
701
				// Exclude those who are already covered as facets
5019 amit.gupta 702
				// and the ones that are marked false for indexing
703
				if(facetFeatureIDs.contains(featureDefID) || !expFeature.getFeatureDefinition().isIndexed()) {
2171 rajveer 704
					continue;
705
				}
706
 
5019 amit.gupta 707
				if(!expFeature.getFeatureDefinition().isIndexed()){
708
					System.out.println("Hello");
709
				}
2171 rajveer 710
				List<ExpandedBullet> expBullets = 
711
					expFeature.getExpandedBullets();
712
 
713
				if(expBullets == null) {
714
					continue;
715
				}
716
 
717
				//<Property Label="">
718
				xmlSnippet.add(this.xmlIndentation[indent] + 
719
						"<Property ID=\"" + expFeature.getFeatureDefinitionID() 
720
						+ "\" Label=\"" + StringEscapeUtils.escapeXml(
721
								expFeature.getFeatureDefinition().getLabel()) + 
722
						"\">");
723
 
724
				//<Value></Value>
725
				for(ExpandedBullet bullet : expBullets) {
4270 rajveer 726
					if(bullet.getValue().trim().equalsIgnoreCase("")){
727
						continue;
728
					}
2171 rajveer 729
					// FFC at bullet level
730
					if(bullet.getFreeformContent() != null) {
731
						ffc.add(bullet.getFreeformContent().getFreeformText());
732
					}
733
 
734
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
735
							StringEscapeUtils.escapeXml(bullet.getValue()) + 
736
							"</Value>");
737
				}
738
 
739
				//</Property>
740
				xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
741
			}
742
		}
743
 
744
		// FFC as Label="Free-form Content"
745
		if(!ffc.isEmpty()) {
746
			xmlSnippet.add(this.xmlIndentation[indent] + 
747
					"<Property ID=\"000000\" Label=\"Free-form Content\">");
748
 
749
			for(String f : ffc) {
750
				if(f != null) {
751
					f = StringEscapeUtils.escapeXml(f);
752
 
753
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
754
						f + "</Value>");
755
				}
756
			}
757
 
758
			xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
759
		}
760
 
761
		// Children slides
762
		// TODO
5074 amit.gupta 763
 
2171 rajveer 764
		return StringUtils.join(xmlSnippet, "\n");
765
	}
766
 
767
}