| 22 |
naveen |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.ui.util;
|
|
|
5 |
|
| 109 |
naveen |
6 |
import in.shop2020.metamodel.core.Bullet;
|
|
|
7 |
import in.shop2020.metamodel.core.Entity;
|
|
|
8 |
import in.shop2020.metamodel.core.Feature;
|
|
|
9 |
import in.shop2020.metamodel.core.PrimitiveDataObject;
|
|
|
10 |
import in.shop2020.metamodel.definitions.Catalog;
|
|
|
11 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
|
|
12 |
import in.shop2020.metamodel.definitions.EntityContainer;
|
| 110 |
naveen |
13 |
import in.shop2020.metamodel.definitions.SlideDefinition;
|
| 109 |
naveen |
14 |
import in.shop2020.metamodel.util.ExpandedCategory;
|
|
|
15 |
import in.shop2020.metamodel.util.ExpandedEntity;
|
|
|
16 |
import in.shop2020.metamodel.util.ExpandedSlide;
|
|
|
17 |
import in.shop2020.util.Utils;
|
|
|
18 |
|
| 22 |
naveen |
19 |
import java.io.BufferedWriter;
|
|
|
20 |
import java.io.File;
|
|
|
21 |
import java.io.FileOutputStream;
|
|
|
22 |
import java.io.OutputStreamWriter;
|
| 108 |
naveen |
23 |
import java.util.ArrayList;
|
|
|
24 |
import java.util.Arrays;
|
| 110 |
naveen |
25 |
import java.util.Collection;
|
|
|
26 |
import java.util.LinkedHashMap;
|
| 108 |
naveen |
27 |
import java.util.List;
|
| 110 |
naveen |
28 |
import java.util.Map;
|
| 22 |
naveen |
29 |
|
| 45 |
naveen |
30 |
import org.apache.commons.lang.StringUtils;
|
| 22 |
naveen |
31 |
import org.apache.velocity.Template;
|
|
|
32 |
import org.apache.velocity.VelocityContext;
|
|
|
33 |
import org.apache.velocity.app.Velocity;
|
|
|
34 |
|
|
|
35 |
/**
|
| 49 |
naveen |
36 |
* Driver class to merge Java data objects with VTL script
|
|
|
37 |
*
|
| 22 |
naveen |
38 |
* @author naveen
|
|
|
39 |
*
|
|
|
40 |
*/
|
|
|
41 |
public class VUI {
|
|
|
42 |
|
|
|
43 |
/**
|
| 163 |
naveen |
44 |
* Usage: VUI [entity|category|category_carousal|diff] [Entity ID|Category ID|EntityID1_EntityID2] {Template file name}
|
| 49 |
naveen |
45 |
*
|
| 22 |
naveen |
46 |
* @param args
|
|
|
47 |
*/
|
|
|
48 |
public static void main(String[] args) throws Exception {
|
| 163 |
naveen |
49 |
//diffAll();
|
|
|
50 |
//System.exit(0);
|
|
|
51 |
|
| 109 |
naveen |
52 |
String[] commands = new String[] {"entity", "category",
|
|
|
53 |
"category_carousal", "diff"};
|
|
|
54 |
String[] datatypes = new String[] {"Entity ID", "Category ID",
|
|
|
55 |
"EntityID1_EntityID2"};
|
| 45 |
naveen |
56 |
|
|
|
57 |
String usage = "Usage: VUI ["+ StringUtils.join(commands, "|") +"] ["+
|
|
|
58 |
StringUtils.join(datatypes, "|") + "] {Template file name}";
|
| 22 |
naveen |
59 |
|
| 45 |
naveen |
60 |
if(args.length < 3) {
|
|
|
61 |
System.out.println(usage);
|
|
|
62 |
System.exit(-1);
|
|
|
63 |
}
|
|
|
64 |
String inputCommand = args[0];
|
|
|
65 |
String inputID = args[1];
|
|
|
66 |
String templateFilename = args[2];
|
| 22 |
naveen |
67 |
|
| 109 |
naveen |
68 |
|
| 45 |
naveen |
69 |
long id = 0;
|
| 109 |
naveen |
70 |
if(inputCommand.equals("entity") || inputCommand.equals("category") ||
|
|
|
71 |
inputCommand.equals("category_carousal")) {
|
|
|
72 |
try {
|
|
|
73 |
id = Long.parseLong(inputID);
|
|
|
74 |
}
|
|
|
75 |
catch (NumberFormatException nfe) {
|
|
|
76 |
System.out.println("Invalid ID - " + inputID);
|
|
|
77 |
System.exit(-1);
|
|
|
78 |
}
|
| 45 |
naveen |
79 |
}
|
| 22 |
naveen |
80 |
|
| 45 |
naveen |
81 |
// Setup
|
| 59 |
naveen |
82 |
Velocity.init("src/in/shop2020/ui/util/velocity.properties");
|
| 45 |
naveen |
83 |
VelocityContext context = new VelocityContext();
|
|
|
84 |
|
| 108 |
naveen |
85 |
String templateFile = Utils.VTL_SRC_PATH + templateFilename + ".vm";
|
| 22 |
naveen |
86 |
|
| 45 |
naveen |
87 |
Template template = null;
|
|
|
88 |
String exportFileName = null;
|
| 22 |
naveen |
89 |
|
| 108 |
naveen |
90 |
DefinitionsContainer defs =
|
|
|
91 |
Catalog.getInstance().getDefinitionsContainer();
|
|
|
92 |
|
|
|
93 |
EntityContainer ents =
|
|
|
94 |
Catalog.getInstance().getEntityContainer();
|
|
|
95 |
|
| 45 |
naveen |
96 |
// Entities
|
|
|
97 |
if(inputCommand.equals("entity")) {
|
|
|
98 |
EntityContainer entContainer =
|
|
|
99 |
Catalog.getInstance().getEntityContainer();
|
|
|
100 |
|
|
|
101 |
ExpandedEntity expEntity = entContainer.getExpandedEntity(id);
|
|
|
102 |
Utils.logger.info("expEntity=" + expEntity);
|
|
|
103 |
|
|
|
104 |
context.put("expentity", expEntity);
|
|
|
105 |
template = Velocity.getTemplate(templateFile);
|
|
|
106 |
|
| 108 |
naveen |
107 |
exportFileName = Utils.EXPORT_ENTITIES_PATH + id + ".html";
|
| 22 |
naveen |
108 |
}
|
|
|
109 |
|
| 163 |
naveen |
110 |
// Category definitions
|
| 108 |
naveen |
111 |
else if(inputCommand.equals("category")) {
|
| 45 |
naveen |
112 |
ExpandedCategory expCategory = defs.getExpandedCategory(id);
|
|
|
113 |
Utils.info("expCategory=" + expCategory);
|
|
|
114 |
|
|
|
115 |
context.put("expcategory", expCategory);
|
|
|
116 |
template = Velocity.getTemplate(templateFile);
|
|
|
117 |
|
| 108 |
naveen |
118 |
exportFileName = Utils.EXPORT_DEFINITIONS_PATH + id + ".html";
|
| 45 |
naveen |
119 |
}
|
| 108 |
naveen |
120 |
|
|
|
121 |
// Category carousal
|
|
|
122 |
else if(inputCommand.equals("category_carousal")) {
|
|
|
123 |
String categoryName = defs.getCategory(id).getLabel();
|
|
|
124 |
List<Entity> entities = ents.getEntities(id);
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
List<String[]> entitiesToShow = new ArrayList<String[]>();
|
|
|
128 |
|
|
|
129 |
for(Entity entity : entities) {
|
|
|
130 |
String snippets[] = new String[4];
|
|
|
131 |
|
|
|
132 |
// Entity ID
|
|
|
133 |
snippets[0] = new Long(entity.getID()).toString();
|
|
|
134 |
|
|
|
135 |
Feature feature = ents.getFeature(entity.getID(),
|
|
|
136 |
Utils.SNIPPETS_FEATURE_DEFINITIONID);
|
|
|
137 |
|
|
|
138 |
List<Bullet> bullets = feature.getBullets();
|
|
|
139 |
|
|
|
140 |
if(bullets.size() > 0) {
|
|
|
141 |
snippets[1] = ((PrimitiveDataObject)bullets.get(0).
|
|
|
142 |
getDataObject()).getValue();
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
if(bullets.size() > 1) {
|
|
|
146 |
snippets[2] = ((PrimitiveDataObject)bullets.get(1).
|
|
|
147 |
getDataObject()).getValue();
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if(bullets.size() > 2) {
|
|
|
151 |
snippets[3] = ((PrimitiveDataObject)bullets.get(2).
|
|
|
152 |
getDataObject()).getValue();
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
Utils.info("snippets=" + Arrays.toString(snippets));
|
|
|
156 |
entitiesToShow.add(snippets);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
Utils.info("categoryName=" + categoryName);
|
|
|
160 |
Utils.info("entitiesToShow=" + entitiesToShow);
|
|
|
161 |
|
|
|
162 |
context.put("categoryName", categoryName);
|
|
|
163 |
context.put("entitiesToShow", entitiesToShow);
|
|
|
164 |
|
|
|
165 |
template = Velocity.getTemplate(templateFile);
|
|
|
166 |
exportFileName = Utils.EXPORT_CATEGORY_HOMES_PATH + id + ".html";
|
|
|
167 |
}
|
| 109 |
naveen |
168 |
|
|
|
169 |
// Difference
|
|
|
170 |
else if(inputCommand.equals("diff")) {
|
|
|
171 |
String[] entityIDs = StringUtils.split(inputID, "_");
|
|
|
172 |
if(entityIDs.length < 2) {
|
|
|
173 |
System.out.println("Invalid IDs - " + inputID);
|
|
|
174 |
System.exit(-1);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
long entityID1 = 0L;
|
|
|
178 |
long entityID2 = 0L;
|
|
|
179 |
try {
|
|
|
180 |
entityID1 = Long.parseLong(entityIDs[0]);
|
|
|
181 |
entityID2 = Long.parseLong(entityIDs[1]);
|
|
|
182 |
} catch(NumberFormatException nfe) {
|
|
|
183 |
System.out.println("Invalid IDs - " + inputID);
|
|
|
184 |
System.exit(-1);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
Utils.info("entityIDs=" + Arrays.toString(entityIDs));
|
|
|
188 |
|
|
|
189 |
ExpandedEntity expEntity1 = ents.getExpandedEntity(entityID1);
|
|
|
190 |
ExpandedEntity expEntity2 = ents.getExpandedEntity(entityID2);
|
|
|
191 |
|
|
|
192 |
List<ExpandedSlide[]> mergedSlides =
|
|
|
193 |
getMergedSlides(expEntity1, expEntity2);
|
| 110 |
naveen |
194 |
Utils.info("mergedSlides=" + mergedSlides);
|
| 109 |
naveen |
195 |
|
| 110 |
naveen |
196 |
Collection<String[]> mergedComparisonScores =
|
|
|
197 |
getMergedComparisonScores(expEntity1, expEntity2);
|
|
|
198 |
Utils.info("mergedComparisonScores=" + mergedComparisonScores);
|
| 109 |
naveen |
199 |
|
| 110 |
naveen |
200 |
int scoreEntity1 = ents.getEntityComparisonScore(
|
|
|
201 |
expEntity1.getID());
|
|
|
202 |
|
|
|
203 |
int scoreEntity2 = ents.getEntityComparisonScore(
|
|
|
204 |
expEntity2.getID());
|
|
|
205 |
|
| 109 |
naveen |
206 |
context.put("expEntity1", expEntity1);
|
|
|
207 |
context.put("expEntity2", expEntity2);
|
|
|
208 |
context.put("mergedSlides", mergedSlides);
|
| 110 |
naveen |
209 |
context.put("mergedComparisonScores", mergedComparisonScores);
|
|
|
210 |
context.put("scoreEntity1", scoreEntity1);
|
|
|
211 |
context.put("scoreEntity2", scoreEntity2);
|
| 109 |
naveen |
212 |
|
|
|
213 |
template = Velocity.getTemplate(templateFile);
|
|
|
214 |
exportFileName = Utils.EXPORT_DIFF_PATH + entityIDs[0] + "_" +
|
|
|
215 |
entityIDs[1] + ".html";
|
|
|
216 |
}
|
| 22 |
naveen |
217 |
|
| 45 |
naveen |
218 |
if(template != null) {
|
|
|
219 |
File exportFile = new File(exportFileName);
|
|
|
220 |
if(!exportFile.exists()) {
|
|
|
221 |
exportFile.createNewFile();
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
BufferedWriter writer = new BufferedWriter(
|
|
|
225 |
new OutputStreamWriter(new FileOutputStream(exportFile)));
|
|
|
226 |
|
|
|
227 |
template.merge(context, writer);
|
|
|
228 |
|
|
|
229 |
writer.flush();
|
|
|
230 |
writer.close();
|
|
|
231 |
Utils.info("Export Complete!");
|
|
|
232 |
}
|
| 22 |
naveen |
233 |
}
|
| 110 |
naveen |
234 |
|
| 109 |
naveen |
235 |
/**
|
|
|
236 |
*
|
|
|
237 |
* @param expEntity1
|
|
|
238 |
* @param expEntity2
|
| 110 |
naveen |
239 |
* @return
|
|
|
240 |
* @throws Exception
|
| 109 |
naveen |
241 |
*/
|
| 110 |
naveen |
242 |
private static Collection<String[]> getMergedComparisonScores(
|
|
|
243 |
ExpandedEntity expEntity1, ExpandedEntity expEntity2)
|
|
|
244 |
throws Exception {
|
|
|
245 |
EntityContainer ents = Catalog.getInstance().getEntityContainer();
|
|
|
246 |
DefinitionsContainer defs =
|
|
|
247 |
Catalog.getInstance().getDefinitionsContainer();
|
|
|
248 |
|
|
|
249 |
Map<String, String[]> mergedSlideScores =
|
|
|
250 |
new LinkedHashMap<String, String[]>();
|
|
|
251 |
|
|
|
252 |
Map<Long, Integer> cmpSlideScores1 = ents.getSlideComparisonScores(
|
|
|
253 |
expEntity1.getID());
|
|
|
254 |
|
|
|
255 |
for(Long slideID : cmpSlideScores1.keySet()) {
|
|
|
256 |
SlideDefinition slideDef = defs.getSlideDefinition(
|
|
|
257 |
slideID.longValue());
|
|
|
258 |
|
|
|
259 |
String slideLabel = slideDef.getLabel();
|
|
|
260 |
String[] details = new String[3];
|
|
|
261 |
|
|
|
262 |
details[0] = slideLabel;
|
|
|
263 |
|
|
|
264 |
Integer score = cmpSlideScores1.get(slideID);
|
|
|
265 |
if(score != null) {
|
|
|
266 |
details[1] = score.toString();
|
|
|
267 |
}
|
|
|
268 |
else {
|
|
|
269 |
details[1] = "-1";
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
// To be determined in loop over 2nd entity's scores
|
|
|
273 |
details[2] = "-1";
|
|
|
274 |
|
|
|
275 |
mergedSlideScores.put(slideLabel, details);
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
Map<Long, Integer> cmpSlideScores2 = ents.getSlideComparisonScores(
|
|
|
279 |
expEntity2.getID());
|
|
|
280 |
|
|
|
281 |
for(Long slideID : cmpSlideScores2.keySet()) {
|
|
|
282 |
SlideDefinition slideDef = defs.getSlideDefinition(
|
|
|
283 |
slideID.longValue());
|
|
|
284 |
|
|
|
285 |
String slideLabel = slideDef.getLabel();
|
|
|
286 |
|
|
|
287 |
String[] details = null;
|
|
|
288 |
if(mergedSlideScores.containsKey(slideLabel)) {
|
|
|
289 |
details = mergedSlideScores.get(slideLabel);
|
|
|
290 |
}
|
|
|
291 |
else {
|
|
|
292 |
details = new String[3];
|
|
|
293 |
|
|
|
294 |
details[0] = slideLabel;
|
|
|
295 |
details[1] = "-1";
|
|
|
296 |
|
|
|
297 |
mergedSlideScores.put(slideLabel, details);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
Integer score = cmpSlideScores2.get(slideID);
|
|
|
301 |
if(score != null) {
|
|
|
302 |
details[2] = score.toString();
|
|
|
303 |
}
|
|
|
304 |
else {
|
|
|
305 |
details[2] = "-1";
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
return mergedSlideScores.values();
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
*
|
|
|
314 |
* @param expEntity1
|
|
|
315 |
* @param expEntity2
|
|
|
316 |
*/
|
| 109 |
naveen |
317 |
private static List<ExpandedSlide[]> getMergedSlides(
|
|
|
318 |
ExpandedEntity expEntity1, ExpandedEntity expEntity2) {
|
|
|
319 |
List<ExpandedSlide[]> mergedSlides = new ArrayList<ExpandedSlide[]>();
|
|
|
320 |
|
|
|
321 |
EntityContainer ents = Catalog.getInstance().getEntityContainer();
|
|
|
322 |
|
|
|
323 |
List<ExpandedSlide> expSlidesEntity1 = expEntity1.getExpandedSlides();
|
|
|
324 |
|
|
|
325 |
for(ExpandedSlide expSlide1 : expSlidesEntity1) {
|
|
|
326 |
String slideLabel = expSlide1.getSlideDefinition().getLabel();
|
|
|
327 |
|
|
|
328 |
ExpandedSlide expSlide2 = ents.getParentExpandedSlide(
|
|
|
329 |
expEntity2, slideLabel);
|
|
|
330 |
|
|
|
331 |
if(expSlide2 != null) {
|
|
|
332 |
ExpandedSlide[] comparisonSlides = new ExpandedSlide[2];
|
|
|
333 |
comparisonSlides[0] = expSlide1;
|
|
|
334 |
comparisonSlides[1] = expSlide2;
|
|
|
335 |
|
|
|
336 |
mergedSlides.add(comparisonSlides);
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
return mergedSlides;
|
|
|
341 |
}
|
| 163 |
naveen |
342 |
|
|
|
343 |
public static void diffAll() throws Exception {
|
|
|
344 |
long[] entityIDs = new long[] {
|
|
|
345 |
1000001,
|
|
|
346 |
1000002,
|
|
|
347 |
1000003,
|
|
|
348 |
1000004,
|
|
|
349 |
1000005,
|
|
|
350 |
1000006,
|
|
|
351 |
1000007,
|
|
|
352 |
1000008,
|
|
|
353 |
1000009,
|
|
|
354 |
1000010,
|
|
|
355 |
1000011,
|
|
|
356 |
1000012,
|
|
|
357 |
1000013,
|
|
|
358 |
1000014,
|
|
|
359 |
1000015
|
|
|
360 |
};
|
|
|
361 |
|
|
|
362 |
for(int i=0;i<entityIDs.length;i++) {
|
|
|
363 |
for(int j=0;j<entityIDs.length;j++) {
|
|
|
364 |
diff(entityIDs[i], entityIDs[j]);
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
public static void diff(long entityID1, long entityID2) throws Exception {
|
|
|
370 |
// Setup
|
|
|
371 |
Velocity.init("src/in/shop2020/ui/util/velocity.properties");
|
|
|
372 |
VelocityContext context = new VelocityContext();
|
|
|
373 |
|
|
|
374 |
String templateFile = Utils.VTL_SRC_PATH + "jquery_diff_slideguide.vm";
|
|
|
375 |
|
|
|
376 |
Template template = null;
|
|
|
377 |
String exportFileName = null;
|
|
|
378 |
|
|
|
379 |
EntityContainer ents = Catalog.getInstance().getEntityContainer();
|
|
|
380 |
|
|
|
381 |
ExpandedEntity expEntity1 = ents.getExpandedEntity(entityID1);
|
|
|
382 |
ExpandedEntity expEntity2 = ents.getExpandedEntity(entityID2);
|
|
|
383 |
|
|
|
384 |
List<ExpandedSlide[]> mergedSlides =
|
|
|
385 |
getMergedSlides(expEntity1, expEntity2);
|
|
|
386 |
Utils.info("mergedSlides=" + mergedSlides);
|
|
|
387 |
|
|
|
388 |
Collection<String[]> mergedComparisonScores =
|
|
|
389 |
getMergedComparisonScores(expEntity1, expEntity2);
|
|
|
390 |
Utils.info("mergedComparisonScores=" + mergedComparisonScores);
|
|
|
391 |
|
|
|
392 |
int scoreEntity1 = ents.getEntityComparisonScore(
|
|
|
393 |
expEntity1.getID());
|
|
|
394 |
|
|
|
395 |
int scoreEntity2 = ents.getEntityComparisonScore(
|
|
|
396 |
expEntity2.getID());
|
|
|
397 |
|
|
|
398 |
context.put("expEntity1", expEntity1);
|
|
|
399 |
context.put("expEntity2", expEntity2);
|
|
|
400 |
context.put("mergedSlides", mergedSlides);
|
|
|
401 |
context.put("mergedComparisonScores", mergedComparisonScores);
|
|
|
402 |
context.put("scoreEntity1", scoreEntity1);
|
|
|
403 |
context.put("scoreEntity2", scoreEntity2);
|
|
|
404 |
|
|
|
405 |
template = Velocity.getTemplate(templateFile);
|
|
|
406 |
exportFileName = Utils.EXPORT_DIFF_PATH + entityID1 + "_" + entityID2 +
|
|
|
407 |
".html";
|
|
|
408 |
|
|
|
409 |
File exportFile = new File(exportFileName);
|
|
|
410 |
if(!exportFile.exists()) {
|
|
|
411 |
exportFile.createNewFile();
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
BufferedWriter writer = new BufferedWriter(
|
|
|
415 |
new OutputStreamWriter(new FileOutputStream(exportFile)));
|
|
|
416 |
|
|
|
417 |
template.merge(context, writer);
|
|
|
418 |
|
|
|
419 |
writer.flush();
|
|
|
420 |
writer.close();
|
|
|
421 |
Utils.info("Export Complete!");
|
|
|
422 |
}
|
| 22 |
naveen |
423 |
}
|