/
AbstractGraphRepositoryProxy.java
199 lines (159 loc) · 7.25 KB
/
AbstractGraphRepositoryProxy.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
/*
* 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.graph.query;
import jakarta.data.page.Page;
import jakarta.data.page.Pageable;
import jakarta.data.repository.BasicRepository;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.NoSQLPage;
import org.eclipse.jnosql.mapping.core.query.AbstractRepositoryProxy;
import org.eclipse.jnosql.mapping.core.repository.DynamicQueryMethodReturn;
import org.eclipse.jnosql.mapping.core.repository.DynamicReturn;
import org.eclipse.jnosql.mapping.core.repository.RepositoryReflectionUtils;
import org.eclipse.jnosql.mapping.graph.GraphConverter;
import org.eclipse.jnosql.mapping.graph.GraphTemplate;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Template method to {@link BasicRepository} proxy on Graph
*
* @param <T> the entity type
* @param <K> the K entity
*/
abstract class AbstractGraphRepositoryProxy<T, K> extends AbstractRepositoryProxy<T, K> {
protected abstract Graph graph();
protected abstract GraphConverter converter();
protected abstract GraphTemplate template();
protected abstract Converters converters();
@Override
protected Object executeQuery(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
DynamicQueryMethodReturn methodReturn = DynamicQueryMethodReturn.builder()
.withArgs(params)
.withMethod(method)
.withTypeClass(type)
.withPrepareConverter(q -> template().prepare(q))
.withQueryConverter(q -> template().query(q)).build();
return methodReturn.execute();
}
@Override
protected Object executeDeleteByAll(Object instance, Method method, Object[] params) {
return executeDeleteMethod(method, params);
}
@Override
protected Object executeFindAll(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
return findAll(method, type, params);
}
@Override
protected Object executeExistByQuery(Object instance, Method method, Object[] params) {
return existsBy(method, params);
}
@Override
protected Object executeCountByQuery(Object instance, Method method, Object[] params) {
return countBy(method, params);
}
@Override
protected Object executeFindByQuery(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
return findBy(method, params, type);
}
@Override
protected Object executeParameterBased(Object instance, Method method, Object[] params) {
Class<?> type = entityMetadata().type();
Map<String, Object> parameters = RepositoryReflectionUtils.INSTANCE.getBy(method, params);
var methodName = "findBy" + parameters.keySet().stream()
.map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
.collect(Collectors.joining("And"));
Supplier<Stream<?>> querySupplier = () -> {
GraphQueryMethod queryMethod = new GraphQueryMethod(entityMetadata(),
graph().traversal().V(),
converters(), null, methodName, params);
return SelectQueryConverter.INSTANCE.apply(queryMethod, params)
.map(converter()::toEntity);
};
return converter(method, type, querySupplier, params);
}
private Object findAll(Method method, Class<?> typeClass, Object[] args) {
Supplier<Stream<?>> querySupplier = () -> {
GraphTraversal<Vertex, Vertex> traversal = graph().traversal().V().hasLabel(entityMetadata().name());
SelectQueryConverter.updateDynamicParameter(args, traversal, entityMetadata());
return traversal.toStream()
.map(converter()::toEntity);
};
return converter(method, typeClass, querySupplier, args);
}
private Object existsBy(Method method, Object[] args) {
Long countBy = (Long) countBy(method, args);
return countBy > 0;
}
private Object countBy(Method method, Object[] args) {
Supplier<Long> querySupplier = () -> {
GraphQueryMethod queryMethod = new GraphQueryMethod(entityMetadata(),
graph().traversal().V(),
converters(), method, args);
return CountQueryConverter.INSTANCE.apply(queryMethod, args);
};
return querySupplier.get();
}
private Object findBy(Method method, Object[] args, Class<?> typeClass) {
Supplier<Stream<?>> querySupplier = () -> {
GraphQueryMethod queryMethod = new GraphQueryMethod(entityMetadata(),
graph().traversal().V(),
converters(), method, args);
return SelectQueryConverter.INSTANCE.apply(queryMethod, args)
.map(converter()::toEntity);
};
return converter(method, typeClass, querySupplier, args);
}
private Object converter(Method method, Class<?> typeClass,
Supplier<Stream<?>> querySupplier,
Object[] args) {
Supplier<Optional<?>> singleSupplier =
DynamicReturn.toSingleResult(method).apply(querySupplier);
Function<Pageable, Page<?>> pageFunction = p -> {
List<?> entities = querySupplier.get().toList();
return NoSQLPage.of(entities, p);
};
DynamicReturn<?> dynamicReturn = DynamicReturn.builder()
.withClassSource(typeClass)
.withMethodSource(method)
.withResult(querySupplier)
.withSingleResult(singleSupplier)
.withPagination(DynamicReturn.findPageable(args))
.withStreamPagination(p -> querySupplier.get())
.withSingleResultPagination(p -> singleSupplier.get())
.withPage(pageFunction)
.build();
return dynamicReturn.execute();
}
private Object executeDeleteMethod(Method method, Object[] args) {
GraphQueryMethod queryMethod = new GraphQueryMethod(entityMetadata(),
graph().traversal().V(),
converters(), method, args);
List<Vertex> vertices = DeleteQueryConverter.INSTANCE.apply(queryMethod);
vertices.forEach(Vertex::remove);
return Void.class;
}
}