Search in sources :

Example 1 with ValidationException

use of org.openmrs.api.ValidationException in project openmrs-module-pihcore by PIH.

the class ResetPasswordPageController method post.

public String post(@RequestParam(value = "activationKey") String activationKey, @RequestParam(value = "newPassword") String newPassword, @RequestParam(value = "confirmPassword") String confirmPassword, @SpringBean("userService") UserService userService, HttpServletRequest request, PageModel model, UiUtils ui) {
    model.addAttribute("activationKey", activationKey);
    model.addAttribute("newPassword", newPassword);
    model.addAttribute("confirmPassword", confirmPassword);
    try {
        Context.addProxyPrivilege(GET_USERS);
        User user = userService.getUserByActivationKey(activationKey);
        if (user == null) {
            throw new InvalidActivationKeyException("activation.key.not.correct");
        }
        Context.setLocale(userService.getDefaultLocaleForUser(user));
        if (StringUtils.isBlank(newPassword) || StringUtils.isBlank(confirmPassword)) {
            throw new ValidationException(ui.message("emr.account.changePassword.newAndConfirmPassword.required"));
        } else if (!newPassword.equals(confirmPassword)) {
            throw new ValidationException(ui.message("emr.account.changePassword.newAndConfirmPassword.DoesNotMatch"));
        }
        userService.changePasswordUsingActivationKey(activationKey, newPassword);
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("emr.account.changePassword.success"));
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
    } catch (Exception e) {
        request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, ui.message("emr.account.changePassword.fail", new Object[] { e.getMessage() }, Context.getLocale()));
        log.warn("An error occurred while trying to reset password", e);
    } finally {
        Context.removeProxyPrivilege(GET_USERS);
    }
    return "redirect:index.htm";
}
Also used : User(org.openmrs.User) InvalidActivationKeyException(org.openmrs.api.InvalidActivationKeyException) ValidationException(org.openmrs.api.ValidationException) ValidationException(org.openmrs.api.ValidationException) InvalidActivationKeyException(org.openmrs.api.InvalidActivationKeyException)

Example 2 with ValidationException

use of org.openmrs.api.ValidationException in project openmrs-core by openmrs.

the class ValidateUtil method validate.

/**
 * Test the given object against all validators that are registered as compatible with the
 * object class
 *
 * @param obj the object to validate
 * @throws ValidationException thrown if a binding exception occurs
 * @should throw APIException if errors occur during validation
 * @should return immediately if validation is disabled
 */
public static void validate(Object obj) throws ValidationException {
    if (disableValidation) {
        return;
    }
    Errors errors = new BindException(obj, "");
    Context.getAdministrationService().validate(obj, errors);
    if (errors.hasErrors()) {
        Set<String> uniqueErrorMessages = new LinkedHashSet<>();
        for (Object objerr : errors.getAllErrors()) {
            ObjectError error = (ObjectError) objerr;
            String message = Context.getMessageSourceService().getMessage(error.getCode());
            if (error instanceof FieldError) {
                message = ((FieldError) error).getField() + ": " + message;
            }
            uniqueErrorMessages.add(message);
        }
        String exceptionMessage = "'" + obj + "' failed to validate with reason: ";
        exceptionMessage += StringUtils.join(uniqueErrorMessages, ", ");
        throw new ValidationException(exceptionMessage, errors);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) ValidationException(org.openmrs.api.ValidationException) BindException(org.springframework.validation.BindException) OpenmrsObject(org.openmrs.OpenmrsObject) FieldError(org.springframework.validation.FieldError)

Aggregations

ValidationException (org.openmrs.api.ValidationException)2 LinkedHashSet (java.util.LinkedHashSet)1 OpenmrsObject (org.openmrs.OpenmrsObject)1 User (org.openmrs.User)1 InvalidActivationKeyException (org.openmrs.api.InvalidActivationKeyException)1 BindException (org.springframework.validation.BindException)1 Errors (org.springframework.validation.Errors)1 FieldError (org.springframework.validation.FieldError)1 ObjectError (org.springframework.validation.ObjectError)1