| 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 |
|
| 64 |
naveen |
9 |
import org.apache.commons.lang.StringUtils;
|
|
|
10 |
|
| 63 |
naveen |
11 |
import in.shop2020.metamodel.core.Entity;
|
|
|
12 |
import in.shop2020.metamodel.core.Feature;
|
|
|
13 |
import in.shop2020.metamodel.core.Slide;
|
|
|
14 |
import in.shop2020.metamodel.definitions.Catalog;
|
| 64 |
naveen |
15 |
import in.shop2020.metamodel.definitions.Category;
|
| 63 |
naveen |
16 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
|
|
17 |
import in.shop2020.metamodel.definitions.EntityContainer;
|
|
|
18 |
import in.shop2020.metamodel.definitions.FeatureDefinition;
|
|
|
19 |
import in.shop2020.metamodel.definitions.SlideDefinition;
|
|
|
20 |
import in.shop2020.metamodel.util.ExpandedCategoryFacetDefinition;
|
| 64 |
naveen |
21 |
import in.shop2020.metamodel.util.ExpandedEntity;
|
| 63 |
naveen |
22 |
import in.shop2020.metamodel.util.ExpandedFacetDefinition;
|
|
|
23 |
|
| 62 |
naveen |
24 |
/**
|
|
|
25 |
* Command line utility to convert IR Definitions into IR Data and IR Meta-data
|
|
|
26 |
*
|
|
|
27 |
* @author naveen
|
|
|
28 |
*
|
|
|
29 |
*/
|
|
|
30 |
public class IR {
|
| 63 |
naveen |
31 |
|
| 62 |
naveen |
32 |
/**
|
| 63 |
naveen |
33 |
* Jython source path
|
|
|
34 |
*/
|
|
|
35 |
public static String JYTHON_SRC_PATH = "src/jython/";
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* IR XML export path
|
|
|
39 |
*/
|
|
|
40 |
public static String EXPORT_IR_PATH =
|
|
|
41 |
"/home/naveen/workspace/eclipse/export/ir/";
|
|
|
42 |
|
| 64 |
naveen |
43 |
/**
|
|
|
44 |
*
|
|
|
45 |
*/
|
| 63 |
naveen |
46 |
private long categoryID;
|
|
|
47 |
|
|
|
48 |
/**
|
| 64 |
naveen |
49 |
* Level - 4, 8, 12, 16
|
|
|
50 |
*/
|
|
|
51 |
private String[] xmlIndentation = {"", " ", " ", " ",
|
|
|
52 |
" "};
|
|
|
53 |
|
|
|
54 |
/**
|
| 62 |
naveen |
55 |
* @param args
|
| 63 |
naveen |
56 |
* @throws Exception
|
| 62 |
naveen |
57 |
*/
|
| 63 |
naveen |
58 |
public static void main(String[] args) throws Exception {
|
|
|
59 |
String usage = "Usage: IR {Category ID}";
|
| 62 |
naveen |
60 |
|
| 64 |
naveen |
61 |
if(args.length > 1) {
|
| 63 |
naveen |
62 |
System.out.println(usage);
|
|
|
63 |
System.exit(-1);
|
|
|
64 |
}
|
| 64 |
naveen |
65 |
|
|
|
66 |
long categoryID = 0L;
|
|
|
67 |
if(args.length > 0) {
|
|
|
68 |
try {
|
|
|
69 |
categoryID = Long.parseLong(args[0]);
|
|
|
70 |
}
|
|
|
71 |
catch (NumberFormatException nfe) {
|
|
|
72 |
System.out.println(usage);
|
|
|
73 |
System.exit(-1);
|
|
|
74 |
}
|
| 63 |
naveen |
75 |
}
|
|
|
76 |
|
|
|
77 |
IR ir = new IR(categoryID);
|
|
|
78 |
ir.exportIRData();
|
| 62 |
naveen |
79 |
}
|
| 63 |
naveen |
80 |
|
|
|
81 |
/**
|
|
|
82 |
*
|
|
|
83 |
* @param categoryID
|
|
|
84 |
*/
|
|
|
85 |
public IR(long categoryID) {
|
|
|
86 |
this.categoryID = categoryID;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* @throws Exception
|
|
|
91 |
*
|
|
|
92 |
*/
|
|
|
93 |
public void exportIRData() throws Exception {
|
|
|
94 |
DefinitionsContainer defs =
|
|
|
95 |
Catalog.getInstance().getDefinitionsContainer();
|
|
|
96 |
|
|
|
97 |
EntityContainer ents =
|
|
|
98 |
Catalog.getInstance().getEntityContainer();
|
|
|
99 |
|
| 64 |
naveen |
100 |
// <IRData>
|
|
|
101 |
List<String> entityXMLSnippets = new ArrayList<String>();
|
|
|
102 |
entityXMLSnippets.add("<IRData>");
|
| 63 |
naveen |
103 |
|
| 64 |
naveen |
104 |
List<Category> categories = null;
|
|
|
105 |
if(this.categoryID != 0L) {
|
|
|
106 |
categories = new ArrayList<Category>();
|
|
|
107 |
categories.add(defs.getCategory(this.categoryID));
|
|
|
108 |
}
|
|
|
109 |
else {
|
|
|
110 |
|
|
|
111 |
// Get all categories
|
|
|
112 |
categories = defs.getChildrenCategories(10001);
|
|
|
113 |
}
|
| 63 |
naveen |
114 |
|
| 64 |
naveen |
115 |
for(Category cat : categories) {
|
|
|
116 |
long catID = cat.getID();
|
|
|
117 |
|
|
|
118 |
// Get all facets for the category
|
|
|
119 |
ExpandedCategoryFacetDefinition expCategoryFacetDef =
|
|
|
120 |
defs.getExpandedCategoryFacetDefinition(catID);
|
|
|
121 |
|
|
|
122 |
List<ExpandedFacetDefinition> expFacetDefs =
|
|
|
123 |
expCategoryFacetDef.getExpandedFacetDefinitions();
|
|
|
124 |
|
|
|
125 |
// Get all entities for the category
|
|
|
126 |
List<Entity> entities = ents.getEntities(catID);
|
|
|
127 |
|
|
|
128 |
if(entities == null) {
|
|
|
129 |
continue;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// For each entity
|
| 63 |
naveen |
133 |
for(Entity entity : entities) {
|
| 64 |
naveen |
134 |
ExpandedEntity expEntity =
|
|
|
135 |
ents.getExpandedEntity(entity.getID());
|
| 63 |
naveen |
136 |
|
| 64 |
naveen |
137 |
List<String> facetXMLSnippets = new ArrayList<String>();
|
|
|
138 |
|
| 63 |
naveen |
139 |
// For each facet execute Rule
|
|
|
140 |
for(ExpandedFacetDefinition expFacetDef : expFacetDefs) {
|
| 64 |
naveen |
141 |
String facetXMLSnip =
|
|
|
142 |
this.processFacet(expEntity, expFacetDef);
|
| 63 |
naveen |
143 |
|
| 64 |
naveen |
144 |
if(!facetXMLSnip.isEmpty()) {
|
|
|
145 |
facetXMLSnippets.add(facetXMLSnip);
|
|
|
146 |
Utils.info("facetXMLSnip=" + facetXMLSnip);
|
| 63 |
naveen |
147 |
}
|
|
|
148 |
}
|
|
|
149 |
|
| 64 |
naveen |
150 |
String facetXMLSnippetsStr =
|
|
|
151 |
StringUtils.join(facetXMLSnippets, "\n");
|
|
|
152 |
|
|
|
153 |
Utils.info("facetXMLSnippets=" + facetXMLSnippetsStr);
|
|
|
154 |
|
|
|
155 |
String entityXMLSnip =
|
|
|
156 |
this.getEntityXMLSnippet(expEntity, facetXMLSnippetsStr, 1);
|
|
|
157 |
|
|
|
158 |
Utils.info(entityXMLSnip);
|
|
|
159 |
|
|
|
160 |
entityXMLSnippets.add(entityXMLSnip);
|
| 63 |
naveen |
161 |
}
|
|
|
162 |
}
|
| 64 |
naveen |
163 |
|
|
|
164 |
// </IRData>
|
|
|
165 |
entityXMLSnippets.add("</IRData>");
|
|
|
166 |
|
|
|
167 |
String irDataXML = StringUtils.join(entityXMLSnippets, "\n");
|
|
|
168 |
Utils.info(irDataXML);
|
|
|
169 |
|
|
|
170 |
// Write it to file
|
|
|
171 |
String irDataFilename = IR.EXPORT_IR_PATH + "irdata.xml";
|
|
|
172 |
DBUtils.store(irDataXML, irDataFilename);
|
| 63 |
naveen |
173 |
}
|
| 64 |
naveen |
174 |
|
|
|
175 |
/**
|
|
|
176 |
*
|
|
|
177 |
* @param expEntity
|
|
|
178 |
* @param expFacetDef
|
|
|
179 |
* @return
|
|
|
180 |
* @throws Exception
|
|
|
181 |
*/
|
|
|
182 |
@SuppressWarnings("unchecked")
|
|
|
183 |
private String processFacet(ExpandedEntity expEntity,
|
|
|
184 |
ExpandedFacetDefinition expFacetDef) throws Exception {
|
|
|
185 |
|
|
|
186 |
EntityContainer ents =
|
|
|
187 |
Catalog.getInstance().getEntityContainer();
|
|
|
188 |
|
|
|
189 |
JythonWrapper jw = new JythonWrapper();
|
|
|
190 |
|
|
|
191 |
jw.setExpandedEntity(expEntity);
|
|
|
192 |
jw.setExpandedFacetDefinition(expFacetDef);
|
|
|
193 |
|
|
|
194 |
// Set FeatureDefinition
|
|
|
195 |
FeatureDefinition featureDef =
|
|
|
196 |
expFacetDef.getFeatureDefinition();
|
|
|
197 |
if(featureDef != null) {
|
|
|
198 |
jw.setFeatureDefinition(featureDef);
|
|
|
199 |
|
|
|
200 |
// Set Feature
|
|
|
201 |
Utils.info("featureDef.getID()=" + featureDef.getID());
|
|
|
202 |
|
|
|
203 |
Feature feature =
|
|
|
204 |
ents.getFeature(expEntity.getID(), featureDef.getID());
|
|
|
205 |
|
|
|
206 |
jw.setFeature(feature);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
// Set SlideDefinition
|
|
|
210 |
SlideDefinition slideDef = expFacetDef.getSlideDefinition();
|
|
|
211 |
if(slideDef != null) {
|
|
|
212 |
jw.setSlideDefinition(slideDef);
|
|
|
213 |
|
|
|
214 |
// Set Slide
|
|
|
215 |
Utils.info("slideDef.getID()=" + slideDef.getID());
|
|
|
216 |
|
|
|
217 |
Slide slide = ents.getSlide(expEntity.getID(), slideDef.getID());
|
|
|
218 |
|
|
|
219 |
jw.setSlide(slide);
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
jw.execIRDataRule();
|
|
|
223 |
|
|
|
224 |
List<Object> values = (List<Object>)jw.getValues();
|
|
|
225 |
Utils.info("values=" + values);
|
|
|
226 |
|
|
|
227 |
String facetXMLSnip = "";
|
|
|
228 |
if(values != null) {
|
|
|
229 |
|
|
|
230 |
// Get IR Data XML snippet for this entity and facet
|
|
|
231 |
facetXMLSnip = this.getFacetXMLSnippet(expFacetDef, values, 2);
|
|
|
232 |
}
|
|
|
233 |
Utils.info(facetXMLSnip);
|
|
|
234 |
|
|
|
235 |
return facetXMLSnip;
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
*
|
|
|
240 |
* @param expEntity
|
|
|
241 |
* @param facetXMLSnippets
|
|
|
242 |
* @param indent
|
|
|
243 |
* @return
|
|
|
244 |
*/
|
|
|
245 |
private String getEntityXMLSnippet(ExpandedEntity expEntity,
|
|
|
246 |
String facetXMLSnippets, int indent) {
|
|
|
247 |
|
|
|
248 |
//<Entity ID="40001">
|
|
|
249 |
List<String> xmlSnippet = new ArrayList<String>();
|
|
|
250 |
|
|
|
251 |
String entityID = new Long(expEntity.getID()).toString();
|
|
|
252 |
xmlSnippet.add(this.xmlIndentation[indent] + "<Entity ID=\""+ entityID +
|
|
|
253 |
"\">");
|
|
|
254 |
|
|
|
255 |
//<Category>Business Phones</Category>
|
|
|
256 |
String category = expEntity.getCategory().getLabel();
|
|
|
257 |
xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Category>" +
|
|
|
258 |
category + "</Category>");
|
|
|
259 |
|
|
|
260 |
//<Title>Nokia E71</Title>
|
|
|
261 |
String title = expEntity.getBrand() + " " + expEntity.getModelName() +
|
|
|
262 |
((expEntity.getModelNumber() != null) ?
|
|
|
263 |
(" " + expEntity.getModelNumber())
|
|
|
264 |
: "");
|
|
|
265 |
|
|
|
266 |
xmlSnippet.add(this.xmlIndentation[indent + 1] +
|
|
|
267 |
"<Title>" + title + "</Title>");
|
|
|
268 |
|
|
|
269 |
xmlSnippet.add(facetXMLSnippets);
|
|
|
270 |
|
|
|
271 |
//</Entity>
|
|
|
272 |
xmlSnippet.add(this.xmlIndentation[indent] + "</Entity>");
|
|
|
273 |
|
|
|
274 |
return StringUtils.join(xmlSnippet, "\n");
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
*
|
|
|
279 |
* @param expFacetDef
|
|
|
280 |
* @param values
|
|
|
281 |
* @param indent
|
|
|
282 |
* @return String XML Snippet
|
|
|
283 |
*/
|
|
|
284 |
private String getFacetXMLSnippet(ExpandedFacetDefinition expFacetDef,
|
|
|
285 |
List<Object> values, int indent) {
|
|
|
286 |
// <Facet Label="Form Factor">
|
|
|
287 |
List<String> xmlSnippet = new ArrayList<String>();
|
|
|
288 |
String target = expFacetDef.getTarget();
|
|
|
289 |
xmlSnippet.add(this.xmlIndentation[indent] + "<Facet Label=\"" +
|
|
|
290 |
target + "\">");
|
|
|
291 |
|
|
|
292 |
//<Value>Candybar</Value>
|
|
|
293 |
for(Object value : values) {
|
|
|
294 |
xmlSnippet.add(this.xmlIndentation[indent + 1] + "<Value>" +
|
|
|
295 |
value.toString() + "</Value>");
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
//</Facet>
|
|
|
299 |
xmlSnippet.add(this.xmlIndentation[indent] + "</Facet>");
|
|
|
300 |
|
|
|
301 |
return StringUtils.join(xmlSnippet, "\n");
|
|
|
302 |
}
|
| 62 |
naveen |
303 |
}
|