Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
339 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.serving.services.SolrSearchService;
7
 
8
import java.io.UnsupportedEncodingException;
9
import java.net.URLEncoder;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.LinkedHashMap;
13
import java.util.List;
14
import java.util.Map;
15
 
16
import javax.servlet.http.HttpServletRequest;
17
 
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.juli.logging.Log;
20
import org.apache.juli.logging.LogFactory;
21
import org.apache.struts2.interceptor.ServletRequestAware;
22
import org.apache.struts2.rest.DefaultHttpHeaders;
23
import org.apache.struts2.rest.HttpHeaders;
24
 
25
/**
26
 * @author naveen
27
 *
28
 */
29
public class SearchController extends BaseController 
30
	implements ServletRequestAware {
31
 
32
	/**
33
	 * 
34
	 */
35
	private static Log log = LogFactory.getLog(SearchController.class);
36
 
37
	/**
38
	 * 
39
	 */
40
	private HttpServletRequest request;
41
 
42
	/**
43
	 * 
44
	 */
45
	private Map<String, String[]> results;
46
 
47
	/**
48
	 * 
49
	 */
50
	private Map<String, List<String[]>> facets;
51
 
52
	/**
53
	 * 
54
	 */
55
	private List<String[]> crumbs;
56
 
57
	/**
58
	 * 
59
	 * @return
60
	 * @throws UnsupportedEncodingException
61
	 */
62
    // GET /query
63
    public HttpHeaders index() throws UnsupportedEncodingException {
64
    	log.info("this.request=" + this.request);
65
 
66
    	String qry = this.request.getParameter("q");
67
    	log.info("qry=" + qry);
68
 
69
    	String[] fqrys = this.request.getParameterValues("fq");
70
 
71
    	this.crumbs = new ArrayList<String[]>();
72
 
73
    	String strCrumb = "";
74
    	String urlCrumb = "";
75
    	if(qry != null) {
76
	    	strCrumb = qry;
77
	    	urlCrumb = "q=" + URLEncoder.encode(qry, "UTF-8"); 
78
	    	this.crumbs.add(new String[] { strCrumb, urlCrumb } );
79
			log.info("acrumb=" + 
80
					Arrays.toString(new String[] { strCrumb, urlCrumb }));
81
    	}
82
 
83
    	if(fqrys != null) {
84
	    	log.info("fqrys=" + Arrays.toString(fqrys));
85
 
86
	    	for(int i=0; i<fqrys.length; i++) {
87
	    		String facetValue = StringUtils.split(fqrys[i], ":")[1];
88
	    		strCrumb += " " + facetValue;
89
 
90
	    		urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");
91
	    		String[] acrumb = new String[] { strCrumb, urlCrumb };
92
	    		log.info("acrumb=" + Arrays.toString(acrumb));
93
 
94
	    		this.crumbs.add(acrumb);
95
	    	}
96
    	}
97
 
98
    	// Hard coded for now
99
    	long[] facetDefIDs = new long[] {50001, 50003, 50004, 50005, 50006, 
100
    			50007, 50008, 50009};
101
 
102
    	// Hard-coded for now
103
    	String[] facetLabels = new String[] {
104
	    	"Brand", "Form Factor", "Carry In Pocket", "Cellular Technologies", 
105
	    	"Data Connectivity", "Camera Resolution", "Built-in Memory", 
106
	    	"Talk time"
107
    	};
108
 
109
    	SolrSearchService search = new SolrSearchService(qry, fqrys,
110
    			facetDefIDs);
111
 
112
    	long[] entityIDs = search.getResultEntityIDs();
113
    	log.info("entityIDs=" + Arrays.toString(entityIDs));
114
 
115
    	String[] entityNames = search.getResultEntityNames();
116
    	log.info("entityNames=" + Arrays.toString(entityNames));
117
 
118
    	this.results = new LinkedHashMap<String, String[]>();
119
 
120
    	if(entityIDs != null) {
121
	    	for(int i=0; i<entityIDs.length; i++) {
122
	    		String key = (new Long(entityIDs[i])).toString();
123
 
124
	    		String[] values = new String[] {entityNames[i]};
125
 
126
	    		this.results.put(key, values);
127
	    	}
128
    	}
129
    	//log.info("this.results=" + this.results);
130
 
131
    	// Facets
132
    	this.facets = new LinkedHashMap<String, List<String[]>>();
133
    	String qryString = this.request.getQueryString();
134
    	log.info("qryString=" + qryString);
135
 
136
    	for (int i=0; i<facetDefIDs.length; i++) {
137
    		long facetDefID = facetDefIDs[i];
138
    		String facetLabel = facetLabels[i];
139
    		//log.info("facetLabel=" + facetLabel);
140
 
141
    		String[] facetValues = search.getFacetValues(facetDefID);
142
    		String[] facetCounts = search.getFacetCounts(facetDefID);
143
 
144
    		List<String[]> values = new ArrayList<String[]>();
145
    		for(int j=0; j<facetValues.length; j++) {
146
 
147
    			if(facetCounts[j].equals("0")) {
148
    				continue;
149
    			}
150
 
151
    			String drilldownURL = qryString;
152
    			drilldownURL += "&fq=F_" + facetDefID + ":" + 
153
    				URLEncoder.encode(facetValues[j], "UTF-8"); 
154
    			//log.info("drilldownURL=" + drilldownURL);
155
 
156
    			String[] afacet = new String[] { facetValues[j], 
157
    					facetCounts[j], drilldownURL };
158
    			//log.info("afacet=" + Arrays.toString(afacet));
159
 
160
    			values.add(afacet);    
161
    		}
162
    		//log.info("values=" + values);
163
 
164
    		this.facets.put(facetLabel, values);
165
    	}
166
    	//log.info("this.facets=" + this.facets);
167
 
168
 
169
        return new DefaultHttpHeaders("index")
170
            .disableCaching();
171
    }
172
 
173
    /**
174
     * 
175
     * @return
176
     */
177
    public Map<String, String[]> getResults() {
178
    	return this.results;
179
    }
180
 
181
    /**
182
     * 
183
     * @return
184
     */
185
    public Map<String, List<String[]>> getFacets() {
186
    	return this.facets;
187
    }
188
 
189
    /**
190
     * 
191
     * @return
192
     */
193
    public List<String[]> getCrumbs() {
194
    	return this.crumbs;
195
    }
196
 
197
    /**
198
     * 
199
     */
200
    @Override
201
	public void setServletRequest(HttpServletRequest request) {
202
		this.request = request;
203
	}
204
}