Subversion Repositories SmartDukaan

Rev

Rev 21293 | Rev 21308 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21293 Rev 21297
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.time.LocalDateTime;
5
import java.time.LocalDateTime;
-
 
6
import java.util.ArrayList;
-
 
7
import java.util.List;
6
 
8
 
7
import org.slf4j.Logger;
9
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
10
import org.slf4j.LoggerFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.http.HttpStatus;
12
import org.springframework.http.HttpStatus;
Line 20... Line 22...
20
import com.eclipsesource.json.JsonObject;
22
import com.eclipsesource.json.JsonObject;
21
import com.eclipsesource.json.JsonValue;
23
import com.eclipsesource.json.JsonValue;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23
import com.spice.profitmandi.web.model.ProfitMandiResponse;
25
import com.spice.profitmandi.web.model.ProfitMandiResponse;
24
import com.spice.profitmandi.web.model.ResponseStatus;
26
import com.spice.profitmandi.web.model.ResponseStatus;
-
 
27
import com.spice.profitmandi.web.res.SolrSearchResultResponse;
-
 
28
import com.spice.profitmandi.web.res.SolrSuggestionResponse;
25
import com.spice.profitmandi.web.services.SolrService;
29
import com.spice.profitmandi.web.services.SolrService;
26
 
30
 
27
import io.swagger.annotations.ApiImplicitParam;
31
import io.swagger.annotations.ApiImplicitParam;
28
import io.swagger.annotations.ApiImplicitParams;
32
import io.swagger.annotations.ApiImplicitParams;
29
import io.swagger.annotations.ApiOperation;
33
import io.swagger.annotations.ApiOperation;
30
 
34
 
-
 
35
import com.google.gson.Gson;
-
 
36
import com.google.gson.reflect.TypeToken;
-
 
37
 
31
@Controller
38
@Controller
32
public class SolrSearchController {
39
public class SolrSearchController {
33
 
40
 
34
	private static final Logger logger=LoggerFactory.getLogger(SolrSearchController.class);
41
	private static final Logger logger=LoggerFactory.getLogger(SolrSearchController.class);
35
 
42
 
Line 51... Line 58...
51
	public ResponseEntity<?> getSearchResults(@RequestParam("search_text") String search_text, @RequestParam("offset") int offset){
58
	public ResponseEntity<?> getSearchResults(@RequestParam("search_text") String search_text, @RequestParam("offset") int offset){
52
		logger.info("search_text : "+search_text+" offset : "+offset);
59
		logger.info("search_text : "+search_text+" offset : "+offset);
53
		String jsonString = null;
60
		String jsonString = null;
54
		try {
61
		try {
55
			jsonString = solrService.getSearchResults(search_text.trim(), offset);
62
			jsonString = solrService.getSearchResults(search_text.trim(), offset);
-
 
63
			logger.info("Response from solr "+jsonString);
56
		} catch (URISyntaxException | IOException e) {
64
		} catch (URISyntaxException | IOException e) {
57
			logger.error("Error while gettting search results from solr",e);
65
			logger.error("Error while gettting search results from solr",e);
58
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SEARCH , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, (String) new JsonArray().toString());
66
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SEARCH , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, new ArrayList<SolrSearchResultResponse>());
59
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
67
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
60
		}
68
		}
61
		JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
69
		JsonArray result_json = Json.parse(jsonString).asObject().get("response").asObject().get("docs").asArray();
62
		for (JsonValue j : result_json ){
70
		for (JsonValue j : result_json ){
63
			j.asObject().add("productUrl", j.asObject().get("ids").asArray().get(0)+"/"+j.asObject().get("id").asString());
71
			j.asObject().add("productUrl", j.asObject().get("ids").asArray().get(0)+"/"+j.asObject().get("id").asString());
64
		}
72
		}
65
		logger.info("Before creating response "+result_json);
73
		Gson gson = new Gson();
-
 
74
		List<SolrSearchResultResponse> solrSearchResultResponse = gson.fromJson(result_json.toString(), new TypeToken<List<SolrSearchResultResponse>>(){}.getType());
66
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SEARCH , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, result_json.toString());
75
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SEARCH , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, solrSearchResultResponse);
67
		logger.info("After creating response "+result_json.toString());
-
 
68
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
76
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
69
	}
77
	}
70
 
78
 
71
 
79
 
72
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SUGGESTION, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
80
	@RequestMapping(value = ProfitMandiConstants.URL_SOLR_SUGGESTION, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
Line 80... Line 88...
80
		String jsonString;
88
		String jsonString;
81
		try {
89
		try {
82
			jsonString = solrService.getSuggestions(search_text.trim());
90
			jsonString = solrService.getSuggestions(search_text.trim());
83
		} catch (URISyntaxException | IOException e) {
91
		} catch (URISyntaxException | IOException e) {
84
			logger.error("Error while getting suggestions from solr",e);
92
			logger.error("Error while getting suggestions from solr",e);
85
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SUGGESTION , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, new JsonArray().toString());
93
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SUGGESTION , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, new ArrayList<SolrSuggestionResponse>());
86
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
94
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
87
		}
95
		}
88
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
96
		result_json = Json.parse(jsonString).asObject().get("grouped").asObject();
89
		logger.info("Before creating response "+result_json);
97
		Gson gson = new Gson();
-
 
98
		List<SolrSuggestionResponse> solrSearchResultResponse = gson.fromJson(sanatizedResults().toString(), new TypeToken<List<SolrSuggestionResponse>>(){}.getType());
90
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SUGGESTION , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, sanatizedResults().toString());
99
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), ProfitMandiConstants.URL_SOLR_SUGGESTION , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS,solrSearchResultResponse);
91
		logger.info("After creating response "+result_json.toString());
-
 
92
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
100
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
93
	}
101
	}
94
 
102
 
95
	private JsonArray sanatizedResults(){
103
	private JsonArray sanatizedResults(){
96
 
104