-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathPersistentEntity.java
441 lines (403 loc) · 15.3 KB
/
PersistentEntity.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.model;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.data.annotation.Embeddable;
import io.micronaut.data.model.naming.NamingStrategy;
import io.micronaut.data.model.runtime.RuntimePersistentEntity;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;
import static io.micronaut.data.model.AssociationUtils.CAMEL_CASE_SPLIT_PATTERN;
/**
* Models a persistent entity and provides an API that can be used both within the compiler and at runtime. The {@link io.micronaut.core.annotation.AnnotationMetadata} provided is consistent both at runtime and compilation time.
*
* @see PersistentProperty
* @author Graeme Rocher
* @since 1.0
*/
@SuppressWarnings("rawtypes")
public interface PersistentEntity extends PersistentElement {
/**
* The entity name including any package prefix.
*
* @return The entity name
*/
@NonNull String getName();
/**
* @return A name to use when referring to this element via an alias.
*/
@NonNull
String getAliasName();
/**
* Has composite identity.
*
* @return The true if composite identity present
*/
default boolean hasCompositeIdentity() {
return getCompositeIdentity() != null;
}
/**
* Has identity.
*
* @return The true if identity present
*/
default boolean hasIdentity() {
return getIdentity() != null;
}
/**
* The composite id.
*
* @return The composite id or null if there isn't one
*/
@Nullable PersistentProperty[] getCompositeIdentity();
/**
* Returns the identity of the instance.
*
* @return The identity or null if there isn't one
*/
@Nullable PersistentProperty getIdentity();
/**
* Returns the version property.
*
* @return the property
*/
@Nullable PersistentProperty getVersion();
/**
* Is the entity versioned for optimistic locking.
*
* @return true if versioned
*/
default boolean isVersioned() {
return getVersion() != null;
}
/**
* A list of properties to be persisted.
* @return A list of PersistentProperty instances
*/
@NonNull Collection<? extends PersistentProperty> getPersistentProperties();
/**
* A list of the associations for this entity. This is typically
* a subset of the list returned by {@link #getPersistentProperties()}
*
* @return A list of associations
*/
@NonNull
default Collection<? extends Association> getAssociations() {
return getPersistentProperties()
.stream()
.filter(bp -> bp instanceof Association)
.map(bp -> (Association) bp)
.collect(Collectors.toList());
}
/**
* A list of embedded associations for this entity. This is typically
* a subset of the list returned by {@link #getPersistentProperties()}
*
* @return A list of associations
*/
default @NonNull Collection<Embedded> getEmbedded() {
return getPersistentProperties().stream()
.filter(p -> p instanceof Embedded)
.map(p -> (Embedded) p)
.collect(Collectors.toList());
}
/**
* Obtains a PersistentProperty instance by name.
*
* @param name The name of the property
* @return The PersistentProperty or null if it doesn't exist
*/
@Nullable PersistentProperty getPropertyByName(String name);
/**
* Obtains an identity PersistentProperty instance by name.
*
* @param name The name of the identity property
* @return The PersistentProperty or null if it doesn't exist
*/
default @Nullable PersistentProperty getIdentityByName(String name) {
PersistentProperty identity = getIdentity();
if (identity != null && identity.getName().equals(name)) {
return identity;
}
PersistentProperty[] compositeIdentities = getCompositeIdentity();
if (compositeIdentities != null) {
for (PersistentProperty compositeIdentity : compositeIdentities) {
if (compositeIdentity.getName().equals(name)) {
return compositeIdentity;
}
}
}
return null;
}
/**
* A list of property names that a persistent.
* @return A List of strings
*/
@NonNull Collection<String> getPersistentPropertyNames();
/**
* @return Is the entity embeddable.
*/
default boolean isEmbeddable() {
return getAnnotationMetadata().hasAnnotation(Embeddable.class);
}
/**
* @return The simple name without the package of entity
*/
@NonNull
default String getSimpleName() {
return NameUtils.getSimpleName(getName());
}
/**
* @return Returns the name of the class decapitalized form
*/
default @NonNull String getDecapitalizedName() {
return NameUtils.decapitalize(getSimpleName());
}
/**
* Returns whether the specified entity asserts ownership over this
* entity.
*
* @param owner The owning entity
* @return True if it does own this entity
*/
boolean isOwningEntity(PersistentEntity owner);
/**
* Returns the parent entity of this entity.
* @return The ParentEntity instance
*/
@Nullable PersistentEntity getParentEntity();
/**
* Computes a dot separated property path for the given camel case path.
* @param camelCasePath The camel case path
* @return The dot separated version or null if it cannot be computed
*/
default Optional<String> getPath(String camelCasePath) {
List<String> path = Arrays.stream(CAMEL_CASE_SPLIT_PATTERN.split(camelCasePath))
.map(NameUtils::decapitalize)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(path)) {
Iterator<String> i = path.iterator();
StringBuilder b = new StringBuilder();
PersistentEntity currentEntity = this;
String name = null;
while (i.hasNext()) {
name = name == null ? i.next() : name + NameUtils.capitalize(i.next());
PersistentProperty sp = currentEntity.getPropertyByName(name);
if (sp == null) {
PersistentProperty identity = currentEntity.getIdentity();
if (identity != null) {
if (identity.getName().equals(name)) {
sp = identity;
} else if (identity instanceof Association) {
PersistentEntity idEntity = ((Association) identity).getAssociatedEntity();
sp = idEntity.getPropertyByName(name);
}
}
}
if (sp != null) {
if (sp instanceof Association) {
b.append(name);
if (i.hasNext()) {
b.append('.');
}
currentEntity = ((Association) sp).getAssociatedEntity();
name = null;
} else if (!i.hasNext()) {
b.append(name);
}
}
}
return b.length() == 0 || b.charAt(b.length() - 1) == '.' ? Optional.empty() : Optional.of(b.toString());
}
return Optional.empty();
}
/**
* Obtains the root entity of an inheritance hierarchy.
* @return The root entity
*/
default @NonNull PersistentEntity getRootEntity() {
return this;
}
/**
* Whether this entity is a root entity.
* @return True if it is a root entity
*/
default boolean isRoot() {
return getRootEntity() == this;
}
/**
* Return a property for a dot separated property path such as {@code foo.bar.prop}
* .
* @param path The path
* @return The property
*/
default Optional<PersistentProperty> getPropertyByPath(String path) {
if (path.indexOf('.') == -1) {
PersistentProperty pp = getPropertyByName(path);
if (pp == null) {
PersistentProperty identity = getIdentity();
if (identity != null) {
if (identity.getName().equals(path)) {
pp = identity;
} else if (identity instanceof Embedded) {
PersistentEntity idEntity = ((Embedded) identity).getAssociatedEntity();
pp = idEntity.getPropertyByName(path);
}
}
}
return Optional.ofNullable(pp);
} else {
String[] tokens = path.split("\\.");
PersistentEntity startingEntity = this;
PersistentProperty prop = null;
for (String token : tokens) {
prop = startingEntity.getPropertyByName(token);
if (prop == null) {
PersistentProperty identity = startingEntity.getIdentity();
if (identity != null && identity.getName().equals(token)) {
prop = identity;
} else {
return Optional.empty();
}
}
if (prop instanceof Association) {
startingEntity = ((Association) prop).getAssociatedEntity();
}
}
return Optional.ofNullable(prop);
}
}
/**
* Return a {@link PersistentPropertyPath} by path such as {@code foo.bar.prop}.
* .
* @param path The path
* @return The properties
*/
@Nullable
default PersistentPropertyPath getPropertyPath(@NonNull String path) {
if (path.indexOf('.') == -1) {
return getPropertyPath(new String[] {path});
}
return getPropertyPath(StringUtils.splitOmitEmptyStringsList(path, '.').toArray(new String[0]));
}
/**
* Return a {@link PersistentPropertyPath} by path such as {@code foo.bar.prop}.
* .
* @param propertyPath The path
* @return The properties
*/
@Nullable
default PersistentPropertyPath getPropertyPath(@NonNull String[] propertyPath) {
if (propertyPath.length == 0) {
return null;
}
if (propertyPath.length == 1) {
String propertyName = propertyPath[0];
PersistentProperty pp = getPropertyByName(propertyName);
if (pp == null) {
PersistentProperty identity = getIdentity();
if (identity != null) {
if (identity.getName().equals(propertyName)) {
pp = identity;
} else if (identity instanceof Embedded) {
PersistentEntity idEntity = ((Embedded) identity).getAssociatedEntity();
pp = idEntity.getPropertyByName(propertyName);
if (pp != null) {
return PersistentPropertyPath.of(Collections.singletonList((Embedded) identity), pp, identity.getName() + "." + pp.getName());
}
}
}
PersistentProperty version = getVersion();
if (version != null) {
if (version.getName().equals(propertyName)) {
pp = version;
}
}
}
return pp == null ? null : PersistentPropertyPath.of(Collections.emptyList(), pp, propertyName);
} else {
List<Association> associations = new ArrayList<>(propertyPath.length - 1);
PersistentEntity startingEntity = this;
for (int i = 0; i < propertyPath.length - 1; i++) {
String propertyName = propertyPath[i];
PersistentProperty prop = startingEntity.getPropertyByName(propertyName);
if (prop instanceof Association association) {
startingEntity = association.getAssociatedEntity();
associations.add(association);
} else {
if (prop == null) {
return null;
}
throw new IllegalArgumentException("Invalid association path. Property [" + propertyName + "] of [" + startingEntity + "] is not an association in [" + String.join(".", propertyPath) + "]");
}
}
PersistentProperty prop = startingEntity.getPropertyByName(propertyPath[propertyPath.length - 1]);
if (prop == null) {
return null;
}
return PersistentPropertyPath.of(associations, prop);
}
}
/**
* Obtain the naming strategy for the entity.
* @return The naming strategy
*/
@NonNull
NamingStrategy getNamingStrategy();
/**
* Find the naming strategy that is defined for the entity.
* @return The optional naming strategy
*/
@NonNull
Optional<NamingStrategy> findNamingStrategy();
/**
* Obtains a PersistentProperty representing id or version property by name.
*
* @param name The name of the id or version property
* @return The PersistentProperty used as id or version or null if it doesn't exist
*/
PersistentProperty getIdOrVersionPropertyByName(String name);
/**
* Creates a new persistent entity representation of the given type. The type
* must be annotated with {@link io.micronaut.core.annotation.Introspected}. This method will create a new instance on demand and does not cache.
*
* @param type The type
* @param <T> The generic type
* @return The entity
*/
static @NonNull <T> RuntimePersistentEntity<T> of(@NonNull Class<T> type) {
ArgumentUtils.requireNonNull("type", type);
return new RuntimePersistentEntity<>(type);
}
/**
* Creates a new persistent entity representation of the given type. The type
* must be annotated with {@link io.micronaut.core.annotation.Introspected}. This method will create a new instance on demand and does not cache.
*
* @param introspection The introspection
* @param <T> The generic type
* @return The entity
*/
static @NonNull <T> RuntimePersistentEntity<T> of(@NonNull BeanIntrospection<T> introspection) {
ArgumentUtils.requireNonNull("introspection", introspection);
return new RuntimePersistentEntity<T>(introspection);
}
}