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