-
Notifications
You must be signed in to change notification settings - Fork 22
/
RepositoryElement.java
136 lines (116 loc) · 5.13 KB
/
RepositoryElement.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
/*
* Copyright (c) 2021 Otávio Santana and others
* 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
* Maximillian Arruda
*/
package org.eclipse.jnosql.lite.mapping.repository;
import jakarta.data.repository.BasicRepository;
import jakarta.data.repository.CrudRepository;
import org.eclipse.jnosql.lite.mapping.ValidationException;
import org.eclipse.jnosql.mapping.DatabaseType;
import org.eclipse.jnosql.mapping.NoSQLRepository;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import static org.eclipse.jnosql.lite.mapping.ProcessorUtil.isTypeElement;
import static org.eclipse.jnosql.lite.mapping.repository.RepositoryUtil.findRepository;
class RepositoryElement {
private final TypeElement element;
private final String entityType;
private final String keyType;
private final String repositoryInterface;
private final DatabaseType type;
private final boolean isNoSQLRepository;
private final List<MethodMetadata> methods;
public RepositoryElement(TypeElement element,
String entityType, String keyType,
String repositoryInterface,
DatabaseType type,
boolean isNoSQLRepository,
List<MethodMetadata> methods) {
this.element = element;
this.entityType = entityType;
this.keyType = keyType;
this.repositoryInterface = repositoryInterface;
this.type = type;
this.isNoSQLRepository = isNoSQLRepository;
this.methods = methods;
}
public String getClassName() {
return element.toString();
}
public DatabaseType getType() {
return type;
}
public RepositoryMetadata getMetadata(DatabaseType type) {
return switch (type) {
case DOCUMENT -> new SemiStructureRepositoryMetadata(this, "Document");
case COLUMN -> new SemiStructureRepositoryMetadata(this, "Column");
case GRAPH -> new SemiStructureRepositoryMetadata(this, "Graph");
case KEY_VALUE -> new KeyValueRepositoryMetadata(this);
default -> throw new UnsupportedOperationException("There is not template to this database type: " + type);
};
}
public String getEntityType() {
return entityType;
}
public String getKeyType() {
return keyType;
}
public String getSimpleName() {
return this.element.getSimpleName().toString();
}
public String getPackage() {
String qualifiedName = this.element.getQualifiedName().toString();
int index = qualifiedName.lastIndexOf('.');
return qualifiedName.substring(0, index);
}
public String getRepository() {
return repositoryInterface;
}
public List<MethodMetadata> getMethods() {
return this.methods;
}
static RepositoryElement of(Element element, ProcessingEnvironment processingEnv, DatabaseType type) {
if (isTypeElement(element)) {
TypeElement typeElement = (TypeElement) element;
Optional<TypeMirror> mirror = findRepository(typeElement.getInterfaces(), processingEnv);
if (mirror.isPresent()) {
boolean isNoSQLRepository = RepositoryUtil.isNoSQLRepository(typeElement.getInterfaces(), processingEnv);
TypeMirror typeMirror = mirror.get();
List<String> parameters = RepositoryUtil.findParameters(typeMirror);
String entityType = parameters.get(0);
String keyType = parameters.get(1);
String repositoryInterface = typeElement.getQualifiedName().toString();
List<MethodMetadata> methods = typeElement.getEnclosedElements()
.stream()
.map(e -> MethodMetadata.of(e, entityType, type, processingEnv))
.filter(Objects::nonNull)
.collect(Collectors.toList());
return new RepositoryElement(typeElement,
entityType, keyType, repositoryInterface, type, isNoSQLRepository, methods);
}
}
throw new ValidationException("The interface %s must extends %s"
.formatted(element.toString(), String.join(" or ",
BasicRepository.class.getName(), CrudRepository.class.getName(), NoSQLRepository.class.getName())));
}
public boolean isNoSQLRepository() {
return isNoSQLRepository;
}
}