Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Staehler, Michael committed Apr 5, 2024
1 parent 1f8678a commit 7478354
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
package de.fred4jupiter.fredbet.web.validation;

import de.fred4jupiter.fredbet.web.profile.ChangePasswordCommand;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fred4jupiter.fredbet.web.profile.ChangePasswordCommand;

/**
* Validates the new password against the password repeat.
*
* @author michael
*
* @author michael
*/
public class ChangePasswordCommandValidator implements ConstraintValidator<PasswordChangeConstraint, ChangePasswordCommand> {

private static final Logger LOG = LoggerFactory.getLogger(ChangePasswordCommandValidator.class);

@Override
public void initialize(PasswordChangeConstraint passwordChangeConstraint) {
}

@Override
public boolean isValid(ChangePasswordCommand value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

if (!value.getNewPassword().equals(value.getNewPasswordRepeat())) {
context.buildConstraintViolationWithTemplate("{msg.passwordChange.passwordMismatch}")
.addPropertyNode("newPassword").addConstraintViolation().disableDefaultConstraintViolation();
LOG.error("newPassword and newPasswordRepeat are different");
return false;
}

if (value.getOldPassword().equals(value.getNewPassword())) {
context.buildConstraintViolationWithTemplate("{msg.passwordChange.oldAndNewPasswordAreSame}")
.addPropertyNode("newPassword").addConstraintViolation().disableDefaultConstraintViolation();
LOG.error("newPassword and newPasswordRepeat are different");
return false;
}

return true;
}
private static final Logger LOG = LoggerFactory.getLogger(ChangePasswordCommandValidator.class);

@Override
public boolean isValid(ChangePasswordCommand value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

if (!value.getNewPassword().equals(value.getNewPasswordRepeat())) {
context.buildConstraintViolationWithTemplate("{msg.passwordChange.passwordMismatch}")
.addPropertyNode("newPassword").addConstraintViolation().disableDefaultConstraintViolation();
LOG.error("newPassword and newPasswordRepeat are different");
return false;
}

if (value.getOldPassword().equals(value.getNewPassword())) {
context.buildConstraintViolationWithTemplate("{msg.passwordChange.oldAndNewPasswordAreSame}")
.addPropertyNode("newPassword").addConstraintViolation().disableDefaultConstraintViolation();
LOG.error("newPassword and newPasswordRepeat are different");
return false;
}

return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
package de.fred4jupiter.fredbet.web.validation;

import de.fred4jupiter.fredbet.web.matches.MatchResultCommand;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import de.fred4jupiter.fredbet.web.matches.MatchResultCommand;

/**
* Validator that checks if you enter both goal results.
*
* @author michael
*
* @author michael
*/
public class TeamResultValidator implements ConstraintValidator<TeamResultConstraint, MatchResultCommand> {

@Override
public void initialize(TeamResultConstraint constraintAnnotation) {
}
@Override
public boolean isValid(MatchResultCommand value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}

Integer teamResultOne = value.getTeamResultOne();
Integer teamResultTwo = value.getTeamResultTwo();

@Override
public boolean isValid(MatchResultCommand value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
if (teamResultOne == null && teamResultTwo == null) {
return true;
}

Integer teamResultOne = value.getTeamResultOne();
Integer teamResultTwo = value.getTeamResultTwo();

if (teamResultOne == null && teamResultTwo == null) {
return true;
}

return (teamResultOne != null && teamResultOne >= 0) && (teamResultTwo != null && teamResultTwo >= 0);
}
return (teamResultOne != null && teamResultOne >= 0) && (teamResultTwo != null && teamResultTwo >= 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
import de.fred4jupiter.fredbet.domain.Country;
import de.fred4jupiter.fredbet.util.Validator;
import de.fred4jupiter.fredbet.web.matches.CreateEditMatchCommand;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class ValidMatchValidator implements ConstraintValidator<ValidMatchConstraint, CreateEditMatchCommand> {

private static final Logger LOG = LoggerFactory.getLogger(ValidMatchValidator.class);

@Override
public void initialize(ValidMatchConstraint constraintAnnotation) {
}

@Override
public boolean isValid(CreateEditMatchCommand value, ConstraintValidatorContext context) {
if (value == null) {
Expand All @@ -41,38 +36,6 @@ public boolean isValid(CreateEditMatchCommand value, ConstraintValidatorContext
return true;
}

private boolean isInvalidTeamSelection(CreateEditMatchCommand value) {
Country countryTeamOne = value.getCountryTeamOne();
Country countryTeamTwo = value.getCountryTeamTwo();

String teamNameOne = value.getTeamNameOne();
String teamNameTwo = value.getTeamNameTwo();

// nothing selected
if ((Validator.isNull(countryTeamOne) && Validator.isNull(countryTeamTwo))
&& (StringUtils.isBlank(teamNameOne) && StringUtils.isBlank(teamNameTwo))) {
return true;
}

if (Validator.isNull(countryTeamOne) && Validator.isNotNull(countryTeamTwo)) {
return true;
}

if (Validator.isNotNull(countryTeamOne) && Validator.isNull(countryTeamTwo)) {
return true;
}

if (StringUtils.isBlank(teamNameOne) && StringUtils.isNotBlank(teamNameTwo)) {
return true;
}

if (StringUtils.isNotBlank(teamNameOne) && StringUtils.isBlank(teamNameTwo)) {
return true;
}

return false;
}

private boolean oneTeamNotSelected(CreateEditMatchCommand value) {
Country countryTeamOne = value.getCountryTeamOne();
Country countryTeamTwo = value.getCountryTeamTwo();
Expand All @@ -81,9 +44,9 @@ private boolean oneTeamNotSelected(CreateEditMatchCommand value) {
String teamNameTwo = value.getTeamNameTwo();

return (Validator.isNull(countryTeamOne) && StringUtils.isBlank(teamNameOne))
|| (Validator.isNotNull(countryTeamOne) && StringUtils.isNotBlank(teamNameOne))
|| (Validator.isNull(countryTeamTwo) && StringUtils.isBlank(teamNameTwo))
|| (Validator.isNotNull(countryTeamTwo) && StringUtils.isNotBlank(teamNameTwo));
|| (Validator.isNotNull(countryTeamOne) && StringUtils.isNotBlank(teamNameOne))
|| (Validator.isNull(countryTeamTwo) && StringUtils.isBlank(teamNameTwo))
|| (Validator.isNotNull(countryTeamTwo) && StringUtils.isNotBlank(teamNameTwo));
}

private boolean hasSameTeamsPlayingAgainstEachOther(CreateEditMatchCommand value) {
Expand Down

0 comments on commit 7478354

Please sign in to comment.