Subversion Repositories SmartDukaan

Rev

Rev 35458 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 35458 Rev 37144
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.io.IOException;
3
import java.io.IOException;
4
import java.net.URISyntaxException;
4
import java.net.URISyntaxException;
-
 
5
import java.util.Collections;
5
import java.util.List;
6
import java.util.List;
-
 
7
import java.util.stream.Collectors;
-
 
8
 
-
 
9
import javax.servlet.http.HttpServletRequest;
6
 
10
 
7
import org.apache.logging.log4j.Logger;
11
import org.apache.logging.log4j.Logger;
8
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.LogManager;
9
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.MediaType;
14
import org.springframework.http.MediaType;
Line 18... Line 22...
18
import com.eclipsesource.json.JsonArray;
22
import com.eclipsesource.json.JsonArray;
19
import com.eclipsesource.json.JsonObject;
23
import com.eclipsesource.json.JsonObject;
20
import com.eclipsesource.json.JsonValue;
24
import com.eclipsesource.json.JsonValue;
21
import com.google.gson.Gson;
25
import com.google.gson.Gson;
22
import com.google.gson.reflect.TypeToken;
26
import com.google.gson.reflect.TypeToken;
-
 
27
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
28
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
29
import com.spice.profitmandi.common.model.UserInfo;
24
import com.spice.profitmandi.common.web.util.ResponseSender;
30
import com.spice.profitmandi.common.web.util.ResponseSender;
-
 
31
import com.spice.profitmandi.service.catalog.BrandsService;
25
import com.spice.profitmandi.web.res.SolrSearchResultResponse;
32
import com.spice.profitmandi.web.res.SolrSearchResultResponse;
26
import com.spice.profitmandi.web.res.SolrSuggestionResponse;
33
import com.spice.profitmandi.web.res.SolrSuggestionResponse;
27
import com.spice.profitmandi.web.services.SolrService;
34
import com.spice.profitmandi.web.services.SolrService;
28
 
35
 
29
import io.swagger.annotations.ApiImplicitParam;
36
import io.swagger.annotations.ApiImplicitParam;
Line 36... Line 43...
36
 
43
 
37
	private static final Logger logger=LogManager.getLogger(SolrSearchController.class);
44
	private static final Logger logger=LogManager.getLogger(SolrSearchController.class);
38
 
45
 
39
	@Autowired
46
	@Autowired
40
	private SolrService solrService;
47
	private SolrService solrService;
41
	
48
 
42
	@Autowired
49
	@Autowired
43
	private ResponseSender<?> responseSender;
50
	private ResponseSender<?> responseSender;
44
 
51
 
-
 
52
	@Autowired
-
 
53
	private BrandsService brandsService;
-
 
54
 
45
	private static final int max_count = 10;
55
	private static final int max_count = 10;
46
	private static final int max_count_accesories = 5;
56
	private static final int max_count_accesories = 5;
47
	private double max_accessory_score, max_mobile_score, max_tablet_score;
57
	private double max_accessory_score, max_mobile_score, max_tablet_score;
48
	private int mobile_records, tablet_records;
58
	private int mobile_records, tablet_records;
49
	private JsonObject result_json;
59
	private JsonObject result_json;
Line 52... Line 62...
52
	@ApiImplicitParams({
62
	@ApiImplicitParams({
53
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
63
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
54
				required = true, dataType = "string", paramType = "header")
64
				required = true, dataType = "string", paramType = "header")
55
	})
65
	})
56
	@ApiOperation(value = "Search Results")
66
	@ApiOperation(value = "Search Results")
57
	public ResponseEntity<?> getSearchResults(@RequestParam("search_text") String search_text, @RequestParam("offset") int offset){
67
	public ResponseEntity<?> getSearchResults(HttpServletRequest request, @RequestParam("search_text") String search_text, @RequestParam("offset") int offset) throws ProfitMandiBusinessException {
58
		logger.info("search_text : "+search_text+" offset : "+offset);
68
		logger.info("search_text : "+search_text+" offset : "+offset);
59
		String jsonString = null;
69
		String jsonString = null;
60
		try {
70
		try {
61
			jsonString = solrService.getSearchResults(search_text.trim(), offset);
71
			jsonString = solrService.getSearchResults(search_text.trim(), offset);
62
			logger.info("Response from solr "+jsonString);
72
			logger.info("Response from solr "+jsonString);
Line 68... Line 78...
68
		for (JsonValue j : result_json ){
78
		for (JsonValue j : result_json ){
69
			j.asObject().add("productUrl", j.asObject().get("ids").asArray().get(0)+"/"+j.asObject().get("id").asString());
79
			j.asObject().add("productUrl", j.asObject().get("ids").asArray().get(0)+"/"+j.asObject().get("id").asString());
70
		}
80
		}
71
		Gson gson = new Gson();
81
		Gson gson = new Gson();
72
		List<SolrSearchResultResponse> solrSearchResultResponse = gson.fromJson(result_json.toString(), new TypeToken<List<SolrSearchResultResponse>>(){}.getType());
82
		List<SolrSearchResultResponse> solrSearchResultResponse = gson.fromJson(result_json.toString(), new TypeToken<List<SolrSearchResultResponse>>(){}.getType());
-
 
83
		// collection1 docs carry no brand field, so restricted brands are dropped by title match
-
 
84
		List<String> restrictedBrands = this.restrictedBrands(request);
-
 
85
		solrSearchResultResponse = solrSearchResultResponse.stream()
-
 
86
				.filter(x -> !isBlockedTitle(x.getTitle(), restrictedBrands)).collect(Collectors.toList());
73
		return responseSender.ok(solrSearchResultResponse);
87
		return responseSender.ok(solrSearchResultResponse);
74
	}
88
	}
75
 
89
 
76
 
90
 
77
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SUGGESTION, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
91
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SUGGESTION, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
78
	@ApiImplicitParams({
92
	@ApiImplicitParams({
79
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
93
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
80
				required = true, dataType = "string", paramType = "header")
94
				required = true, dataType = "string", paramType = "header")
81
	})
95
	})
82
	@ApiOperation(value = "Auto Suggest")
96
	@ApiOperation(value = "Auto Suggest")
83
	public ResponseEntity<?> getSuggestions(@RequestParam("search_text") String searchText){
97
	public ResponseEntity<?> getSuggestions(HttpServletRequest request, @RequestParam("search_text") String searchText) throws ProfitMandiBusinessException {
84
		logger.info("Suggestion text : "+searchText);
98
		logger.info("Suggestion text : "+searchText);
85
		String jsonString;
99
		String jsonString;
86
		try {
100
		try {
87
			jsonString = solrService.getSuggestions(searchText.trim());
101
			jsonString = solrService.getSuggestions(searchText.trim());
88
		} catch (URISyntaxException | IOException e) {
102
		} catch (URISyntaxException | IOException e) {
Line 90... Line 104...
90
			return responseSender.internalServerError(e);
104
			return responseSender.internalServerError(e);
91
		}
105
		}
92
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
106
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
93
		Gson gson = new Gson();
107
		Gson gson = new Gson();
94
		List<SolrSuggestionResponse> solrSearchResultResponse = gson.fromJson(sanatizedResults().toString(), new TypeToken<List<SolrSuggestionResponse>>(){}.getType());
108
		List<SolrSuggestionResponse> solrSearchResultResponse = gson.fromJson(sanatizedResults().toString(), new TypeToken<List<SolrSuggestionResponse>>(){}.getType());
-
 
109
		List<String> restrictedBrands = this.restrictedBrands(request);
-
 
110
		solrSearchResultResponse = solrSearchResultResponse.stream()
-
 
111
				.filter(x -> !isBlockedTitle(x.getTitle(), restrictedBrands)).collect(Collectors.toList());
95
		return responseSender.ok(solrSearchResultResponse);
112
		return responseSender.ok(solrSearchResultResponse);
96
	}
113
	}
97
 
114
 
-
 
115
	private List<String> restrictedBrands(HttpServletRequest request) {
-
 
116
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
117
		if (userInfo == null) {
-
 
118
			return Collections.emptyList();
-
 
119
		}
-
 
120
		try {
-
 
121
			return brandsService.restrictedBrands(userInfo.getRetailerId());
-
 
122
		} catch (ProfitMandiBusinessException e) {
-
 
123
			logger.error("Could not resolve restricted brands", e);
-
 
124
			return Collections.emptyList();
-
 
125
		}
-
 
126
	}
-
 
127
 
-
 
128
	private boolean isBlockedTitle(String title, List<String> blockedBrands) {
-
 
129
		if (title == null || blockedBrands.isEmpty()) {
-
 
130
			return false;
-
 
131
		}
-
 
132
		return blockedBrands.stream().anyMatch(x -> title.regionMatches(true, 0, x, 0, x.length())
-
 
133
				&& (title.length() == x.length() || !Character.isLetterOrDigit(title.charAt(x.length()))));
-
 
134
	}
-
 
135
 
98
	private JsonArray sanatizedResults(){
136
	private JsonArray sanatizedResults(){
99
		//Need to re-write this section.This sucks badly
137
		//Need to re-write this section.This sucks badly
100
		JsonArray output = Json.array().asArray();
138
		JsonArray output = Json.array().asArray();
101
		JsonArray temp_mobiles = getResults(3);
139
		JsonArray temp_mobiles = getResults(3);
102
		JsonArray temp_tablets = getResults(5);
140
		JsonArray temp_tablets = getResults(5);