-
Notifications
You must be signed in to change notification settings - Fork 72
/
AnnotationOperation.java
249 lines (226 loc) · 9.17 KB
/
AnnotationOperation.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
247
248
249
/*
* Copyright (c) 2023 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.core.query;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import static java.util.stream.StreamSupport.stream;
/**
* Enumeration representing the operations with annotations.
* Each operation defines a specific behavior when invoked.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public enum AnnotationOperation {
/**
* Represents the {@link jakarta.data.repository.Insert} operation.
*/
INSERT {
@Override
public Object invoke(Operation operation) {
checkParameterNumber(operation);
Object param = operation.params[0];
ReturnType returnType = new ReturnType(operation.method);
if (param instanceof Iterable entities) {
List<?> result = operation.repository.insertAll(stream(entities.spliterator(), false).toList());
return returnInsert(returnType, result);
} else if (param.getClass().isArray()) {
Iterable<?> result = operation.repository.insertAll(Arrays.asList((Object[]) param));
return returnInsert(returnType, result);
} else {
var result = operation.repository.insert(param);
return returnInsert(returnType, result);
}
}
private static Object returnInsert(ReturnType returnType, Object result) {
if (returnType.isVoid()) {
return Void.TYPE;
} else if (returnType.isInt()) {
return 1;
} else if (returnType.isLong()) {
return 1L;
} else {
return result;
}
}
},
/**
* Represents the {@link jakarta.data.repository.Update} operation.
*/
UPDATE {
@Override
public Object invoke(Operation operation) {
checkParameterNumber(operation);
Object param = operation.params[0];
ReturnType returnType = new ReturnType(operation.method);
if (param instanceof Iterable entities) {
return executeIterable(operation, entities, returnType, false, null);
} else if (param.getClass().isArray()) {
List<Object> entities = Arrays.asList((Object[]) param);
return executeIterable(operation, entities, returnType, true, param);
} else {
return executeSingleEntity(operation, param, returnType);
}
}
private static Object executeIterable(Operation operation, Iterable entities, ReturnType returnType,
boolean isArray, Object param) {
List<?> result = operation.repository.updateAll(stream(entities.spliterator(), false).toList());
if (returnType.isVoid()) {
return Void.TYPE;
} else if (returnType.isBoolean()) {
return true;
} else if (returnType.isInt()) {
return result.size();
} else if (returnType.isLong()) {
return (long) result.size();
} else if (isArray) {
return result.toArray();
} else {
return entities;
}
}
private static Object executeSingleEntity(Operation operation, Object param, ReturnType returnType) {
var result = operation.repository.update(param);
if (returnType.isVoid()) {
return Void.TYPE;
} else if (returnType.isBoolean()) {
return result;
} else if (returnType.isInt()) {
return 1;
} else if (returnType.isLong()) {
return 1L;
} else {
return param;
}
}
},
/**
* Represents the {@link jakarta.data.repository.Delete} operation.
*/
DELETE {
@Override
public Object invoke(Operation operation) {
checkParameterNumber(operation);
Object param = operation.params[0];
ReturnType returnType = new ReturnType(operation.method);
if (param instanceof Iterable entities) {
return executeIterable(operation, entities, returnType);
} else if (param.getClass().isArray()) {
List<Object> entities = Arrays.asList((Object[]) param);
return executeIterable(operation, entities, returnType);
} else {
return executeSingleEntity(operation, param, returnType);
}
}
private static Object executeIterable(Operation operation, Iterable entities, ReturnType returnType) {
operation.repository.deleteAll(stream(entities.spliterator(), false).toList());
if (returnType.isVoid()) {
return Void.TYPE;
} else if (returnType.isBoolean()) {
return true;
} else if (returnType.isInt()) {
return (int) stream(entities.spliterator(), false).count();
} else if (returnType.isLong()) {
return stream(entities.spliterator(), false).count();
}
return null;
}
private static Object executeSingleEntity(Operation operation, Object param, ReturnType returnType) {
operation.repository.delete(param);
if (returnType.isVoid()) {
return Void.TYPE;
} else if (returnType.isBoolean()) {
return true;
} else if (returnType.isInt()) {
return 1;
} else if (returnType.isLong()) {
return 1L;
}
return null;
}
},
/**
* Represents the {@link jakarta.data.repository.Save} operation.
*/
SAVE {
@Override
public Object invoke(Operation operation) {
checkParameterNumber(operation);
Object param = operation.params[0];
ReturnType returnType = new ReturnType(operation.method);
if (param instanceof Iterable entities) {
Iterable<?> result = operation.repository.saveAll(stream(entities.spliterator(), false).toList());
return returnType.isVoid() ? Void.TYPE : result;
} else if (param.getClass().isArray()) {
Iterable<?> result = operation.repository.saveAll(Arrays.asList((Object[]) param));
return returnType.isVoid() ? Void.TYPE : result;
} else {
Object result = operation.repository.save(param);
return returnType.isVoid() ? Void.TYPE : result;
}
}
};
private static void checkParameterNumber(Operation operation) {
if (operation.params.length != 1) {
throw new UnsupportedOperationException("The method operation requires one parameter, please check the method: "
+ operation.method);
}
}
public abstract Object invoke(Operation operation);
public record Operation(Method method, Object[] params, AbstractRepository repository) {
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Operation operation = (Operation) o;
return Objects.equals(method, operation.method)
&& Arrays.equals(params, operation.params)
&& Objects.equals(repository, operation.repository);
}
@Override
public int hashCode() {
return Objects.hash(method, repository) + 31 * Arrays.hashCode(params);
}
@Override
public String toString() {
return "Operation{" +
"method=" + method +
", params=" + Arrays.toString(params) +
", repository=" + repository +
'}';
}
}
private record ReturnType(Method method) {
boolean isVoid() {
return method.getReturnType().equals(Void.TYPE);
}
boolean isBoolean() {
return method.getReturnType().equals(Boolean.class)
|| method.getReturnType().equals(Boolean.TYPE);
}
boolean isInt() {
return method.getReturnType().equals(Integer.class)
|| method.getReturnType().equals(Integer.TYPE);
}
public boolean isLong() {
return method.getReturnType().equals(Long.class)
|| method.getReturnType().equals(Long.TYPE);
}
}
}