Example usage for org.springframework.ui Model asMap

List of usage examples for org.springframework.ui Model asMap

Introduction

In this page you can find the example usage for org.springframework.ui Model asMap.

Prototype

Map<String, Object> asMap();

Source Link

Document

Return the current set of model attributes as a Map.

Usage

From source file:com.jd.survey.web.settings.SurveyDefinitionController.java

/**
 * Deletes a survey definition //  ww  w. j ava2  s.  co m
 * @param id
 * @param uiModel
 * @param httpServletRequest
 * @return
 */
@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN" })
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
public String delete(@PathVariable("id") Long id, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("delete(): id=" + id);
    try {
        String login = principal.getName();
        User user = userService.user_findByLogin(login);
        SurveyDefinition surveyDefinition = surveySettingsService.surveyDefinition_findById(id);
        Long departmentId = surveyDefinition.getDepartment().getId();

        //Check if the user is authorized
        if (!securityService.userIsAuthorizedToManageSurvey(id, user)) {
            log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo()
                    + " attempted by user login:" + principal.getName() + "from IP:"
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";
        }
        if (surveyDefinition.getStatus().equals(SurveyDefinitionStatus.P)
                || surveyDefinition.getStatus().equals(SurveyDefinitionStatus.D)) {
            //you may not delete a survey that has been published  
            log.warn("Attempt to delete a survey that has been published path:"
                    + httpServletRequest.getPathInfo() + " attempted by user login:" + principal.getName()
                    + "from IP:" + httpServletRequest.getLocalAddr());
            return "accessDenied";
        }

        surveySettingsService.surveyDefinition_remove(surveyDefinition);
        uiModel.asMap().clear();
        return "redirect:/settings/surveyDefinitions/";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.settings.SurveyDefinitionPageController.java

@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN" })
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String createPost(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid SurveyDefinitionPage surveyDefinitionPage, BindingResult bindingResult, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {
    log.info("create(): handles " + RequestMethod.POST.toString());
    try {/*from  w ww  . j av a 2s  .  c o  m*/
        User user = userService.user_findByLogin(principal.getName());
        if (!securityService.userIsAuthorizedToManageSurvey(surveyDefinitionPage.getSurveyDefinition().getId(),
                user)
                && !securityService.userBelongsToDepartment(
                        surveyDefinitionPage.getSurveyDefinition().getDepartment().getId(), user)) {
            log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo()
                    + " attempted by user login:" + principal.getName() + "from IP:"
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";
        }

        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, surveyDefinitionPage, user);
                return "settings/surveyDefinitionPages/create";
            }
            uiModel.asMap().clear();
            surveyDefinitionPage = surveySettingsService.surveyDefinitionPage_merge(surveyDefinitionPage);
            return "settings/surveyDefinitionPages/saved";

        }

        else {
            return "redirect:/settings/surveyDefinitions/" + encodeUrlPathSegment(
                    surveyDefinitionPage.getSurveyDefinition().getId().toString(), httpServletRequest);

        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}

From source file:com.jd.survey.web.settings.SurveyDefinitionPageController.java

@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid SurveyDefinitionPage surveyDefinitionPage, BindingResult bindingResult, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {
    log.info("update(): handles PUT");
    try {/*from   w  w  w .java  2 s  .co m*/
        User user = userService.user_findByLogin(principal.getName());
        if (!securityService.userIsAuthorizedToManageSurvey(surveyDefinitionPage.getSurveyDefinition().getId(),
                user)
                && !securityService.userBelongsToDepartment(
                        surveyDefinitionPage.getSurveyDefinition().getDepartment().getId(), user)) {
            log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo()
                    + " attempted by user login:" + principal.getName() + "from IP:"
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";
        }
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, surveyDefinitionPage, user);
                return "settings/surveyDefinitionPages/update";
            }
            //validate VisibilityExpression
            boolean isValid = true;
            ParserContext ctx = new ParserContext();
            ctx.setStrongTyping(true);
            ctx.addInput("surveyDefinition", SurveyDefinition.class);
            uiModel.asMap().clear();
            surveyDefinitionPage = surveySettingsService.surveyDefinitionPage_merge(surveyDefinitionPage);
            return "settings/surveyDefinitionPages/saved";

        } else {

            return "redirect:/settings/surveyDefinitions/" + encodeUrlPathSegment(
                    surveyDefinitionPage.getSurveyDefinition().getId().toString(), httpServletRequest);

        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.settings.SurveyDefinitionPageController.java

@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN" })
@RequestMapping(value = "/fork", method = RequestMethod.POST, produces = "text/html")
public String updateSkipAndBranckLogic(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid SurveyDefinitionPage surveyDefinitionPage, BindingResult bindingResult, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {
    log.info("create(): handles " + RequestMethod.POST.toString());
    try {/*from  w  w w  . j a  va 2 s.  c  om*/
        User user = userService.user_findByLogin(principal.getName());
        if (!securityService.userIsAuthorizedToManageSurvey(surveyDefinitionPage.getSurveyDefinition().getId(),
                user)
                && !securityService.userBelongsToDepartment(
                        surveyDefinitionPage.getSurveyDefinition().getDepartment().getId(), user)) {
            log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo()
                    + " attempted by user login:" + principal.getName() + "from IP:"
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";
        }

        if (proceed != null) {
            uiModel.asMap().clear();
            surveySettingsService.surveyDefinitionPage_updateSkipAndBranckLogic(surveyDefinitionPage);
            return "settings/surveyDefinitionPages/saved";
        } else {
            return "redirect:/settings/surveyDefinitions/" + encodeUrlPathSegment(
                    surveyDefinitionPage.getSurveyDefinition().getId().toString(), httpServletRequest);

        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}

From source file:com.jd.survey.web.settings.SurveyDefinitionPageController.java

@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN" })
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
public String delete(@PathVariable("id") Long id, Principal principal, Model uiModel,
        HttpServletRequest httpServletRequest) {
    log.info("delete(): id=" + id);
    try {/*from  w w w. j  a v  a  2 s. c o m*/
        User user = userService.user_findByLogin(principal.getName());
        if (!securityService.userIsAuthorizedToManageSurvey(id, user)) {
            log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo()
                    + " attempted by user login:" + principal.getName() + "from IP:"
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";
        }

        SurveyDefinitionPage surveyDefinitionPage = surveySettingsService.surveyDefinitionPage_findById(id);
        surveySettingsService.surveyDefinitionPage_remove(surveyDefinitionPage);
        uiModel.asMap().clear();
        return "redirect:/settings/surveyDefinitions/" + encodeUrlPathSegment(
                surveyDefinitionPage.getSurveyDefinition().getId().toString(), httpServletRequest);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.settings.VelocityTemplateController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid VelocityTemplate velocityTemplate, BindingResult bindingResult, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {
    log.info("create(): handles " + RequestMethod.POST.toString());
    try {/*from w w  w.  j  a v  a 2s . c  om*/
        User user = userService.user_findByLogin(principal.getName());
        if (!user.isAdmin()) {
            return "accessDenied";
        }
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, velocityTemplate, user);
                return "admin/templates/create";
            }
            uiModel.asMap().clear();
            velocityTemplate = surveySettingsService.velocityTemplate_merge(velocityTemplate);
            log.info("redirecting to: " + "redirect:/admin/templates/"
                    + encodeUrlPathSegment(velocityTemplate.getId().toString(), httpServletRequest));
            return "redirect:/admin/templates";
        } else {
            return "redirect:/admin/templates";
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.settings.VelocityTemplateController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
public String update(@RequestParam(value = "_proceed", required = false) String proceed,
        @Valid VelocityTemplate velocityTemplate, BindingResult bindingResult, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {
    log.info("update(): handles PUT");
    try {/*from  ww w.  j av  a 2s.co  m*/
        User user = userService.user_findByLogin(principal.getName());
        if (!user.isAdmin()) {
            return "accessDenied";
        }
        if (proceed != null) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, velocityTemplate, user);
                return "velocityTemplates/update";
            }

            Policy policy = Policy.getInstance(this.getClass().getResource(POLICY_FILE_LOCATION));
            AntiSamy as = new AntiSamy();
            CleanResults cr = as.scan(velocityTemplate.getDefinition(), policy);
            velocityTemplate.setDefinition(cr.getCleanHTML());

            uiModel.asMap().clear();
            velocityTemplate = surveySettingsService.velocityTemplate_merge(velocityTemplate);
            log.info("redirecting to: " + "redirect:/admin/templates/"
                    + encodeUrlPathSegment(velocityTemplate.getId().toString(), httpServletRequest));
            return "redirect:/admin/templates?page=1&size=25";
        } else {
            return "redirect:/admin/templates?page=1&size=25";

        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.settings.VelocityTemplateController.java

@Secured({ "ROLE_ADMIN" })
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "size", required = false) Integer size, Principal principal, Model uiModel) {
    log.info("delete(): id=" + id);
    try {//  w w w .ja  va 2  s .  c  o  m
        User user = userService.user_findByLogin(principal.getName());
        if (!user.isAdmin()) {
            return "accessDenied";
        }
        VelocityTemplate surveyDefinition = surveySettingsService.velocityTemplate_findById(id);
        surveySettingsService.velocityTemplate_remove(surveyDefinition);
        uiModel.asMap().clear();
        uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
        uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
        return "redirect:/admin/templates";
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.survey.PrivateSurveyController.java

/**
 * Returns the survey logo image binary  
 * @param departmentId/*  w w  w.  ja v  a  2  s .  co m*/
 * @param uiModel
 * @param httpServletRequest
 * @return
 */
@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN", "ROLE_SURVEY_PARTICIPANT" })
@RequestMapping(value = "/logo/{id}", produces = "text/html")
public void getSurveyLogo(@PathVariable("id") Long surveyDefinitionId, Model uiModel, Principal principal,
        HttpServletRequest httpServletRequest, HttpServletResponse response) {
    try {
        uiModel.asMap().clear();
        User user = userService.user_findByLogin(principal.getName());
        //Check if the user is authorized
        if (!securityService.userIsAuthorizedToCreateSurvey(surveyDefinitionId, user)) {
            log.warn("Unauthorized access to url path " + httpServletRequest.getPathInfo()
                    + " attempted by user login:" + principal.getName() + "from IP:"
                    + httpServletRequest.getLocalAddr());
            throw (new RuntimeException("Unauthorized access to logo"));
        } else {
            SurveyDefinition surveyDefinition = surveySettingsService
                    .surveyDefinition_findById(surveyDefinitionId);
            //response.setContentType("image/png");
            ServletOutputStream servletOutputStream = response.getOutputStream();
            servletOutputStream.write(surveyDefinition.getLogo());
            servletOutputStream.flush();
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }
}

From source file:com.jd.survey.web.survey.PrivateSurveyController.java

/**
 * Handles the post from the submit page
 * @param proceed//w w w.j  ava 2  s .c  o m
 * @param survey
 * @param bindingResult
 * @param uiModel
 * @param httpServletRequest
 * @return
 */
@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN", "ROLE_SURVEY_PARTICIPANT" })
@RequestMapping(value = "/submit", method = RequestMethod.POST, produces = "text/html")
public String submitSurvey(@RequestParam(value = "id", required = true) Long surveyId,
        @RequestParam(value = "_submit", required = false) String proceedAction, Principal principal,
        Model uiModel, HttpServletRequest httpServletRequest) {

    log.info("submitPost(): id= " + surveyId);

    try {

        String login = principal.getName();
        //Check if the user is authorized
        if (!surveyService.survey_findById(surveyId).getLogin().equals(login)) {
            log.warn(UNAUTHORIZED_ATTEMPT_TO_ACCESS_SURVEY_WARNING_MESSAGE + surveyId
                    + REQUEST_PATH_WARNING_MESSAGE + httpServletRequest.getPathInfo()
                    + FROM_USER_LOGIN_WARNING_MESSAGE + principal.getName() + FROM_IP_WARNING_MESSAGE
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";

        }

        //Check that the survey was not submitted
        Survey dbSurvey = surveyService.survey_findById(surveyId);
        if (!(dbSurvey.getStatus().equals(SurveyStatus.I) || dbSurvey.getStatus().equals(SurveyStatus.R))) {
            log.warn(UNAUTHORIZED_ATTEMPT_TO_EDIT_SUBMITTED_SURVEY_WARNING_MESSAGE + surveyId
                    + REQUEST_PATH_WARNING_MESSAGE + httpServletRequest.getPathInfo()
                    + FROM_USER_LOGIN_WARNING_MESSAGE + principal.getName() + FROM_IP_WARNING_MESSAGE
                    + httpServletRequest.getLocalAddr());
            return "accessDenied";

        }

        if (proceedAction != null) { //submit button
            uiModel.asMap().clear();
            Survey survey = surveyService.survey_submit(surveyId);
            return "redirect:/private/"
                    + encodeUrlPathSegment(survey.getTypeId().toString(), httpServletRequest) + "?list";
        } else {
            uiModel.asMap().clear();
            Survey survey = surveyService.survey_findById(surveyId);
            List<SurveyPage> surveyPages = surveyService.surveyPage_getAll(surveyId,
                    messageSource.getMessage(DATE_FORMAT, null, LocaleContextHolder.getLocale()));
            Short order = (short) surveyPages.size();
            return "redirect:/private/" + encodeUrlPathSegment(survey.getId().toString(), httpServletRequest)
                    + "/" + encodeUrlPathSegment(order.toString(), httpServletRequest);
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw (new RuntimeException(e));
    }

}