Subversion Repositories SmartDukaan

Rev

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

/**
 * @author Varun Gupta
 */

package in.shop2020.serving.controllers;

import in.shop2020.serving.utils.Utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.WordUtils;
import org.apache.log4j.Logger;

public class GeneratedController extends BaseController {

        private static Logger logger = Logger.getLogger(GeneratedController.class);
        private static String BASE_PATH = Utils.EXPORT_ENTITIES_PATH + "../../partners/";
        private static Map<String, String> snippetFileNames = new HashMap<String, String>();
        private static Map<String, String> snippets = new HashMap<String, String>();
        
        private String id;
        
        static  {
                try {
                        snippetFileNames.put("product-index", BASE_PATH + "productindex.html");
                        snippetFileNames.put("accessories-compatibility-index", BASE_PATH + "accessories-compatibility-index.html");
                        snippetFileNames.put("most-compared-phones", BASE_PATH + "most-compared-index.html");
                        snippetFileNames.put("most-frequently-searched", BASE_PATH + "most-frequently-searched.html");
                        
                        for (String index: snippetFileNames.keySet())   {
                                
                                FileReader fr = new FileReader(snippetFileNames.get(index));
                                BufferedReader br = new BufferedReader(fr);
                        StringBuilder sb = new StringBuilder();
                        String str = null;
                        
                                while ((str = br.readLine()) != null) {
                                        sb.append(str);
                                }
                                snippets.put(index, sb.toString());
                        }
                } catch (IOException e) {
                        logger.error("", e);
                }
        }
        
        public GeneratedController()    {
                super();
        }
        
        public String show()    {
                return "show";
        }
        
        public String getHTML() throws Exception {
                String filePath;
                String p1=request.getParameter("p1").trim();
                String p2=request.getParameter("p2").trim();
                if(snippets.containsKey(id)){
                        return snippets.get(id);
                } else if(p1 == null && p2==null){
                        filePath = BASE_PATH + id + ".html";
                }       
                else {
                        if(p1 != null || p2 !=null){
                                if(p1==null){
                                        p1 = p2;
                                }
                        }
                        if(p2==null){
                                filePath = BASE_PATH + "most-compared-" + p1 + ".html";
                        } else {
                                filePath = BASE_PATH + "most-compared-" + p1 + "-vs-" + p2 + ".html";
                        }
                }
                return readFile(filePath);
        }
        
        public String getTitle()        {
                String[] urlChunks = request.getServletPath().split("/");
                String title = urlChunks[urlChunks.length - 1].replaceAll("-", " ");
                
                return WordUtils.capitalize(title);
        }
        
        public void setId(String id) {
                this.id = id;
        }
        
        private String readFile(String path) throws IOException {
                  FileInputStream stream = new FileInputStream(new File(path));
                  try {
                    FileChannel fc = stream.getChannel();
                    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
                    /* Instead of using default, pass in a decoder. */
                    return Charset.defaultCharset().decode(bb).toString();
                  }
                  finally {
                    stream.close();
                  }
                }

}