use of com.github.bordertech.wcomponents.WDateField in project wcomponents by BorderTech.
the class DateFieldPivotValidator method isValid.
/**
* {@inheritDoc}
*/
@Override
protected boolean isValid() {
// Get the date we are validating.
WDateField dateField = (WDateField) this.getInputField();
Date date = dateField.getDate();
if (date == null) {
// No date, so nothing to validate.
return true;
}
// Determine the pivot date
Date pivot = null;
if (variablePivot != null) {
pivot = variablePivot.getDate();
if (pivot == null) {
// No pivot value, so default to true.
return true;
}
} else if (fixedPivot != null) {
pivot = fixedPivot;
}
// We take a null pivot date as meaning "today"
if (pivot == null) {
pivot = new Date();
}
// Round the dates to nearest day.
pivot = DateUtilities.roundToDay(pivot);
date = DateUtilities.roundToDay(date);
// Perform the comparison with the pivot
switch(operator) {
case BEFORE:
return date.before(pivot);
case BEFORE_OR_EQUAL:
return !pivot.before(date);
case EQUAL:
return date.equals(pivot);
case AFTER_OR_EQUAL:
return !pivot.after(date);
case AFTER:
return date.after(pivot);
default:
throw new SystemException("Unknown operator. [" + operator + "]");
}
}
use of com.github.bordertech.wcomponents.WDateField in project wcomponents by BorderTech.
the class DateFieldPivotValidator_Test method runTest.
/**
* @param operator the date operator
* @return the test results
*/
private String[][] runTest(final int operator) {
String[][] result = new String[DATES.length][DATES[0].length];
for (int run = 0; run < DATES.length; run++) {
DateFieldPivotValidator validator = new DateFieldPivotValidator(operator);
validator.setFixedPivot(DATES[run][2]);
WDateField dateField = new WDateField();
dateField.addValidator(validator);
List<Diagnostic> diags = new ArrayList<>();
for (int i = 0; i < DATES[run].length; i++) {
diags.clear();
dateField.setDate(DATES[run][i]);
dateField.validate(diags);
Assert.assertTrue("Should only have a maximum of 1 error", diags.size() < 2);
if (!diags.isEmpty()) {
result[run][i] = (diags.get(0)).getDescription();
}
}
}
return result;
}
Aggregations