Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.ProfitMandiResponse;
import com.spice.profitmandi.common.model.ResponseStatus;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.web.res.EntityResponse;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
public class EntityController {

        private static final Logger logger=LoggerFactory.getLogger(EntityController.class);
        
        @Value("${saholic.api.host}")
        private String host;
        @Value("${saholic.api.port}")
        private int port;
        @Value("${saholic.api.webapp}")
        private String webapp;
        
        @Autowired
        ResponseSender<?> responseSender;
        
        @RequestMapping(value = ProfitMandiConstants.URL_ENTITY, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Get entity content")
        public ResponseEntity<?> getEntity(HttpServletRequest request, @PathVariable(value="entityId") String entityId){
                logger.info("Entity Id "+entityId);
                RestClient rc = new RestClient(SchemeType.HTTP, host , port);
                Map<String, String> params = new HashMap<String, String>();
                String response = null;
                String uri = webapp+"/entity/"+entityId;
                EntityResponse entityResponse=null;
                try {
                        response = rc.get(uri, params);
                        logger.info("Response is "+response);
                } catch (Exception e) {
                        logger.error("Unable to get entity "+entityId,e);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, entityResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
                Gson gson = new Gson();
                entityResponse = gson.fromJson(response, EntityResponse.class);
                return responseSender.ok(entityResponse);
        }


        

}