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.MapRuanganToKelasService; import com.jasamedika.medifirst2000.vo.MapRuanganToKelasVO; import com.jasamedika.medifirst2000.vo.MapRuanganToKelasVO_; 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.List; 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("/mapRuanganToKelas") public class MapRuanganToKelasController extends LocaleController { private static final Logger LOGGER = getLogger(MapRuanganToKelasController.class); @Autowired private MapRuanganToKelasService mapRuanganToKelasService; @RequestMapping(value = "/save-map-ruangan-to-kelas", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) public ResponseEntity addVO(@Valid @RequestBody MapRuanganToKelasVO_ vo, HttpServletRequest request) { try { List result = mapRuanganToKelasService.addVo(vo); if (null != result) mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse("", CREATED, mapHeaderMessage); } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when addVo", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when addVo", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/update-map-ruangan-to-kelas", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) public ResponseEntity editVO(@Valid @RequestBody MapRuanganToKelasVO_ vo) { try { List result = mapRuanganToKelasService.updateVo(vo); if (null != result) addHeaderMessage(ERROR_MESSAGE, ""); getJsonHttpStatus(CREATED); return getJsonResponse("", CREATED); } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when updateVo", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when updateVo", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-ruangan-by-kelas", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getRuanganByRuangan( @RequestParam(value = "kelasId", required = false) Integer kelasId, HttpServletRequest request) { try { Map result = mapRuanganToKelasService.getRuanganByKelas(kelasId); 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 getRuanganByKelas", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getRuanganByKelas", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-kelas-by-ruangan", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getKelasByRuangan( @RequestParam(value = "ruanganId", required = false) Integer ruanganId, HttpServletRequest request) { try { Map result = mapRuanganToKelasService.getKelasByRuangan(ruanganId); 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 getKelasByRuangan", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getKelasByRuangan", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/get-kamar-by-kelas", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getKamarByKelas( @RequestParam(value = "kelasId", required = false) Integer kelasId, @RequestParam(value = "ruanganId", required = false) Integer ruanganId, HttpServletRequest request) { try { Map result = mapRuanganToKelasService.getKamarByKelas(kelasId, ruanganId); 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 getKamarByKelas", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getKamarByKelas", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } }