/
RepositoryType.java
175 lines (160 loc) · 5.63 KB
/
RepositoryType.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
/*
* 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.core.query;
import jakarta.data.repository.BasicRepository;
import jakarta.data.repository.CrudRepository;
import jakarta.data.repository.Delete;
import jakarta.data.repository.Insert;
import jakarta.data.repository.OrderBy;
import jakarta.data.repository.BasicRepository;
import jakarta.data.repository.Query;
import jakarta.data.repository.Save;
import jakarta.data.repository.Update;
import jakarta.enterprise.inject.spi.CDI;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
/**
* It defines the operation that might be from the Method
*/
public enum RepositoryType {
/**
* Methods from either {@link CrudRepository}, {@link BasicRepository} and {@link BasicRepository}
*/
DEFAULT(""),
/**
* General query method returning the repository type.It starts with "findBy" key word
*/
FIND_BY("findBy"),
/**
* Delete query method returning either no result (void) or the delete count. It starts with "deleteBy" keyword
*/
DELETE_BY("deleteBy"),
/**
* Method that has the "FindAll" keyword
*/
FIND_ALL("findAll"),
/**
* Count projection returning a numeric result. It starts with "countBy" keyword
*/
COUNT_BY("countBy"),
/**
* Exists projection, returning typically a boolean result. It starts with "existsBy" keyword
*/
EXISTS_BY("existsBy"),
/**
* The last condition is parameter based. That will match the parameter in a simple query.
*/
PARAMETER_BASED(""),
/**
* Methods from {@link Object}
*/
OBJECT_METHOD(""),
/**
* The method that belongs to the interface using a default method.
*/
DEFAULT_METHOD(""),
/**
* The method that belongs to the interface using a custom repository.
*/
CUSTOM_REPOSITORY(""),
/**
* Method that has {@link jakarta.data.repository.OrderBy} annotation
*/
ORDER_BY(""),
/**
* Method that has {@link Query} annotation
*/
QUERY("", Query.class),
/**
* Method that has {@link jakarta.data.repository.Save} annotation
*/
SAVE("", Save.class),
/**
* Method that has {@link jakarta.data.repository.Insert} annotation
*/
INSERT("", Insert.class),
/**
* Method that has {@link jakarta.data.repository.Delete} annotation
*/
DELETE("", Delete.class),
/**
* Method that has {@link jakarta.data.repository.Update} annotation
*/
UPDATE("", Update.class);
private static final Predicate<Class<?>> IS_REPOSITORY_METHOD = Predicate.<Class<?>>isEqual(CrudRepository.class)
.or(Predicate.isEqual(BasicRepository.class))
.or(Predicate.isEqual(BasicRepository.class));
private static final Set<RepositoryType> KEY_WORLD_METHODS = EnumSet.of(FIND_BY, DELETE_BY, COUNT_BY, EXISTS_BY);
private static final Set<RepositoryType> OPERATION_ANNOTATIONS = EnumSet.of(INSERT, SAVE, DELETE, UPDATE, QUERY);
private final String keyword;
private final Class<? extends Annotation> annotation;
RepositoryType(String keyword) {
this.keyword = keyword;
this.annotation = null;
}
RepositoryType(String keyword, Class<? extends Annotation> annotation) {
this.keyword = keyword;
this.annotation = annotation;
}
/**
* Returns an operation type from the {@link Method}
*
* @param method the method
* @return a repository type
*/
public static RepositoryType of(Method method, Class<?> repositoryType) {
Objects.requireNonNull(method, "method is required");
Class<?> declaringClass = method.getDeclaringClass();
if (method.isDefault()) {
return DEFAULT_METHOD;
}
if (Object.class.equals(declaringClass)) {
return OBJECT_METHOD;
}
if (IS_REPOSITORY_METHOD.test(declaringClass)) {
return DEFAULT;
}
if (method.getAnnotationsByType(OrderBy.class).length > 0) {
return ORDER_BY;
}
if (!repositoryType.equals(declaringClass) && isCustomRepository(declaringClass)) {
return CUSTOM_REPOSITORY;
}
String methodName = method.getName();
if (FIND_ALL.keyword.equals(methodName)) {
return FIND_ALL;
}
Predicate<RepositoryType> hasAnnotation = a -> method.getAnnotation(a.annotation) != null;
if (OPERATION_ANNOTATIONS.stream().anyMatch(hasAnnotation)) {
return OPERATION_ANNOTATIONS.stream()
.filter(hasAnnotation)
.findFirst().orElseThrow();
}
return KEY_WORLD_METHODS.stream()
.filter(k -> methodName.startsWith(k.keyword))
.findFirst().orElse(PARAMETER_BASED);
}
private static boolean isCustomRepository(Class<?> type) {
try {
return CDI.current().select(type).isResolvable();
}catch (Exception e) {
return false;
}
}
}