/
BaseSemistructuredRepository.java
246 lines (211 loc) · 10.4 KB
/
BaseSemistructuredRepository.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* 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.semistructured.query;
import jakarta.data.Limit;
import jakarta.data.Sort;
import jakarta.data.page.Page;
import jakarta.data.page.PageRequest;
import org.eclipse.jnosql.communication.Params;
import org.eclipse.jnosql.communication.query.DeleteQuery;
import org.eclipse.jnosql.communication.query.SelectQuery;
import org.eclipse.jnosql.communication.query.method.DeleteMethodProvider;
import org.eclipse.jnosql.communication.query.method.SelectMethodProvider;
import org.eclipse.jnosql.communication.semistructured.CommunicationObserverParser;
import org.eclipse.jnosql.communication.semistructured.CriteriaCondition;
import org.eclipse.jnosql.communication.semistructured.DeleteQueryParams;
import org.eclipse.jnosql.communication.semistructured.DeleteQueryParser;
import org.eclipse.jnosql.communication.semistructured.Element;
import org.eclipse.jnosql.communication.semistructured.QueryParams;
import org.eclipse.jnosql.communication.semistructured.SelectQueryParser;
import org.eclipse.jnosql.mapping.semistructured.MappingQuery;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.NoSQLPage;
import org.eclipse.jnosql.mapping.semistructured.SemistructuredTemplate;
import org.eclipse.jnosql.mapping.core.query.AbstractRepositoryProxy;
import org.eclipse.jnosql.mapping.core.repository.SpecialParameters;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.core.repository.DynamicReturn;
import org.eclipse.jnosql.mapping.core.util.ParamsBinder;
import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* A base abstract class for implementing column-oriented repositories in a Java NoSQL database.
* This class provides common functionality for executing CRUD operations using column queries.
*
* @param <T> The type of entities managed by the repository.
*
*/
public abstract class BaseSemistructuredRepository<T, K> extends AbstractRepositoryProxy<T, K> {
private static final SelectQueryParser SELECT_PARSER = new SelectQueryParser();
private static final DeleteQueryParser DELETE_PARSER = new DeleteQueryParser();
private static final Object[] EMPTY_PARAM = new Object[0];
/**
* Retrieves the Converters instance responsible for converting data types.
*
* @return The Converters instance.
*/
protected abstract Converters converters();
/**
* Retrieves the metadata information about the entity managed by the repository.
*
* @return The EntityMetadata instance.
*/
protected abstract EntityMetadata entityMetadata();
/**
* Retrieves the SemistructuredTemplate instance for executing column queries.
*
* @return The SemistructuredTemplate instance.
*/
protected abstract SemistructuredTemplate template();
private CommunicationObserverParser parser;
private ParamsBinder paramsBinder;
protected org.eclipse.jnosql.communication.semistructured.SelectQuery query(Method method, Object[] args) {
SelectMethodProvider provider = SelectMethodProvider.INSTANCE;
SelectQuery selectQuery = provider.apply(method, entityMetadata().name());
QueryParams queryParams = SELECT_PARSER.apply(selectQuery, parser());
org.eclipse.jnosql.communication.semistructured.SelectQuery query = queryParams.query();
Params params = queryParams.params();
paramsBinder().bind(params, args(args), method);
return updateQueryDynamically(args(args), query);
}
private static Object[] args(Object[] args) {
return args == null ? EMPTY_PARAM : args;
}
protected org.eclipse.jnosql.communication.semistructured.DeleteQuery deleteQuery(Method method, Object[] args) {
DeleteMethodProvider deleteMethodFactory = DeleteMethodProvider.INSTANCE;
DeleteQuery deleteQuery = deleteMethodFactory.apply(method, entityMetadata().name());
DeleteQueryParams queryParams = DELETE_PARSER.apply(deleteQuery, parser());
var query = queryParams.query();
Params params = queryParams.params();
paramsBinder().bind(params, args(args), method);
return query;
}
/**
* Retrieves the ColumnObserverParser instance for parsing column observations.
*
* @return The ColumnObserverParser instance.
*/
protected CommunicationObserverParser parser() {
if (parser == null) {
this.parser = new RepositorySemistructuredObserverParser(entityMetadata());
}
return parser;
}
/**
* Retrieves the ParamsBinder instance for binding parameters to queries.
*
* @return The ParamsBinder instance.
*/
protected ParamsBinder paramsBinder() {
if (Objects.isNull(paramsBinder)) {
this.paramsBinder = new ParamsBinder(entityMetadata(), converters());
}
return paramsBinder;
}
@SuppressWarnings("unchecked")
protected Object executeFindByQuery(Method method, Object[] args, Class<?> typeClass, org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(typeClass)
.withMethodSource(method)
.withResult(() -> template().select(query))
.withSingleResult(() -> template().singleResult(query))
.withPagination(DynamicReturn.findPageRequest(args))
.withStreamPagination(streamPagination(query))
.withSingleResultPagination(getSingleResult(query))
.withPage(getPage(query))
.build();
return dynamicReturn.execute();
}
private org.eclipse.jnosql.communication.semistructured.SelectQuery includeInheritance(org.eclipse.jnosql.communication.semistructured.SelectQuery query){
EntityMetadata metadata = this.entityMetadata();
if(metadata.inheritance().isPresent()){
InheritanceMetadata inheritanceMetadata = metadata.inheritance().orElseThrow();
if(!inheritanceMetadata.parent().equals(metadata.type())){
CriteriaCondition condition = CriteriaCondition.eq(Element.of(inheritanceMetadata.discriminatorColumn(),
inheritanceMetadata.discriminatorValue()));
if(query.condition().isPresent()){
CriteriaCondition columnCondition = query.condition().orElseThrow();
condition = condition.and(columnCondition);
}
return new MappingQuery(query.sorts(), query.limit(), query.skip(),
condition, query.name());
}
}
return query;
}
protected Long executeCountByQuery(org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
return template().count(query);
}
protected boolean executeExistsByQuery(org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
return template().exists(query);
}
protected Function<PageRequest, Page<T>> getPage(org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
return p -> {
Stream<T> entities = template().select(query);
return NoSQLPage.of(entities.toList(), p);
};
}
protected Function<PageRequest, Optional<T>> getSingleResult(org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
return p -> template().singleResult(query);
}
protected Function<PageRequest, Stream<T>> streamPagination(org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
return p -> template().select(query);
}
protected org.eclipse.jnosql.communication.semistructured.SelectQuery updateQueryDynamically(Object[] args,
org.eclipse.jnosql.communication.semistructured.SelectQuery query) {
var documentQuery = includeInheritance(query);
SpecialParameters special = DynamicReturn.findSpecialParameters(args);
if (special.isEmpty()) {
return documentQuery;
}
Optional<Limit> limit = special.limit();
if (special.hasOnlySort()) {
List<Sort<?>> sorts = new ArrayList<>();
sorts.addAll(documentQuery.sorts());
sorts.addAll(special.sorts());
long skip = limit.map(l -> l.startAt() - 1).orElse(documentQuery.skip());
long max = limit.map(Limit::maxResults).orElse((int) documentQuery.limit());
return new MappingQuery(sorts, max,
skip,
documentQuery.condition().orElse(null),
documentQuery.name());
}
if (limit.isPresent()) {
long skip = limit.map(l -> l.startAt() - 1).orElse(documentQuery.skip());
long max = limit.map(Limit::maxResults).orElse((int) documentQuery.limit());
return new MappingQuery(documentQuery.sorts(), max,
skip,
documentQuery.condition().orElse(null),
documentQuery.name());
}
return special.pageRequest().<org.eclipse.jnosql.communication.semistructured.SelectQuery>map(p -> {
long size = p.size();
long skip = NoSQLPage.skip(p);
List<Sort<?>> sorts = documentQuery.sorts();
if (!special.sorts().isEmpty()) {
sorts = new ArrayList<>(documentQuery.sorts());
sorts.addAll(special.sorts());
}
return new MappingQuery(sorts, size, skip,
documentQuery.condition().orElse(null), documentQuery.name());
}).orElse(documentQuery);
}
}