Example usage for org.springframework.web.servlet.support RequestContextUtils getLocale

List of usage examples for org.springframework.web.servlet.support RequestContextUtils getLocale

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support RequestContextUtils getLocale.

Prototype

public static Locale getLocale(HttpServletRequest request) 

Source Link

Document

Retrieve the current locale from the given request, using the LocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the request's accept-header Locale.

Usage

From source file:alfio.controller.api.ReservationApiController.java

@RequestMapping(value = "/event/{eventName}/ticket/{ticketIdentifier}/assign", method = RequestMethod.POST, headers = "X-Requested-With=XMLHttpRequest")
public Map<String, Object> assignTicketToPerson(@PathVariable("eventName") String eventName,
        @PathVariable("ticketIdentifier") String ticketIdentifier,
        @RequestParam(value = "single-ticket", required = false, defaultValue = "false") boolean singleTicket,
        UpdateTicketOwnerForm updateTicketOwner, BindingResult bindingResult, HttpServletRequest request,
        Model model, Authentication authentication) throws Exception {

    Optional<UserDetails> userDetails = Optional.ofNullable(authentication).map(Authentication::getPrincipal)
            .filter(UserDetails.class::isInstance).map(UserDetails.class::cast);

    Optional<Triple<ValidationResult, Event, Ticket>> assignmentResult = ticketHelper.assignTicket(eventName,
            ticketIdentifier, updateTicketOwner, Optional.of(bindingResult), request, t -> {
                Locale requestLocale = RequestContextUtils.getLocale(request);
                model.addAttribute("ticketFieldConfiguration",
                        ticketHelper.findTicketFieldConfigurationAndValue(t.getMiddle().getId(), t.getRight(),
                                requestLocale));
                model.addAttribute("value", t.getRight());
                model.addAttribute("validationResult", t.getLeft());
                model.addAttribute("countries", TicketHelper.getLocalizedCountries(requestLocale));
                model.addAttribute("event", t.getMiddle());
                model.addAttribute("useFirstAndLastName", t.getMiddle().mustUseFirstAndLastName());
                model.addAttribute("availableLanguages", i18nManager.getEventLanguages(eventName).stream()
                        .map(ContentLanguage.toLanguage(requestLocale)).collect(Collectors.toList()));
                String uuid = t.getRight().getUuid();
                model.addAttribute("urlSuffix", singleTicket ? "ticket/" + uuid + "/view" : uuid);
                model.addAttribute("elementNamePrefix", "");
            }, userDetails);/*  w  ww  .ja  va 2 s  .c  om*/
    Map<String, Object> result = new HashMap<>();

    Optional<ValidationResult> validationResult = assignmentResult.map(Triple::getLeft);
    if (validationResult.isPresent() && validationResult.get().isSuccess()) {
        result.put("partial",
                templateManager.renderServletContextResource("/WEB-INF/templates/event/assign-ticket-result.ms",
                        model.asMap(), request, TemplateManager.TemplateOutput.HTML));
    }
    result.put("validationResult", validationResult.orElse(
            ValidationResult.failed(new ValidationResult.ErrorDescriptor("fullName", "error.fullname"))));
    return result;
}

From source file:org.kmnet.com.fw.web.codelist.CodeListInterceptor.java

/**
 * Sets codelist to the attribute of {@link HttpServletRequest}
 * <p>/*w w  w  . j av a  2 s. c  o  m*/
 * Sets codelist to the attribute of {@link HttpServletRequest} after the execution of Controller is finished.
 * </p>
 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#postHandle(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
 */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) {

    if (codeLists == null) {
        return;
    }

    Locale locale = RequestContextUtils.getLocale(request);
    logger.debug("locale for I18nCodelist is '{}'.", locale);

    for (CodeList codeList : codeLists) {
        String attributeName = codeList.getCodeListId();
        if (codeList instanceof I18nCodeList) {
            I18nCodeList i18nCodeList = (I18nCodeList) codeList;
            Map<String, String> codeListMap = getLocalizedCodeMap(i18nCodeList, locale);
            request.setAttribute(attributeName, codeListMap);
        } else {
            request.setAttribute(attributeName, codeList.asMap());
        }
    }

}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.weather.AddWeatherController.java

@RequestMapping(value = "lists/weather-definitions/edit.html", method = RequestMethod.GET)
protected String showEditForm(@RequestParam("id") String idString, @RequestParam("groupid") String idString2,
        ModelMap model, HttpServletRequest request) {
    AddWeatherCommand data = new AddWeatherCommand();
    if (auth.userIsExperimenter() || auth.isAdmin()) {
        model.addAttribute("userIsExperimenter", true);
        if (idString2 != null) {
            int id = Integer.parseInt(idString2);
            data.setResearchGroupId(id);
            if (id != DEFAULT_ID) {
                String title = researchGroupDao.getResearchGroupTitle(id);
                data.setResearchGroupTitle(title);
            } else {
                String defaultWeather = messageSource.getMessage("label.defaultWeather", null,
                        RequestContextUtils.getLocale(request));
                data.setResearchGroupTitle(defaultWeather);
            }/* www  .  jav  a 2s  .  co m*/
        }
        if (idString != null) {
            // Editation of existing weather
            int id = Integer.parseInt(idString);

            log.debug("Loading weather to the command object for editing.");
            Weather weather = weatherDao.read(id);

            data.setId(id);
            data.setTitle(weather.getTitle());
            data.setDescription(weather.getDescription());
        }
        model.addAttribute("addWeather", data);

        return "lists/weather/addItemForm";
    } else {
        return "lists/userNotExperimenter";
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.hardware.AddHardwareController.java

@RequestMapping(value = "lists/hardware-definitions/edit.html", method = RequestMethod.GET)
protected String showEditForm(@RequestParam("id") String idString, @RequestParam("groupid") String idString2,
        ModelMap model, HttpServletRequest request) {
    AddHardwareCommand data = new AddHardwareCommand();
    if (auth.userIsExperimenter() || auth.isAdmin()) {
        model.addAttribute("userIsExperimenter", true);
        if (idString2 != null) {
            int id = Integer.parseInt(idString2);
            data.setResearchGroupId(id);
            if (id != DEFAULT_ID) {
                String title = researchGroupDao.getResearchGroupTitle(id);
                data.setResearchGroupTitle(title);
            } else {
                String defaultHardware = messageSource.getMessage("label.defaultHardware", null,
                        RequestContextUtils.getLocale(request));
                data.setResearchGroupTitle(defaultHardware);
            }//from   w ww.  ja v  a  2s . c  o m
        }
        if (idString != null) {
            // Editation of existing hardware
            int id = Integer.parseInt(idString);

            log.debug("Loading hardware to the command object for editing.");
            Hardware hardware = hardwareDao.read(id);
            data.setId(id);
            data.setDescription(hardware.getDescription());
            data.setTitle(hardware.getTitle());
            data.setType(hardware.getType());
        }
        model.addAttribute("addHardware", data);

        return "lists/hardware/addItemForm";
    } else {
        return "lists/userNotExperimenter";
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.group.MembershipRequestController.java

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
        BindException bindException) throws Exception {
    EditGroupRoleCommand groupRoleCommand = (EditGroupRoleCommand) command;

    Person user = personDao.getPerson(ControllerUtils.getLoggedUserName());
    ResearchGroup group = researchGroupDao.read(groupId);
    GroupPermissionRequest gpr = new GroupPermissionRequest();
    gpr.setPerson(user);/*w  ww. j av  a  2s. co m*/
    gpr.setRequestedPermission(Util.GROUP_EXPERIMENTER);
    gpr.setResearchGroup(group);
    groupPermissionRequestDao.create(gpr);
    String emailAdmin = group.getPerson().getEmail();

    try {
        mailService.sendRequestForJoiningGroupMail(emailAdmin, gpr.getRequestId(), user.getUsername(),
                group.getTitle(), RequestContextUtils.getLocale(request));
    } catch (MailException e) {
        log.debug("E-mail was NOT sent");
        e.printStackTrace();
    }

    log.debug("Returning MAV");
    ModelAndView mav = new ModelAndView("groups/requestSent");
    GroupMultiController.setPermissionToRequestGroupRole(mav, personDao.getLoggedPerson());
    return mav;
}

From source file:com.krawler.spring.organizationChart.organizationChartController.java

public ModelAndView getUnmappedUsers(HttpServletRequest request, HttpServletResponse response) {
    JSONObject jobj = new JSONObject();
    JSONObject jobj1 = new JSONObject();
    try {/*from ww w . ja v a 2  s.c  om*/

        String companyid = sessionHandlerImplObj.getCompanyid(request);
        Locale locale = RequestContextUtils.getLocale(request);
        jobj = organizationService.getUnmappedUsers(companyid, locale);

        jobj1.put("valid", true);
        jobj1.put("data", jobj);
        jobj1.put("success", true);

    } catch (Exception e) {
        logger.warn("General exception in getUnmappedUsers()", e);
    }
    return new ModelAndView("jsonView", "model", jobj1.toString());
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.personoptparamdef.AddPersonOptParamDefController.java

@RequestMapping(value = "lists/person-optional-parameters/edit.html", method = RequestMethod.GET)
protected String showEditForm(@RequestParam("id") String idString, @RequestParam("groupid") String idString2,
        ModelMap model, HttpServletRequest request) {
    AddPersonOptParamDefCommand data = new AddPersonOptParamDefCommand();
    if (auth.userIsExperimenter() || auth.isAdmin()) {
        model.addAttribute("userIsExperimenter", true);
        if (idString2 != null) {
            int id = Integer.parseInt(idString2);
            data.setResearchGroupId(id);
            if (id != DEFAULT_ID) {
                String title = researchGroupDao.getResearchGroupTitle(id);
                data.setResearchGroupTitle(title);
            } else {
                String defaultPersonOptParamDef = messageSource.getMessage("label.defaultPersonOptParamDef",
                        null, RequestContextUtils.getLocale(request));
                data.setResearchGroupTitle(defaultPersonOptParamDef);
            }//from   ww w .ja v a 2s  .  c  om
        }
        if (idString != null) {
            // Editation of existing personOptParamDef
            int id = Integer.parseInt(idString);

            log.debug("Loading personOptParamDef to the command object for editing.");
            PersonOptParamDef personOptParamDef = personOptParamDefDao.read(id);

            data.setId(id);
            data.setParamName(personOptParamDef.getParamName());
            data.setParamDataType(personOptParamDef.getParamDataType());
        }
        model.addAttribute("addPersonOptParamDef", data);

        return "lists/personAdditionalParams/addItemForm";
    } else {
        return "lists/userNotExperimenter";
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.person.AddPersonController.java

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
        BindException bindException) throws Exception {
    AddPersonCommand apc = (AddPersonCommand) command;
    boolean userIsExperimenter = auth.userIsExperimenter();

    Person person = personService.createPerson(apc);

    try {//from w w w  .  ja  v  a 2 s .c om
        mailService.sendRegistrationConfirmMail(person, RequestContextUtils.getLocale(request));
    } catch (MailException e) {
        log.error("E-mail was NOT sent");
        log.error(e);
        e.printStackTrace();
    }

    log.debug("Returning MAV");
    ModelAndView mav = new ModelAndView(getSuccessView() + person.getPersonId());
    mav.addObject("userIsExperimenter", userIsExperimenter);
    return mav;
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.filemetadata.AddFileMetadataParamDefController.java

@RequestMapping(value = "lists/file-metadata-definitions/edit.html", method = RequestMethod.GET)
protected String showEditForm(@RequestParam("id") String idString, @RequestParam("groupid") String idString2,
        ModelMap model, HttpServletRequest request) {
    AddFileMetadataParamDefCommand data = new AddFileMetadataParamDefCommand();
    if (auth.userIsExperimenter() || auth.isAdmin()) {
        model.addAttribute("userIsExperimenter", true);
        if (idString2 != null) {
            int id = Integer.parseInt(idString2);
            data.setResearchGroupId(id);
            if (id != DEFAULT_ID) {
                String title = researchGroupDao.getResearchGroupTitle(id);
                data.setResearchGroupTitle(title);
            } else {
                String defaultFileMetadataParamDef = messageSource.getMessage(
                        "label.defaultFileMetadataParamDef", null, RequestContextUtils.getLocale(request));
                data.setResearchGroupTitle(defaultFileMetadataParamDef);
            }//from   w  ww .j  a v a 2 s .c o  m
        }
        if (idString != null) {
            // Editation of existing fileMetadataParamDef
            int id = Integer.parseInt(idString);

            log.debug("Loading fileMetadataParamDef to the command object for editing.");
            FileMetadataParamDef fileMetadataParamDef = fileMetadataParamDefDao.read(id);

            data.setId(id);
            data.setParamName(fileMetadataParamDef.getParamName());
            data.setParamDataType(fileMetadataParamDef.getParamDataType());
        }
        model.addAttribute("addFileMetadataParamDef", data);

        return "lists/fileMetadataParams/addItemForm";
    } else {
        return "lists/userNotExperimenter";
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.experimentoptparamdef.AddExperimentOptParamDefController.java

@RequestMapping(value = "lists/experiment-optional-parameters/edit.html", method = RequestMethod.GET)
protected String showEditForm(@RequestParam("id") String idString, @RequestParam("groupid") String idString2,
        ModelMap model, HttpServletRequest request) {
    AddExperimentOptParamDefCommand data = new AddExperimentOptParamDefCommand();
    if (auth.userIsExperimenter() || auth.isAdmin()) {
        model.addAttribute("userIsExperimenter", true);
        if (idString2 != null) {
            int id = Integer.parseInt(idString2);
            data.setResearchGroupId(id);
            if (id != DEFAULT_ID) {
                String title = researchGroupDao.getResearchGroupTitle(id);
                data.setResearchGroupTitle(title);
            } else {
                String defaultExperimentOptParamDef = messageSource.getMessage(
                        "label.defaultExperimentOptParamDef", null, RequestContextUtils.getLocale(request));
                data.setResearchGroupTitle(defaultExperimentOptParamDef);
            }//w  ww  . j a  v a2s  .  c  o m
        }
        if (idString != null) {
            // Editation of existing experimentOptParamDef
            int id = Integer.parseInt(idString);

            log.debug("Loading experimentOptParamDef to the command object for editing.");
            ExperimentOptParamDef experimentOptParamDef = experimentOptParamDefDao.read(id);

            data.setId(id);
            data.setParamName(experimentOptParamDef.getParamName());
            data.setParamDataType(experimentOptParamDef.getParamDataType());
        }
        model.addAttribute("addExperimentOptParamDef", data);

        return "lists/measurationAdditionalParams/addItemForm";
    } else {
        return "lists/userNotExperimenter";
    }
}