/*
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
*/
|
package com.megatim.fdxcommons.tools.database.queries.validation;
|
|
import com.megatim.fdxcommons.model.enumeration.TypeDonnee;
|
import com.megatim.fdxcommons.model.integration.json.JsonStructure;
|
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
|
import com.megatim.fdxcommons.tools.database.queries.metadata.InsertionLocalDateTimeValue;
|
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Optional;
|
import org.apache.commons.validator.GenericValidator;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class JsonDataValidationError {
|
|
private final ArrayList<String> errors = new ArrayList<>();
|
private final List<LinkedHashMap<String, Object>> data;
|
private final JsonStructure jsonStructure;
|
|
public JsonDataValidationError(List<LinkedHashMap<String, Object>> data, JsonStructure jsonStructure) {
|
this.data = data;
|
this.jsonStructure = jsonStructure;
|
}
|
|
public String validationError() {
|
if (errors.isEmpty()) {
|
int index = 1;
|
for (Map<String, Object> dataItem : data) {
|
String error = processDataItem(dataItem, jsonStructure);
|
|
if (error != null && !error.trim().isEmpty()) {
|
errors.add(elementNumberString(index++) + " : " + error);
|
}
|
}
|
}
|
return error();
|
}
|
|
private String processDataItem(Map<String, Object> dataItem, JsonStructure jsonStruct) {
|
StringBuilder lineError = new StringBuilder("");
|
|
//Vérifie si une colonne obligatoire est absente
|
lineError.append(addErrorForNotFoundColumn(dataItem, jsonStruct.getFields()));
|
|
for (Map.Entry<String, Object> dataItemEntry : dataItem.entrySet()) {
|
|
String columnName = dataItemEntry.getKey();
|
Object columnValue = dataItemEntry.getValue();
|
|
Optional<JsonStructure> optField = jsonStruct.getFields().stream().filter(f -> f.getName().equalsIgnoreCase(columnName)).findFirst();
|
|
if (!optField.isPresent()) {
|
lineError.append("La colonne ").append(columnName).append(" n'existe pas pour ce fichier;");
|
continue;
|
}
|
JsonStructure field = optField.get();
|
|
if (field.isCollection() || field.getTypeDonnee().equals(TypeDonnee.OBJET)) {
|
if (field.getTypeDonnee().equals(TypeDonnee.OBJET)) {
|
|
if (field.isCollection()) {
|
if (columnValue instanceof List) {
|
List liste = (List) columnValue;
|
int i = 1;
|
|
for (Object obj : liste) {
|
if (obj instanceof Map) {
|
lineError.append(processDataItem((Map<String, Object>) obj, field));
|
} else {
|
lineError.append("L'élément No ").append(i++).append(" de la colonne '").append(columnName).append("' doit être un objet;");
|
}
|
}
|
} else {
|
lineError.append(" La donnée de la colonne '").append(columnName).append("' doit être une liste;");
|
}
|
} else {
|
if (columnValue instanceof Map) {
|
lineError.append(processDataItem((Map<String, Object>) columnValue, field));
|
} else {
|
lineError.append(" La donnée de la colonne '").append(columnName).append("' doit être un objet;");
|
}
|
}
|
} else {
|
if (columnValue instanceof List) {
|
simpleListValueError(columnValue, field);
|
} else {
|
lineError.append(" La donnée de la colonne '").append(columnName).append("' doit être une liste;");
|
}
|
}
|
} else {
|
columnValueError(lineError, field, columnValue);
|
}
|
}
|
return lineError.toString();
|
}
|
|
private String simpleListValueError(Object columnValue, JsonStructure field) {
|
List liste = (List) columnValue;
|
StringBuilder arrayError = new StringBuilder("");
|
int i = 1;
|
|
for (Object obj : liste) {
|
arrayError.append(columnValueError(obj, field, i++));
|
}
|
return arrayError.toString().trim();
|
}
|
|
private void columnValueError(StringBuilder lineError, JsonStructure field, Object columnValue) {
|
String error = columnValueError(columnValue, field);
|
if (error != null && !error.trim().isEmpty()) {
|
lineError.append(error.trim());
|
}
|
}
|
|
private String error() {
|
StringBuilder stringBuilder = new StringBuilder("");
|
errors.forEach(e -> {
|
if (e != null && !e.trim().isEmpty()) {
|
stringBuilder.append(e.trim()).append("\n");
|
}
|
}
|
);
|
return stringBuilder.toString();
|
}
|
|
private String addErrorForNotFoundColumn(Map<String, Object> dataItem, List<JsonStructure> fields) {
|
StringBuilder message = new StringBuilder("");
|
|
fields.forEach(f -> {
|
String error = addErrorForNotFoundColumn(dataItem, f);
|
if (!error.isEmpty()) {
|
message.append("\n").append(error);
|
}
|
});
|
return message.toString();
|
}
|
|
private String addErrorForNotFoundColumn(Map<String, Object> dataItem, JsonStructure jsonStruct) {
|
String fieldName = jsonStruct.getName().toLowerCase();
|
if (jsonStruct.isRequired() && !columnNameExist(dataItem, fieldName)) {
|
return "La colonne " + fieldName + " est manquante. ";
|
}
|
return "";
|
}
|
|
private String columnValueError(Object columnValue, JsonStructure jsonStruct, int i) {
|
TypeDonnee typeDonnee = jsonStruct.getTypeDonnee();
|
|
switch (typeDonnee) {
|
case ALPHANUMERIQUE:
|
if (!isAlphaNumericColumn(columnValue)) {
|
return "L'élément No " + i + " doit être alphanumérique";
|
} else if (!lengthLowerOrEquals((String) columnValue, jsonStruct.getLengthh())) {
|
return "La taille de l'élément No " + i + " doit être inférieure ou égale à" + jsonStruct.getLengthh() + ";";
|
}
|
break;
|
case DECIMAL:
|
if (columnValue != null) {
|
|
String stringValue = columnValue.toString();
|
if (!GenericValidator.isDouble(stringValue)) {
|
return "L'élément No " + i + " doit être décimale";
|
} else if (!lengthLowerOrEquals(stringValue, jsonStruct.getLengthh())) {
|
return "La taille de l'élément No " + i + " doit être inférieure ou égale à " + jsonStruct.getLengthh() + ";";
|
}
|
|
}
|
break;
|
case NUMERIQUE:
|
if (columnValue != null) {
|
|
String stringValue = columnValue.toString();
|
if (!GenericValidator.isLong(stringValue)) {
|
return "L'élément No " + i + " doit être numérique;";
|
} else if (!lengthLowerOrEquals(stringValue, jsonStruct.getLengthh())) {
|
return "La taille de l'élément No " + i + " doit être inférieure ou égale à " + jsonStruct.getLengthh() + ";";
|
}
|
|
}
|
break;
|
case DATE:
|
if (columnValue != null) {
|
|
String stringValue = columnValue.toString();
|
if (!GenericValidator.isDate(stringValue, jsonStruct.getFormatDate(), true)) {
|
return "L'élément No " + i + " doit être une date";
|
} else if (!lengthEquals(stringValue, jsonStruct.getLengthh())) {
|
return "La taille de l'élément No " + i + " doit être égale à " + jsonStruct.getLengthh() + ";";
|
}
|
|
try {
|
new InsertionLocalDateTimeValue(jsonStruct.getFormatDate(), columnValue).value();
|
} catch (LocalDateTimeValueParseError ex) {
|
return "Impossible de parser la valeur " + stringValue + " au format " + jsonStruct.getFormatDate() + " pour l'élément No " + i + ";";
|
}
|
|
}
|
break;
|
default:
|
break;
|
}
|
|
return "";
|
}
|
|
private String columnValueError(Object columnValue, JsonStructure jsonStruct) {
|
TypeDonnee typeDonnee = jsonStruct.getTypeDonnee();
|
|
switch (typeDonnee) {
|
case ALPHANUMERIQUE:
|
if (!isAlphaNumericColumn(columnValue)) {
|
return "La colonne " + jsonStruct.getName() + " doit être alphanumérique;";
|
} else if (!lengthLowerOrEquals((String) columnValue, jsonStruct.getLengthh())) {
|
return "La taille de la colonne " + jsonStruct.getName() + " doit être inférieure ou égale à" + jsonStruct.getLengthh() + ";";
|
}
|
break;
|
case DECIMAL:
|
if (columnValue != null) {
|
|
String stringValue = columnValue.toString();
|
if (!GenericValidator.isDouble(stringValue)) {
|
return "La colonne " + jsonStruct.getName() + " doit être décimale";
|
} else if (!lengthLowerOrEquals(stringValue, jsonStruct.getLengthh())) {
|
return "La taille de la colonne " + jsonStruct.getName() + " doit être inférieure ou égale à " + jsonStruct.getLengthh() + ";";
|
}
|
|
}
|
break;
|
case NUMERIQUE:
|
if (columnValue != null) {
|
|
String stringValue = columnValue.toString();
|
if (!GenericValidator.isLong(stringValue)) {
|
return "La colonne " + jsonStruct.getName() + " doit être numérique;";
|
} else if (!lengthLowerOrEquals(stringValue, jsonStruct.getLengthh())) {
|
return "La taille de la colonne " + jsonStruct.getName() + " doit être inférieure ou égale à " + jsonStruct.getLengthh() + ";";
|
}
|
|
}
|
break;
|
case DATE:
|
if (columnValue != null) {
|
|
String stringValue = columnValue.toString();
|
if (!GenericValidator.isDate(stringValue, jsonStruct.getFormatDate(), true)) {
|
return "La colonne " + jsonStruct.getName() + " doit être une date;";
|
} else if (!lengthEquals(stringValue, jsonStruct.getLengthh())) {
|
return "La taille de la colonne " + jsonStruct.getName() + " doit être égale à " + jsonStruct.getLengthh() + ";";
|
}
|
|
try {
|
new InsertionLocalDateTimeValue(jsonStruct.getFormatDate(), columnValue).value();
|
} catch (LocalDateTimeValueParseError ex) {
|
return "Impossible de parser la valeur " + stringValue + " au format " + jsonStruct.getFormatDate() + " pour la colonne " + jsonStruct.getName() + ";";
|
}
|
|
}
|
break;
|
default:
|
break;
|
}
|
|
return "";
|
}
|
|
private boolean isAlphaNumericColumn(Object columnValue) {
|
return (columnValue == null || columnValue instanceof String);
|
}
|
|
private boolean lengthEquals(String value, int columnMaxLength) {
|
return value.trim().length() == columnMaxLength;
|
}
|
|
private boolean lengthLowerOrEquals(String value, int columnMaxLength) {
|
return value.trim().length() <= columnMaxLength;
|
}
|
|
private String elementNumberString(int index) {
|
return "Elément No" + index;
|
}
|
|
private boolean columnNameExist(Map<String, Object> dataItem, String jsonStructName) {
|
return dataItem.entrySet().stream().map(e -> e.getKey()).filter(key -> key.trim().toLowerCase().equals(jsonStructName)).findAny().isPresent();
|
}
|
}
|