Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.social.controllers;

import in.shop2020.social.services.SolrSearchService;
import in.shop2020.social.utils.FileUtils;
import in.shop2020.social.utils.Utils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

/**
 * @author Varun Gupta
 */

public class MyPhonesController implements ServletRequestAware, ServletResponseAware, ServletContextAware{

        protected HttpServletResponse response;
        protected HttpServletRequest request;
        protected ServletContext context;
        
        private static final long serialVersionUID = -8392433517042806559L;

        private List<String> results = null;

        private Map<String, String> snippets;
        
        private String sortOrder = null;
        private Double minPrice = null;
        private Double maxPrice = null;

        private long windowSize = 10;
        private long page = 1;
        private long totalResults = 0;
        private long categoryId = 10000;

        public MyPhonesController(){
                snippets = new HashMap<String, String>();
        }
        
        // GET /Show Form
        public String index() {
                return "index";
        }
        
        public String create()  {
                if(this.request.getParameter("q") != null && ! this.request.getParameter("q").trim().isEmpty()) {
                        String query = this.request.getParameter("q");  //"F_50010:Mobile+Phones"
                        String[] fqrys = new String[]   {"F_50010:Mobile Phones"};
                String[] facetDefIDs = new String[]{}; //new String[] {"F_50010"};
                        
                        SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page - 1) * windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder);
                        this.totalResults = search.getTotalResults();
                        this.results = search.getResultMap();
                }
                return "index";
        }

    public List<String> getResults()    {
        return this.results;
    }
    
    public long getResultCount()        {
        return this.totalResults;
    }
    
    public Map<String, String> getSnippets() throws Exception {
        if(results != null)     {
                snippets = new HashMap<String, String>();
//              snippets.put("1000468", FileUtils.read("/home/varungupta/snippets/SearchSnippet1.html"));
//              snippets.put("1000534", FileUtils.read("/home/varungupta/snippets/SearchSnippet2.html"));
//              snippets.put("1001211", FileUtils.read("/home/varungupta/snippets/SearchSnippet3.html"));
//              snippets.put("1001223", FileUtils.read("/home/varungupta/snippets/SearchSnippet4.html"));

                for(String docId: results){
                        String html = FileUtils.read(Utils.EXPORT_ENTITIES_PATH + docId + File.separator + "PhonesIOwnSnippet.html");
                        System.out.println(Utils.EXPORT_ENTITIES_PATH + docId + File.separator + "PhonesIOwnSnippet.html");
                                snippets.put(docId, html);
                        }
        }
                return snippets;
    }
        @Override
        public void setServletResponse(HttpServletResponse response) {
                this.response = response;
        }

        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
        }

        @Override
        public void setServletContext(ServletContext context) {
                this.context = context;
        }
        
        public String getServletContextPath(){
                return context.getContextPath();
        }
}