Subversion Repositories SmartDukaan

Rev

Rev 22319 | Rev 22931 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21339 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
4
import java.util.HashMap;
5
import java.util.Map;
6
 
7
import javax.servlet.http.HttpServletRequest;
8
 
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
22319 amit.gupta 11
import org.springframework.beans.factory.annotation.Autowired;
21339 kshitij.so 12
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.http.HttpStatus;
14
import org.springframework.http.MediaType;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.stereotype.Controller;
17
import org.springframework.web.bind.annotation.PathVariable;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
20
 
21
import com.google.gson.Gson;
21643 ashik.ali 22
import com.spice.profitmandi.common.enumuration.SchemeType;
21339 kshitij.so 23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 25
import com.spice.profitmandi.common.model.ProfitMandiResponse;
26
import com.spice.profitmandi.common.model.ResponseStatus;
21643 ashik.ali 27
import com.spice.profitmandi.common.web.client.RestClient;
22319 amit.gupta 28
import com.spice.profitmandi.common.web.util.ResponseSender;
21339 kshitij.so 29
import com.spice.profitmandi.web.res.EntityResponse;
30
 
31
import io.swagger.annotations.ApiImplicitParam;
32
import io.swagger.annotations.ApiImplicitParams;
33
import io.swagger.annotations.ApiOperation;
34
 
35
@Controller
36
public class EntityController {
37
 
38
	private static final Logger logger=LoggerFactory.getLogger(EntityController.class);
39
 
40
	@Value("${saholic.api.host}")
41
	private String host;
42
	@Value("${saholic.api.port}")
43
	private int port;
44
	@Value("${saholic.api.webapp}")
45
	private String webapp;
46
 
22319 amit.gupta 47
	@Autowired
48
	ResponseSender<?> responseSender;
49
 
21339 kshitij.so 50
	@RequestMapping(value = ProfitMandiConstants.URL_ENTITY, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
51
	@ApiImplicitParams({
52
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
53
				required = true, dataType = "string", paramType = "header")
54
	})
55
	@ApiOperation(value = "Get entity content")
56
	public ResponseEntity<?> getEntity(HttpServletRequest request, @PathVariable(value="entityId") String entityId){
57
		logger.info("Entity Id "+entityId);
58
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
59
		Map<String, String> params = new HashMap<String, String>();
60
		String response = null;
61
		String uri = webapp+"/entity/"+entityId;
21359 kshitij.so 62
		EntityResponse entityResponse=null;
21339 kshitij.so 63
		try {
64
			response = rc.get(uri, params);
65
			logger.info("Response is "+response);
22906 amit.gupta 66
		} catch (Exception e) {
21339 kshitij.so 67
			logger.error("Unable to get entity "+entityId,e);
21359 kshitij.so 68
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, entityResponse);
21339 kshitij.so 69
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
70
		}
71
		Gson gson = new Gson();
21359 kshitij.so 72
		entityResponse = gson.fromJson(response, EntityResponse.class);
22319 amit.gupta 73
		return responseSender.ok(entityResponse);
21339 kshitij.so 74
	}
75
 
76
 
77
 
78
 
79
}