package com.jasamedika.medifirst2000.controller; import com.jasamedika.medifirst2000.constants.MessageResource; import com.jasamedika.medifirst2000.controller.base.LocaleController; import com.jasamedika.medifirst2000.exception.ServiceVOException; import com.jasamedika.medifirst2000.service.IpsrsPeminjamanAlatService; import com.jasamedika.medifirst2000.util.rest.RestUtil; import com.jasamedika.medifirst2000.vo.IpsrsPeminjamanAlatVO; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.orm.jpa.JpaSystemException; import org.springframework.web.bind.annotation.RequestBody; 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 javax.validation.Valid; import java.util.Map; import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE; import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS; import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus; import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse; import static org.slf4j.LoggerFactory.getLogger; 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("/ipsrs-peminjaman-alat") public class IpsrsPeminjamanAlatController extends LocaleController { private static final Logger LOGGER = getLogger(IpsrsPeminjamanAlatController.class); @Autowired private IpsrsPeminjamanAlatService service; @RequestMapping(value = "/get-login-user/", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getLoginUser(HttpServletRequest request) { try { Map result = service.getUserLogin(); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } else { return getJsonResponse(null, NOT_FOUND, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when getUserLogin", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemExceptions {} when getUserLogin", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-no-peminjaman-alat", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getNoOrder(HttpServletRequest request) { try { Map result = service.getNoPeminjamanAlatTable(); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } else { return getJsonResponse(null, NOT_FOUND, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when getNoPeminjamanAlatTable", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getNoPeminjamanAlatTable", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-ruangan-peminjaman-alat", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getRuanganPeminjamanAlat(HttpServletRequest request) { try { Map result = service.getRuanganPeminjamanAlat(); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } else { return getJsonResponse(null, NOT_FOUND, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when getRuanganPeminjamanAlat", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getRuanganPeminjamanAlat", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-aset-peminjaman-alat", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getAsetPeminjamanAlat(@RequestParam(value = "id") Integer id, HttpServletRequest request) { try { Map result = service.getAsetPeminjamanAlat(id); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } else { return getJsonResponse(null, NOT_FOUND, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when getAsetPeminjamanAlat", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getAsetPeminjamanAlat", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-petugas-peminjaman-alat", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getPetugasPeminjamanAlat(HttpServletRequest request) { try { Map result = service.getPetugasPeminjamanAlat(); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } else { return getJsonResponse(null, NOT_FOUND, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when getPetugasPeminjamanAlat", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getPetugasPeminjamanAlat", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/save-peminjaman-alat", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) public ResponseEntity> savePemeliharaan(@Valid @RequestBody IpsrsPeminjamanAlatVO vo, HttpServletRequest request) { try { Map result = service.savePeminjamanAlat(vo); if (null != result) mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, CREATED, mapHeaderMessage); } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when savePeminjamanAlat", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when savePeminjamanAlat", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-all-peminjaman-alat", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getAllPeminjamanAlat(@RequestParam(value = "status") String status, HttpServletRequest request) { try { Map result = service.getAllPeminjamanAlat(status); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, OK, mapHeaderMessage); } else { return getJsonResponse(null, NOT_FOUND, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when getAllPeminjamanAlat", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getAllPeminjamanAlat", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/update-status-peminjaman", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) public ResponseEntity> updateStatusPeminjaman(@Valid @RequestBody IpsrsPeminjamanAlatVO vo, HttpServletRequest request) { try { Map result = service.updateStatusPeminjaman(vo); if (null != result) mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, CREATED, mapHeaderMessage); } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when updateStatusPeminjaman", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return RestUtil.getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when updateStatusPeminjaman", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return RestUtil.getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } }