/*
|
* 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.queryadhoc.queries.metadata;
|
|
import com.megatim.queryadhoc.enums.TypeDonnee;
|
import com.megatim.queryadhoc.exceptions.BadDataValueException;
|
import com.megatim.queryadhoc.exceptions.ColumnNotFoundException;
|
import com.megatim.queryadhoc.exceptions.LocalDateTimeValueParseError;
|
import com.megatim.queryadhoc.model.DbColumnDescription;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
import org.apache.commons.validator.GenericValidator;
|
|
/**
|
*
|
* @author ASUS
|
*/
|
public class ParsedDataRow {
|
|
private final Map<String, Object> data;
|
private final String tableName;
|
private final Map<String, DbColumnDescription> columnNameToDbColumnDescription;
|
private final Map<String, Object> row = new HashMap<>();
|
|
public ParsedDataRow(Map<String, Object> data, List<DbColumnDescription> columnsDescription, String tableName) {
|
this.data = data;
|
this.tableName = tableName;
|
this.columnNameToDbColumnDescription
|
= columnsDescription
|
.stream()
|
.collect(Collectors.toMap(c -> c.getName().trim().toLowerCase(), c -> c));
|
}
|
|
public Map<String, Object> dataRow() throws ColumnNotFoundException, BadDataValueException, LocalDateTimeValueParseError {
|
|
if (row.isEmpty()) {
|
|
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
|
String key = entry.getKey();
|
Object value = entry.getValue();
|
|
DbColumnDescription columnDesc = columnNameToDbColumnDescription.get(key.trim());
|
if (columnDesc == null) {
|
throw new ColumnNotFoundException(key, tableName);
|
}
|
|
row.put(key.trim(), parse(columnDesc, value));
|
}
|
|
}
|
|
return row;
|
}
|
|
private Object parse(DbColumnDescription columnDesc, Object value) throws BadDataValueException, LocalDateTimeValueParseError {
|
|
TypeDonnee columnType = columnDesc.getTypeDonnee();
|
|
if (columnType.equals(TypeDonnee.ALPHANUMERIQUE) && (value == null
|
|| (value instanceof String && lengthLowerOrEquals((String) value, columnDesc.getTaille())))) {
|
|
return value != null ? value.toString().replaceAll("\\s+", " ").trim() : value;
|
|
} else if (columnType.equals(TypeDonnee.NUMERIQUE)) {
|
|
if (value == null) {
|
return 0L;
|
}
|
|
String stringValue = value.toString();
|
if (lengthLowerOrEquals(stringValue, columnDesc.getTaille()) && GenericValidator.isLong(stringValue)) {
|
return Long.valueOf(stringValue);
|
}
|
|
} else if (columnType.equals(TypeDonnee.DECIMAL)) {
|
|
if (value == null) {
|
return 0.0;
|
}
|
|
String stringValue = value.toString();
|
if (lengthLowerOrEquals(stringValue, columnDesc.getTaille()) && GenericValidator.isDouble(stringValue)) {
|
return Double.valueOf(stringValue);
|
}
|
|
} else if (columnType.equals(TypeDonnee.DATE)) {
|
|
if (value == null) {
|
return null;
|
}
|
String stringValue = value.toString().trim();
|
|
if (stringValue.isEmpty()) {
|
return null;
|
}
|
|
if (lengthEquals(stringValue, columnDesc.getTaille()) && GenericValidator.isDate(stringValue, columnDesc.getFormatDate(), true)) {
|
return new LocalDateTimeValue(columnDesc.getFormatDate(), value).value();
|
}
|
}
|
|
throw new BadDataValueException("La valeur " + value + " ne correspond pas au format attendu pour la colonne " + columnDesc.getName());
|
}
|
|
private boolean lengthEquals(String value, int columnMaxLength) {
|
return value.trim().length() == columnMaxLength;
|
}
|
|
private boolean lengthLowerOrEquals(String value, int columnMaxLength) {
|
return value.trim().length() <= columnMaxLength;
|
}
|
}
|