Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.model.v1.widgets.WidgetService.Client;
import in.shop2020.serving.pages.PageContentKeys;
import in.shop2020.serving.pages.PageEnum;
import in.shop2020.serving.services.CategoryManager;
import in.shop2020.serving.services.PageLoaderHandler;
import in.shop2020.serving.services.SolrSearchService;
import in.shop2020.serving.utils.FileUtils;
import in.shop2020.thrift.clients.WidgetServiceClient;
import in.shop2020.util.Utils;

import java.io.File;
import java.io.IOException;
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 java.util.StringTokenizer;

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

/**
 * @author rajveer
 *
 */
public class CategoryController extends BaseController {
        
        /**
         * 
         */
        private static Log log = LogFactory.getLog(CategoryController.class);

        /**
         * 
         */
        private Map<String,String> htmlSnippets;
        private Map<String, String[]> results;
        private Map<String, String> snippets;
        private Map<String, List<String[]>> facets;
        /**
         * 
         */
        private String id;
        
        private String categoryName;
        
        private String query;
        
        private Double minPrice;
        private Double maxPrice;
        
        
        private long windowSize = 20;
        private long page = 1;
        private long totalResults;
        private long beginIndex = 0;
        private String url;
        private String priceUrl;

        /**
         * GET /category/10004
         * 
         */
    public HttpHeaders show() throws SecurityException, IOException {
        log.info("id=" + id);
        this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(Long.parseLong(id));
        
        
        htmlSnippets = new HashMap<String, String>();
        PageLoaderHandler pageLoader = new PageLoaderHandler();
                
                htmlSnippets.put("HEADER", pageLoader.getHeaderHtml(userinfo.getUserId(), userinfo.isSessionId(), userinfo.getNameOfUser()));
                htmlSnippets.put("MAIN_MENU", pageLoader.getMainMenuHtml());
                htmlSnippets.put("SEARCH_BAR", pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000));
                htmlSnippets.put("CUSTOMER_SERVICE", pageLoader.getCustomerServiceHtml());
                htmlSnippets.put("MY_RESEARCH", pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isSessionId()));
                htmlSnippets.put("FOOTER",pageLoader.getFooterHtml());
                
                htmlSnippets.put("CATEGORY_HEADER", pageLoader.getCategoryHeaderSnippet());
                htmlSnippets.put("MAIN_BANNER", "");
                htmlSnippets.put("SIDE_BANNER", "");
                htmlSnippets.put("TAB_BUTTONS", "");
                


                
                String[] facetDefIDs = new String[] {"Category","F_50002","F_50001",  "F_50006", "F_50007" };
        
        String[] facetLabels = new String[] {"Category","Price", "Brand", "Data Connectivity", "Camera Resolution"      };

        String[] fqrys = this.request.getParameterValues("fq");

        url = "?";
        
        
                
        query = "*";
        
        int length = 1;
        
        if(fqrys!= null){
                length += fqrys.length;
        }
        
        String[] newfqrys = new String[length];
        newfqrys[0] = "Category:"+categoryName;
        String urlCrumb = url;
        
        for(int i=1; i<length; i++) {
                newfqrys[i] = fqrys[i-1];
                urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
                priceUrl += "&fq=" + URLEncoder.encode(fqrys[i-1], "UTF-8");
        }
                url = urlCrumb;
                
        if(this.request.getParameter("page") != null){
                this.page = Long.parseLong(this.request.getParameter("page"));
                this.beginIndex = this.windowSize * (this.page-1);
        }
        if(this.request.getParameter("min-price") != null){
                this.minPrice = (new Double(this.request.getParameter("min-price")));
                url= url + "&min-price=" + this.request.getParameter("min-price");
        }
        if(this.request.getParameter("max-price") != null){
                this.maxPrice = (new Double(this.request.getParameter("max-price")));
                url= url + "&max-price=" + this.request.getParameter("max-price");
        }       
        
        SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice);
        this.results = new LinkedHashMap<String, String[]>();
        this.results =  search.getResultMap(); 

        
        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[]>();
                String drilldownURL = new String();
                for(String facet: facetDetailMap.keySet()){
                        
                        /*
                        if(qryString != null){
                                drilldownURL = qryString;
                                
                        }
                        */
                        drilldownURL = url;
                        
                        
                        drilldownURL = 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();
        if(priceMap != null){
                this.minPrice = priceMap.get("min");
                this.maxPrice = priceMap.get("max");
        }else{
                this.minPrice = 0.0;
                this.maxPrice = 0.0;
        }
        this.totalResults = search.getTotalResults();
        return new DefaultHttpHeaders("show");
    }
    
    /**
     * 
     * @param id
     */
    public void setId(String id) {
        StringTokenizer tokenizer = new StringTokenizer(id,"-");
        while(tokenizer.hasMoreTokens()){
                this.id = tokenizer.nextToken();
        }
    }
    
    /**
     * 
     */
    public String getId() {
        return this.id;
    }
    
    public String getCategoryName() {
        return this.categoryName;
    }
    
    
    public String getHeaderSnippet(){
                return htmlSnippets.get("HEADER");
        }
        
        public String getMainMenuSnippet(){
                return htmlSnippets.get("MAIN_MENU");
        }
        
        public String getSearchBarSnippet(){
                return htmlSnippets.get("SEARCH_BAR");
        }
                        
        public String getCustomerServiceSnippet(){
                return htmlSnippets.get("CUSTOMER_SERVICE");
        }
        
        public String getSearchHeaderSnippet(){
                return htmlSnippets.get("SEARCH_HEADER");
        }
        
        public String getSearchDetailsSnippet(){
                return htmlSnippets.get("SEARCH_DETAILS");
        }
        
        public String getMyResearchSnippet(){
                return htmlSnippets.get("MY_RESEARCH");
        }
                                
        public String getFooterSnippet(){
                return htmlSnippets.get("FOOTER");
        }
        
        public String getJsFileSnippet(){
                return htmlSnippets.get("JS_FILES");
        }
        
        public String getCssFileSnippet(){
                return htmlSnippets.get("CSS_FILES");
        }

    /**
     * 
     * @return
     */
    public String getQuery() {
        return this.query;
    }
    
    
    /**
     * 
     * @return
     */
    public Map<String, String[]> getResults() {
        return this.results;
    }
    
    /**
     * 
     * @return
     */
    public Map<String, List<String[]>> getFacets() {
        return this.facets;
    }
    
    
    public long getTotalResults(){
        return totalResults;
    }
    
    public String getUrl(){
        return this.url;
    }

    public String getPriceUrl(){
        return this.priceUrl;
    }
    
        public long getBeginIndex(){
                if(totalResults>0)
                        return beginIndex+1;
                return beginIndex;
        }

        public long getTotalPages() {
                return 1 + totalResults/windowSize;
        }

        public long getCurrentPage() {
                return this.page;
        }

    public Double getMinPrice() {
        return this.minPrice;
    }
    
    public Double getMaxPrice() {
        return this.maxPrice;
    }
    
    public Map<String, String> getSnippets() throws Exception {
        if(results != null){
                snippets = new HashMap<String, String>();       
                for(String docId: results.keySet()){
                                snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
                        }
        }
                return snippets;
    }

}