<commit_before>package org.synyx.urlaubsverwaltung.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.synyx.urlaubsverwaltung.settings.SettingsService;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * Validates {@link AccountForm}.
 */
@Component
class AccountFormValidator implements Validator {

    private static final String ERROR_MANDATORY_FIELD = "error.entry.mandatory";
    private static final String ERROR_INTEGER_FIELD = "error.entry.integer";
    private static final String ERROR_COMMENT_TO_LONG = "error.entry.commentTooLong";
    private static final String ERROR_FULL_OR_HALF_NUMBER = "error.entry.fullOrHalfNumber";

    private static final String ATTRIBUTE_ANNUAL_VACATION_DAYS = "annualVacationDays";
    private static final String ATTRIBUTE_ACTUAL_VACATION_DAYS = "actualVacationDays";
    private static final String ATTRIBUTE_REMAINING_VACATION_DAYS = "remainingVacationDays";
    private static final String ATTRIBUTE_REMAINING_VACATION_DAYS_NOT_EXPIRING = "remainingVacationDaysNotExpiring";
    private static final String ATTRIBUTE_COMMENT = "comment";
    private static final String ATTR_HOLIDAYS_ACCOUNT_VALID_FROM = "holidaysAccountValidFrom";
    private static final String ATTR_HOLIDAYS_ACCOUNT_VALID_TO = "holidaysAccountValidTo";
    private static final String ERROR_ENTRY_MIN = "error.entry.min";

    private final SettingsService settingsService;

    @Autowired
    AccountFormValidator(SettingsService settingsService) {
        this.settingsService = settingsService;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return AccountForm.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        final BigDecimal maxAnnualVacationDays = getMaximumAnnualVacationDays();

        final AccountForm form = (AccountForm) target;
        validatePeriod(form, errors);
        validateAnnualVacation(form, errors, maxAnnualVacationDays);
        validateActualVacation(form, errors);
        validateRemainingVacationDays(form, errors, maxAnnualVacationDays);
        validateRemainingVacationDaysNotExpiring(form, errors);
        validateComment(form, errors);
    }

    void validateComment(AccountForm form, Errors errors) {

        final String comment = form.getComment();
        if (comment != null && comment.length() > 200) {
            errors.rejectValue(ATTRIBUTE_COMMENT, ERROR_COMMENT_TO_LONG);
        }
    }

    void validatePeriod(AccountForm form, Errors errors) {

        final LocalDate holidaysAccountValidFrom = form.getHolidaysAccountValidFrom();
        final LocalDate holidaysAccountValidTo = form.getHolidaysAccountValidTo();

        validateDateNotNull(holidaysAccountValidFrom, ATTR_HOLIDAYS_ACCOUNT_VALID_FROM, errors);
        validateDateNotNull(holidaysAccountValidTo, ATTR_HOLIDAYS_ACCOUNT_VALID_TO, errors);

        if (holidaysAccountValidFrom != null && holidaysAccountValidTo != null) {
            final int year = form.getHolidaysAccountYear();
            final int fromYear = holidaysAccountValidFrom.getYear();<commit_msg>Add @NonNull for AccountFormValidator<commit_after>package org.synyx.urlaubsverwaltung.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.synyx.urlaubsverwaltung.settings.SettingsService;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * Validates {@link AccountForm}.
 */
@Component
class AccountFormValidator implements Validator {

    private static final String ERROR_MANDATORY_FIELD = "error.entry.mandatory";
    private static final String ERROR_INTEGER_FIELD = "error.entry.integer";
    private static final String ERROR_COMMENT_TO_LONG = "error.entry.commentTooLong";
    private static final String ERROR_FULL_OR_HALF_NUMBER = "error.entry.fullOrHalfNumber";

    private static final String ATTRIBUTE_ANNUAL_VACATION_DAYS = "annualVacationDays";
    private static final String ATTRIBUTE_ACTUAL_VACATION_DAYS = "actualVacationDays";
    private static final String ATTRIBUTE_REMAINING_VACATION_DAYS = "remainingVacationDays";
    private static final String ATTRIBUTE_REMAINING_VACATION_DAYS_NOT_EXPIRING = "remainingVacationDaysNotExpiring";
    private static final String ATTRIBUTE_COMMENT = "comment";
    private static final String ATTR_HOLIDAYS_ACCOUNT_VALID_FROM = "holidaysAccountValidFrom";
    private static final String ATTR_HOLIDAYS_ACCOUNT_VALID_TO = "holidaysAccountValidTo";
    private static final String ERROR_ENTRY_MIN = "error.entry.min";

    private final SettingsService settingsService;

    @Autowired
    AccountFormValidator(SettingsService settingsService) {
        this.settingsService = settingsService;
    }

    @Override
    public boolean supports(@NonNull Class<?> clazz) {
        return AccountForm.class.equals(clazz);
    }

    @Override
    public void validate(@NonNull Object target, @NonNull Errors errors) {

        final BigDecimal maxAnnualVacationDays = getMaximumAnnualVacationDays();

        final AccountForm form = (AccountForm) target;
        validatePeriod(form, errors);
        validateAnnualVacation(form, errors, maxAnnualVacationDays);
        validateActualVacation(form, errors);
        validateRemainingVacationDays(form, errors, maxAnnualVacationDays);
        validateRemainingVacationDaysNotExpiring(form, errors);
        validateComment(form, errors);
    }

    void validateComment(AccountForm form, Errors errors) {

        final String comment = form.getComment();
        if (comment != null && comment.length() > 200) {
            errors.rejectValue(ATTRIBUTE_COMMENT, ERROR_COMMENT_TO_LONG);
        }
    }

    void validatePeriod(AccountForm form, Errors errors) {

        final LocalDate holidaysAccountValidFrom = form.getHolidaysAccountValidFrom();
        final LocalDate holidaysAccountValidTo = form.getHolidaysAccountValidTo();

        validateDateNotNull(holidaysAccountValidFrom, ATTR_HOLIDAYS_ACCOUNT_VALID_FROM, errors);
        validateDateNotNull(holidaysAccountValidTo, ATTR_HOLIDAYS_ACCOUNT_VALID_TO, errors);

        if (holidaysAccountValidFrom != null && holidaysAccountValidTo != null) {
            final int year = form.getHolidaysAccountYear();
            final int fromYear = holidaysAccountValidFrom.getYear();
{'fraction_non_alphanumeric': 0.07276007215874924, 'fraction_numerical': 0.0009019843656043296, 'mean_word_length': 4.696061643835616, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2279236', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1845, 'n_words': 387}