Done ticket GEPAFINBE-18
This commit is contained in:
@@ -3,12 +3,17 @@ 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 {
|
||||
|
||||
@@ -18,6 +23,10 @@ public class FieldValidator {
|
||||
return new FieldValidator();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private VatCheckDao vatCheckDao;
|
||||
|
||||
|
||||
public FieldValidator notNull(Object object, String fieldName) {
|
||||
if (Objects.isNull(object)) {
|
||||
errors.add(MessageFormat.format(Translator.toLocale(GepafinConstant.FIELD_NOT_NULL), fieldName));
|
||||
@@ -65,4 +74,77 @@ public class FieldValidator {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user