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
 
81 naveen 6
import in.shop2020.metamodel.core.Bullet;
63 naveen 7
import in.shop2020.metamodel.core.Entity;
8
import in.shop2020.metamodel.core.Feature;
81 naveen 9
import in.shop2020.metamodel.core.PrimitiveDataObject;
63 naveen 10
import in.shop2020.metamodel.core.Slide;
11
import in.shop2020.metamodel.definitions.Catalog;
64 naveen 12
import in.shop2020.metamodel.definitions.Category;
63 naveen 13
import in.shop2020.metamodel.definitions.DefinitionsContainer;
14
import in.shop2020.metamodel.definitions.EntityContainer;
15
import in.shop2020.metamodel.definitions.FeatureDefinition;
16
import in.shop2020.metamodel.definitions.SlideDefinition;
67 naveen 17
import in.shop2020.metamodel.util.ExpandedBullet;
63 naveen 18
import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;
64 naveen 19
import in.shop2020.metamodel.util.ExpandedEntity;
81 naveen 20
import in.shop2020.metamodel.util.ExpandedFacetRuleDefinition;
65 naveen 21
import in.shop2020.metamodel.util.ExpandedFeature;
22
import in.shop2020.metamodel.util.ExpandedSlide;
63 naveen 23
 
81 naveen 24
import java.util.ArrayList;
25
import java.util.List;
26
 
27
import org.apache.commons.lang.ArrayUtils;
28
import org.apache.commons.lang.StringEscapeUtils;
29
import org.apache.commons.lang.StringUtils;
30
 
62 naveen 31
/**
32
 * Command line utility to convert IR Definitions into IR Data and IR Meta-data
33
 * 
81 naveen 34
 * Usage: IR [irmetadata|irdata] {Category ID}
35
 * 
62 naveen 36
 * @author naveen
37
 *
38
 */
39
public class IR {
63 naveen 40
 
62 naveen 41
	/**
64 naveen 42
	 * 
43
	 */
63 naveen 44
	private long categoryID;
45
 
46
	/**
64 naveen 47
	 * Level - 4, 8, 12, 16
48
	 */
49
	private String[] xmlIndentation = {"", "    ", "        ", "            ", 
50
			"                "};
51
 
52
	/**
62 naveen 53
	 * @param args
63 naveen 54
	 * @throws Exception 
62 naveen 55
	 */
63 naveen 56
	public static void main(String[] args) throws Exception {
81 naveen 57
		String[] commands = new String[] {"irmetadata", "irdata"};
62 naveen 58
 
81 naveen 59
		String usage = "Usage: IR ["+ StringUtils.join(commands, "|") +
60
			"] {Category ID}\n";
61
 
62
		if(args.length < 1) {
63 naveen 63
			System.out.println(usage);
64
			System.exit(-1);
65
		}
81 naveen 66
 
67
		String inputCommand = args[0];
68
 
69
		if(!ArrayUtils.contains(commands, inputCommand)) {
70
			System.out.println(usage);
71
			System.exit(-1);
72
		}
64 naveen 73
 
74
		long categoryID = 0L;
81 naveen 75
		if(args.length > 1) {
64 naveen 76
			try {
81 naveen 77
				categoryID = Long.parseLong(args[1]);
64 naveen 78
			}
79
			catch (NumberFormatException nfe) {
80
				System.out.println(usage);
81
				System.exit(-1);
82
			}
63 naveen 83
		}
84
 
85
		IR ir = new IR(categoryID);
81 naveen 86
 
87
		if (inputCommand.equals("irdata")) {
88
			ir.exportIRData();
89
 
90
			return;
91
		}
92
 
93
		if (inputCommand.equals("irmetadata")) {
94
			ir.exportIRMetaData();
95
 
96
			return;
97
		}
98
 
62 naveen 99
	}
63 naveen 100
 
101
	/**
102
	 * 
103
	 * @param categoryID
104
	 */
105
	public IR(long categoryID) {
106
		this.categoryID = categoryID;
107
	}
108
 
109
	/**
81 naveen 110
	 * 
111
	 * @throws Exception
112
	 */
113
	public void exportIRMetaData() throws Exception {
114
		/*
115
		DefinitionsContainer defs = 
116
			Catalog.getInstance().getDefinitionsContainer();
117
 
118
		EntityContainer ents = 
119
			Catalog.getInstance().getEntityContainer();
120
		*/
121
 
122
		// <IRMetaData>
123
		List<String> entityXMLSnippets = new ArrayList<String>();
124
		entityXMLSnippets.add("<IRMetaData>");
125
 
126
		// Hard coded Brand facet
127
		// REVISIT - May be moving it into Python is better idea
128
		//String brandIRMetaData = this.getBrandIRMetaData();
129
		//entityXMLSnippets.add(brandIRMetaData);
130
 
131
		// Iterate over all facet definitions
132
		// TODO
133
 
134
		// Iterate over all feature definitions
135
		// TODO
136
 
137
		// </IRMetaData>
138
		entityXMLSnippets.add("</IRMetaData>");
139
 
140
		String irMetaDataXML = StringUtils.join(entityXMLSnippets, "\n");
141
		Utils.info(irMetaDataXML);
142
 
143
		// Write it to file
144
		String irMetaDataFilename = Utils.EXPORT_IR_PATH + "irmetadata.xml";
145
		DBUtils.store(irMetaDataXML, irMetaDataFilename);
146
	}
147
 
148
	/**
149
	 * 
150
	 * @return
63 naveen 151
	 * @throws Exception 
81 naveen 152
	 */
153
	private String getBrandIRMetaData() throws Exception {
154
		EntityContainer ents = 
155
			Catalog.getInstance().getEntityContainer();
156
 
157
		List<String> brandXMLSnippets = new ArrayList<String>();
158
 
159
		// <Facet>
160
		brandXMLSnippets.add("\t<Facet>");
161
 
162
		List<Bullet> brandBullets =  
163
			ents.getLearnedBullets(Utils.BRAND_FEATURE_DEFINITION_ID);
164
 
165
		brandXMLSnippets.add("\t\t<FacetDefinitionID>" + 
166
				Utils.BRAND_FACET_DEFINITION_ID + "</FacetDefinitionID>");
167
 
168
		brandXMLSnippets.add("\t\t<Label>Brand</Label>");
169
		brandXMLSnippets.add("\t\t<IsMultivalue>false</IsMultivalue>");
170
		brandXMLSnippets.add("\t\t<HierarchyType>Flat</HierarchyType>");
171
		brandXMLSnippets.add("\t\t<NullBehavior>Reject</NullBehavior>");
172
		brandXMLSnippets.add("\t\t<Datatype>string</Datatype>");
173
 
174
		brandXMLSnippets.add("\t\t<FacetValues>");
175
		for(Bullet bullet : brandBullets) {
176
			PrimitiveDataObject pDO = 
177
				(PrimitiveDataObject)bullet.getDataObject();
178
 
179
			brandXMLSnippets.add("\t\t\t<FacetValue>");
180
			brandXMLSnippets.add("\t\t\t\t<Value>" + pDO.getValue() + 
181
					"</Value>");
182
 
183
			brandXMLSnippets.add("\t\t\t</FacetValue>");
184
		}
185
 
186
		brandXMLSnippets.add("\t\t</FacetValues>");
187
 
188
		// </Facet>
189
		brandXMLSnippets.add("\t</Facet>");
190
 
191
		String brandIRMetaDataXML = StringUtils.join(brandXMLSnippets, "\n");
192
		Utils.info(brandIRMetaDataXML);
193
 
194
		return brandIRMetaDataXML;
195
	}
196
 
197
 
198
	/**
199
	 * @throws Exception 
63 naveen 200
	 * 
201
	 */
202
	public void exportIRData() throws Exception {
203
		DefinitionsContainer defs = 
204
			Catalog.getInstance().getDefinitionsContainer();
205
 
206
		EntityContainer ents = 
207
			Catalog.getInstance().getEntityContainer();
208
 
64 naveen 209
		// <IRData>
210
		List<String> entityXMLSnippets = new ArrayList<String>();
211
		entityXMLSnippets.add("<IRData>");
63 naveen 212
 
64 naveen 213
		List<Category> categories = null;
214
		if(this.categoryID != 0L) {
215
			categories = new ArrayList<Category>();
216
			categories.add(defs.getCategory(this.categoryID));
217
		}
218
		else {
219
 
220
			// Get all categories
221
			categories = defs.getChildrenCategories(10001);
222
		}
63 naveen 223
 
64 naveen 224
		for(Category cat : categories) {
225
			long catID = cat.getID();
226
 
227
			// Get all facets for the category
228
			ExpandedCategoryFacetDefinition expCategoryFacetDef = 
229
				defs.getExpandedCategoryFacetDefinition(catID);
230
 
81 naveen 231
			List<ExpandedFacetRuleDefinition> expFacetRuleDefs = 
232
				expCategoryFacetDef.getExpandedFacetRuleDefinitions();
64 naveen 233
 
234
			// Get all entities for the category
235
			List<Entity> entities = ents.getEntities(catID);
236
 
237
			if(entities == null) {
238
				continue;
239
			}
240
 
241
			// For each entity 
63 naveen 242
			for(Entity entity : entities) {
64 naveen 243
				ExpandedEntity expEntity = 
244
					ents.getExpandedEntity(entity.getID());
63 naveen 245
 
64 naveen 246
				List<String> facetXMLSnippets = new ArrayList<String>();
247
 
67 naveen 248
				// Collect FACETs
249
 
250
				List<Long> facetFeatureIDs = new ArrayList<Long>();
251
 
63 naveen 252
				// For each facet execute Rule
81 naveen 253
				for(ExpandedFacetRuleDefinition expFacetRuleDef : 
254
						expFacetRuleDefs) {
64 naveen 255
					String facetXMLSnip = 
81 naveen 256
						this.processFacet(expEntity, expFacetRuleDef);
63 naveen 257
 
64 naveen 258
					if(!facetXMLSnip.isEmpty()) {
259
						facetXMLSnippets.add(facetXMLSnip);
260
						Utils.info("facetXMLSnip=" + facetXMLSnip);
63 naveen 261
					}
67 naveen 262
 
263
					// Collect features already covered as Facet
81 naveen 264
					if(expFacetRuleDef.getFeatureDefinition() != null) {
67 naveen 265
						facetFeatureIDs.add(
81 naveen 266
								new Long(expFacetRuleDef.getFeatureDefinitionID()));
67 naveen 267
					}
63 naveen 268
				}
269
 
64 naveen 270
				String facetXMLSnippetsStr = 
271
					StringUtils.join(facetXMLSnippets, "\n");
272
 
273
				Utils.info("facetXMLSnippets=" + facetXMLSnippetsStr);
274
 
67 naveen 275
				// Collect PROPERTIES
276
				String propertiesXMLSnippetsStr = 
277
					this.getPropertiesXMLSnippet(expEntity, facetFeatureIDs, 2);
64 naveen 278
 
67 naveen 279
				Utils.info(propertiesXMLSnippetsStr);
280
 
281
				String entityXMLSnip = this.getEntityXMLSnippet(expEntity, 
282
						facetXMLSnippetsStr, propertiesXMLSnippetsStr, 1);
283
 
64 naveen 284
				Utils.info(entityXMLSnip);
285
 
286
				entityXMLSnippets.add(entityXMLSnip);
63 naveen 287
			}
288
		}
64 naveen 289
 
290
		// </IRData>
291
		entityXMLSnippets.add("</IRData>");
292
 
293
		String irDataXML = StringUtils.join(entityXMLSnippets, "\n");
294
		Utils.info(irDataXML);
295
 
296
		// Write it to file
70 naveen 297
		String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
64 naveen 298
		DBUtils.store(irDataXML, irDataFilename);
63 naveen 299
	}
64 naveen 300
 
301
	/**
302
	 * 
303
	 * @param expEntity
304
	 * @param expFacetDef
305
	 * @return
306
	 * @throws Exception 
307
	 */
308
	@SuppressWarnings("unchecked")
309
	private String processFacet(ExpandedEntity expEntity, 
81 naveen 310
			ExpandedFacetRuleDefinition expFacetRuleDef) throws Exception {
64 naveen 311
 
81 naveen 312
		Utils.info("expFacetRuleDef=" + expFacetRuleDef);
313
 
64 naveen 314
		EntityContainer ents = 
315
			Catalog.getInstance().getEntityContainer();
316
 
81 naveen 317
		IRDataJythonWrapper jw = new IRDataJythonWrapper();
64 naveen 318
 
319
		jw.setExpandedEntity(expEntity);
81 naveen 320
		jw.setExpandedFacetDefinition(expFacetRuleDef);
64 naveen 321
 
322
		// Set FeatureDefinition
323
		FeatureDefinition featureDef = 
81 naveen 324
			expFacetRuleDef.getFeatureDefinition();
325
 
64 naveen 326
		if(featureDef != null) {
327
			jw.setFeatureDefinition(featureDef);
328
 
329
			// Set Feature
330
			Utils.info("featureDef.getID()=" + featureDef.getID());
331
 
81 naveen 332
 
64 naveen 333
			Feature feature = 
334
				ents.getFeature(expEntity.getID(), featureDef.getID());
335
 
81 naveen 336
			// Can happen with un-referred features like Brand
337
			if(feature != null) {
338
				ExpandedFeature expFeature = new ExpandedFeature(feature);
339
 
340
				jw.setExpandedFeature(expFeature);
341
			}
64 naveen 342
		}
343
 
344
		// Set SlideDefinition
81 naveen 345
		SlideDefinition slideDef = expFacetRuleDef.getSlideDefinition();
64 naveen 346
		if(slideDef != null) {
347
			jw.setSlideDefinition(slideDef);
348
 
349
			// Set Slide
350
			Utils.info("slideDef.getID()=" + slideDef.getID());
351
 
352
			Slide slide = ents.getSlide(expEntity.getID(), slideDef.getID());
65 naveen 353
			ExpandedSlide expSlide = new ExpandedSlide(slide);
64 naveen 354
 
65 naveen 355
			jw.setExpandedSlide(expSlide);
64 naveen 356
		}
357
 
70 naveen 358
		// Execute Python script
64 naveen 359
		jw.execIRDataRule();
360
 
70 naveen 361
		// Parse returned Python list
64 naveen 362
		List<Object> values = (List<Object>)jw.getValues();
363
		Utils.info("values=" + values);
364
 
365
		String facetXMLSnip = "";
366
		if(values != null) {
367
 
368
			// Get IR Data XML snippet for this entity and facet
81 naveen 369
			facetXMLSnip = this.getFacetXMLSnippet(expFacetRuleDef, values, 2);
64 naveen 370
		}
371
		Utils.info(facetXMLSnip);
372
 
373
		return facetXMLSnip;
374
	}
67 naveen 375
 
376
	/**
377
	 * 
378
	 * @param expEntity
379
	 * @param facetFeatureIDs
380
	 * @param indent
381
	 * @return
382
	 */
383
	private String getPropertiesXMLSnippet(ExpandedEntity expEntity, 
384
			List<Long> facetFeatureIDs, int indent) {
385
 
386
		List<String> xmlSnippet = new ArrayList<String>();
387
 
388
		// Collect all free-form content here
389
		List<String> ffc = new ArrayList<String>();
390
 
391
		// Features
392
		List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
393
 
394
		for(ExpandedSlide expSlide : expSlides) {
395
			List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
396
 
397
			if(expSlide.getFreeformContent() != null) {
398
				ffc.add(expSlide.getFreeformContent().getContent());
399
			}
400
 
401
			if(expFeatures == null) {
402
				continue;
403
			}
404
 
405
			for(ExpandedFeature expFeature : expFeatures) {
406
				Long featureDefID = 
407
					new Long(expFeature.getFeatureDefinitionID());
408
 
409
				// FFC at feature level
410
				if(expFeature.getFreeformContent() != null) {
411
					ffc.add(expFeature.getFreeformContent().getContent());
412
				}
413
 
414
				// Exclude those who are already covered as facets
415
				if(facetFeatureIDs.contains(featureDefID)) {
416
					continue;
417
				}
418
 
419
				List<ExpandedBullet> expBullets = 
420
					expFeature.getExpandedBullets();
421
 
422
				if(expBullets == null) {
423
					continue;
424
				}
425
 
426
				//<Property Label="">
427
				xmlSnippet.add(this.xmlIndentation[indent] + 
428
						"<Property Label=\"" + StringEscapeUtils.escapeXml(
429
								expFeature.getFeatureDefinition().getLabel()) + 
430
						"\">");
431
 
432
				//<Value></Value>
433
				for(ExpandedBullet bullet : expBullets) {
434
 
435
					// FFC at bullet level
436
					if(bullet.getFreeformContent() != null) {
437
						ffc.add(bullet.getFreeformContent().getContent());
438
					}
439
 
440
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
441
							StringEscapeUtils.escapeXml(bullet.getValue()) + 
442
							"</Value>");
443
				}
444
 
445
				//</Property>
446
				xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
447
			}
448
		}
449
 
450
		// FFC as Label="Free-form Content"
451
		if(!ffc.isEmpty()) {
452
			xmlSnippet.add(this.xmlIndentation[indent] + 
453
					"<Property Label=\"Free-form Content\">");
454
 
455
			for(String f : ffc) {
456
				if(f != null) {
457
					f = StringEscapeUtils.escapeXml(f);
458
 
459
					xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
460
						f + "</Value>");
461
				}
462
			}
463
 
464
			xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
465
		}
466
 
467
		// Children slides
468
		// TODO
469
 
470
		return StringUtils.join(xmlSnippet, "\n");
471
	}
64 naveen 472
 
473
	/**
474
	 * 
475
	 * @param expEntity
476
	 * @param facetXMLSnippets
477
	 * @param indent
478
	 * @return
479
	 */
480
	private String getEntityXMLSnippet(ExpandedEntity expEntity, 
67 naveen 481
			String facetXMLSnippets, String propertiesXMLSnippets, int indent) {
64 naveen 482
 
483
		//<Entity ID="40001">
484
		List<String> xmlSnippet = new ArrayList<String>();
485
 
486
		String entityID = new Long(expEntity.getID()).toString();
487
		xmlSnippet.add(this.xmlIndentation[indent] + "<Entity ID=\""+ entityID + 
488
				"\">");
489
 
490
		//<Category>Business Phones</Category>
491
		String category = expEntity.getCategory().getLabel();
492
		xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Category>" + 
67 naveen 493
				StringEscapeUtils.escapeXml(category) + "</Category>");
64 naveen 494
 
495
		//<Title>Nokia E71</Title>
67 naveen 496
		String title = StringEscapeUtils.escapeXml(expEntity.getBrand()) + " " +
497
		StringEscapeUtils.escapeXml(expEntity.getModelName()) + 
64 naveen 498
			((expEntity.getModelNumber() != null) ? 
67 naveen 499
					(" " + 
500
						StringEscapeUtils.escapeXml(expEntity.getModelNumber()))
64 naveen 501
					: "");
502
 
503
		xmlSnippet.add(this.xmlIndentation[indent + 1] + 
67 naveen 504
				"<Title>" + StringEscapeUtils.escapeXml(title) + "</Title>");
64 naveen 505
 
506
		xmlSnippet.add(facetXMLSnippets);
67 naveen 507
 
508
		xmlSnippet.add(propertiesXMLSnippets);
64 naveen 509
 
510
		//</Entity>
511
		xmlSnippet.add(this.xmlIndentation[indent] + "</Entity>");
512
 
513
		return StringUtils.join(xmlSnippet, "\n");
514
	}
515
 
516
	/**
517
	 * 
518
	 * @param expFacetDef
519
	 * @param values
520
	 * @param indent
521
	 * @return String XML Snippet
522
	 */
81 naveen 523
	private String getFacetXMLSnippet(
524
			ExpandedFacetRuleDefinition expFacetRuleDef, 
64 naveen 525
			List<Object> values, int indent) {
81 naveen 526
 
64 naveen 527
		// <Facet Label="Form Factor">
528
		List<String> xmlSnippet = new ArrayList<String>();
81 naveen 529
		String target = expFacetRuleDef.getFacetDefinition().getTarget();
530
 
64 naveen 531
		xmlSnippet.add(this.xmlIndentation[indent] + "<Facet Label=\"" + 
532
				target + "\">");
533
 
534
		//<Value>Candybar</Value>
535
		for(Object value : values) {
536
			xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" + 
537
					value.toString() + "</Value>");
538
		}
539
 
540
		//</Facet>
541
		xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");
542
 
543
		return StringUtils.join(xmlSnippet, "\n");
544
	}
62 naveen 545
}