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