| 22 |
naveen |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.ui.util;
|
|
|
5 |
|
|
|
6 |
import java.io.BufferedWriter;
|
|
|
7 |
import java.io.File;
|
|
|
8 |
import java.io.FileOutputStream;
|
|
|
9 |
import java.io.OutputStreamWriter;
|
|
|
10 |
|
| 45 |
naveen |
11 |
import org.apache.commons.lang.StringUtils;
|
| 22 |
naveen |
12 |
import org.apache.velocity.Template;
|
|
|
13 |
import org.apache.velocity.VelocityContext;
|
|
|
14 |
import org.apache.velocity.app.Velocity;
|
|
|
15 |
|
|
|
16 |
import in.shop2020.metamodel.definitions.Catalog;
|
| 45 |
naveen |
17 |
import in.shop2020.metamodel.definitions.DefinitionsContainer;
|
| 22 |
naveen |
18 |
import in.shop2020.metamodel.definitions.EntityContainer;
|
| 45 |
naveen |
19 |
import in.shop2020.metamodel.util.ExpandedCategory;
|
| 24 |
naveen |
20 |
import in.shop2020.metamodel.util.ExpandedEntity;
|
| 22 |
naveen |
21 |
import in.shop2020.util.Utils;
|
|
|
22 |
|
|
|
23 |
/**
|
| 49 |
naveen |
24 |
* Driver class to merge Java data objects with VTL script
|
|
|
25 |
*
|
| 22 |
naveen |
26 |
* @author naveen
|
|
|
27 |
*
|
|
|
28 |
*/
|
|
|
29 |
public class VUI {
|
| 45 |
naveen |
30 |
public static String VTL_SRC_PATH =
|
|
|
31 |
"src/in/shop2020/ui/velocity/";
|
|
|
32 |
|
|
|
33 |
public static String EXPORT_ENTITIES_PATH =
|
|
|
34 |
"/home/naveen/workspace/eclipse/export/html/entities/";
|
|
|
35 |
|
|
|
36 |
public static String EXPORT_DEFINITIONS_PATH =
|
|
|
37 |
"/home/naveen/workspace/eclipse/export/html/definitions/";
|
| 22 |
naveen |
38 |
|
|
|
39 |
/**
|
| 49 |
naveen |
40 |
* Usage: VUI [entity|category] {Template file name}
|
|
|
41 |
*
|
| 22 |
naveen |
42 |
* @param args
|
|
|
43 |
*/
|
|
|
44 |
public static void main(String[] args) throws Exception {
|
| 45 |
naveen |
45 |
String[] commands = new String[] {"entity", "category"};
|
|
|
46 |
String[] datatypes = new String[] {"Entity ID", "Category ID"};
|
|
|
47 |
|
|
|
48 |
String usage = "Usage: VUI ["+ StringUtils.join(commands, "|") +"] ["+
|
|
|
49 |
StringUtils.join(datatypes, "|") + "] {Template file name}";
|
| 22 |
naveen |
50 |
|
| 45 |
naveen |
51 |
if(args.length < 3) {
|
|
|
52 |
System.out.println(usage);
|
|
|
53 |
System.exit(-1);
|
|
|
54 |
}
|
|
|
55 |
String inputCommand = args[0];
|
|
|
56 |
String inputID = args[1];
|
|
|
57 |
String templateFilename = args[2];
|
| 22 |
naveen |
58 |
|
| 45 |
naveen |
59 |
long id = 0;
|
|
|
60 |
try {
|
|
|
61 |
id = Long.parseLong(inputID);
|
|
|
62 |
}
|
|
|
63 |
catch (NumberFormatException nfe) {
|
|
|
64 |
System.out.println("Invalid ID - " + inputID);
|
|
|
65 |
System.exit(-1);
|
|
|
66 |
}
|
| 22 |
naveen |
67 |
|
| 45 |
naveen |
68 |
// Setup
|
|
|
69 |
Velocity.init(VTL_SRC_PATH + "velocity.properties");
|
|
|
70 |
VelocityContext context = new VelocityContext();
|
|
|
71 |
|
|
|
72 |
String templateFile = VTL_SRC_PATH + templateFilename + ".vm";
|
| 22 |
naveen |
73 |
|
| 45 |
naveen |
74 |
Template template = null;
|
|
|
75 |
String exportFileName = null;
|
| 22 |
naveen |
76 |
|
| 45 |
naveen |
77 |
// Entities
|
|
|
78 |
if(inputCommand.equals("entity")) {
|
|
|
79 |
EntityContainer entContainer =
|
|
|
80 |
Catalog.getInstance().getEntityContainer();
|
|
|
81 |
|
|
|
82 |
ExpandedEntity expEntity = entContainer.getExpandedEntity(id);
|
|
|
83 |
Utils.logger.info("expEntity=" + expEntity);
|
|
|
84 |
|
|
|
85 |
context.put("expentity", expEntity);
|
|
|
86 |
template = Velocity.getTemplate(templateFile);
|
|
|
87 |
|
|
|
88 |
exportFileName = EXPORT_ENTITIES_PATH + id + ".html";
|
| 22 |
naveen |
89 |
}
|
|
|
90 |
|
| 45 |
naveen |
91 |
// Category definitons
|
|
|
92 |
else if(inputCommand.equals("category")) {
|
|
|
93 |
DefinitionsContainer defs =
|
|
|
94 |
Catalog.getInstance().getDefinitionsContainer();
|
|
|
95 |
|
|
|
96 |
ExpandedCategory expCategory = defs.getExpandedCategory(id);
|
|
|
97 |
Utils.info("expCategory=" + expCategory);
|
|
|
98 |
|
|
|
99 |
context.put("expcategory", expCategory);
|
|
|
100 |
template = Velocity.getTemplate(templateFile);
|
|
|
101 |
|
|
|
102 |
exportFileName = EXPORT_DEFINITIONS_PATH + id + ".html";
|
|
|
103 |
}
|
| 22 |
naveen |
104 |
|
| 45 |
naveen |
105 |
if(template != null) {
|
|
|
106 |
File exportFile = new File(exportFileName);
|
|
|
107 |
if(!exportFile.exists()) {
|
|
|
108 |
exportFile.createNewFile();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
BufferedWriter writer = new BufferedWriter(
|
|
|
112 |
new OutputStreamWriter(new FileOutputStream(exportFile)));
|
|
|
113 |
|
|
|
114 |
template.merge(context, writer);
|
|
|
115 |
|
|
|
116 |
writer.flush();
|
|
|
117 |
writer.close();
|
|
|
118 |
Utils.info("Export Complete!");
|
|
|
119 |
}
|
| 22 |
naveen |
120 |
}
|
|
|
121 |
|
|
|
122 |
}
|