public String getMin(ConstraintConfiguration conf) { try { return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MIN, conf); } catch (ArgumentNotSetException e) { throw new IllegalStateException(e); } }
public String getVerifierRule(ConstraintConfiguration config) { return template.replaceAll("\\{0\\}", String.valueOf(System.nanoTime())).replaceAll("\\{1\\}", config.getFactType()).replaceAll("\\{2\\}", config.getFieldName()).replaceAll("\\{3\\}", this.getMin(config)).replaceAll("\\{4\\}", this.getMax(config)); }
public Constraint buildConstraint(ConstraintConfiguration config) { if (NotNullConstraint.NAME.equals(config.getConstraintName())) { return new NotNullConstraint(); } else if (IntegerConstraint.NAME.equals(config.getConstraintName())) { return new IntegerConstraint(); } else if (RangeConstraint.NAME.equals(config.getConstraintName())) { return new RangeConstraint(); } else if (NotMatchesConstraint.NAME.equals(config.getConstraintName())) { return new NotMatchesConstraint(); }else if (MatchesConstraint.NAME.equals(config.getConstraintName())) { return new MatchesConstraint(); }else if (InvalidFieldConstraint.NAME.equals(config.getConstraintName())) { return new InvalidFieldConstraint(); }else if (MandatoryFieldConstraint.NAME.equals(config.getConstraintName())) { return new MandatoryFieldConstraint(); } else { throw new IllegalArgumentException("Constraint unknown: " + config.getConstraintName()); } }
public Constraint buildConstraint(ConstraintConfiguration config) { if (NotNullConstraint.NAME.equals(config.getConstraintName())) { return new NotNullConstraint(); } else if (IntegerConstraint.NAME.equals(config.getConstraintName())) { return new IntegerConstraint(); } else if (RangeConstraint.NAME.equals(config.getConstraintName())) { return new RangeConstraint(); } else if (NotMatchesConstraint.NAME.equals(config.getConstraintName())) { return new NotMatchesConstraint(); }else if (MatchesConstraint.NAME.equals(config.getConstraintName())) { return new MatchesConstraint(); }else if (InvalidFieldConstraint.NAME.equals(config.getConstraintName())) { return new InvalidFieldConstraint(); }else if (MandatoryFieldConstraint.NAME.equals(config.getConstraintName())) { return new MandatoryFieldConstraint(); } else { throw new IllegalArgumentException("Constraint unknown: " + config.getConstraintName()); } }
public String getVerifierRule(ConstraintConfiguration config) { return template.replaceAll("\\{0\\}", String.valueOf(System.nanoTime())).replaceAll("\\{1\\}", config.getFactType()).replaceAll("\\{2\\}", config.getFieldName()).replaceAll("\\{3\\}", this.getMin(config)).replaceAll("\\{4\\}", this.getMax(config)); }
@Before public void setup() { cons = new RangeConstraint(); conf = new SimpleConstraintConfigurationImpl(); conf.setFactType("Person"); conf.setFieldName("age"); }
public String getMax(ConstraintConfiguration conf) { try { return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MAX, conf); } catch (ArgumentNotSetException e) { throw new IllegalStateException(e); } }
public ValidationResult validate(Object value, ConstraintConfiguration config) { ValidationResult result = new ValidationResult(); try { if (value == null || !(value instanceof Number || value instanceof String)) { result.setSuccess(false); if (value == null) { result.setMessage("The value is null"); // TODO: I18N } else { result.setMessage("Invalid value type " + value.getClass().getName()); // TODO: // I18N } } else { double min = Double.parseDouble(getMin(config)); double max = Double.parseDouble(getMax(config)); double d = Double.parseDouble(value.toString()); result.setSuccess(d > min && d < max); if (!result.isSuccess()) { result.setMessage("The value should be between " + min + " and " + max); // TODO: // I18N } } } catch (Throwable t) { result.setSuccess(false); result.setMessage(t.getMessage()); // TODO: I18N } return result; }
@Before public void setup() { cons = new RangeConstraint(); conf = new SimpleConstraintConfigurationImpl(); conf.setFactType("Person"); conf.setFieldName("age"); conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0"); conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "120"); System.out.println("Validation Rule:\n" + cons.getVerifierRule(conf) + "\n\n"); }
public String getMax(ConstraintConfiguration conf) { try { return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MAX, conf); } catch (ArgumentNotSetException e) { throw new IllegalStateException(e); } }
public ValidationResult validate(Object value, ConstraintConfiguration config) { ValidationResult result = new ValidationResult(); try { if (value == null || !(value instanceof Number || value instanceof String)) { result.setSuccess(false); if (value == null) { result.setMessage("The value is null"); // TODO: I18N } else { result.setMessage("Invalid value type " + value.getClass().getName()); // TODO: // I18N } } else { double min = Double.parseDouble(getMin(config)); double max = Double.parseDouble(getMax(config)); double d = Double.parseDouble(value.toString()); result.setSuccess(d > min && d < max); if (!result.isSuccess()) { result.setMessage("The value should be between " + min + " and " + max); // TODO: // I18N } } } catch (Throwable t) { result.setSuccess(false); result.setMessage(t.getMessage()); // TODO: I18N } return result; }
@Before public void setup() { cons = new RangeConstraint(); conf = new SimpleConstraintConfigurationImpl(); conf.setFactType("Person"); conf.setFieldName("age"); conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MIN, "0"); conf.setArgumentValue(RangeConstraint.RANGE_CONSTRAINT_MAX, "120"); System.out.println("Validation Rule:\n" + cons.getVerifierRule(conf) + "\n\n"); }
public String getMin(ConstraintConfiguration conf) { try { return (String) this.getMandatoryArgument(RANGE_CONSTRAINT_MIN, conf); } catch (ArgumentNotSetException e) { throw new IllegalStateException(e); } }