Subversion Repositories SmartDukaan

Rev

Rev 70 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 70 Rev 81
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.util;
4
package in.shop2020.util;
5
 
5
 
6
import java.util.ArrayList;
-
 
7
import java.util.List;
-
 
8
 
-
 
9
import org.apache.commons.lang.StringEscapeUtils;
-
 
10
import org.apache.commons.lang.StringUtils;
6
import in.shop2020.metamodel.core.Bullet;
11
 
-
 
12
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.core.Entity;
13
import in.shop2020.metamodel.core.Feature;
8
import in.shop2020.metamodel.core.Feature;
-
 
9
import in.shop2020.metamodel.core.PrimitiveDataObject;
14
import in.shop2020.metamodel.core.Slide;
10
import in.shop2020.metamodel.core.Slide;
15
import in.shop2020.metamodel.definitions.Catalog;
11
import in.shop2020.metamodel.definitions.Catalog;
16
import in.shop2020.metamodel.definitions.Category;
12
import in.shop2020.metamodel.definitions.Category;
17
import in.shop2020.metamodel.definitions.DefinitionsContainer;
13
import in.shop2020.metamodel.definitions.DefinitionsContainer;
18
import in.shop2020.metamodel.definitions.EntityContainer;
14
import in.shop2020.metamodel.definitions.EntityContainer;
19
import in.shop2020.metamodel.definitions.FeatureDefinition;
15
import in.shop2020.metamodel.definitions.FeatureDefinition;
20
import in.shop2020.metamodel.definitions.SlideDefinition;
16
import in.shop2020.metamodel.definitions.SlideDefinition;
21
import in.shop2020.metamodel.util.ExpandedBullet;
17
import in.shop2020.metamodel.util.ExpandedBullet;
22
import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;
18
import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;
23
import in.shop2020.metamodel.util.ExpandedEntity;
19
import in.shop2020.metamodel.util.ExpandedEntity;
24
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
20
import in.shop2020.metamodel.util.ExpandedFacetRuleDefinition;
25
import in.shop2020.metamodel.util.ExpandedFeature;
21
import in.shop2020.metamodel.util.ExpandedFeature;
26
import in.shop2020.metamodel.util.ExpandedSlide;
22
import in.shop2020.metamodel.util.ExpandedSlide;
27
 
23
 
-
 
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
 
28
/**
31
/**
29
 * Command line utility to convert IR Definitions into IR Data and IR Meta-data
32
 * Command line utility to convert IR Definitions into IR Data and IR Meta-data
30
 * 
33
 * 
-
 
34
 * Usage: IR [irmetadata|irdata] {Category ID}
-
 
35
 * 
31
 * @author naveen
36
 * @author naveen
32
 *
37
 *
33
 */
38
 */
34
public class IR {
39
public class IR {
35
	
40
	
Line 47... Line 52...
47
	/**
52
	/**
48
	 * @param args
53
	 * @param args
49
	 * @throws Exception 
54
	 * @throws Exception 
50
	 */
55
	 */
51
	public static void main(String[] args) throws Exception {
56
	public static void main(String[] args) throws Exception {
52
		String usage = "Usage: IR {Category ID}";
57
		String[] commands = new String[] {"irmetadata", "irdata"};
53
		
58
		
-
 
59
		String usage = "Usage: IR ["+ StringUtils.join(commands, "|") +
-
 
60
			"] {Category ID}\n";
-
 
61
		
54
		if(args.length > 1) {
62
		if(args.length < 1) {
-
 
63
			System.out.println(usage);
-
 
64
			System.exit(-1);
-
 
65
		}
-
 
66
		
-
 
67
		String inputCommand = args[0];
-
 
68
		
-
 
69
		if(!ArrayUtils.contains(commands, inputCommand)) {
55
			System.out.println(usage);
70
			System.out.println(usage);
56
			System.exit(-1);
71
			System.exit(-1);
57
		}
72
		}
58
 
73
 
59
		long categoryID = 0L;
74
		long categoryID = 0L;
60
		if(args.length > 0) {
75
		if(args.length > 1) {
61
			try {
76
			try {
62
				categoryID = Long.parseLong(args[0]);
77
				categoryID = Long.parseLong(args[1]);
63
			}
78
			}
64
			catch (NumberFormatException nfe) {
79
			catch (NumberFormatException nfe) {
65
				System.out.println(usage);
80
				System.out.println(usage);
66
				System.exit(-1);
81
				System.exit(-1);
67
			}
82
			}
68
		}
83
		}
69
		
84
		
70
		IR ir = new IR(categoryID);
85
		IR ir = new IR(categoryID);
-
 
86
		
-
 
87
		if (inputCommand.equals("irdata")) {
71
		ir.exportIRData();
88
			ir.exportIRData();
-
 
89
			
-
 
90
			return;
-
 
91
		}
-
 
92
		
-
 
93
		if (inputCommand.equals("irmetadata")) {
-
 
94
			ir.exportIRMetaData();
-
 
95
			
-
 
96
			return;
-
 
97
		}
-
 
98
		
72
	}
99
	}
73
	
100
	
74
	/**
101
	/**
75
	 * 
102
	 * 
76
	 * @param categoryID
103
	 * @param categoryID
Line 78... Line 105...
78
	public IR(long categoryID) {
105
	public IR(long categoryID) {
79
		this.categoryID = categoryID;
106
		this.categoryID = categoryID;
80
	}
107
	}
81
	
108
	
82
	/**
109
	/**
-
 
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
-
 
151
	 * @throws Exception 
-
 
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
	/**
83
	 * @throws Exception 
199
	 * @throws Exception 
84
	 * 
200
	 * 
85
	 */
201
	 */
86
	public void exportIRData() throws Exception {
202
	public void exportIRData() throws Exception {
87
		DefinitionsContainer defs = 
203
		DefinitionsContainer defs = 
Line 110... Line 226...
110
			
226
			
111
			// Get all facets for the category
227
			// Get all facets for the category
112
			ExpandedCategoryFacetDefinition expCategoryFacetDef = 
228
			ExpandedCategoryFacetDefinition expCategoryFacetDef = 
113
				defs.getExpandedCategoryFacetDefinition(catID);
229
				defs.getExpandedCategoryFacetDefinition(catID);
114
			
230
			
115
			List<ExpandedFacetDefinition> expFacetDefs = 
231
			List<ExpandedFacetRuleDefinition> expFacetRuleDefs = 
116
				expCategoryFacetDef.getExpandedFacetDefinitions();
232
				expCategoryFacetDef.getExpandedFacetRuleDefinitions();
117
			
233
			
118
			// Get all entities for the category
234
			// Get all entities for the category
119
			List<Entity> entities = ents.getEntities(catID);
235
			List<Entity> entities = ents.getEntities(catID);
120
			
236
			
121
			if(entities == null) {
237
			if(entities == null) {
Line 132... Line 248...
132
				// Collect FACETs
248
				// Collect FACETs
133
				
249
				
134
				List<Long> facetFeatureIDs = new ArrayList<Long>();
250
				List<Long> facetFeatureIDs = new ArrayList<Long>();
135
				
251
				
136
				// For each facet execute Rule
252
				// For each facet execute Rule
137
				for(ExpandedFacetDefinition expFacetDef : expFacetDefs) {
253
				for(ExpandedFacetRuleDefinition expFacetRuleDef : 
-
 
254
						expFacetRuleDefs) {
138
					String facetXMLSnip = 
255
					String facetXMLSnip = 
139
						this.processFacet(expEntity, expFacetDef);
256
						this.processFacet(expEntity, expFacetRuleDef);
140
					
257
					
141
					if(!facetXMLSnip.isEmpty()) {
258
					if(!facetXMLSnip.isEmpty()) {
142
						facetXMLSnippets.add(facetXMLSnip);
259
						facetXMLSnippets.add(facetXMLSnip);
143
						Utils.info("facetXMLSnip=" + facetXMLSnip);
260
						Utils.info("facetXMLSnip=" + facetXMLSnip);
144
					}
261
					}
145
					
262
					
146
					// Collect features already covered as Facet
263
					// Collect features already covered as Facet
147
					if(expFacetDef.getFeatureDefinition() != null) {
264
					if(expFacetRuleDef.getFeatureDefinition() != null) {
148
						facetFeatureIDs.add(
265
						facetFeatureIDs.add(
149
								new Long(expFacetDef.getFeatureDefinitionID()));
266
								new Long(expFacetRuleDef.getFeatureDefinitionID()));
150
					}
267
					}
151
				}
268
				}
152
				
269
				
153
				String facetXMLSnippetsStr = 
270
				String facetXMLSnippetsStr = 
154
					StringUtils.join(facetXMLSnippets, "\n");
271
					StringUtils.join(facetXMLSnippets, "\n");
Line 188... Line 305...
188
	 * @return
305
	 * @return
189
	 * @throws Exception 
306
	 * @throws Exception 
190
	 */
307
	 */
191
	@SuppressWarnings("unchecked")
308
	@SuppressWarnings("unchecked")
192
	private String processFacet(ExpandedEntity expEntity, 
309
	private String processFacet(ExpandedEntity expEntity, 
193
			ExpandedFacetDefinition expFacetDef) throws Exception {
310
			ExpandedFacetRuleDefinition expFacetRuleDef) throws Exception {
-
 
311
		
-
 
312
		Utils.info("expFacetRuleDef=" + expFacetRuleDef);
194
		
313
		
195
		EntityContainer ents = 
314
		EntityContainer ents = 
196
			Catalog.getInstance().getEntityContainer();
315
			Catalog.getInstance().getEntityContainer();
197
		
316
		
198
		IRJythonWrapper jw = new IRJythonWrapper();
317
		IRDataJythonWrapper jw = new IRDataJythonWrapper();
199
		
318
		
200
		jw.setExpandedEntity(expEntity);
319
		jw.setExpandedEntity(expEntity);
201
		jw.setExpandedFacetDefinition(expFacetDef);
320
		jw.setExpandedFacetDefinition(expFacetRuleDef);
202
		
321
		
203
		// Set FeatureDefinition
322
		// Set FeatureDefinition
204
		FeatureDefinition featureDef = 
323
		FeatureDefinition featureDef = 
205
			expFacetDef.getFeatureDefinition();
324
			expFacetRuleDef.getFeatureDefinition();
-
 
325
		
206
		if(featureDef != null) {
326
		if(featureDef != null) {
207
			jw.setFeatureDefinition(featureDef);
327
			jw.setFeatureDefinition(featureDef);
208
			
328
			
209
			// Set Feature
329
			// Set Feature
210
			Utils.info("featureDef.getID()=" + featureDef.getID());
330
			Utils.info("featureDef.getID()=" + featureDef.getID());
211
			
331
			
-
 
332
			
212
			Feature feature = 
333
			Feature feature = 
213
				ents.getFeature(expEntity.getID(), featureDef.getID());
334
				ents.getFeature(expEntity.getID(), featureDef.getID());
214
			
335
			
-
 
336
			// Can happen with un-referred features like Brand
-
 
337
			if(feature != null) {
215
			ExpandedFeature expFeature = new ExpandedFeature(feature);
338
				ExpandedFeature expFeature = new ExpandedFeature(feature);
216
			
339
				
217
			jw.setExpandedFeature(expFeature);
340
				jw.setExpandedFeature(expFeature);
-
 
341
			}
218
		}
342
		}
219
		
343
		
220
		// Set SlideDefinition
344
		// Set SlideDefinition
221
		SlideDefinition slideDef = expFacetDef.getSlideDefinition();
345
		SlideDefinition slideDef = expFacetRuleDef.getSlideDefinition();
222
		if(slideDef != null) {
346
		if(slideDef != null) {
223
			jw.setSlideDefinition(slideDef);
347
			jw.setSlideDefinition(slideDef);
224
			
348
			
225
			// Set Slide
349
			// Set Slide
226
			Utils.info("slideDef.getID()=" + slideDef.getID());
350
			Utils.info("slideDef.getID()=" + slideDef.getID());
Line 240... Line 364...
240
		
364
		
241
		String facetXMLSnip = "";
365
		String facetXMLSnip = "";
242
		if(values != null) {
366
		if(values != null) {
243
			
367
			
244
			// Get IR Data XML snippet for this entity and facet
368
			// Get IR Data XML snippet for this entity and facet
245
			facetXMLSnip = this.getFacetXMLSnippet(expFacetDef, values, 2);
369
			facetXMLSnip = this.getFacetXMLSnippet(expFacetRuleDef, values, 2);
246
		}
370
		}
247
		Utils.info(facetXMLSnip);
371
		Utils.info(facetXMLSnip);
248
		
372
		
249
		return facetXMLSnip;
373
		return facetXMLSnip;
250
	}
374
	}
Line 394... Line 518...
394
	 * @param expFacetDef
518
	 * @param expFacetDef
395
	 * @param values
519
	 * @param values
396
	 * @param indent
520
	 * @param indent
397
	 * @return String XML Snippet
521
	 * @return String XML Snippet
398
	 */
522
	 */
399
	private String getFacetXMLSnippet(ExpandedFacetDefinition expFacetDef, 
523
	private String getFacetXMLSnippet(
-
 
524
			ExpandedFacetRuleDefinition expFacetRuleDef, 
400
			List<Object> values, int indent) {
525
			List<Object> values, int indent) {
-
 
526
		
401
		// <Facet Label="Form Factor">
527
		// <Facet Label="Form Factor">
402
		List<String> xmlSnippet = new ArrayList<String>();
528
		List<String> xmlSnippet = new ArrayList<String>();
403
		String target = expFacetDef.getTarget();
529
		String target = expFacetRuleDef.getFacetDefinition().getTarget();
-
 
530
		
404
		xmlSnippet.add(this.xmlIndentation[indent] + "<Facet Label=\"" + 
531
		xmlSnippet.add(this.xmlIndentation[indent] + "<Facet Label=\"" + 
405
				target + "\">");
532
				target + "\">");
406
		
533
		
407
		//<Value>Candybar</Value>
534
		//<Value>Candybar</Value>
408
		for(Object value : values) {
535
		for(Object value : values) {