Subversion Repositories SmartDukaan

Rev

Rev 339 | Blame | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.controllers;

import in.shop2020.serving.services.SolrSearchService;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

/**
 * @author naveen
 *
 */
public class SearchController extends BaseController 
        implements ServletRequestAware {

        /**
         * 
         */
        private static Log log = LogFactory.getLog(SearchController.class);
        
        /**
         * 
         */
        private HttpServletRequest request;
        
        /**
         * 
         */
        private Map<String, String[]> results;
        
        /**
         * 
         */
        private Map<String, List<String[]>> facets;
        
        /**
         * 
         */
        private List<String[]> crumbs;
        
        private Double minPrice;
        private Double maxPrice;

        /**
         * 
         * @return
         * @throws UnsupportedEncodingException
         */
    // GET /query
    public HttpHeaders index() throws UnsupportedEncodingException {
        log.info("this.request=" + this.request);
        
        String qry = this.request.getParameter("q");
        log.info("qry=" + qry);
        
        String[] fqrys = this.request.getParameterValues("fq");

        this.crumbs = new ArrayList<String[]>();
        
        String strCrumb = "";
        String urlCrumb = "";
        if(qry != null) {
                strCrumb = qry;
                urlCrumb = "q=" + URLEncoder.encode(qry, "UTF-8"); 
                this.crumbs.add(new String[] { strCrumb, urlCrumb } );
                        log.info("acrumb=" + 
                                        Arrays.toString(new String[] { strCrumb, urlCrumb }));
        }else {
                /**
                 * FIXME to handle if qry is null
                 */
        }
        
//      HashMap<String,String> facetqueries = new HashMap<String, String>();
        
        if(fqrys != null) {
                log.info("fqrys=" + Arrays.toString(fqrys));
        
                for(int i=0; i<fqrys.length; i++) {
                        String facetValue = StringUtils.split(fqrys[i], ":")[1];
        //              facetqueries.put(StringUtils.split(fqrys[i], ":")[0], StringUtils.split(fqrys[i], ":")[1]);
                        //strCrumb += " " + facetValue;
                        strCrumb =  facetValue;
                        urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
                        String[] acrumb = new String[] { strCrumb, urlCrumb };
                        log.info("acrumb=" + Arrays.toString(acrumb));
                        
                        this.crumbs.add(acrumb);
                }
        }
        
        /**
                 * FIXME facte definition ids
                 */
        // Hard coded for now
//      long[] facetDefIDs = new long[] {50001, 50003, 50004, 50005, 50006, 
//                      50007, 50008, 50009};
        
        String[] facetDefIDs = new String[] {"Category","F_50002","F_50001", "F_50003", "F_50004", "F_50005", "F_50006", 
                        "F_50007", "F_50008", "F_50009"};
        /**
                 * FIXME facet labels
                 */
        // Hard-coded for now
        String[] facetLabels = new String[] {"Category","Price",
                "Brand", "Form Factor", "Carry In Pocket", "Cellular Technologies", 
                "Data Connectivity", "Camera Resolution", "Built-in Memory", 
                "Talk time"
        };
        
        SolrSearchService search = new SolrSearchService(qry, fqrys, facetDefIDs);
        this.results = new LinkedHashMap<String, String[]>();
        this.results =  search.getResultMap(); 
        
    /*  
        long[] entityIDs = search.getResultEntityIDs();
        log.info("entityIDs=" + Arrays.toString(entityIDs));
        
        String[] entityNames = search.getResultEntityNames();
        log.info("entityNames=" + Arrays.toString(entityNames));
        
        this.results = new LinkedHashMap<String, String[]>();
        
        if(entityIDs != null) {
                for(int i=0; i<entityIDs.length; i++) {
                        String key = (new Long(entityIDs[i])).toString();
                        
                        String[] values = new String[] {entityNames[i]};
                        
                        this.results.put(key, values);
                }
        }
        */
        //log.info("this.results=" + this.results);
        
        // Facets
        this.facets = new LinkedHashMap<String, List<String[]>>();
        String qryString = this.request.getQueryString();
        log.info("qryString=" + qryString);
        
        
        for (int i=0; i<facetDefIDs.length; i++) {
                String facetDefID = facetDefIDs[i];
                String facetLabel = facetLabels[i];
                HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);
                if(facetDetailMap==null)
                        continue;
                List<String[]> values = new ArrayList<String[]>();
                for(String facet: facetDetailMap.keySet()){
//                      String extraInfo = "LINK";
//                      if(facetqueries.containsKey(facet)){
//                              extraInfo = "NO_LINK";
//                      }
                        String drilldownURL = qryString;
                        drilldownURL += "&fq=" + facetDefID + ":" + 
                                URLEncoder.encode(facet, "UTF-8"); 
                        String[] afacet = new String[] { facet, 
                                        facetDetailMap.get(facet).toString(), drilldownURL  };
                        values.add(afacet);    
                  
                }
                
                this.facets.put(facetLabel, values);
        }
        
        Map<String, Double> priceMap = search.getPriceStatsMap();
        this.minPrice = priceMap.get("min");
        this.maxPrice = priceMap.get("max");
        /*
        for (int i=0; i<facetDefIDs.length; i++) {
                long facetDefID = facetDefIDs[i];
                String facetLabel = facetLabels[i];
                //log.info("facetLabel=" + facetLabel);
                
                String[] facetValues = search.getFacetValues(facetDefID);
                String[] facetCounts = search.getFacetCounts(facetDefID);
                
                
                List<String[]> values = new ArrayList<String[]>();
        
                
                for(int j=0; j<facetValues.length; j++) {
                        
                        if(facetCounts[j].equals("0")) {
                                continue;
                        }
                        
                        String drilldownURL = qryString;
                        drilldownURL += "&fq=F_" + facetDefID + ":" + 
                                URLEncoder.encode(facetValues[j], "UTF-8"); 
                        //log.info("drilldownURL=" + drilldownURL);
                        
                        String[] afacet = new String[] { facetValues[j], 
                                        facetCounts[j], drilldownURL };
                        //log.info("afacet=" + Arrays.toString(afacet));
                        
                        values.add(afacet);    
                }
                //log.info("values=" + values);
                
                this.facets.put(facetLabel, values);
                
        }
        //log.info("this.facets=" + this.facets);
        */
        
        return new DefaultHttpHeaders("index")
            .disableCaching();
    }
    
    /**
     * 
     * @return
     */
    public Map<String, String[]> getResults() {
        return this.results;
    }
    
    /**
     * 
     * @return
     */
    public Map<String, List<String[]>> getFacets() {
        return this.facets;
    }
    
    public Double getMinPrice() {
        return this.minPrice;
    }
    
    public Double getMaxPrice() {
        return this.maxPrice;
    }
    
    /**
     * 
     * @return
     */
    public List<String[]> getCrumbs() {
        return this.crumbs;
    }
    
    /**
     * 
     */
    @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
        }
}