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