Subversion Repositories SmartDukaan

Rev

Rev 5497 | Rev 5638 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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");
5566 amit.gupta 35
			snippetFileNames.put("accessories-compatibility-index", BASE_PATH + "accessories-compatibility-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 {
5566 amit.gupta 64
		String filePath;
65
		String p1=request.getParameter("p1");
66
		String p2=request.getParameter("p2");
5497 amit.gupta 67
		if(snippets.containsKey(id)){
68
			return snippets.get(id);
5566 amit.gupta 69
		} else if(p1 == null && p2==null){
70
			filePath = BASE_PATH + id + ".html";
71
		}	
72
		else {
73
			if(p1 != null || p2 !=null){
5497 amit.gupta 74
				if(p1==null){
5566 amit.gupta 75
					p1 = p2;
5497 amit.gupta 76
				}
77
			}
5566 amit.gupta 78
			if(p2==null){
5497 amit.gupta 79
				filePath = BASE_PATH + "most-compared-" + p1 + ".html";
80
			} else {
81
				filePath = BASE_PATH + "most-compared-" + p1 + "-vs-" + p2 + ".html";
82
			}
83
		}
5566 amit.gupta 84
		return readFile(filePath);
5121 varun.gupt 85
	}
86
 
5356 varun.gupt 87
	public String getTitle()	{
88
		String[] urlChunks = request.getServletPath().split("/");
89
		String title = urlChunks[urlChunks.length - 1].replaceAll("-", " ");
90
 
91
		return WordUtils.capitalize(title);
5121 varun.gupt 92
	}
93
 
94
	public void setId(String id) {
95
		this.id = id;
96
	}
5497 amit.gupta 97
 
98
	private String readFile(String path) throws IOException {
99
		  FileInputStream stream = new FileInputStream(new File(path));
100
		  try {
101
		    FileChannel fc = stream.getChannel();
102
		    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
103
		    /* Instead of using default, pass in a decoder. */
104
		    return Charset.defaultCharset().decode(bb).toString();
105
		  }
106
		  finally {
107
		    stream.close();
108
		  }
109
		}
110
 
5121 varun.gupt 111
}