package com.jasamedika.medifirst2000.controller; import com.jasamedika.medifirst2000.constants.MessageResource; import com.jasamedika.medifirst2000.controller.base.LocaleController; import com.jasamedika.medifirst2000.entities.PgStatActivity; import com.jasamedika.medifirst2000.exception.ServiceVOException; import com.jasamedika.medifirst2000.service.PgStatActivityService; import com.jasamedika.medifirst2000.vo.LoginUserVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.orm.jpa.JpaSystemException; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.util.List; import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS; import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse; import static org.springframework.http.HttpStatus.*; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.web.bind.annotation.RequestMethod.GET; import static org.springframework.web.bind.annotation.RequestMethod.POST; @RestController @RequestMapping("/integrasi") public class IntegrationController extends LocaleController { private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationController.class); @Autowired private PgStatActivityService pgStatActivityService; @RequestMapping(value = "/pg-stat-activity/in-state-for-minutes", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getPgStatActivityInStateForMinutes(HttpServletRequest request) { try { List result = pgStatActivityService.findByStateAndInterval(); mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when pgStatActivityService.findByStateAndInterval", e.getMessage()); mapHeaderMessage.put(LABEL_SUCCESS, e.getMessage()); return getJsonResponse(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when pgStatActivityService.findByStateAndInterval", jse.getMessage()); mapHeaderMessage.put(LABEL_SUCCESS, jse.getMessage()); return getJsonResponse(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/pg-stat-activity/cancel-backend", method = POST) public ResponseEntity callPgCancelBackend(HttpServletRequest request, @RequestParam(value = "pid") Integer pid) { try { Boolean result = pgStatActivityService.callCancelBackend(pid); mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } catch (ServiceVOException sve) { LOGGER.error("Got ServiceVOException {} when pgStatActivityService.callCancelBackend : {}", sve.getMessage(), pid); mapHeaderMessage.put(LABEL_SUCCESS, sve.getMessage()); return getJsonResponse(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when pgStatActivityService.callCancelBackend : {}", jse.getMessage(), pid); mapHeaderMessage.put(LABEL_SUCCESS, jse.getMessage()); return getJsonResponse(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/pg-stat-activity/terminate-backend", method = POST) public ResponseEntity callPgTerminateBackend(HttpServletRequest request, @RequestParam(value = "pid") Integer pid) { try { Boolean result = pgStatActivityService.callTerminateBackend(pid); mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } catch (ServiceVOException sve) { LOGGER.error("Got ServiceVOException {} when pgStatActivityService.callTerminateBackend : {}", sve.getMessage(), pid); mapHeaderMessage.put(LABEL_SUCCESS, sve.getMessage()); return getJsonResponse(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when pgStatActivityService.callTerminateBackend : {}", jse.getMessage(), pid); mapHeaderMessage.put(LABEL_SUCCESS, jse.getMessage()); return getJsonResponse(CONFLICT, mapHeaderMessage); } } }