Subversion Repositories SmartDukaan

Rev

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");
5638 amit.gupta 37
			snippetFileNames.put("most-frequently-searched", BASE_PATH + "most-frequently-searched.html");
5121 varun.gupt 38
 
39
			for (String index: snippetFileNames.keySet())	{
40
 
41
				FileReader fr = new FileReader(snippetFileNames.get(index));
42
				BufferedReader br = new BufferedReader(fr);
43
		        StringBuilder sb = new StringBuilder();
44
		        String str = null;
45
 
46
				while ((str = br.readLine()) != null) {
47
					sb.append(str);
48
				}
49
				snippets.put(index, sb.toString());
50
			}
51
		} catch (IOException e) {
52
			logger.error("", e);
53
		}
54
	}
55
 
56
	public GeneratedController()	{
57
		super();
58
	}
59
 
60
	public String show()	{
61
		return "show";
62
	}
63
 
5497 amit.gupta 64
	public String getHTML()	throws Exception {
5566 amit.gupta 65
		String filePath;
6996 anupam.sin 66
		String p1=request.getParameter("p1").trim();
67
		String p2=request.getParameter("p2").trim();
5497 amit.gupta 68
		if(snippets.containsKey(id)){
69
			return snippets.get(id);
5566 amit.gupta 70
		} else if(p1 == null && p2==null){
71
			filePath = BASE_PATH + id + ".html";
72
		}	
73
		else {
74
			if(p1 != null || p2 !=null){
5497 amit.gupta 75
				if(p1==null){
5566 amit.gupta 76
					p1 = p2;
5497 amit.gupta 77
				}
78
			}
5566 amit.gupta 79
			if(p2==null){
5497 amit.gupta 80
				filePath = BASE_PATH + "most-compared-" + p1 + ".html";
81
			} else {
82
				filePath = BASE_PATH + "most-compared-" + p1 + "-vs-" + p2 + ".html";
83
			}
84
		}
5566 amit.gupta 85
		return readFile(filePath);
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
}