/
EntityManager.java
276 lines (251 loc) · 11.2 KB
/
EntityManager.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package org.babyfish.jimmer.sql.runtime;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.meta.impl.DatabaseIdentifiers;
import org.babyfish.jimmer.sql.JoinTable;
import org.babyfish.jimmer.sql.ManyToOne;
import org.babyfish.jimmer.sql.association.meta.AssociationType;
import org.babyfish.jimmer.sql.meta.MiddleTable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class EntityManager {
private final Map<ImmutableType, ImmutableTypeInfo> map;
// Map<MicroServiceName, Map<UpperCase(TableName), *>>
private final Map<String, Map<String, ImmutableType>> tableNameTypeMap;
public EntityManager(Class<?> ... classes) {
this(Arrays.asList(classes));
}
public EntityManager(Collection<Class<?>> classes) {
if (!(classes instanceof Set<?>)) {
classes = new LinkedHashSet<>(classes);
}
Map<ImmutableType, ImmutableTypeInfo> map = new LinkedHashMap<>();
for (Class<?> clazz : classes) {
if (clazz != null) {
ImmutableType immutableType = ImmutableType.get(clazz);
if (!immutableType.isEntity()) {
throw new IllegalArgumentException(
"\"" +
immutableType +
"\" is not entity"
);
}
map.put(immutableType, new ImmutableTypeInfo());
immutableType = immutableType.getSuperType();
while (immutableType != null && (immutableType.isEntity() || immutableType.isMappedSuperclass())) {
map.put(immutableType, new ImmutableTypeInfo());
immutableType = immutableType.getSuperType();
}
}
}
for (Map.Entry<ImmutableType, ImmutableTypeInfo> e : map.entrySet()) {
ImmutableType type = e.getKey();
ImmutableTypeInfo info = e.getValue();
if (type.isMappedSuperclass()) {
for (ImmutableType otherType : map.keySet()) {
if (isImplementationType(type, otherType)) {
info.implementationTypes.add(otherType);
}
}
} else {
for (ImmutableType otherType : map.keySet()) {
if (type != otherType && type.isAssignableFrom(otherType)) {
info.allDerivedTypes.add(otherType);
if (type == otherType.getSuperType()) {
info.directDerivedTypes.add(otherType);
}
}
}
for (ImmutableProp prop : entityProps(type)) {
ImmutableType targetType = prop.getTargetType();
if (targetType != null && targetType.isEntity()) {
ImmutableTypeInfo targetInfo = map.get(targetType);
if (targetInfo == null) {
throw new IllegalArgumentException(
"The target type \"" +
targetType +
"\" of the property \"" +
prop +
"\" is not manged by the current entity manager"
);
}
targetInfo.backProps.add(prop);
}
}
}
}
for (ImmutableTypeInfo info : map.values()) {
info.implementationTypes = Collections.unmodifiableList(info.implementationTypes);
info.directDerivedTypes = Collections.unmodifiableList(info.directDerivedTypes);
info.allDerivedTypes = Collections.unmodifiableList(info.allDerivedTypes);
info.backProps = Collections.unmodifiableList(info.backProps);
}
this.map = Collections.unmodifiableMap(map);
this.tableNameTypeMap = createTableNameTypeMap();
}
public static EntityManager combine(EntityManager ... entityManagers) {
if (entityManagers.length == 0) {
throw new IllegalArgumentException("No entity managers");
}
if (entityManagers.length == 1) {
return entityManagers[0];
}
Set<Class<?>> classes = new LinkedHashSet<>();
for (EntityManager entityManager : entityManagers) {
for (ImmutableType type : entityManager.getAllTypes(null)) {
classes.add(type.getJavaClass());
}
}
return new EntityManager(classes);
}
public Set<ImmutableType> getAllTypes(String microServiceName) {
if (microServiceName == null) {
return map.keySet();
}
Set<ImmutableType> set = microServiceName.isEmpty() ?
new LinkedHashSet<>((map.size() * 4 + 2) / 3) :
new LinkedHashSet<>();
for (ImmutableType type : map.keySet()) {
if (type.getMicroServiceName().equals(microServiceName)) {
set.add(type);
}
}
return set;
}
public List<ImmutableType> getImplementationTypes(ImmutableType type) {
return info(type).implementationTypes;
}
public List<ImmutableType> getDirectDerivedTypes(ImmutableType type) {
return info(type).directDerivedTypes;
}
public List<ImmutableType> getAllDerivedTypes(ImmutableType type) {
return info(type).allDerivedTypes;
}
public List<ImmutableProp> getAllBackProps(ImmutableType type) {
return info(type).backProps;
}
@Nullable
public ImmutableType getTypeByTableName(String microServiceName, String tableName) {
Map<String, ImmutableType> subMap = tableNameTypeMap.get(microServiceName);
if (subMap == null) {
return null;
}
String standardTableName = DatabaseIdentifiers.comparableIdentifier(tableName);
return subMap.get(standardTableName);
}
@NotNull
public ImmutableType getNonNullTypeByTableName(String microServiceName, String tableName) {
ImmutableType type = getTypeByTableName(microServiceName, tableName);
if (type == null) {
throw new IllegalArgumentException(
"The table \"" +
tableName +
"\" of micro service \"" +
microServiceName +
"\" is not managed by current EntityManager"
);
}
return type;
}
private ImmutableTypeInfo info(ImmutableType type) {
ImmutableTypeInfo info = map.get(type);
if (info == null) {
throw new IllegalArgumentException(
"\"" + type + "\" is not managed by current EntityManager"
);
}
return info;
}
private boolean isImplementationType(ImmutableType mappedSuperClass, ImmutableType type) {
if (!mappedSuperClass.isMappedSuperclass()) {
throw new AssertionError("Internal bug");
}
if (!type.isEntity()) {
return false;
}
if (!mappedSuperClass.isAssignableFrom(type)) {
return false;
}
return type.getSuperType().isMappedSuperclass();
}
private Map<String, Map<String, ImmutableType>> createTableNameTypeMap() {
Map<String, Map<String, ImmutableType>> tableNameTypeMap = new HashMap<>();
for (ImmutableType type : map.keySet()) {
if (!type.isEntity()) {
continue;
}
Map<String, ImmutableType> subMap = tableNameTypeMap.computeIfAbsent(
type.getMicroServiceName(),
it -> new HashMap<>()
);
String tableName = DatabaseIdentifiers.comparableIdentifier(type.getTableName());
ImmutableType oldType = subMap.put(tableName, type);
if (oldType != null) {
tableSharedBy(tableName, oldType, type);
}
subMap.put(tableName, type);
for (ImmutableProp prop : entityProps(type)) {
if (prop.getStorage() instanceof MiddleTable) {
AssociationType associationType = AssociationType.of(prop);
String associationTableName = DatabaseIdentifiers.comparableIdentifier(associationType.getTableName());
oldType = subMap.put(associationTableName, associationType);
if (oldType != null && !oldType.equals(associationType)) {
tableSharedBy(tableName, oldType, associationType);
}
subMap.put(associationTableName, associationType);
}
}
}
return tableNameTypeMap;
}
private static void tableSharedBy(String tableName, ImmutableType type1, ImmutableType type2) {
if (type1 instanceof AssociationType && type2 instanceof AssociationType) {
AssociationType associationType1 = (AssociationType) type1;
AssociationType associationType2 = (AssociationType) type2;
if (associationType1.getSourceType() == associationType2.getTargetType() &&
associationType1.getTargetType() == associationType2.getSourceType()) {
throw new IllegalArgumentException(
"Illegal entity manager, the table \"" +
tableName +
"\" is shared by both \"" +
type1 +
"\" and \"" +
type2 +
"\". These two associations seem to form a bidirectional association, " +
"if so, please make one of them real (using @" +
JoinTable.class +
") and the other image (specify `mappedBy` of @" +
ManyToOne.class +
")"
);
}
}
throw new IllegalArgumentException(
"Illegal entity manager, the table \"" +
tableName +
"\" is shared by both \"" +
type1 +
"\" and \"" +
type2 +
"\""
);
}
private static Collection<ImmutableProp> entityProps(ImmutableType type) {
ImmutableType superType = type.getSuperType();
if (superType != null && superType.isMappedSuperclass()) {
return type.getProps().values();
}
return type.getDeclaredProps().values();
}
private static class ImmutableTypeInfo {
// For mapped super class
List<ImmutableType> implementationTypes = new ArrayList<>();
// For entity
List<ImmutableType> directDerivedTypes = new ArrayList<>();
// For entity
List<ImmutableType> allDerivedTypes = new ArrayList<>();
// For entity
List<ImmutableProp> backProps = new ArrayList<>();
}
}