Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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;
    }
}