Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
62 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.util;
5
 
102 naveen 6
import in.shop2020.metamodel.core.Bullet;
63 naveen 7
import in.shop2020.metamodel.core.Entity;
8
import in.shop2020.metamodel.core.Feature;
102 naveen 9
import in.shop2020.metamodel.core.PrimitiveDataObject;
63 naveen 10
import in.shop2020.metamodel.core.Slide;
102 naveen 11
import in.shop2020.metamodel.definitions.BulletDefinition;
63 naveen 12
import in.shop2020.metamodel.definitions.Catalog;
64 naveen 13
import in.shop2020.metamodel.definitions.Category;
83 naveen 14
import in.shop2020.metamodel.definitions.DatatypeDefinition;
63 naveen 15
import in.shop2020.metamodel.definitions.DefinitionsContainer;
16
import in.shop2020.metamodel.definitions.EntityContainer;
82 naveen 17
import in.shop2020.metamodel.definitions.FacetDefinition;
88 naveen 18
import in.shop2020.metamodel.definitions.FacetRuleDefinition;
63 naveen 19
import in.shop2020.metamodel.definitions.FeatureDefinition;
20
import in.shop2020.metamodel.definitions.SlideDefinition;
67 naveen 21
import in.shop2020.metamodel.util.ExpandedBullet;
83 naveen 22
import in.shop2020.metamodel.util.ExpandedBulletDefinition;
63 naveen 23
import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;
64 naveen 24
import in.shop2020.metamodel.util.ExpandedEntity;
82 naveen 25
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
81 naveen 26
import in.shop2020.metamodel.util.ExpandedFacetRuleDefinition;
65 naveen 27
import in.shop2020.metamodel.util.ExpandedFeature;
83 naveen 28
import in.shop2020.metamodel.util.ExpandedFeatureDefinition;
65 naveen 29
import in.shop2020.metamodel.util.ExpandedSlide;
451 rajveer 30
import in.shop2020.model.v1.catalog.CatalogService.Client;
31
import in.shop2020.model.v1.catalog.Item;
32
import in.shop2020.thrift.clients.CatalogServiceClient;
63 naveen 33
 
341 rajveer 34
import java.io.File;
35
import java.io.FileOutputStream;
81 naveen 36
import java.util.ArrayList;
91 naveen 37
import java.util.HashMap;
81 naveen 38
import java.util.List;
82 naveen 39
import java.util.Map;
451 rajveer 40
import java.util.Map.Entry;
81 naveen 41
 
341 rajveer 42
import javax.xml.transform.TransformerConfigurationException;
43
import javax.xml.transform.TransformerFactory;
44
import javax.xml.transform.Transformer;
45
import javax.xml.transform.stream.StreamResult;
46
import javax.xml.transform.stream.StreamSource;
47
 
81 naveen 48
import org.apache.commons.lang.ArrayUtils;
49
import org.apache.commons.lang.StringEscapeUtils;
50
import org.apache.commons.lang.StringUtils;
51
 
341 rajveer 52
 
62 naveen 53
/**
54
 * Command line utility to convert IR Definitions into IR Data and IR Meta-data
55
 * 
81 naveen 56
 * Usage: IR [irmetadata|irdata] {Category ID}
57
 * 
62 naveen 58
 * @author naveen
59
 *
60
 */
61
public class IR {
63 naveen 62
 
62 naveen 63
	/**
64 naveen 64
	 * 
65
	 */
63 naveen 66
	private long categoryID;
67
 
68
	/**
64 naveen 69
	 * Level - 4, 8, 12, 16
70
	 */
71
	private String[] xmlIndentation = {"", "    ", "        ", "            ", 
72
			"                "};
83 naveen 73
 
74
	private String[] xmlTabIndentation = {"", "\t", "\t\t", "\t\t\t", 
75
			"\t\t\t\t", "\t\t\t\t\t", "\t\t\t\t\t\t"};
91 naveen 76
 
77
	private Map<Long, List<String>> facetIDFacetValues = 
78
		new HashMap<Long, List<String>>();
64 naveen 79
 
80
	/**
62 naveen 81
	 * @param args
63 naveen 82
	 * @throws Exception 
62 naveen 83
	 */
63 naveen 84
	public static void main(String[] args) throws Exception {
81 naveen 85
		String[] commands = new String[] {"irmetadata", "irdata"};
62 naveen 86
 
81 naveen 87
		String usage = "Usage: IR ["+ StringUtils.join(commands, "|") +
88
			"] {Category ID}\n";
89
 
90
		if(args.length < 1) {
63 naveen 91
			System.out.println(usage);
92
			System.exit(-1);
93
		}
81 naveen 94
 
95
		String inputCommand = args[0];
96
 
97
		if(!ArrayUtils.contains(commands, inputCommand)) {
98
			System.out.println(usage);
99
			System.exit(-1);
100
		}
64 naveen 101
 
102
		long categoryID = 0L;
81 naveen 103
		if(args.length > 1) {
64 naveen 104
			try {
81 naveen 105
				categoryID = Long.parseLong(args[1]);
64 naveen 106
			}
107
			catch (NumberFormatException nfe) {
108
				System.out.println(usage);
109
				System.exit(-1);
110
			}
63 naveen 111
		}
112
 
113
		IR ir = new IR(categoryID);
81 naveen 114
 
115
		if (inputCommand.equals("irdata")) {
116
			ir.exportIRData();
341 rajveer 117
			ir.transformIrDataXMLtoSolrXML();
81 naveen 118
			return;
119
		}
120
 
121
		if (inputCommand.equals("irmetadata")) {
122
			ir.exportIRMetaData();
341 rajveer 123
			ir.transformIrMetaDataXMLSolrSchemaXML();
81 naveen 124
			return;
125
		}
126
 
62 naveen 127
	}
63 naveen 128
 
129
	/**
130
	 * 
131
	 * @param categoryID
132
	 */
133
	public IR(long categoryID) {
134
		this.categoryID = categoryID;
135
	}
136
 
341 rajveer 137
 
63 naveen 138
	/**
81 naveen 139
	 * 
341 rajveer 140
	 * @param inXMLFilename
141
	 * @param xslFilename
142
	 * @param outXMLFilename
143
	 * @throws Exception 
144
	 */
145
 
146
	public void xsltTransformation(String inXMLFilename, String xslFilename, String outXMLFilename) throws Exception{
147
		// Use the static TransformerFactory.newInstance() method to instantiate 
148
		  // a TransformerFactory. The javax.xml.transform.TransformerFactory 
149
		  // system property setting determines the actual class to instantiate --
150
		  // org.apache.xalan.transformer.TransformerImpl.
151
		TransformerFactory tFactory = TransformerFactory.newInstance();
152
 
153
			// Use the TransformerFactory to instantiate a Transformer that will work with  
154
			// the stylesheet you specify. This method call also processes the stylesheet
155
		  // into a compiled Templates object.
156
		Transformer transformer = tFactory.newTransformer(new StreamSource(xslFilename));
157
 
158
			// Use the Transformer to apply the associated Templates object to an XML document
159
			// (foo.xml) and write the output to a file (foo.out).
160
		transformer.transform(new StreamSource(inXMLFilename), new StreamResult(new FileOutputStream(outXMLFilename)));
161
 
162
	}
163
 
164
	/**
165
	 * 
81 naveen 166
	 * @throws Exception
167
	 */
341 rajveer 168
	public void transformIrDataXMLtoSolrXML() throws Exception {
169
		String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
170
		String irSolrDataFilename = Utils.EXPORT_SOLR_PATH + "irdata_solr.xml";
171
		String irXslFilename = "src/xsl/irdata_solrdata.xsl";
172
		System.out.println(irSolrDataFilename);
173
		File solrFile = new File(irSolrDataFilename);
174
		if(!solrFile.exists()){
175
			solrFile.createNewFile();
176
		}
177
		xsltTransformation(irDataFilename, irXslFilename, irSolrDataFilename);
178
	}
179
 
180
 
181
	/**
182
	 * 
183
	 * @throws Exception
184
	 */
185
	public void transformIrMetaDataXMLSolrSchemaXML() throws Exception {
186
		String irDataFilename = Utils.EXPORT_IR_PATH + "irmetadata.xml";
187
		String irSolrDataFilename = Utils.EXPORT_SOLR_PATH + "irmetadata_solrschema.xml";
188
		String irXslFilename = "src/xsl/irmetadata_solrschema.xsl";
189
		System.out.println(irSolrDataFilename);
190
		File solrFile = new File(irSolrDataFilename);
191
		if(!solrFile.exists()){
192
			solrFile.createNewFile();
193
		}
194
		xsltTransformation(irDataFilename, irXslFilename, irSolrDataFilename);
195
	}
196
 
197
 
198
 
199
	/**
200
	 * 
201
	 * @throws Exception
202
	 */
81 naveen 203
	public void exportIRMetaData() throws Exception {
204
		DefinitionsContainer defs = 
205
			Catalog.getInstance().getDefinitionsContainer();
82 naveen 206
 
81 naveen 207
		// <IRMetaData>
82 naveen 208
		List<String> xmlSnippets = new ArrayList<String>();
209
		xmlSnippets.add("<IRMetaData>");
83 naveen 210
		xmlSnippets.add("\t<Facets>");
81 naveen 211
 
82 naveen 212
		IRMetaDataJythonWrapper jy = new IRMetaDataJythonWrapper();
81 naveen 213
 
83 naveen 214
		List<Long> facetFeatures = new ArrayList<Long>();
215
 
81 naveen 216
		// Iterate over all facet definitions
82 naveen 217
		Map<Long, FacetDefinition> facetDefs = defs.getFacetDefinitions();
218
		for(FacetDefinition facetDef : facetDefs.values()) {
219
 
220
			jy.reset();
221
			jy.initialize();
222
 
223
			ExpandedFacetDefinition expFacetDef = 
224
				new ExpandedFacetDefinition(facetDef);
225
 
226
			jy.setExpandedFacetDefinition(expFacetDef);
227
 
228
			jy.executeRule();
229
 
230
			String facetXMLSnip = jy.getXMLSnippet();
231
			Utils.info("facetXMLSnip=" + facetXMLSnip);
232
 
233
			xmlSnippets.add(facetXMLSnip);
83 naveen 234
 
235
			facetFeatures.add(new Long(expFacetDef.getFeatureDefinitionID()));
82 naveen 236
		}
83 naveen 237
		xmlSnippets.add("\t</Facets>");
81 naveen 238
 
83 naveen 239
		Utils.info("facetFeatures=" + facetFeatures);
240
 
241
		xmlSnippets.add("\n\t<Properties>");
242
 
81 naveen 243
		// Iterate over all feature definitions
83 naveen 244
		Map<Long, FeatureDefinition> featureDefs = defs.getFeatureDefinitions();
245
		for(FeatureDefinition featureDef : featureDefs.values()) {
246
			if(facetFeatures.contains(new Long(featureDef.getID()))) {
247
				// Ignore, is already covered as facet
248
				continue;
249
			}
250
			String propertyXMLSnip = this.getPropertyXMLSnippet(featureDef);
251
			Utils.info("propertyXMLSnip=" + propertyXMLSnip);
252
 
253
			xmlSnippets.add(propertyXMLSnip);
254
		}
81 naveen 255
 
83 naveen 256
		xmlSnippets.add("\t</Properties>");
257
 
258
		xmlSnippets.add("\n\t<Categories>");
259
 
260
		// Iterate over all categories
261
		Category rootCategory = defs.getCategory(10000);
262
		List<Category> children = rootCategory.getChildrenCategory();
263
		for (Category child : children) {
264
			String categoryXMLSnip = this.getCategoryXMLSnippet(child, 2);
265
			Utils.info("categoryXMLSnip=" + categoryXMLSnip);
266
 
267
			xmlSnippets.add(categoryXMLSnip);
268
		}
269
		xmlSnippets.add("\t</Categories>");
270
 
81 naveen 271
		// </IRMetaData>
82 naveen 272
		xmlSnippets.add("</IRMetaData>");
81 naveen 273
 
82 naveen 274
		String irMetaDataXML = StringUtils.join(xmlSnippets, "\n");
81 naveen 275
		Utils.info(irMetaDataXML);
276
 
277
		// Write it to file
278
		String irMetaDataFilename = Utils.EXPORT_IR_PATH + "irmetadata.xml";
279
		DBUtils.store(irMetaDataXML, irMetaDataFilename);
280
	}
281
 
282
	/**
83 naveen 283
	 * 
284
	 * @param category
285
	 * @return
63 naveen 286
	 * @throws Exception 
83 naveen 287
	 */
288
	private String getCategoryXMLSnippet(Category category, int indent) 
289
		throws Exception {
290
 
291
		DefinitionsContainer defs = 
292
			Catalog.getInstance().getDefinitionsContainer();
293
 
294
		String xmlSnip = this.xmlTabIndentation[indent] + "<Category";
295
 
296
		xmlSnip += " ID=\"" + category.getID() + "\"";
297
		xmlSnip += " label=\"" + category.getLabel() + "\"";
298
		xmlSnip += ">\n";
299
 
300
		List<Category> children = category.getChildrenCategory();
301
 
302
		if(children != null) {
303
			for(Category child : children) {
304
				xmlSnip += this.getCategoryXMLSnippet(child, indent+1);
305
			}
306
		}
307
		else {
308
			// Only leaf category will have entities
309
			// Facet IDs
310
			xmlSnip += this.xmlTabIndentation[indent+1] + "<Facets>\n";
311
 
312
			List<Long> facetDefIDs = 
313
				defs.getFacetDefinitionIDs(category.getID());
314
			Utils.info("facetDefIDs=" + facetDefIDs);
315
 
316
			for(Long facetDefID : facetDefIDs) {
317
				xmlSnip += this.xmlTabIndentation[indent+2] + 
318
					"<FacetID>" + facetDefID + "</FacetID>\n";
319
			}
320
 
321
			xmlSnip += this.xmlTabIndentation[indent+1] + "</Facets>\n\n";
322
 
323
			// Feature IDs
324
			xmlSnip += this.xmlTabIndentation[indent+1] + "<Properties>\n";
325
 
326
			List<Long> featureDefIDs = 
327
				defs.getFeatureDefinitionIDs(category.getID());
328
			Utils.info("featureDefIDs=" + featureDefIDs);
329
 
330
			for(Long featureDefID : featureDefIDs) {
331
				xmlSnip += this.xmlTabIndentation[indent+2] + 
332
					"<FeatureID>" + featureDefID + "</FeatureID>\n";
333
			}
334
 
335
			xmlSnip += this.xmlTabIndentation[indent+1] + "</Properties>\n";
336
		}
337
 
338
		xmlSnip += this.xmlTabIndentation[indent] + "</Category>\n";
339
 
340
		return xmlSnip;
341
	}
342
 
343
	/**
63 naveen 344
	 * 
83 naveen 345
	 * @param featureDef
346
	 * @return
347
	 * @throws Exception 
63 naveen 348
	 */
83 naveen 349
	private String getPropertyXMLSnippet(FeatureDefinition featureDef) 
350
		throws Exception {
351
		String xmlSnip = "\t\t<Property";
352
 
353
		xmlSnip += " ID=\"" + featureDef.getID() + "\"";
354
		xmlSnip += " label=\"" + featureDef.getLabel() + "\"";
355
 
356
		ExpandedFeatureDefinition expFeatureDef = 
357
			new ExpandedFeatureDefinition(featureDef);
358
 
359
		ExpandedBulletDefinition expBulletDef = 
360
			expFeatureDef.getExpandedBulletDefinition();
361
 
362
		String datatype = "string";
363
		if(expBulletDef.isComposite() || expBulletDef.isEnumerated()) {
364
			datatype = "string";
365
		}
366
		else {
367
			DatatypeDefinition datatypeDef = 
368
				expBulletDef.getDatatypeDefinition();
369
 
370
			datatype = datatypeDef.getName();
371
 
372
			// REVISIT
373
			if(datatype.equals("hours_mins") || datatype.equals("days_hours") ||
374
					datatype.equals("hours_mins") || 
375
					datatype.equals("days_hours")) {
376
				datatype = "string";
377
			}
378
		}
379
		xmlSnip += " datatype=\"" + datatype + "\"";
380
 
381
		String multivalue = "false";
382
		if (expBulletDef.isMultivalue()) {
383
			multivalue = "true";
384
		}
385
		xmlSnip += " isMultivalue=\"" + multivalue + "\"";
386
 
387
		xmlSnip += "/>";
388
		return xmlSnip;
389
	}
390
 
391
	/**
392
	 * @throws Exception 
393
	 * 
394
	 */
63 naveen 395
	public void exportIRData() throws Exception {
396
		DefinitionsContainer defs = 
397
			Catalog.getInstance().getDefinitionsContainer();
398
 
399
		EntityContainer ents = 
400
			Catalog.getInstance().getEntityContainer();
401
 
64 naveen 402
		// <IRData>
403
		List<String> entityXMLSnippets = new ArrayList<String>();
404
		entityXMLSnippets.add("<IRData>");
63 naveen 405
 
64 naveen 406
		List<Category> categories = null;
407
		if(this.categoryID != 0L) {
408
			categories = new ArrayList<Category>();
409
			categories.add(defs.getCategory(this.categoryID));
410
		}
411
		else {
412
 
413
			// Get all categories
414
			categories = defs.getChildrenCategories(10001);
415
		}
63 naveen 416
 
64 naveen 417
		for(Category cat : categories) {
418
			long catID = cat.getID();
419
 
420
			// Get all facets for the category
421
			ExpandedCategoryFacetDefinition expCategoryFacetDef = 
422
				defs.getExpandedCategoryFacetDefinition(catID);
423
 
81 naveen 424
			List<ExpandedFacetRuleDefinition> expFacetRuleDefs = 
425
				expCategoryFacetDef.getExpandedFacetRuleDefinitions();
64 naveen 426
 
427
			// Get all entities for the category
428
			List<Entity> entities = ents.getEntities(catID);
429
 
430
			if(entities == null) {
431
				continue;
432
			}
433
 
434
			// For each entity 
63 naveen 435
			for(Entity entity : entities) {
64 naveen 436
				ExpandedEntity expEntity = 
437
					ents.getExpandedEntity(entity.getID());
346 rajveer 438
				//Skip if entity doesnt exist
439
				if(expEntity == null){
440
					continue;
441
				}
64 naveen 442
				List<String> facetXMLSnippets = new ArrayList<String>();
443
 
88 naveen 444
				// Collect features which have become FACETs
67 naveen 445
				List<Long> facetFeatureIDs = new ArrayList<Long>();
446
 
63 naveen 447
				// For each facet execute Rule
81 naveen 448
				for(ExpandedFacetRuleDefinition expFacetRuleDef : 
449
						expFacetRuleDefs) {
64 naveen 450
					String facetXMLSnip = 
81 naveen 451
						this.processFacet(expEntity, expFacetRuleDef);
63 naveen 452
 
88 naveen 453
					if(facetXMLSnip != null && 
454
							!StringUtils.trim(facetXMLSnip).isEmpty()) {
64 naveen 455
						facetXMLSnippets.add(facetXMLSnip);
88 naveen 456
						Utils.info("1 facetXMLSnip=" + facetXMLSnip);
63 naveen 457
					}
67 naveen 458
 
459
					// Collect features already covered as Facet
81 naveen 460
					if(expFacetRuleDef.getFeatureDefinition() != null) {
88 naveen 461
						facetFeatureIDs.add(new Long(
462
								expFacetRuleDef.getFeatureDefinitionID()));
67 naveen 463
					}
63 naveen 464
				}
465
 
88 naveen 466
				// Handle borrowed slides
467
				List<Slide> borrowedSlides = ents.getBorrowedSlides(entity);
468
 
469
				for (Slide borrowedSlide : borrowedSlides) {
470
					String facetXMLSnip = this.processBorrowedSlide(expEntity,
471
							borrowedSlide);
472
 
473
					if(facetXMLSnip != null && 
474
							!StringUtils.trim(facetXMLSnip).isEmpty()) {
475
						facetXMLSnippets.add(facetXMLSnip);
476
					}
477
 
478
					Utils.info("2 facetXMLSnip=" + facetXMLSnip);
479
				}
480
 
64 naveen 481
				String facetXMLSnippetsStr = 
482
					StringUtils.join(facetXMLSnippets, "\n");
483
 
88 naveen 484
				Utils.info("facetXMLSnippetsStr=" + facetXMLSnippetsStr);
64 naveen 485
 
67 naveen 486
				// Collect PROPERTIES
487
				String propertiesXMLSnippetsStr = 
488
					this.getPropertiesXMLSnippet(expEntity, facetFeatureIDs, 2);
64 naveen 489
 
67 naveen 490
				Utils.info(propertiesXMLSnippetsStr);
491
 
492
				String entityXMLSnip = this.getEntityXMLSnippet(expEntity, 
493
						facetXMLSnippetsStr, propertiesXMLSnippetsStr, 1);
494
 
64 naveen 495
				Utils.info(entityXMLSnip);
496
 
497
				entityXMLSnippets.add(entityXMLSnip);
63 naveen 498
			}
499
		}
64 naveen 500
 
501
		// </IRData>
502
		entityXMLSnippets.add("</IRData>");
503
 
504
		String irDataXML = StringUtils.join(entityXMLSnippets, "\n");
505
		Utils.info(irDataXML);
506
 
507
		// Write it to file
70 naveen 508
		String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
64 naveen 509
		DBUtils.store(irDataXML, irDataFilename);
91 naveen 510
 
511
		// Store facet values
457 rajveer 512
		String facetValuesFilename = Utils.CONTENT_DB_PATH + "entities" + File.separator + "facetvalues.ser";
91 naveen 513
 
514
		Utils.info("this.facetIDFacetValues=" + this.facetIDFacetValues);
515
		DBUtils.store(this.facetIDFacetValues, facetValuesFilename);
63 naveen 516
	}
64 naveen 517
 
518
	/**
519
	 * 
88 naveen 520
	 * @param borrowedSlides
521
	 * @return
522
	 */
523
	private String processBorrowedSlide(ExpandedEntity expEntity, 
524
			Slide borrowedSlide) throws Exception {
525
		// Borrowed category ID
526
		long borrowedCategoryID = borrowedSlide.getBorrowedCategoryID();
527
		Utils.info("borrowedCategoryID=" + borrowedCategoryID);
528
 
529
		// Slide definition ID
530
		long slideDefID = borrowedSlide.getSlideDefinitionID();
531
		Utils.info("borrowed slideDefID=" + slideDefID);
532
 
533
		DefinitionsContainer defs = 
534
			Catalog.getInstance().getDefinitionsContainer();
535
 
536
		// Get IR Data Rule
537
		FacetRuleDefinition facetRuleDef = 
538
			defs.getFacetRuleDefinitionForSlide(borrowedCategoryID, slideDefID);
539
 
540
		Utils.info("borrowed facetRuleDef=" + facetRuleDef);
541
		String facetXMLSnippet = null; 
542
 
543
		// If there is direct IR data rule defined for borrowed slide
544
		if(facetRuleDef != null) {
545
			ExpandedFacetRuleDefinition expFacetRuleDef = 
546
				new ExpandedFacetRuleDefinition(facetRuleDef);
547
 
548
			// Return XML snippet
549
			facetXMLSnippet = this.processFacet(expEntity, expFacetRuleDef);
550
		}
551
		else {
552
			List<String> facetXMLSnippets = new ArrayList<String>();
553
 
554
			// Get FacetRuleDefinition objects for all features in 
555
			// borrowed slide for which IR rule is defined
556
			List<Feature> features = borrowedSlide.getFeatures();
557
			for (Feature feature : features) {
558
				String featureFacetXMLSnippet = 
559
					this.processBorrowedFeature(borrowedCategoryID, expEntity, 
560
						feature);
561
 
562
				if(featureFacetXMLSnippet != null && !StringUtils.trim(
563
						featureFacetXMLSnippet).isEmpty()) {
564
					facetXMLSnippets.add(featureFacetXMLSnippet);
565
				}
566
			}
567
 
568
			// Get FacetRuleDefinition objects for all children slides in 
569
			// borrowed slide for which IR rule is defined
570
			if(borrowedSlide.hasChildrenSlides()) {
571
				String childrenSlidesFacetXMLSnippet = 
572
					this.processBorrowedChildrenSlides(borrowedCategoryID, 
573
							expEntity, borrowedSlide);
574
 
575
				if(childrenSlidesFacetXMLSnippet != null && !StringUtils.trim(
576
						childrenSlidesFacetXMLSnippet).isEmpty()) {
577
					facetXMLSnippets.add(childrenSlidesFacetXMLSnippet);
578
				}
579
			}
580
 
581
			facetXMLSnippet = StringUtils.join(facetXMLSnippets, "\n");
582
		}
583
 
584
		if(StringUtils.trim(facetXMLSnippet).isEmpty()) {
585
			return null;
586
		}
587
 
588
		return facetXMLSnippet;
589
	}
590
 
591
	/**
592
	 * 
593
	 * @param borrowedCategoryID
64 naveen 594
	 * @param expEntity
88 naveen 595
	 * @param feature
596
	 * @return
597
	 * @throws Exception 
598
	 */
599
	private String processBorrowedFeature(long borrowedCategoryID, 
600
			ExpandedEntity expEntity, Feature feature) throws Exception {
601
 
602
		long featureDefID = feature.getFeatureDefinitionID();
603
		Utils.info("borrowed featureDefID=" + featureDefID);
604
 
605
		DefinitionsContainer defs = 
606
			Catalog.getInstance().getDefinitionsContainer();
607
 
608
		FacetRuleDefinition facetRuleDefForFeature = 
609
			defs.getFacetRuleDefinitionForFeature(borrowedCategoryID, 
610
				featureDefID);
611
 
612
		List<String> facetXMLSnippets = new ArrayList<String>();
613
 
614
		Utils.info("borrowed facetRuleDefForFeature=" + 
615
				facetRuleDefForFeature);
616
 
617
		if(facetRuleDefForFeature != null) {
618
			ExpandedFacetRuleDefinition expFacetRuleDefForFeature = 
619
				new ExpandedFacetRuleDefinition(facetRuleDefForFeature);
620
 
621
			String snip = this.processFacet(expEntity, 
622
					expFacetRuleDefForFeature);
623
 
624
			if(snip != null) {
625
				facetXMLSnippets.add(snip);
626
			}
627
		}
628
 
629
 
630
		String xmlSnip =  StringUtils.join(facetXMLSnippets, "\n");
631
 
632
		if(StringUtils.trim(xmlSnip).isEmpty()) {
633
			return null;
634
		}
635
 
636
		return xmlSnip;
637
	}
638
 
639
	/**
640
	 * 
641
	 * @param expEntity
642
	 * @param borrowedSlide
643
	 * @return
644
	 * @throws Exception 
645
	 */
646
	private String processBorrowedChildrenSlides(long borrowedCategoryID, 
647
			ExpandedEntity expEntity, Slide borrowedSlide) throws Exception {
648
		DefinitionsContainer defs = 
649
			Catalog.getInstance().getDefinitionsContainer();
650
 
651
		List<Slide> childrenSlides = borrowedSlide.getChildrenSlides();
652
		List<String> facetXMLSnippets = new ArrayList<String>();
653
 
654
		for (Slide childSlide : childrenSlides) {
655
			long childSlideDefID = childSlide.getSlideDefinitionID();
656
			Utils.info("borrowed childSlideDefID=" + childSlideDefID);
657
 
658
			FacetRuleDefinition facetRuleDefForSlide = 
659
				defs.getFacetRuleDefinitionForSlide(borrowedCategoryID, 
660
						childSlideDefID);
661
 
662
			Utils.info("borrowed facetRuleDefForSlide=" + 
663
					facetRuleDefForSlide);
664
 
665
			if(facetRuleDefForSlide != null) {
666
				ExpandedFacetRuleDefinition expFacetRuleDefForSlide = 
667
					new ExpandedFacetRuleDefinition(
668
							facetRuleDefForSlide);
669
 
670
				String snip = this.processFacet(expEntity, 
671
						expFacetRuleDefForSlide);
672
 
673
				if(snip != null && !StringUtils.trim(snip).isEmpty()) {
674
					facetXMLSnippets.add(snip);
675
				}
676
			}
677
 
678
			// Features?
679
			if(childSlide.hasFeatures()) {
680
				List<Feature> features = childSlide.getFeatures();
681
				for(Feature feature : features) {
682
					String childrenSlideFeatureFacetXMLSnippet = 
683
						this.processBorrowedFeature(borrowedCategoryID, expEntity, 
684
							feature);
685
 
686
					if(childrenSlideFeatureFacetXMLSnippet != null &&
687
						!StringUtils.trim(childrenSlideFeatureFacetXMLSnippet).
688
							isEmpty()) {
689
 
690
						facetXMLSnippets.add(
691
								childrenSlideFeatureFacetXMLSnippet);
692
 
693
					}
694
				}
695
			}
696
 
697
			// Children slides
698
			if(childSlide.hasChildrenSlides()) {
699
				String childrenSlidesFacetXMLSnippet = 
700
					this.processBorrowedChildrenSlides(borrowedCategoryID, 
701
							expEntity, childSlide);
702
 
703
				if(childrenSlidesFacetXMLSnippet != null &&
704
					!StringUtils.trim(childrenSlidesFacetXMLSnippet).
705
						isEmpty()) {
706
 
707
					facetXMLSnippets.add(childrenSlidesFacetXMLSnippet);
708
				}
709
			}
710
		}
711
 
712
		String xmlSnip =  StringUtils.join(facetXMLSnippets, "\n");
713
 
714
		if(StringUtils.trim(xmlSnip).isEmpty()) {
715
			return null;
716
		}
717
 
718
		return xmlSnip;
719
	}
720
 
721
 
722
	/**
723
	 * 
724
	 * @param expEntity
64 naveen 725
	 * @param expFacetDef
726
	 * @return
727
	 * @throws Exception 
728
	 */
729
	@SuppressWarnings("unchecked")
730
	private String processFacet(ExpandedEntity expEntity, 
81 naveen 731
			ExpandedFacetRuleDefinition expFacetRuleDef) throws Exception {
64 naveen 732
 
81 naveen 733
		Utils.info("expFacetRuleDef=" + expFacetRuleDef);
734
 
451 rajveer 735
 
736
		// to get the price from services and pass it on to fill in solr
737
 
738
		CatalogServiceClient catalogServiceClient = new CatalogServiceClient();
739
		in.shop2020.model.v1.catalog.InventoryService.Client client = catalogServiceClient.getClient();
740
		double itemPrice = 0.0;
741
		Item item;
742
		if(expEntity.getID() == 1000003){
743
			item = client.getItemByCatalogId(expEntity.getID());
744
 
745
		Map<Long, Double> price = item.getPrice();
746
 
747
		for(Entry<Long, Double> e: price.entrySet()){
748
			itemPrice = e.getValue();
749
		}
750
		}
751
 
752
 
753
 
754
 
64 naveen 755
		EntityContainer ents = 
756
			Catalog.getInstance().getEntityContainer();
757
 
102 naveen 758
		DefinitionsContainer defs = 
759
			Catalog.getInstance().getDefinitionsContainer();
760
 
81 naveen 761
		IRDataJythonWrapper jw = new IRDataJythonWrapper();
64 naveen 762
 
763
		jw.setExpandedEntity(expEntity);
82 naveen 764
		jw.setExpandedFacetRuleDefinition(expFacetRuleDef);
451 rajveer 765
		jw.setEntityPrice(itemPrice);
64 naveen 766
 
767
		// Set FeatureDefinition
768
		FeatureDefinition featureDef = 
81 naveen 769
			expFacetRuleDef.getFeatureDefinition();
770
 
64 naveen 771
		if(featureDef != null) {
772
			jw.setFeatureDefinition(featureDef);
773
 
774
			// Set Feature
775
			Utils.info("featureDef.getID()=" + featureDef.getID());
776
 
777
			Feature feature = 
778
				ents.getFeature(expEntity.getID(), featureDef.getID());
779
 
90 naveen 780
			// Special case for Brand
781
			if(expFacetRuleDef.getFacetDefinitionID() == 50001) {
782
 
783
				// Execute Python script
784
				jw.executeRule();
785
 
786
			}
787
 
788
			// Can happen when to slide's feature which is dropped in favor 
789
			// of a borrowed slide
790
			else if(feature != null) {
102 naveen 791
 
792
				// Normalize
793
				if(defs.needsNormalization(feature.getFeatureDefinitionID())) {
794
					Utils.info("needsNormalization feature=" + feature);
795
 
796
					feature = this.normalize(feature);
797
				}
798
 
81 naveen 799
				ExpandedFeature expFeature = new ExpandedFeature(feature);
800
 
801
				jw.setExpandedFeature(expFeature);
88 naveen 802
 
803
				// Execute Python script
804
				jw.executeRule();
81 naveen 805
			}
64 naveen 806
		}
807
 
346 rajveer 808
 
809
 
810
 
64 naveen 811
		// Set SlideDefinition
81 naveen 812
		SlideDefinition slideDef = expFacetRuleDef.getSlideDefinition();
64 naveen 813
		if(slideDef != null) {
814
			jw.setSlideDefinition(slideDef);
815
 
816
			// Set Slide
817
			Utils.info("slideDef.getID()=" + slideDef.getID());
818
 
819
			Slide slide = ents.getSlide(expEntity.getID(), slideDef.getID());
88 naveen 820
 
821
			// Slide may not have been included infavor of a borrowed slide
822
			if(slide == null) {
823
				return null;
824
			}
825
 
65 naveen 826
			ExpandedSlide expSlide = new ExpandedSlide(slide);
64 naveen 827
 
65 naveen 828
			jw.setExpandedSlide(expSlide);
88 naveen 829
 
830
			// Execute Python script
831
			jw.executeRule();
64 naveen 832
		}
833
 
346 rajveer 834
		//
64 naveen 835
 
346 rajveer 836
		if(slideDef == null && featureDef == null){
837
			if(expFacetRuleDef.getIRDataRuleDefinition().getScript() != null){
838
				jw.executeRule();
839
			}
840
		}
841
 
842
 
64 naveen 843
		List<Object> values = (List<Object>)jw.getValues();
844
		Utils.info("values=" + values);
845
 
91 naveen 846
		// Append to facet values
847
		long facetDefID = expFacetRuleDef.getFacetDefinitionID();
848
		Utils.info("facetDefID=" + facetDefID);
849
 
850
		List<String> facetValues = this.facetIDFacetValues.get(
851
				new Long(facetDefID));
852
 
853
		if(facetValues == null) {
854
			facetValues = new ArrayList<String>();
855
			this.facetIDFacetValues.put(new Long(facetDefID), facetValues);
856
		}
857
 
858
		if(values != null) {
859
			for(Object value : values) {
860
				String strValue = value.toString();
861
 
862
				if(!facetValues.contains(strValue)) {
863
					facetValues.add(strValue);
864
				}
865
			}
866
		}
867
 
868
		// Parse returned Python list
88 naveen 869
		String facetXMLSnip = null;
64 naveen 870
		if(values != null) {
871
 
872
			// Get IR Data XML snippet for this entity and facet
81 naveen 873
			facetXMLSnip = this.getFacetXMLSnippet(expFacetRuleDef, values, 2);
64 naveen 874
		}
875
 
88 naveen 876
		Utils.info("0 facetXMLSnip=" + facetXMLSnip);
64 naveen 877
		return facetXMLSnip;
878
	}
102 naveen 879
 
880
	/**
881
	 * 
882
	 * @param feature
883
	 * @return Feature
884
	 * @throws Exception 
885
	 */
886
	private Feature normalize(Feature feature) throws Exception {
887
		ExpandedFeature expFeature = new ExpandedFeature(feature);
67 naveen 888
 
102 naveen 889
		BulletDefinition bulletDef = 
890
			expFeature.getFeatureDefinition().getBulletDefinition();
891
 
892
		ExpandedBulletDefinition expBulletDef = new ExpandedBulletDefinition(
893
				bulletDef);
894
 
895
		if(expBulletDef.isPrimitive()) {
896
			PrimitiveNormalizationJythonWrapper jy = 
897
				new PrimitiveNormalizationJythonWrapper();
898
 
899
			jy.setExpandedFeature(expFeature);
900
 
901
			jy.excuteRule();
902
 
903
			String newValue = jy.getNewValue();
904
			long newUnitID = jy.getNewUnitID();
905
 
906
			List<Bullet> newBullets = new ArrayList<Bullet>();
907
			Bullet newBullet = new Bullet(new PrimitiveDataObject(newValue));
908
			newBullet.setUnitID(newUnitID);
909
 
910
			newBullets.add(newBullet);
911
 
912
			feature.setBullets(newBullets);
913
		}
914
		else {
915
			Utils.severe("Normalization not defined for non-primitives");
916
		}
917
 
918
		return feature;
919
	}
920
 
67 naveen 921
	/**
922
	 * 
923
	 * @param expEntity
924
	 * @param facetFeatureIDs
925
	 * @param indent
926
	 * @return
927
	 */
928
	private String getPropertiesXMLSnippet(ExpandedEntity expEntity, 
929
			List<Long> facetFeatureIDs, int indent) {
930
 
931
		List<String> xmlSnippet = new ArrayList<String>();
932
 
933
		// Collect all free-form content here
934
		List<String> ffc = new ArrayList<String>();
935
 
936
		// Features
937
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
938
 
939
		for(ExpandedSlide expSlide : expSlides) {
940
			List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
941
 
942
			if(expSlide.getFreeformContent() != null) {
199 naveen 943
				ffc.add(expSlide.getFreeformContent().getFreeformText());
67 naveen 944
			}
945
 
946
			if(expFeatures == null) {
947
				continue;
948
			}
949
 
950
			for(ExpandedFeature expFeature : expFeatures) {
951
				Long featureDefID = 
952
					new Long(expFeature.getFeatureDefinitionID());
953
 
954
				// FFC at feature level
955
				if(expFeature.getFreeformContent() != null) {
199 naveen 956
					ffc.add(expFeature.getFreeformContent().getFreeformText());
67 naveen 957
				}
958
 
959
				// Exclude those who are already covered as facets
960
				if(facetFeatureIDs.contains(featureDefID)) {
961
					continue;
962
				}
963
 
964
				List<ExpandedBullet> expBullets = 
965
					expFeature.getExpandedBullets();
966
 
967
				if(expBullets == null) {
968
					continue;
969
				}
970
 
971
				//<Property Label="">
972
				xmlSnippet.add(this.xmlIndentation[indent] + 
83 naveen 973
						"<Property ID=\"" + expFeature.getFeatureDefinitionID() 
974
						+ "\" Label=\"" + StringEscapeUtils.escapeXml(
67 naveen 975
								expFeature.getFeatureDefinition().getLabel()) + 
976
						"\">");
977
 
978
				//<Value></Value>
979
				for(ExpandedBullet bullet : expBullets) {
980
 
981
					// FFC at bullet level
982
					if(bullet.getFreeformContent() != null) {
199 naveen 983
						ffc.add(bullet.getFreeformContent().getFreeformText());
67 naveen 984
					}
985
 
986
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
987
							StringEscapeUtils.escapeXml(bullet.getValue()) + 
988
							"</Value>");
989
				}
990
 
991
				//</Property>
992
				xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
993
			}
994
		}
995
 
996
		// FFC as Label="Free-form Content"
997
		if(!ffc.isEmpty()) {
998
			xmlSnippet.add(this.xmlIndentation[indent] + 
108 naveen 999
					"<Property ID=\"000000\" Label=\"Free-form Content\">");
67 naveen 1000
 
1001
			for(String f : ffc) {
1002
				if(f != null) {
1003
					f = StringEscapeUtils.escapeXml(f);
1004
 
1005
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
1006
						f + "</Value>");
1007
				}
1008
			}
1009
 
1010
			xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
1011
		}
1012
 
1013
		// Children slides
1014
		// TODO
1015
 
1016
		return StringUtils.join(xmlSnippet, "\n");
1017
	}
64 naveen 1018
 
1019
	/**
1020
	 * 
1021
	 * @param expEntity
1022
	 * @param facetXMLSnippets
1023
	 * @param indent
1024
	 * @return
1025
	 */
1026
	private String getEntityXMLSnippet(ExpandedEntity expEntity, 
67 naveen 1027
			String facetXMLSnippets, String propertiesXMLSnippets, int indent) {
64 naveen 1028
 
1029
		//<Entity ID="40001">
1030
		List<String> xmlSnippet = new ArrayList<String>();
1031
 
1032
		String entityID = new Long(expEntity.getID()).toString();
1033
		xmlSnippet.add(this.xmlIndentation[indent] + "<Entity ID=\""+ entityID + 
1034
				"\">");
1035
 
1036
		//<Category>Business Phones</Category>
1037
		String category = expEntity.getCategory().getLabel();
1038
		xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Category>" + 
67 naveen 1039
				StringEscapeUtils.escapeXml(category) + "</Category>");
64 naveen 1040
 
1041
		//<Title>Nokia E71</Title>
67 naveen 1042
		String title = StringEscapeUtils.escapeXml(expEntity.getBrand()) + " " +
1043
		StringEscapeUtils.escapeXml(expEntity.getModelName()) + 
64 naveen 1044
			((expEntity.getModelNumber() != null) ? 
67 naveen 1045
					(" " + 
1046
						StringEscapeUtils.escapeXml(expEntity.getModelNumber()))
64 naveen 1047
					: "");
1048
 
1049
		xmlSnippet.add(this.xmlIndentation[indent + 1] + 
67 naveen 1050
				"<Title>" + StringEscapeUtils.escapeXml(title) + "</Title>");
64 naveen 1051
 
1052
		xmlSnippet.add(facetXMLSnippets);
67 naveen 1053
 
1054
		xmlSnippet.add(propertiesXMLSnippets);
64 naveen 1055
 
1056
		//</Entity>
1057
		xmlSnippet.add(this.xmlIndentation[indent] + "</Entity>");
1058
 
1059
		return StringUtils.join(xmlSnippet, "\n");
1060
	}
1061
 
1062
	/**
1063
	 * 
1064
	 * @param expFacetDef
1065
	 * @param values
1066
	 * @param indent
1067
	 * @return String XML Snippet
1068
	 */
81 naveen 1069
	private String getFacetXMLSnippet(
1070
			ExpandedFacetRuleDefinition expFacetRuleDef, 
64 naveen 1071
			List<Object> values, int indent) {
81 naveen 1072
 
64 naveen 1073
		// <Facet Label="Form Factor">
1074
		List<String> xmlSnippet = new ArrayList<String>();
81 naveen 1075
		String target = expFacetRuleDef.getFacetDefinition().getTarget();
1076
 
83 naveen 1077
		xmlSnippet.add(this.xmlIndentation[indent] + "<Facet ID=\"" + 
1078
				expFacetRuleDef.getFacetDefinitionID() + "\" Label=\"" + 
64 naveen 1079
				target + "\">");
1080
 
1081
		//<Value>Candybar</Value>
1082
		for(Object value : values) {
1083
			xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
1084
					value.toString() + "</Value>");
1085
		}
1086
 
1087
		//</Facet>
1088
		xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");
1089
 
1090
		return StringUtils.join(xmlSnippet, "\n");
1091
	}
62 naveen 1092
}