Subversion Repositories SmartDukaan

Rev

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