Subversion Repositories SmartDukaan

Rev

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

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

import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
import in.shop2020.serving.cache.SnippetCacheWrapper;
import in.shop2020.serving.services.SolrSearchService;
import in.shop2020.serving.utils.Utils;
import in.shop2020.utils.CategoryManager;

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.Map.Entry;
import java.util.StringTokenizer;

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;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

/**
 * @author rajveer
 *
 */
@Result(name = "show", location = "category-show.vm")
public class CategoryController 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 id;
        
        private String categoryName;
        private String categoryTitle;
        private String facetSelection = "";
        
        private String query;
        
        private Double minPrice;
        private Double maxPrice;
        private long categoryId;
        
        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("/all-mobile-phones"),
                @Action("/all-mobile-accessories"),
                @Action("/all-tablets"),
                @Action("/all-laptops"),
                
                @Action("/business-phones"),
                @Action("/high-end-multimedia-phones"),
                @Action("/low-end-multimedia-phones"),
                @Action("/basic-phones"),
                
                @Action("/bluetooth-headset"),
                @Action("/memory-card"),
                @Action("/battery"),
                @Action("/headset"),
                @Action("/charger"),
                @Action("/pen-drive"),
                @Action("/carrying-case"),
                @Action("/car-charger"),
                @Action("/screen-guard"),
                
                @Action("/face-plate"),
                @Action("/decal"),
                @Action("/data-cable"),
                @Action("/ear-buds"),
                @Action("/cleaning-kit"),
                @Action("/speaker"),
                
                
                
                @Action("/category")
        })
         
    public HttpHeaders show() throws SecurityException, IOException {
        log.info("id=" + id);
        categoryId = Long.parseLong(id);
        this.categoryName = CategoryManager.getCategoryManager().getCategoryLabel(categoryId);
        
        if (categoryId == Utils.MOBILE_PHONES_CATEGORY || categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY || categoryId == Utils.TABLETS_CATEGORY ){
                this.categoryTitle = categoryName + " Price List India" + " | " + this.categoryName;
        }
        else {
                this.categoryTitle = this.categoryName.replace("Phones", "Mobile Phones");
        }

                String[] facetDefIDs = Utils.facetDefIDs;
        String[] facetLabels = Utils.facetLabels;

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

        url = "?";
                
        query = "*";
        
        int length = 1;
        
        if(fqrys!= null){
                length += fqrys.length;
        }
        
        String[] newfqrys = new String[length];
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id() == Utils.ROOT_CATEGORY){
                newfqrys[0] = "F_50010:"+categoryName;
        }else{
                newfqrys[0] = "F_50011:"+categoryName;
        }
        
        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");
                StringBuilder filterUrl = new StringBuilder(url);
                String facetName = StringUtils.split(fqrys[i-1], ":")[0];
                String facetValue = StringUtils.split(fqrys[i-1], ":")[1];
                String facetLabel = facetLabels[arrList.indexOf(facetName)];
                this.categoryTitle += " | " + facetLabel + " " + facetValue;
                this.facetSelection += facetValue + " ";
                for(int j=1; j<length; j++) {
                        if(i==j){
                                continue;
                        }
                        filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j-1], "UTF-8"));
                }
                String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };
                log.info("acrumb=" + Arrays.toString(acrumb));
                this.crumbs.add(acrumb);

        }
                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("minPrice") != null){
                this.minPrice = (new Double(this.request.getParameter("minPrice")));
                url= url + "&minPrice=" + this.request.getParameter("minPrice");
        }
        if(this.request.getParameter("maxPrice") != null){
                this.maxPrice = (new Double(this.request.getParameter("maxPrice")));
                url= url + "&maxPrice=" + this.request.getParameter("maxPrice");
        }       

        String sortOrder = "F_50002+asc";
        
        SolrSearchService search = new SolrSearchService(query, newfqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, Utils.ROOT_CATEGORY, sortOrder, sourceId);
        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[]>();
                for(Entry<String, Integer> facetEntry: facetDetailMap.entrySet()){
                    String facet = facetEntry.getKey();
                        String drilldownURL = url + "&fq=" + facetDefID + ":" + 
                                URLEncoder.encode(facet, "UTF-8");
                        String[] afacet = new String[] { facet, 
                              facetEntry.getValue().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");
    }
    
    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 getPageMetaDesc() {
        return "Best price "
                + this.facetSelection.replaceAll("Phones", "")
                + this.categoryName
                        .replaceAll("Mobile Phones", "mobile phones")
                        .replaceAll("Phones", "mobile phones")
                + " in India. Experience n' buy online. FREE Next Day delivery. Original product - Full manufacturer warranty. Comprehensive reviews."; 
    }
    
    public String getPageMetaKeywords() {
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_PHONES_CATEGORY){
                return this.categoryName + ", mobile phone";
        }
        if(CategoryManager.getCategoryManager().getCategory(Long.parseLong(id)).getParent_category_id()== Utils.MOBILE_ACCESSORIES_CATEGORY){
                return this.categoryName + ", phone accessories";
        }
        return "";
    }
    
    public String getCategoryTitle() {
        return this.categoryTitle;
    }   
        
        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 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 String getPriceUrl() {
        return this.priceUrl;
    }
    
    public List<String[]> getCrumbs() {
        return this.crumbs;
    }

    public Map<String, String> getSnippets(){
        if(results != null){
                snippets = new HashMap<String, String>();       
                for(String docId: results){
                    String snippet = (String) SnippetCacheWrapper.getSnippet(CacheKeys.CATEGORY_SNIPPET_CACHE_KEY, docId, sourceId);
                    if (snippet != null) {
                        snippets.put(docId, snippet);
                    }
                        }
        }
                return snippets;
    }
    
    @Override
        public String getHeaderSnippet() {
                String url = request.getQueryString();
                if (url == null)        {
                        url = "";
                } else  {
                        url = "?" + url;
                }
                url = request.getRequestURI() + url;
                
                return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), url , categoryId, true);
        }
}