| 62 |
naveen |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.util;
|
|
|
5 |
|
| 64 |
naveen |
6 |
import java.util.ArrayList;
|
| 63 |
naveen |
7 |
import java.util.List;
|
|
|
8 |
|
| 67 |
naveen |
9 |
import org.apache.commons.lang.StringEscapeUtils;
|
| 64 |
naveen |
10 |
import org.apache.commons.lang.StringUtils;
|
|
|
11 |
|
| 63 |
naveen |
12 |
import in.shop2020.metamodel.core.Entity;
|
|
|
13 |
import in.shop2020.metamodel.core.Feature;
|
|
|
14 |
import in.shop2020.metamodel.core.Slide;
|
|
|
15 |
import in.shop2020.metamodel.definitions.Catalog;
|
| 64 |
naveen |
16 |
import in.shop2020.metamodel.definitions.Category;
|
| 63 |
naveen |
17 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
|
|
18 |
import in.shop2020.metamodel.definitions.EntityContainer;
|
|
|
19 |
import in.shop2020.metamodel.definitions.FeatureDefinition;
|
|
|
20 |
import in.shop2020.metamodel.definitions.SlideDefinition;
|
| 67 |
naveen |
21 |
import in.shop2020.metamodel.util.ExpandedBullet;
|
| 63 |
naveen |
22 |
import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;
|
| 64 |
naveen |
23 |
import in.shop2020.metamodel.util.ExpandedEntity;
|
| 63 |
naveen |
24 |
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
|
| 65 |
naveen |
25 |
import in.shop2020.metamodel.util.ExpandedFeature;
|
|
|
26 |
import in.shop2020.metamodel.util.ExpandedSlide;
|
| 63 |
naveen |
27 |
|
| 62 |
naveen |
28 |
/**
|
|
|
29 |
* Command line utility to convert IR Definitions into IR Data and IR Meta-data
|
|
|
30 |
*
|
|
|
31 |
* @author naveen
|
|
|
32 |
*
|
|
|
33 |
*/
|
|
|
34 |
public class IR {
|
| 63 |
naveen |
35 |
|
| 62 |
naveen |
36 |
/**
|
| 64 |
naveen |
37 |
*
|
|
|
38 |
*/
|
| 63 |
naveen |
39 |
private long categoryID;
|
|
|
40 |
|
|
|
41 |
/**
|
| 64 |
naveen |
42 |
* Level - 4, 8, 12, 16
|
|
|
43 |
*/
|
|
|
44 |
private String[] xmlIndentation = {"", " ", " ", " ",
|
|
|
45 |
" "};
|
|
|
46 |
|
|
|
47 |
/**
|
| 62 |
naveen |
48 |
* @param args
|
| 63 |
naveen |
49 |
* @throws Exception
|
| 62 |
naveen |
50 |
*/
|
| 63 |
naveen |
51 |
public static void main(String[] args) throws Exception {
|
|
|
52 |
String usage = "Usage: IR {Category ID}";
|
| 62 |
naveen |
53 |
|
| 64 |
naveen |
54 |
if(args.length > 1) {
|
| 63 |
naveen |
55 |
System.out.println(usage);
|
|
|
56 |
System.exit(-1);
|
|
|
57 |
}
|
| 64 |
naveen |
58 |
|
|
|
59 |
long categoryID = 0L;
|
|
|
60 |
if(args.length > 0) {
|
|
|
61 |
try {
|
|
|
62 |
categoryID = Long.parseLong(args[0]);
|
|
|
63 |
}
|
|
|
64 |
catch (NumberFormatException nfe) {
|
|
|
65 |
System.out.println(usage);
|
|
|
66 |
System.exit(-1);
|
|
|
67 |
}
|
| 63 |
naveen |
68 |
}
|
|
|
69 |
|
|
|
70 |
IR ir = new IR(categoryID);
|
|
|
71 |
ir.exportIRData();
|
| 62 |
naveen |
72 |
}
|
| 63 |
naveen |
73 |
|
|
|
74 |
/**
|
|
|
75 |
*
|
|
|
76 |
* @param categoryID
|
|
|
77 |
*/
|
|
|
78 |
public IR(long categoryID) {
|
|
|
79 |
this.categoryID = categoryID;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @throws Exception
|
|
|
84 |
*
|
|
|
85 |
*/
|
|
|
86 |
public void exportIRData() throws Exception {
|
|
|
87 |
DefinitionsContainer defs =
|
|
|
88 |
Catalog.getInstance().getDefinitionsContainer();
|
|
|
89 |
|
|
|
90 |
EntityContainer ents =
|
|
|
91 |
Catalog.getInstance().getEntityContainer();
|
|
|
92 |
|
| 64 |
naveen |
93 |
// <IRData>
|
|
|
94 |
List<String> entityXMLSnippets = new ArrayList<String>();
|
|
|
95 |
entityXMLSnippets.add("<IRData>");
|
| 63 |
naveen |
96 |
|
| 64 |
naveen |
97 |
List<Category> categories = null;
|
|
|
98 |
if(this.categoryID != 0L) {
|
|
|
99 |
categories = new ArrayList<Category>();
|
|
|
100 |
categories.add(defs.getCategory(this.categoryID));
|
|
|
101 |
}
|
|
|
102 |
else {
|
|
|
103 |
|
|
|
104 |
// Get all categories
|
|
|
105 |
categories = defs.getChildrenCategories(10001);
|
|
|
106 |
}
|
| 63 |
naveen |
107 |
|
| 64 |
naveen |
108 |
for(Category cat : categories) {
|
|
|
109 |
long catID = cat.getID();
|
|
|
110 |
|
|
|
111 |
// Get all facets for the category
|
|
|
112 |
ExpandedCategoryFacetDefinition expCategoryFacetDef =
|
|
|
113 |
defs.getExpandedCategoryFacetDefinition(catID);
|
|
|
114 |
|
|
|
115 |
List<ExpandedFacetDefinition> expFacetDefs =
|
|
|
116 |
expCategoryFacetDef.getExpandedFacetDefinitions();
|
|
|
117 |
|
|
|
118 |
// Get all entities for the category
|
|
|
119 |
List<Entity> entities = ents.getEntities(catID);
|
|
|
120 |
|
|
|
121 |
if(entities == null) {
|
|
|
122 |
continue;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
// For each entity
|
| 63 |
naveen |
126 |
for(Entity entity : entities) {
|
| 64 |
naveen |
127 |
ExpandedEntity expEntity =
|
|
|
128 |
ents.getExpandedEntity(entity.getID());
|
| 63 |
naveen |
129 |
|
| 64 |
naveen |
130 |
List<String> facetXMLSnippets = new ArrayList<String>();
|
|
|
131 |
|
| 67 |
naveen |
132 |
// Collect FACETs
|
|
|
133 |
|
|
|
134 |
List<Long> facetFeatureIDs = new ArrayList<Long>();
|
|
|
135 |
|
| 63 |
naveen |
136 |
// For each facet execute Rule
|
|
|
137 |
for(ExpandedFacetDefinition expFacetDef : expFacetDefs) {
|
| 64 |
naveen |
138 |
String facetXMLSnip =
|
|
|
139 |
this.processFacet(expEntity, expFacetDef);
|
| 63 |
naveen |
140 |
|
| 64 |
naveen |
141 |
if(!facetXMLSnip.isEmpty()) {
|
|
|
142 |
facetXMLSnippets.add(facetXMLSnip);
|
|
|
143 |
Utils.info("facetXMLSnip=" + facetXMLSnip);
|
| 63 |
naveen |
144 |
}
|
| 67 |
naveen |
145 |
|
|
|
146 |
// Collect features already covered as Facet
|
|
|
147 |
if(expFacetDef.getFeatureDefinition() != null) {
|
|
|
148 |
facetFeatureIDs.add(
|
|
|
149 |
new Long(expFacetDef.getFeatureDefinitionID()));
|
|
|
150 |
}
|
| 63 |
naveen |
151 |
}
|
|
|
152 |
|
| 64 |
naveen |
153 |
String facetXMLSnippetsStr =
|
|
|
154 |
StringUtils.join(facetXMLSnippets, "\n");
|
|
|
155 |
|
|
|
156 |
Utils.info("facetXMLSnippets=" + facetXMLSnippetsStr);
|
|
|
157 |
|
| 67 |
naveen |
158 |
// Collect PROPERTIES
|
|
|
159 |
String propertiesXMLSnippetsStr =
|
|
|
160 |
this.getPropertiesXMLSnippet(expEntity, facetFeatureIDs, 2);
|
| 64 |
naveen |
161 |
|
| 67 |
naveen |
162 |
Utils.info(propertiesXMLSnippetsStr);
|
|
|
163 |
|
|
|
164 |
String entityXMLSnip = this.getEntityXMLSnippet(expEntity,
|
|
|
165 |
facetXMLSnippetsStr, propertiesXMLSnippetsStr, 1);
|
|
|
166 |
|
| 64 |
naveen |
167 |
Utils.info(entityXMLSnip);
|
|
|
168 |
|
|
|
169 |
entityXMLSnippets.add(entityXMLSnip);
|
| 63 |
naveen |
170 |
}
|
|
|
171 |
}
|
| 64 |
naveen |
172 |
|
|
|
173 |
// </IRData>
|
|
|
174 |
entityXMLSnippets.add("</IRData>");
|
|
|
175 |
|
|
|
176 |
String irDataXML = StringUtils.join(entityXMLSnippets, "\n");
|
|
|
177 |
Utils.info(irDataXML);
|
|
|
178 |
|
|
|
179 |
// Write it to file
|
| 70 |
naveen |
180 |
String irDataFilename = Utils.EXPORT_IR_PATH + "irdata.xml";
|
| 64 |
naveen |
181 |
DBUtils.store(irDataXML, irDataFilename);
|
| 63 |
naveen |
182 |
}
|
| 64 |
naveen |
183 |
|
|
|
184 |
/**
|
|
|
185 |
*
|
|
|
186 |
* @param expEntity
|
|
|
187 |
* @param expFacetDef
|
|
|
188 |
* @return
|
|
|
189 |
* @throws Exception
|
|
|
190 |
*/
|
|
|
191 |
@SuppressWarnings("unchecked")
|
|
|
192 |
private String processFacet(ExpandedEntity expEntity,
|
|
|
193 |
ExpandedFacetDefinition expFacetDef) throws Exception {
|
|
|
194 |
|
|
|
195 |
EntityContainer ents =
|
|
|
196 |
Catalog.getInstance().getEntityContainer();
|
|
|
197 |
|
| 70 |
naveen |
198 |
IRJythonWrapper jw = new IRJythonWrapper();
|
| 64 |
naveen |
199 |
|
|
|
200 |
jw.setExpandedEntity(expEntity);
|
|
|
201 |
jw.setExpandedFacetDefinition(expFacetDef);
|
|
|
202 |
|
|
|
203 |
// Set FeatureDefinition
|
|
|
204 |
FeatureDefinition featureDef =
|
|
|
205 |
expFacetDef.getFeatureDefinition();
|
|
|
206 |
if(featureDef != null) {
|
|
|
207 |
jw.setFeatureDefinition(featureDef);
|
|
|
208 |
|
|
|
209 |
// Set Feature
|
|
|
210 |
Utils.info("featureDef.getID()=" + featureDef.getID());
|
|
|
211 |
|
|
|
212 |
Feature feature =
|
|
|
213 |
ents.getFeature(expEntity.getID(), featureDef.getID());
|
|
|
214 |
|
| 65 |
naveen |
215 |
ExpandedFeature expFeature = new ExpandedFeature(feature);
|
|
|
216 |
|
|
|
217 |
jw.setExpandedFeature(expFeature);
|
| 64 |
naveen |
218 |
}
|
|
|
219 |
|
|
|
220 |
// Set SlideDefinition
|
|
|
221 |
SlideDefinition slideDef = expFacetDef.getSlideDefinition();
|
|
|
222 |
if(slideDef != null) {
|
|
|
223 |
jw.setSlideDefinition(slideDef);
|
|
|
224 |
|
|
|
225 |
// Set Slide
|
|
|
226 |
Utils.info("slideDef.getID()=" + slideDef.getID());
|
|
|
227 |
|
|
|
228 |
Slide slide = ents.getSlide(expEntity.getID(), slideDef.getID());
|
| 65 |
naveen |
229 |
ExpandedSlide expSlide = new ExpandedSlide(slide);
|
| 64 |
naveen |
230 |
|
| 65 |
naveen |
231 |
jw.setExpandedSlide(expSlide);
|
| 64 |
naveen |
232 |
}
|
|
|
233 |
|
| 70 |
naveen |
234 |
// Execute Python script
|
| 64 |
naveen |
235 |
jw.execIRDataRule();
|
|
|
236 |
|
| 70 |
naveen |
237 |
// Parse returned Python list
|
| 64 |
naveen |
238 |
List<Object> values = (List<Object>)jw.getValues();
|
|
|
239 |
Utils.info("values=" + values);
|
|
|
240 |
|
|
|
241 |
String facetXMLSnip = "";
|
|
|
242 |
if(values != null) {
|
|
|
243 |
|
|
|
244 |
// Get IR Data XML snippet for this entity and facet
|
|
|
245 |
facetXMLSnip = this.getFacetXMLSnippet(expFacetDef, values, 2);
|
|
|
246 |
}
|
|
|
247 |
Utils.info(facetXMLSnip);
|
|
|
248 |
|
|
|
249 |
return facetXMLSnip;
|
|
|
250 |
}
|
| 67 |
naveen |
251 |
|
|
|
252 |
/**
|
|
|
253 |
*
|
|
|
254 |
* @param expEntity
|
|
|
255 |
* @param facetFeatureIDs
|
|
|
256 |
* @param indent
|
|
|
257 |
* @return
|
|
|
258 |
*/
|
|
|
259 |
private String getPropertiesXMLSnippet(ExpandedEntity expEntity,
|
|
|
260 |
List<Long> facetFeatureIDs, int indent) {
|
|
|
261 |
|
|
|
262 |
List<String> xmlSnippet = new ArrayList<String>();
|
|
|
263 |
|
|
|
264 |
// Collect all free-form content here
|
|
|
265 |
List<String> ffc = new ArrayList<String>();
|
|
|
266 |
|
|
|
267 |
// Features
|
|
|
268 |
List<ExpandedSlide> expSlides = expEntity.getExpandedSlides();
|
|
|
269 |
|
|
|
270 |
for(ExpandedSlide expSlide : expSlides) {
|
|
|
271 |
List<ExpandedFeature> expFeatures = expSlide.getExpandedFeatures();
|
|
|
272 |
|
|
|
273 |
if(expSlide.getFreeformContent() != null) {
|
|
|
274 |
ffc.add(expSlide.getFreeformContent().getContent());
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
if(expFeatures == null) {
|
|
|
278 |
continue;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
for(ExpandedFeature expFeature : expFeatures) {
|
|
|
282 |
Long featureDefID =
|
|
|
283 |
new Long(expFeature.getFeatureDefinitionID());
|
|
|
284 |
|
|
|
285 |
// FFC at feature level
|
|
|
286 |
if(expFeature.getFreeformContent() != null) {
|
|
|
287 |
ffc.add(expFeature.getFreeformContent().getContent());
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
// Exclude those who are already covered as facets
|
|
|
291 |
if(facetFeatureIDs.contains(featureDefID)) {
|
|
|
292 |
continue;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
List<ExpandedBullet> expBullets =
|
|
|
296 |
expFeature.getExpandedBullets();
|
|
|
297 |
|
|
|
298 |
if(expBullets == null) {
|
|
|
299 |
continue;
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
//<Property Label="">
|
|
|
303 |
xmlSnippet.add(this.xmlIndentation[indent] +
|
|
|
304 |
"<Property Label=\"" + StringEscapeUtils.escapeXml(
|
|
|
305 |
expFeature.getFeatureDefinition().getLabel()) +
|
|
|
306 |
"\">");
|
|
|
307 |
|
|
|
308 |
//<Value></Value>
|
|
|
309 |
for(ExpandedBullet bullet : expBullets) {
|
|
|
310 |
|
|
|
311 |
// FFC at bullet level
|
|
|
312 |
if(bullet.getFreeformContent() != null) {
|
|
|
313 |
ffc.add(bullet.getFreeformContent().getContent());
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +
|
|
|
317 |
StringEscapeUtils.escapeXml(bullet.getValue()) +
|
|
|
318 |
"</Value>");
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
//</Property>
|
|
|
322 |
xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
// FFC as Label="Free-form Content"
|
|
|
327 |
if(!ffc.isEmpty()) {
|
|
|
328 |
xmlSnippet.add(this.xmlIndentation[indent] +
|
|
|
329 |
"<Property Label=\"Free-form Content\">");
|
|
|
330 |
|
|
|
331 |
for(String f : ffc) {
|
|
|
332 |
if(f != null) {
|
|
|
333 |
f = StringEscapeUtils.escapeXml(f);
|
|
|
334 |
|
|
|
335 |
xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +
|
|
|
336 |
f + "</Value>");
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
xmlSnippet.add(this.xmlIndentation[indent] + "</Property>");
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
// Children slides
|
|
|
344 |
// TODO
|
|
|
345 |
|
|
|
346 |
return StringUtils.join(xmlSnippet, "\n");
|
|
|
347 |
}
|
| 64 |
naveen |
348 |
|
|
|
349 |
/**
|
|
|
350 |
*
|
|
|
351 |
* @param expEntity
|
|
|
352 |
* @param facetXMLSnippets
|
|
|
353 |
* @param indent
|
|
|
354 |
* @return
|
|
|
355 |
*/
|
|
|
356 |
private String getEntityXMLSnippet(ExpandedEntity expEntity,
|
| 67 |
naveen |
357 |
String facetXMLSnippets, String propertiesXMLSnippets, int indent) {
|
| 64 |
naveen |
358 |
|
|
|
359 |
//<Entity ID="40001">
|
|
|
360 |
List<String> xmlSnippet = new ArrayList<String>();
|
|
|
361 |
|
|
|
362 |
String entityID = new Long(expEntity.getID()).toString();
|
|
|
363 |
xmlSnippet.add(this.xmlIndentation[indent] + "<Entity ID=\""+ entityID +
|
|
|
364 |
"\">");
|
|
|
365 |
|
|
|
366 |
//<Category>Business Phones</Category>
|
|
|
367 |
String category = expEntity.getCategory().getLabel();
|
|
|
368 |
xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Category>" +
|
| 67 |
naveen |
369 |
StringEscapeUtils.escapeXml(category) + "</Category>");
|
| 64 |
naveen |
370 |
|
|
|
371 |
//<Title>Nokia E71</Title>
|
| 67 |
naveen |
372 |
String title = StringEscapeUtils.escapeXml(expEntity.getBrand()) + " " +
|
|
|
373 |
StringEscapeUtils.escapeXml(expEntity.getModelName()) +
|
| 64 |
naveen |
374 |
((expEntity.getModelNumber() != null) ?
|
| 67 |
naveen |
375 |
(" " +
|
|
|
376 |
StringEscapeUtils.escapeXml(expEntity.getModelNumber()))
|
| 64 |
naveen |
377 |
: "");
|
|
|
378 |
|
|
|
379 |
xmlSnippet.add(this.xmlIndentation[indent + 1] +
|
| 67 |
naveen |
380 |
"<Title>" + StringEscapeUtils.escapeXml(title) + "</Title>");
|
| 64 |
naveen |
381 |
|
|
|
382 |
xmlSnippet.add(facetXMLSnippets);
|
| 67 |
naveen |
383 |
|
|
|
384 |
xmlSnippet.add(propertiesXMLSnippets);
|
| 64 |
naveen |
385 |
|
|
|
386 |
//</Entity>
|
|
|
387 |
xmlSnippet.add(this.xmlIndentation[indent] + "</Entity>");
|
|
|
388 |
|
|
|
389 |
return StringUtils.join(xmlSnippet, "\n");
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
/**
|
|
|
393 |
*
|
|
|
394 |
* @param expFacetDef
|
|
|
395 |
* @param values
|
|
|
396 |
* @param indent
|
|
|
397 |
* @return String XML Snippet
|
|
|
398 |
*/
|
|
|
399 |
private String getFacetXMLSnippet(ExpandedFacetDefinition expFacetDef,
|
|
|
400 |
List<Object> values, int indent) {
|
|
|
401 |
// <Facet Label="Form Factor">
|
|
|
402 |
List<String> xmlSnippet = new ArrayList<String>();
|
|
|
403 |
String target = expFacetDef.getTarget();
|
|
|
404 |
xmlSnippet.add(this.xmlIndentation[indent] + "<Facet Label=\"" +
|
|
|
405 |
target + "\">");
|
|
|
406 |
|
|
|
407 |
//<Value>Candybar</Value>
|
|
|
408 |
for(Object value : values) {
|
|
|
409 |
xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +
|
|
|
410 |
value.toString() + "</Value>");
|
|
|
411 |
}
|
|
|
412 |
|
|
|
413 |
//</Facet>
|
|
|
414 |
xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");
|
|
|
415 |
|
|
|
416 |
return StringUtils.join(xmlSnippet, "\n");
|
|
|
417 |
}
|
| 62 |
naveen |
418 |
}
|