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