/
AbstractMapperQuery.java
155 lines (121 loc) · 5.3 KB
/
AbstractMapperQuery.java
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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.mapping.document;
import org.eclipse.jnosql.communication.document.Document;
import org.eclipse.jnosql.communication.document.DocumentCondition;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.core.util.ConverterUtil;
import java.util.List;
import java.util.stream.StreamSupport;
import static java.util.Arrays.asList;
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
abstract class AbstractMapperQuery {
protected final String documentCollection;
protected boolean negate;
protected DocumentCondition condition;
protected boolean and;
protected String name;
protected transient final EntityMetadata mapping;
protected transient final Converters converters;
protected transient final JNoSQLDocumentTemplate template;
protected long start;
protected long limit;
AbstractMapperQuery(EntityMetadata mapping, Converters converters, JNoSQLDocumentTemplate template) {
this.mapping = mapping;
this.converters = converters;
this.documentCollection = mapping.name();
this.template = template;
mapping.inheritance().ifPresent(i -> {
if(!i.parent().equals(mapping.type())){
this.condition = DocumentCondition.eq(Document.of(i.discriminatorColumn(), i.discriminatorValue()));
this.and = true;
}
});
}
protected void appendCondition(DocumentCondition incomingCondition) {
DocumentCondition documentCondition = getDocumentCondition(incomingCondition);
if (nonNull(condition)) {
this.condition = and ? condition.and(documentCondition) : condition.or(documentCondition);
} else {
this.condition = documentCondition;
}
this.negate = false;
this.name = null;
}
protected <T> void betweenImpl(T valueA, T valueB) {
requireNonNull(valueA, "valueA is required");
requireNonNull(valueB, "valueB is required");
DocumentCondition newCondition = DocumentCondition
.between(Document.of(mapping.columnField(name), asList(getValue(valueA), getValue(valueB))));
appendCondition(newCondition);
}
protected <T> void inImpl(Iterable<T> values) {
requireNonNull(values, "values is required");
List<Object> convertedValues = StreamSupport.stream(values.spliterator(), false)
.map(this::getValue).collect(toList());
DocumentCondition newCondition = DocumentCondition
.in(Document.of(mapping.columnField(name), convertedValues));
appendCondition(newCondition);
}
protected <T> void eqImpl(T value) {
requireNonNull(value, "value is required");
DocumentCondition newCondition = DocumentCondition
.eq(Document.of(mapping.columnField(name), getValue(value)));
appendCondition(newCondition);
}
protected void likeImpl(String value) {
requireNonNull(value, "value is required");
DocumentCondition newCondition = DocumentCondition
.like(Document.of(mapping.columnField(name), getValue(value)));
appendCondition(newCondition);
}
protected <T> void gteImpl(T value) {
requireNonNull(value, "value is required");
DocumentCondition newCondition = DocumentCondition
.gte(Document.of(mapping.columnField(name), getValue(value)));
appendCondition(newCondition);
}
protected <T> void gtImpl(T value) {
requireNonNull(value, "value is required");
DocumentCondition newCondition = DocumentCondition
.gt(Document.of(mapping.columnField(name), getValue(value)));
appendCondition(newCondition);
}
protected <T> void ltImpl(T value) {
requireNonNull(value, "value is required");
DocumentCondition newCondition = DocumentCondition
.lt(Document.of(mapping.columnField(name), getValue(value)));
appendCondition(newCondition);
}
protected <T> void lteImpl(T value) {
requireNonNull(value, "value is required");
DocumentCondition newCondition = DocumentCondition
.lte(Document.of(mapping.columnField(name), getValue(value)));
appendCondition(newCondition);
}
protected Object getValue(Object value) {
return ConverterUtil.getValue(value, mapping, name, converters);
}
private DocumentCondition getDocumentCondition(DocumentCondition newCondition) {
if (negate) {
return newCondition.negate();
} else {
return newCondition;
}
}
}