154 lines
5.3 KiB
Java
154 lines
5.3 KiB
Java
package net.gepafin.tendermanagement.util;
|
|
|
|
import java.text.MessageFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
|
|
import net.gepafin.tendermanagement.config.Translator;
|
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
|
import net.gepafin.tendermanagement.dao.FormDao;
|
|
import net.gepafin.tendermanagement.dao.VatCheckDao;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.ValidationException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
public class FieldValidator {
|
|
|
|
private final List<String> errors = new ArrayList<>();
|
|
|
|
public static FieldValidator create() {
|
|
return new FieldValidator();
|
|
}
|
|
|
|
@Autowired
|
|
private VatCheckDao vatCheckDao;
|
|
|
|
|
|
public FieldValidator notNull(Object object, String fieldLabel) {
|
|
if (Objects.isNull(object)) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_NULL), fieldLabel));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public FieldValidator notEmpty(List<?> list, String fieldLabel) {
|
|
if (list == null || list.isEmpty()) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_EMPTY), fieldLabel));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public void validate() {
|
|
if (!errors.isEmpty()) {
|
|
throw new ValidationException(Status.VALIDATION_ERROR, errors, Translator.toLocale(GepafinConstant.VALIDATION_MESSAGE));
|
|
}
|
|
}
|
|
public FieldValidator minLength(String value, Long minLength, String fieldLabel) {
|
|
if (minLength != null && value != null && value.length() < minLength) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_MIN_LENGTH), fieldLabel, minLength));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public FieldValidator maxLength(String value, Long maxLength, String fieldLabel) {
|
|
if (maxLength != null && value != null && value.length() > maxLength) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_MAX_LENGTH), fieldLabel, maxLength));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public FieldValidator matchesPattern(String value, String pattern, String fieldLabel) {
|
|
if (value != null && pattern != null && !value.matches(pattern)) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_PATTERN), fieldLabel));
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public FieldValidator addError( String errorMessage) {
|
|
errors.add(errorMessage);
|
|
return this;
|
|
}
|
|
public static boolean isNullOrZero(Long value) {
|
|
return value == null || value == 0L;
|
|
}
|
|
|
|
public FieldValidator validateCustom(String value, String customRule, String fieldId) {
|
|
if (customRule == null || value == null) {
|
|
return this; // No custom rule to validate
|
|
}
|
|
|
|
switch (customRule) {
|
|
|
|
case GepafinConstant.IS_PIVA:
|
|
// VAT number: max 11 digits, can start with 0
|
|
if (!value.matches("^\\d{1,11}$")) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_PIVA), fieldId, customRule));
|
|
}
|
|
break;
|
|
case GepafinConstant.IS_CODICE_FISCALE:
|
|
// 16 characters: 6 letters, 2 digits, 1 letter, 2 digits, 1 letter, 3 digits, 1 letter
|
|
if (!value.matches("^[A-Z]{6}[0-9]{2}[A-Z]{1}[0-9]{2}[A-Z]{1}[0-9]{3}[A-Z]{1}$")) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_CODICE_FISCALE), fieldId, customRule));
|
|
}
|
|
break;
|
|
|
|
case GepafinConstant.IS_CAP:
|
|
// 5 digits (can start with 0)
|
|
if (!value.matches("^[0-9]{5}$")) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_CAP), fieldId, customRule));
|
|
}
|
|
break;
|
|
|
|
case GepafinConstant.IS_IBAN:
|
|
// IBAN must be 27 characters
|
|
if (value.length() != 27) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_IBAN), fieldId, customRule));
|
|
}
|
|
break;
|
|
|
|
case GepafinConstant.IS_EMAIL:
|
|
case GepafinConstant.IS_EMAIL_PEC:
|
|
// Email validation (using a simple regex for email)
|
|
if (!value.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$")) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_EMAIL), fieldId, customRule));
|
|
}
|
|
break;
|
|
|
|
case GepafinConstant.IS_URL:
|
|
// URL validation (simple regex for URL)
|
|
if (!value.matches("^(https?|ftp)://[^\s/$.?#].[^\s]*$")) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_URL), fieldId, customRule));
|
|
}
|
|
break;
|
|
|
|
case GepafinConstant.IS_MARCA_DA_BOLLO:
|
|
// Length must be 14 digits (can start with 0)
|
|
if (!value.matches("^[0-9]{14}$")) {
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_MARCA_DA_BOLLO), fieldId, customRule));
|
|
}
|
|
break;
|
|
|
|
default:
|
|
// If the custom rule is unknown, just log or add an error (optional)
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.VALIDATION_FIELD_CUSTOM), fieldId, customRule));
|
|
break;
|
|
}
|
|
|
|
return this;
|
|
}
|
|
public FieldValidator isRequired(String value,Boolean isRequired, String fieldName) {
|
|
if (Boolean.TRUE.equals(isRequired)) { // Only check if isRequired is true
|
|
if (Objects.isNull(value) || value.isEmpty()) { // Check if value is null or empty
|
|
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_NULL), fieldName));
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
public boolean hasErrors() {
|
|
return !errors.isEmpty();
|
|
}
|
|
}
|