Rev 21309 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.time.LocalDateTime;import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.spice.profitmandi.common.ResponseCodeHolder;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.Retailer;import com.spice.profitmandi.dao.repository.RetailerRepository;import com.spice.profitmandi.web.model.ProfitMandiResponse;import com.spice.profitmandi.web.model.Response;import com.spice.profitmandi.web.model.ResponseStatus;@Controllerpublic class RetailerController {private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);@AutowiredRetailerRepository retailerRepository;@RequestMapping(value = ProfitMandiConstants.URL_API, method=RequestMethod.POST)public ResponseEntity<?> createRetailer(HttpServletRequest request){LOGGER.info("requested url : "+request.getRequestURL().toString());final Retailer retailer = (Retailer)request.getAttribute(ProfitMandiConstants.RETAILER);request.removeAttribute(ProfitMandiConstants.API);try {retailer.setCreateTimestamp(LocalDateTime.now());retailer.setUpdateTimestamp(LocalDateTime.now());retailerRepository.persist(retailer);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1000"));return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);}catch (ProfitMandiBusinessException pmbe) {LOGGER.error("ProfitMandi error: ", pmbe);final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);}catch (Exception e) {LOGGER.error("Internal Server Error : ",e);final Response response=new Response("","","", e.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)public ResponseEntity<?> getAll(HttpServletRequest request){LOGGER.info("requested url : "+request.getRequestURL().toString());try {final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectAll());return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}catch (Exception e) {LOGGER.error("Internal Server Error : ",e);final Response response=new Response("","","", e.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") String id){LOGGER.info("requested url : "+request.getRequestURL().toString());try {final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectById(id));return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}catch (ProfitMandiBusinessException pmbe) {LOGGER.error("ProfitMandi error: ", pmbe);final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);}catch (Exception e) {LOGGER.error("Internal Server Error : ",e);final Response response=new Response("","","", e.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){LOGGER.info("requested url : "+request.getRequestURL().toString());try {final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectByName(name));return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}catch (ProfitMandiBusinessException pmbe) {LOGGER.error("ProfitMandi error: ", pmbe);final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);}catch (Exception e) {LOGGER.error("Internal Server Error : ",e);final Response response=new Response("","","", e.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") String id){LOGGER.info("requested url : "+request.getRequestURL().toString());try {retailerRepository.deleteById(id);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}catch (ProfitMandiBusinessException pmbe) {LOGGER.error("ProfitMandi error: ", pmbe);final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);}catch (Exception e) {LOGGER.error("Internal Server error : ",e);final Response response=new Response("","","", e.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);}}@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") String shopId, @RequestParam(name = "retailerId") String retailerId){LOGGER.info("requested url : "+request.getRequestURL().toString());try {retailerRepository.removeShop(shopId, retailerId);final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);}catch (ProfitMandiBusinessException pmbe) {LOGGER.error("ProfitMandi error: ", pmbe);final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);}catch (Exception e) {LOGGER.error("Internal Server error : ",e);final Response response=new Response("","","", e.getMessage());final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);}}}