package com.megatim.fdxcommons.tools.database.queries.metadata;
|
|
import com.megatim.fdxcommons.model.enumeration.Operateur;
|
import com.megatim.fdxcommons.model.pojo.BetweenOperatorValues;
|
import com.megatim.fdxcommons.tools.database.exceptions.BadQueryCriteriaException;
|
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
|
import com.megatim.fdxcommons.tools.database.tables.appcolumns.DateCreationColumnDefinition;
|
import java.math.BigDecimal;
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.LinkedHashMap;
|
|
/**
|
*
|
* @author Gabuntu
|
*/
|
public class QueryCriteriaCorrectValue {
|
|
private final int dataType;
|
private final Object criteriaValue;
|
private final Operateur operateur;
|
private final String dateFormat;
|
private final boolean isDateActionColumn;
|
|
public QueryCriteriaCorrectValue(int dataType, Object criteriaValue, Operateur operateur, String dateFormat, boolean isDateActionColumn) {
|
this.dataType = dataType;
|
this.criteriaValue = criteriaValue;
|
this.operateur = operateur;
|
this.dateFormat = dateFormat;
|
this.isDateActionColumn = isDateActionColumn;
|
}
|
|
public Object value() throws LocalDateTimeValueParseError, BadQueryCriteriaException {
|
if (operateurWithBetween()) {
|
Object lowerBound = null;
|
Object upperBound = null;
|
|
if (criteriaValue instanceof BetweenOperatorValues) {
|
BetweenOperatorValues values = (BetweenOperatorValues) criteriaValue;
|
if (values != null) {
|
lowerBound = values.getLowerBound();
|
upperBound = values.getUpperBound();
|
}
|
} else {
|
LinkedHashMap map = (LinkedHashMap) criteriaValue;
|
|
if (map != null) {
|
lowerBound = map.get("lowerBound");
|
upperBound = map.get("upperBound");
|
}
|
}
|
if (lowerBound != null && upperBound != null) {
|
if (dataTypeIsInt()) {
|
return new BetweenOperatorValues(intValue(lowerBound), intValue(upperBound));
|
} else if (dataTypeIsDecimal()) {
|
return new BetweenOperatorValues(decimalValue(lowerBound), decimalValue(upperBound));
|
} else if (dataTypeIsString()) {
|
return new BetweenOperatorValues(stringValue(lowerBound), stringValue(upperBound));
|
} else if (dataTypeIsTimeStamp()) {
|
return new BetweenOperatorValues(dateValue(lowerBound), dateValue(upperBound));
|
}
|
}
|
} else if (operateurWithIn()) {
|
if (criteriaValue instanceof Collection<?>) {
|
Collection<?> collection = (Collection<?>) criteriaValue;
|
return collectionValues(collection);
|
}
|
} else if (operateurWithComparison()) {
|
if (dataTypeIsInt()) {
|
return intValue(criteriaValue);
|
} else if (dataTypeIsDecimal()) {
|
return decimalValue(criteriaValue);
|
} else if (dataTypeIsString()) {
|
return stringValue(criteriaValue);
|
} else if (dataTypeIsTimeStamp()) {
|
return dateValue(criteriaValue);
|
} else if(dataTypeIsBoolean()) {
|
return booleanValue(criteriaValue);
|
}
|
} else if (operateurWithLike()) {
|
if (dataTypeIsString()) {
|
return stringValue(criteriaValue);
|
}
|
|
} else if (operateurWithoutValue()) {
|
return null;
|
}
|
|
throw new BadQueryCriteriaException(
|
"Opérateur inconnu");
|
}
|
|
private boolean operateurWithoutValue() {
|
return this.operateur.equals(Operateur.IS_NOT_NULL)
|
|| this.operateur.equals(Operateur.IS_NULL);
|
}
|
|
private boolean operateurWithBetween() {
|
return this.operateur.equals(Operateur.BETWEEN)
|
|| this.operateur.equals(Operateur.NOT_BETWEEN);
|
}
|
|
private boolean operateurWithIn() {
|
return this.operateur.equals(Operateur.IN)
|
|| this.operateur.equals(Operateur.NOT_IN);
|
}
|
|
private boolean operateurWithComparison() {
|
return this.operateur.equals(Operateur.EQUALS)
|
|| this.operateur.equals(Operateur.NOT_EQUALS)
|
|| this.operateur.equals(Operateur.GREATER_OR_EQUALS_THAN)
|
|| this.operateur.equals(Operateur.GREATER_THAN)
|
|| this.operateur.equals(Operateur.LOWER_OR_EQUALS_THAN)
|
|| this.operateur.equals(Operateur.LOWER_THAN);
|
}
|
|
private boolean operateurWithLike() {
|
return this.operateur.equals(Operateur.LIKE);
|
}
|
|
private boolean dataTypeIsString() {
|
return this.dataType == 12;
|
}
|
|
private boolean dataTypeIsTimeStamp() {
|
return this.dataType == 93;
|
}
|
|
private boolean dataTypeIsInt() {
|
return this.dataType == 4 || this.dataType == 5 || this.dataType == -5;
|
}
|
|
private boolean dataTypeIsBoolean() {
|
return this.dataType == -7;
|
}
|
|
private boolean dataTypeIsDecimal() {
|
return this.dataType == 3;
|
}
|
|
private Collection<?> collectionValues(Collection<?> collection) throws LocalDateTimeValueParseError, BadQueryCriteriaException {
|
if (dataTypeIsInt()) {
|
return collectionOfInt(collection);
|
} else if (dataTypeIsDecimal()) {
|
return collectionOfBigDecimal(collection);
|
} else if (dataTypeIsTimeStamp()) {
|
return collectionOfLocalDateTime(collection);
|
} else if (dataTypeIsString()) {
|
return collectionOfString(collection);
|
}
|
throw new BadQueryCriteriaException("La valeur de ce critère doit une liste");
|
}
|
|
private Collection<String> collectionOfString(Collection<?> collection) throws BadQueryCriteriaException {
|
Collection<String> cleanValues = new ArrayList<>();
|
for (Object elt : collection) {
|
cleanValues.add(stringValue(elt));
|
}
|
return cleanValues;
|
}
|
|
private String stringValue(Object element) throws BadQueryCriteriaException {
|
if (!(element instanceof String)) {
|
throw new BadQueryCriteriaException("Les critères associés à cette requête sont incorrects.");
|
}
|
return (String) element;
|
}
|
|
private boolean booleanValue(Object element) throws BadQueryCriteriaException {
|
if (!(element instanceof Boolean)) {
|
throw new BadQueryCriteriaException("Les critères associés à cette requête sont incorrects.");
|
}
|
return (Boolean) element;
|
}
|
|
private Collection<LocalDateTime> collectionOfLocalDateTime(Collection<?> collection) throws LocalDateTimeValueParseError {
|
Collection cleanValues = new ArrayList<>();
|
for (Object element : collection) {
|
cleanValues.add(dateValue(element));
|
}
|
return cleanValues;
|
}
|
|
private LocalDateTime dateValue(Object value) throws LocalDateTimeValueParseError {
|
if (isDateActionColumn) {
|
if (!(value instanceof LocalDateTime)) {
|
throw new LocalDateTimeValueParseError("Impossible de parser la valeur " + value + " pour la colonne " + new DateCreationColumnDefinition().name());
|
}
|
return (LocalDateTime) value;
|
} else {
|
return new CriteriaLocalDateTimeValue(dateFormat, value).value();
|
}
|
}
|
|
private Long intValue(Object value) throws BadQueryCriteriaException {
|
try {
|
return Long.valueOf(value.toString());
|
} catch (Exception ex) {
|
throw new BadQueryCriteriaException("La valeur de ce critère doit être un nombre entier ");
|
}
|
}
|
|
private BigDecimal decimalValue(Object value) throws BadQueryCriteriaException {
|
try {
|
return new BigDecimal(value.toString());
|
} catch (Exception ex) {
|
throw new BadQueryCriteriaException("La valeur de ce critère doit être un nombre décimal ");
|
}
|
}
|
|
private Collection<BigDecimal> collectionOfBigDecimal(Collection<?> collection) throws BadQueryCriteriaException {
|
Collection cleanValues = new ArrayList<>();
|
for (Object element : collection) {
|
cleanValues.add(decimalValue(element));
|
}
|
return cleanValues;
|
}
|
|
private Collection<Long> collectionOfInt(Collection<?> collection) throws BadQueryCriteriaException {
|
Collection cleanValues = new ArrayList<>();
|
for (Object element : collection) {
|
cleanValues.add(intValue(element));
|
}
|
return cleanValues;
|
}
|
}
|