Example usage for java.util EnumMap containsKey

List of usage examples for java.util EnumMap containsKey

Introduction

In this page you can find the example usage for java.util EnumMap containsKey.

Prototype

public boolean containsKey(Object key) 

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:sg.ncl.MainController.java

@RequestMapping(value = "/teams/join_team", method = RequestMethod.POST)
public String checkJoinTeamInfo(@Valid TeamPageJoinTeamForm teamPageJoinForm, BindingResult bindingResult,
        Model model, HttpSession session, final RedirectAttributes redirectAttributes)
        throws WebServiceRuntimeException {

    final String LOG_PREFIX = "Existing user join team: {}";

    if (bindingResult.hasErrors()) {
        log.warn(LOG_PREFIX, "Application form error " + teamPageJoinForm.toString());
        return "team_page_join_team";
    }// ww  w  . ja va2  s . c  o m

    JSONObject mainObject = new JSONObject();
    JSONObject teamFields = new JSONObject();
    JSONObject userFields = new JSONObject();

    mainObject.put("team", teamFields);
    mainObject.put("user", userFields);

    userFields.put("id", session.getAttribute("id")); // ncl-id

    teamFields.put("name", teamPageJoinForm.getTeamName());

    log.info(LOG_PREFIX, USER_PREFIX + session.getAttribute("id") + ", team " + teamPageJoinForm.getTeamName());

    HttpEntity<String> request = createHttpEntityWithBody(mainObject.toString());
    ResponseEntity response;

    try {
        restTemplate.setErrorHandler(new MyResponseErrorHandler());
        response = restTemplate.exchange(properties.getJoinRequestExistingUser(), HttpMethod.POST, request,
                String.class);
        String responseBody = response.getBody().toString();

        if (RestUtil.isError(response.getStatusCode())) {
            // prepare the exception mapping
            EnumMap<ExceptionState, String> exceptionMessageMap = new EnumMap<>(ExceptionState.class);
            exceptionMessageMap.put(USER_NOT_FOUND_EXCEPTION, "User not found");
            exceptionMessageMap.put(USER_ID_NULL_OR_EMPTY_EXCEPTION, "User id is null or empty");
            exceptionMessageMap.put(TEAM_NOT_FOUND_EXCEPTION, "Team name not found");
            exceptionMessageMap.put(TEAM_NAME_NULL_OR_EMPTY_EXCEPTION, "Team name is null or empty");
            exceptionMessageMap.put(USER_ALREADY_IN_TEAM_EXCEPTION, "User already in team");
            exceptionMessageMap.put(TEAM_MEMBER_ALREADY_EXISTS_EXCEPTION, "Team member already exists");
            exceptionMessageMap.put(ADAPTER_CONNECTION_EXCEPTION, "Connection to adapter failed");
            exceptionMessageMap.put(ADAPTER_INTERNAL_ERROR_EXCEPTION, "Internal server error on adapter");
            exceptionMessageMap.put(DETERLAB_OPERATION_FAILED_EXCEPTION, "Operation failed on DeterLab");

            MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class);
            ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError());

            final String errorMessage = exceptionMessageMap.containsKey(exceptionState) ? error.getMessage()
                    : ERR_SERVER_OVERLOAD;

            log.warn(LOG_PREFIX, responseBody);
            redirectAttributes.addFlashAttribute(MESSAGE, errorMessage);
            return "redirect:/teams/join_team";

        } else {
            log.info(LOG_PREFIX, "Application for join team " + teamPageJoinForm.getTeamName() + " submitted");
            return "redirect:/teams/join_application_submitted/" + teamPageJoinForm.getTeamName();
        }

    } catch (ResourceAccessException | IOException e) {
        throw new WebServiceRuntimeException(e.getMessage());
    }
}