| 5121 |
varun.gupt |
1 |
/**
|
|
|
2 |
* @author Varun Gupta
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
package in.shop2020.serving.controllers;
|
|
|
6 |
|
| 5497 |
amit.gupta |
7 |
import in.shop2020.serving.utils.Utils;
|
|
|
8 |
|
| 5121 |
varun.gupt |
9 |
import java.io.BufferedReader;
|
| 5497 |
amit.gupta |
10 |
import java.io.File;
|
|
|
11 |
import java.io.FileInputStream;
|
| 5121 |
varun.gupt |
12 |
import java.io.FileReader;
|
|
|
13 |
import java.io.IOException;
|
| 5497 |
amit.gupta |
14 |
import java.nio.MappedByteBuffer;
|
|
|
15 |
import java.nio.channels.FileChannel;
|
|
|
16 |
import java.nio.charset.Charset;
|
| 5121 |
varun.gupt |
17 |
import java.util.HashMap;
|
|
|
18 |
import java.util.Map;
|
|
|
19 |
|
| 5356 |
varun.gupt |
20 |
import org.apache.commons.lang.WordUtils;
|
| 5121 |
varun.gupt |
21 |
import org.apache.log4j.Logger;
|
|
|
22 |
|
|
|
23 |
public class GeneratedController extends BaseController {
|
|
|
24 |
|
|
|
25 |
private static Logger logger = Logger.getLogger(GeneratedController.class);
|
|
|
26 |
private static String BASE_PATH = Utils.EXPORT_ENTITIES_PATH + "../../partners/";
|
|
|
27 |
private static Map<String, String> snippetFileNames = new HashMap<String, String>();
|
|
|
28 |
private static Map<String, String> snippets = new HashMap<String, String>();
|
|
|
29 |
|
|
|
30 |
private String id;
|
|
|
31 |
|
|
|
32 |
static {
|
|
|
33 |
try {
|
|
|
34 |
snippetFileNames.put("product-index", BASE_PATH + "productindex.html");
|
|
|
35 |
snippetFileNames.put("accessories-compatibility-index", BASE_PATH + "compatible-accessories-index.html");
|
| 5190 |
varun.gupt |
36 |
snippetFileNames.put("most-compared-phones", BASE_PATH + "most-compared-index.html");
|
| 5121 |
varun.gupt |
37 |
|
|
|
38 |
for (String index: snippetFileNames.keySet()) {
|
|
|
39 |
|
|
|
40 |
FileReader fr = new FileReader(snippetFileNames.get(index));
|
|
|
41 |
BufferedReader br = new BufferedReader(fr);
|
|
|
42 |
StringBuilder sb = new StringBuilder();
|
|
|
43 |
String str = null;
|
|
|
44 |
|
|
|
45 |
while ((str = br.readLine()) != null) {
|
|
|
46 |
sb.append(str);
|
|
|
47 |
}
|
|
|
48 |
snippets.put(index, sb.toString());
|
|
|
49 |
}
|
|
|
50 |
} catch (IOException e) {
|
|
|
51 |
logger.error("", e);
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public GeneratedController() {
|
|
|
56 |
super();
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public String show() {
|
|
|
60 |
return "show";
|
|
|
61 |
}
|
|
|
62 |
|
| 5497 |
amit.gupta |
63 |
public String getHTML() throws Exception {
|
|
|
64 |
if(snippets.containsKey(id)){
|
|
|
65 |
return snippets.get(id);
|
|
|
66 |
}else {
|
|
|
67 |
String p1="";
|
|
|
68 |
String p2="";
|
|
|
69 |
if(request.getParameter("p1") != null && request.getParameter("p2") != null){
|
|
|
70 |
p1 = request.getParameter("p1");
|
|
|
71 |
p2 = request.getParameter("p2");
|
|
|
72 |
}else if (request.getParameter("p1") != null || request.getParameter("p2") != null){
|
|
|
73 |
p1 = request.getParameter("p1");
|
|
|
74 |
if(p1==null){
|
|
|
75 |
p1 = request.getParameter("p2");
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
String filePath;
|
|
|
79 |
if(p2.equals("")){
|
|
|
80 |
filePath = BASE_PATH + "most-compared-" + p1 + ".html";
|
|
|
81 |
} else {
|
|
|
82 |
filePath = BASE_PATH + "most-compared-" + p1 + "-vs-" + p2 + ".html";
|
|
|
83 |
}
|
|
|
84 |
return readFile(filePath);
|
|
|
85 |
}
|
| 5121 |
varun.gupt |
86 |
}
|
|
|
87 |
|
| 5356 |
varun.gupt |
88 |
public String getTitle() {
|
|
|
89 |
String[] urlChunks = request.getServletPath().split("/");
|
|
|
90 |
String title = urlChunks[urlChunks.length - 1].replaceAll("-", " ");
|
|
|
91 |
|
|
|
92 |
return WordUtils.capitalize(title);
|
| 5121 |
varun.gupt |
93 |
}
|
|
|
94 |
|
|
|
95 |
public void setId(String id) {
|
|
|
96 |
this.id = id;
|
|
|
97 |
}
|
| 5497 |
amit.gupta |
98 |
|
|
|
99 |
private String readFile(String path) throws IOException {
|
|
|
100 |
FileInputStream stream = new FileInputStream(new File(path));
|
|
|
101 |
try {
|
|
|
102 |
FileChannel fc = stream.getChannel();
|
|
|
103 |
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
|
|
|
104 |
/* Instead of using default, pass in a decoder. */
|
|
|
105 |
return Charset.defaultCharset().decode(bb).toString();
|
|
|
106 |
}
|
|
|
107 |
finally {
|
|
|
108 |
stream.close();
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
| 5121 |
varun.gupt |
112 |
}
|