FutureTimestampValidator.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package net.bananasplayground.validator;
  2. import java.time.Duration;
  3. import java.time.LocalDateTime;
  4. import java.time.Period;
  5. import java.time.temporal.TemporalAmount;
  6. import org.apache.commons.lang3.StringUtils;
  7. import com.vaadin.data.ValidationResult;
  8. import com.vaadin.data.ValueContext;
  9. import com.vaadin.data.validator.AbstractValidator;
  10. /**
  11. * Klimbim Software collection, A bag full of things
  12. * Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * @see net.bananasplayground.validator.FutureTimestampDefinition
  29. */
  30. public class FutureTimestampValidator extends AbstractValidator<LocalDateTime> {
  31. private final String errorMessage;
  32. private final String timeToAdd;
  33. protected FutureTimestampValidator(String errorMessage, String timeToAdd) {
  34. super(errorMessage);
  35. this.errorMessage = errorMessage;
  36. this.timeToAdd = timeToAdd;
  37. }
  38. @Override
  39. public ValidationResult apply(LocalDateTime value, ValueContext context) {
  40. if(StringUtils.isBlank(timeToAdd)) return ValidationResult.ok();
  41. boolean isValid = false;
  42. // date-based units (year, month, week, day) are denoted with uppercase abbreviations
  43. // (Y, M, W and D) while the time-based ones (hour and minute) are lowercase (h and m).
  44. // Test the case of the last character of the string to decide whether to parse into
  45. // a Period or a Duration. I exploit the fact that both of Period.parse and Duration.parse
  46. // accept the letters in either case.
  47. TemporalAmount toAddToCurrentDateTime;
  48. if (Character.isUpperCase(timeToAdd.charAt(timeToAdd.length() - 1))) {
  49. toAddToCurrentDateTime = Period.parse("P" + timeToAdd);
  50. } else {
  51. toAddToCurrentDateTime = Duration.parse("PT" + timeToAdd);
  52. }
  53. LocalDateTime now = LocalDateTime.now();
  54. LocalDateTime futureDateTime = now.plus(toAddToCurrentDateTime);
  55. if(value.isEqual(futureDateTime) || value.isAfter(futureDateTime)) {
  56. isValid = true;
  57. }
  58. return isValid ? ValidationResult.ok() : ValidationResult.error(this.errorMessage);
  59. }
  60. }