package com.jasamedika.medifirst2000.controller; import com.jasamedika.medifirst2000.constants.MessageResource; import com.jasamedika.medifirst2000.controller.base.LocaleController; import com.jasamedika.medifirst2000.dto.GantiPasswordDTO; import com.jasamedika.medifirst2000.entities.LoginUser; import com.jasamedika.medifirst2000.exception.ServiceVOException; import com.jasamedika.medifirst2000.security.model.AppPermission; import com.jasamedika.medifirst2000.security.service.TokenAuthenticationService; import com.jasamedika.medifirst2000.service.LoginUserService; import com.jasamedika.medifirst2000.util.CommonUtil; import com.jasamedika.medifirst2000.util.PasswordUtil; 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.security.core.Authentication; 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.Collection; import java.util.HashMap; 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.*; import static com.jasamedika.medifirst2000.core.web.WebConstants.PageParameter.*; import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus; 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("/user") public class LoginUserController extends LocaleController { @Autowired private TokenAuthenticationService tokenAuthenticationService; @Autowired private LoginUserService loginUserService; private static final Logger LOGGER = LoggerFactory.getLogger(LoginUserController.class); @RequestMapping(value = "/update-password-user/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) @AppPermission(AppPermission.SPECIALS) public ResponseEntity updatePassword(@Valid @RequestBody GantiPasswordDTO ubah, HttpServletRequest request) { try { Authentication authentication = tokenAuthenticationService.getAuthentication(request); String namaUser = authentication.getName(); List loginUser = loginUserService.findByNamaUser(namaUser); if (!loginUser.isEmpty()) { LoginUser user = loginUser.get(0); PasswordUtil passwordUtil = new PasswordUtil(); boolean isValidPassword = passwordUtil.isPasswordEqual(ubah.getPassword(), user.getKataSandi()); if (isValidPassword) { LoginUserVO vo = loginUserService.findById(ubah.getId()); vo.setId(ubah.getId()); vo.setKataSandi(ubah.getKataSandi()); LoginUserVO result = loginUserService.update(vo); if (null != result) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS)); return getJsonResponse(String.valueOf(vo.getId()), OK, mapHeaderMessage); } else { LOGGER.error("Got exception when update password User failed"); addHeaderMessage(ERROR_MESSAGE, "update password User failed"); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } } else { LOGGER.error("Got exception when update Pegawai, password invalid"); addHeaderMessage(ERROR_MESSAGE, "Invalid Password"); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } } else { LOGGER.error("Got exception when update Pegawai"); addHeaderMessage(ERROR_MESSAGE, "User is unauthorized"); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when update", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when update", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } catch (Exception ex) { LOGGER.error("Got Exception {} when update", ex.getMessage()); addHeaderMessage(ERROR_MESSAGE, ex.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @SuppressWarnings("unchecked") public ResponseEntity> getAllVOWithQueryString(HttpServletRequest request, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "limit", required = false) Integer limit, @RequestParam(value = "sort", required = false, defaultValue = "id") String sort, @RequestParam(value = "dir", required = false, defaultValue = "asc") String dir) { Map resultPageMap = loginUserService.findAllWithPageAndLimitAndSortByAndDirectionParameter(page, limit, sort, dir); return constructListPageResult(resultPageMap, request); } @SuppressWarnings("rawtypes") private ResponseEntity constructListPageResult(Map map, HttpServletRequest request) { if (map == null) { Map mapHeaderMessage = new HashMap<>(); mapHeaderMessage.put(TOTAL_PAGE_HEADER, "0"); mapHeaderMessage.put(TOTAL_COUNT_HEADER, "0"); mapHeaderMessage.put(ERROR_MESSAGE, "Data not found."); return getJsonResponse(null, BAD_REQUEST, mapHeaderMessage); } else { @SuppressWarnings("unchecked") Collection vos = (Collection) map.get(LIST_DATA); Map mapHeaderMessage = new HashMap<>(); mapHeaderMessage.put(TOTAL_PAGE_HEADER, String.valueOf(map.get(TOTAL_PAGES))); mapHeaderMessage.put(TOTAL_COUNT_HEADER, String.valueOf(map.get(TOTAL_ELEMENTS))); mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(vos, OK, mapHeaderMessage); } } @RequestMapping(value = "/get-user", method = GET) public ResponseEntity getUser() { LoginUser loginUser = loginUserService.getLoginUser(); return getJsonResponse(loginUser, OK, mapHeaderMessage); } @RequestMapping(value = "/get-all-user/", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getAllUser(HttpServletRequest request) { try { Map result = loginUserService.getAllUser(); boolean dataFound = (boolean) result.get("dataFound"); if (dataFound) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); } else { mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request)); } return getJsonResponse(result, OK); } catch (Exception e) { throw new ServiceVOException(e.getMessage()); } } @RequestMapping(value = "/get-load-data", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity> getLoadData(HttpServletRequest request) { try { Map result = loginUserService.getLoadData(); 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 getLoadData", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when getLoadData", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } @RequestMapping(value = "/save-login-user", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE) public ResponseEntity> saveLoginUser(@Valid @RequestBody LoginUserVO vo, HttpServletRequest request) { try { Map result = loginUserService.saveUpdateLoginUser(vo); if (CommonUtil.isNullOrEmpty(result.get("statusError"))) { mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request)); return getJsonResponse(result, CREATED, mapHeaderMessage); } else { Map headerMessageCustom = new HashMap<>(); headerMessageCustom.put("label-success", result.get("statusError").toString()); return getJsonResponse(result, CREATED, headerMessageCustom); } } catch (ServiceVOException e) { LOGGER.error("Got ServiceVOException {} when saveUpdateLoginUser", e.getMessage()); addHeaderMessage(ERROR_MESSAGE, e.getMessage()); return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage); } catch (JpaSystemException jse) { LOGGER.error("Got JpaSystemException {} when saveUpdateLoginUser", jse.getMessage()); addHeaderMessage(ERROR_MESSAGE, jse.getMessage()); return getJsonHttpStatus(CONFLICT, mapHeaderMessage); } } }