Subversion Repositories SmartDukaan

Rev

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