Subversion Repositories SmartDukaan

Rev

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

package com.hotspotstore.controllers;


import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.StringTokenizer;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.URIBuilder;
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.convention.annotation.Results;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.hotspotstore.model.SolrResultPojo;

@Results({
        @Result(name = "show", location = "category-show.vm"),
        @Result(name = "redirect", location = "${redirectUrl}", type = "redirect", params={"statusCode", "301"})
})

public class CategoryController extends BaseController{

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

        private String id;
        private SolrResultPojo ssr;
        private long windowSize = 24;
        private Map<String, List<List<String>>> crumbs;
        public static final Map<String, String> FACET_LABEL_MAP = Collections.unmodifiableMap(
                        new HashMap<String, String>(){
                                /**
                                 * 
                                 */
                                private static final long serialVersionUID = 1L;

                                {
                                        put("F_50010","Category");
                                        put("F_50011","Sub Category");
                                        put("F_50002","Price");
                                        put("F_50001","Brand");
                                        put("F_50006","Data Connectivity");
                                        put("F_50007","Camera Resolution");
                                        put("F_50039","");
                                        put("F_50012","Display");
                                        put("F_50013","Operating System");
                                        put("F_50014","RAM");
                                        put("F_50015","Storage Capacity");
                                        put("F_50017","Processor");
                                        put("F_50018","Capacity");
                                        put("F_50019","Class");
                                        put("F_50020","Capacity");
                                        put("F_50021","Capacity");
                                        put("F_50022","Type");
                                        put("F_50023","Interface");
                                        //Resolution for Composite
                                        put("F_50024","Resolution");
                                        put("F_50025","Optical Zoom");
                                        put("F_50026","Display Size");
                                        //Resolution for DSLR
                                        put("F_50027","Resolution");
                                        put("F_50028","Availability");
                                        put("F_50031","Operating System");
                                        put("F_50032","Screen Size");   //Screen size for mobiles
                                        put("F_50033","Screen Size");   //Screen size for laptop
                                        put("F_50034","Screen Size");   //Screen size for tablets
                                        put("F_50035","Operating System");      //Screen size for tablets
                                        put("F_50036","Camera Resolution");     //Screen size for tablets
                                        put("F_50037","Voice Call Facility");   //Screen size for tablets
                                        put("F_50038","Mobile Type");   //Mobile Type
                                        put("F_50028","Availability");
                                }
                });
    

        public Map<String, List<List<String>>> getCrumbs() {
                return crumbs;
        }

        public void setCrumbs(Map<String, List<List<String>>> crumbs) {
                this.crumbs = crumbs;
        }

        public long getWindowSize() {
                return windowSize;
        }

        public void setWindowSize(long windowSize) {
                this.windowSize = windowSize;
        }

        public long getPage() {
                return page;
        }

        public void setPage(long page) {
                this.page = page;
        }

        public long getBeginIndex(){
                if(totalResults>0)
                        return beginIndex+1;
                return beginIndex;
        }

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

        public void setBeginIndex(long beginIndex) {
                this.beginIndex = beginIndex;
        }

        private long page = 1;
        private long totalResults;
        private long beginIndex = 0;
        private String url;


        public String getUrl() {
                return url;
        }

        public void setUrl(String url) {
                this.url = url;
        }

        public long getTotalResults() {
                return totalResults;
        }

        public void setTotalResults(long totalResults) {
                this.totalResults = totalResults;
        }

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

        private static String mobileApiUrl = "http://127.0.0.1:8080/mobileapi";

        @Actions({
                @Action("/all-mobile-phones"),
                @Action("/all-mobile-accessories"),
                @Action("/all-laptop-accessories"),
                @Action("/all-tablets"),
                @Action("/all-laptops"),
                @Action("/all-cameras"),
                @Action("/business-phones"),
                @Action("/mobile-phone"),
                @Action("/high-end-multimedia-phones"),
                @Action("/low-end-multimedia-phones"),
                @Action("/basic-phones"),
                @Action("/dslr-cameras"),
                @Action("/compact-cameras"),

                @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("/hard-disk-drive"),
                @Action("/external-hard-disks"),
                @Action("/headphones"),
                @Action("/portable-music-players"),
                @Action("/data-cards"),
                @Action("/printer-cartridges"),
                @Action("/power-banks"),
                @Action("/mouse-pads"),
                @Action("/mouse"),
                @Action("/laptop-bags"),
                @Action("/laptop-batteries"),
                @Action("/laptop-chargers"),
                @Action("/all-smart-watches"),
                @Action("/wi-fi-routers"),
                @Action("/aux-cable"),
                @Action("/category"),
                @Action("/vehicle-mounts"),
                @Action("/projectors"),
                @Action("/watch-strap"),
                @Action("/laptop-stand")
        })

        public String show() throws URISyntaxException, IOException, JSONException{
                long category_id = Long.parseLong(id);
                String[] fqrys = this.request.getParameterValues("fq");
                String query = "*";
                int length = 1;
                if(fqrys!= null){
                        length += fqrys.length;
                }
                url = "?";

                String[] newfqrys = new String[length];

                String urlCrumb = url;

                this.crumbs = new HashMap<String, List<List<String>>>();

                if(query != null) {
                        urlCrumb = url; 
                }

                if(fqrys != null) {
                        log.info("fqrys=" + Arrays.toString(fqrys));
                        String filterUrl;
                        for(int i=0; i<fqrys.length; i++){
                                urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
                                newfqrys[i+1] = fqrys[i];
                                String facetName = StringUtils.split(fqrys[i], ":")[0];
                                String facetValue = StringUtils.split(fqrys[i], ":")[1];
                                String facetLabel = FACET_LABEL_MAP.get(facetName);
                                filterUrl = "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8"); 
                                List<String> acrumb = Arrays.asList(facetValue, filterUrl);
                                if(!crumbs.containsKey(facetLabel)) {
                                        crumbs.put(facetLabel, new ArrayList<List<String>>());
                                }

                                this.crumbs.get(facetLabel).add(acrumb);
                        }
                }
                //newfqrys[0] ="H_00001:true";

                url = urlCrumb;
                
                log.info("crumbs "+crumbs);

                String category_url = mobileApiUrl+"/category/"+category_id;
                URIBuilder generalSearchUrl = new URIBuilder(category_url);
                generalSearchUrl.addParameter("q", query);
                for (String s : newfqrys){
                        if (s==null || s.isEmpty()){
                                continue;
                        }
                        generalSearchUrl.addParameter("fq", s.trim());  
                }

                if(this.request.getParameter("page") != null){
                        this.page = Long.parseLong(this.request.getParameter("page"));
                        this.beginIndex = this.windowSize * (this.page-1);
                }
                generalSearchUrl.addParameter("index", String.valueOf(beginIndex));
                generalSearchUrl.addParameter("offset", String.valueOf(windowSize));
                URL url = generalSearchUrl.build().toURL();
                log.info("Search Url Search Results"+url.toString());
                InputStream is = url.openStream();
                String jsonString;
                try{
                        jsonString = IOUtils.toString(is, "UTF-8");
                }
                finally{
                        is.close();
                }
                JSONObject jsonObj = new JSONObject(jsonString);
                Gson gson = new Gson();
                ssr = gson.fromJson(jsonObj.getString("result"), SolrResultPojo.class);
                this.totalResults = ssr.getTotalCount();
                return "show";
        }
        
        public String getEncodedCompontent(String input) throws UnsupportedEncodingException{
                return URLEncoder.encode(input, "UTF-8");
        }

        public String getId() {
                return id;
        }

        public SolrResultPojo getSsr() {
                return ssr;
        }

        public void setSsr(SolrResultPojo ssr) {
                this.ssr = ssr;
        }

        public void setId(String id) {
                StringTokenizer tokenizer = new StringTokenizer(id,"-");
                while(tokenizer.hasMoreTokens()){
                        this.id = tokenizer.nextToken();
                }
        }

}