Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.serving.services.SolrSearchService;
import in.shop2020.serving.utils.FileUtils;
import in.shop2020.serving.utils.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 org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;

/**
 * @author rajveer
 *
 */
@Result(name = "index", location = "brand-index.vm")
public class BrandController extends BaseController {
        
        private static final long serialVersionUID = 8477108528765030321L;

        private static Logger log = Logger.getLogger(Class.class);

        private List<String> results;
        private Map<String, String> snippets;
        private Map<String, List<String[]>> facets;
        private List<String[]> crumbs;
        /**
         * 
         */
        private String brandName;
        private String brandTitle;
        private String facetSelection = "";
        
        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 /abc/10004
         * 
         */
        @Actions({
            @Action("/nokia"),
            @Action("/samsung"),
                @Action("/spice"),
                @Action("/blackberry"),
                @Action("/sony-ericsson"),
                @Action("/micromax"),
                @Action("/lg"),
                @Action("/htc"),
                @Action("/motorola"),
                @Action("/dell"),
                @Action("/apple"),
                
                @Action("/brand")
        })
         
    public String index() throws SecurityException, IOException {
        
            String uri = request.getRequestURI();
            this.brandName = uri.replace("/", "");
            if(this.brandName.equals("blackberry")){
            this.brandName = "BlackBerry";
        }else if(this.brandName.equals("lg")){
            this.brandName = "LG";
        }else if(this.brandName.equals("htc")){
            this.brandName = "HTC";
        }else{
                String[] parts = this.brandName.split("-");
                StringBuffer brand = new StringBuffer(); 
                for(int i=0; i < parts.length; i++){
                    if(i>0)
                    brand.append(" ");
                    brand.append(Character.toUpperCase(parts[i].charAt(0)));
                    brand.append(parts[i].substring(1)); 
                }
                this.brandName = brand.toString();
        }
            
            if (this.brandName.equals("Samsung") || this.brandName.equals("Sony Ericsson")) {
                this.brandTitle = this.brandName + " Mobile Phones India | Buy " + this.brandName + " Phones" ;
            }
            else {
            this.brandTitle = this.brandName + " Mobile Price India" ;
            }
        

        String[] facetDefIDs = new String[] {"F_50010","F_50011","F_50002","F_50001",  "F_50006", "F_50007" };
        String[] facetLabels = new String[] {"Category","Sub 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+1];
        
        newfqrys[0] = "F_50001:" + brandName;
        
        
        String urlCrumb = url;
        
        this.crumbs = new ArrayList<String[]>();
        List<String> arrList = Arrays.asList(facetDefIDs);
        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");
                String filterUrl = url;
                String facetName = StringUtils.split(fqrys[i-1], ":")[0];
                String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
                String facetLabel = facetLabels[arrList.indexOf(facetName)];
                this.brandTitle += " | " + facetLabel + " " + facetValue;
                this.facetSelection += facetValue + " ";
                for(int j=1; j<length; j++) {
                        if(i==j){
                                continue;
                        }
                        filterUrl += "&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8");
                }
                String[] acrumb = new String[] { facetLabel, facetValue, filterUrl };
                log.info("acrumb=" + Arrays.toString(acrumb));
                this.crumbs.add(acrumb);

        }
        
        newfqrys[length] = "F_50010:" + "Mobile Phones";
        
                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");
        }       

        query = query + "&sort=F_50002+asc";
        
        SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, 10000, null);
        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 "index";
    }
    
    
    public String getBrandName() {
        return this.brandName;
    }   
        
    public String getPageMetaDesc() {
        return "Best price " + this.facetSelection + this.brandName.replaceAll("Phones", "mobile phones") 
            + " mobile phones in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
    }
    
    public String getPageMetaKeywords() {
                return this.brandName + " mobile phone, " + this.brandName + " mobile phone price, "  + this.brandName + " mobile phone deals, "+ this.brandName + " mobile phone india, buy " + this.brandName + " mobiles";
    }
    
    public String getBrandTitle() {
        return this.brandTitle;
    }   
        
        public String getQuery() {
        return this.query;
    }
    
    
    public List<String> getResults() {
        return this.results;
    }
    
    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-1)/windowSize;
        }

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

    public Double getMinPrice() {
        return this.minPrice;
    }
    
    public Double getMaxPrice() {
        return this.maxPrice;
    }
    
    public List<String[]> getCrumbs() {
        return this.crumbs;
    }

    public Map<String, String> getSnippets(){
        if(results != null){
                snippets = new HashMap<String, String>();       
                for(String docId: results){
                                try {
                                        snippets.put(docId, FileUtils.read( Utils.EXPORT_ENTITIES_PATH + docId + File.separator +"CategorySnippet.html"));
                                } catch (IOException e) {
                                        log.error(e);
                                }
                        }
        }
                return snippets;
    }

}