20
20
import static org .omnifaces .utils .stream .Streams .stream ;
21
21
22
22
import java .lang .reflect .AnnotatedElement ;
23
- import java .lang .reflect .Member ;
24
23
import java .time .LocalDate ;
25
24
import java .time .LocalDateTime ;
26
25
import java .time .LocalTime ;
33
32
import java .util .stream .Collectors ;
34
33
import java .util .stream .Stream ;
35
34
35
+ import javax .naming .InitialContext ;
36
+
37
+ import jakarta .ejb .SessionContext ;
36
38
import jakarta .enterprise .inject .Typed ;
37
39
import jakarta .persistence .EntityManager ;
38
40
import jakarta .persistence .EnumType ;
54
56
import jakarta .persistence .metamodel .EntityType ;
55
57
import jakarta .persistence .metamodel .ListAttribute ;
56
58
import jakarta .persistence .metamodel .MapAttribute ;
57
- import jakarta .persistence .metamodel .Metamodel ;
58
59
import jakarta .persistence .metamodel .PluralAttribute ;
59
60
import jakarta .persistence .metamodel .SetAttribute ;
60
61
import jakarta .persistence .metamodel .SingularAttribute ;
61
62
62
63
import org .omnifaces .persistence .criteria .Numeric ;
64
+ import org .omnifaces .persistence .service .BaseEntityService ;
63
65
64
66
/**
65
67
* JPA utilities.
@@ -92,7 +94,7 @@ private JPA() {
92
94
* @return The currently configured bean validation mode.
93
95
*/
94
96
public static ValidationMode getValidationMode (EntityManager entityManager ) {
95
- Object validationMode = entityManager .getEntityManagerFactory ().getProperties ().get (PROPERTY_VALIDATION_MODE );
97
+ var validationMode = entityManager .getEntityManagerFactory ().getProperties ().get (PROPERTY_VALIDATION_MODE );
96
98
return validationMode != null ? ValidationMode .valueOf (validationMode .toString ().toUpperCase ()) : ValidationMode .AUTO ;
97
99
}
98
100
@@ -235,6 +237,23 @@ public static <K, T, V> Map<K, V> getResultMap(TypedQuery<T> typedQuery, Functio
235
237
236
238
// Entity utils -----------------------------------------------------------------------------------------------------------------------
237
239
240
+ /**
241
+ * Returns the currently active {@link BaseEntityService} from the {@link SessionContext}.
242
+ * @return The currently active {@link BaseEntityService} from the {@link SessionContext}.
243
+ * @throws IllegalStateException if there is none, which can happen if this method is called outside EJB context,
244
+ * or when currently invoked EJB service is not an instance of {@link BaseEntityService}.
245
+ */
246
+ @ SuppressWarnings ("unchecked" )
247
+ public static BaseEntityService <?, ?> getCurrentBaseEntityService () {
248
+ try {
249
+ var ejbContext = (SessionContext ) new InitialContext ().lookup ("java:comp/EJBContext" );
250
+ return (BaseEntityService <?, ?>) ejbContext .getBusinessObject (ejbContext .getInvokedBusinessInterface ());
251
+ }
252
+ catch (Exception e ) {
253
+ throw new IllegalStateException (e );
254
+ }
255
+ }
256
+
238
257
/**
239
258
* Returns count of all foreign key references to entity of given entity type with given ID of given identifier type.
240
259
* This is particularly useful in case you intend to check if the given entity is still referenced elsewhere in database.
@@ -247,7 +266,7 @@ public static <K, T, V> Map<K, V> getResultMap(TypedQuery<T> typedQuery, Functio
247
266
* @return Count of all foreign key references to entity of given entity type with given ID of given identifier type.
248
267
*/
249
268
public static <T , I > long countForeignKeyReferences (EntityManager entityManager , Class <T > entityType , Class <I > identifierType , I id ) {
250
- Metamodel metamodel = entityManager .getMetamodel ();
269
+ var metamodel = entityManager .getMetamodel ();
251
270
SingularAttribute <? super T , I > idAttribute = metamodel .entity (entityType ).getId (identifierType );
252
271
return metamodel .getEntities ().stream ()
253
272
.flatMap (entity -> getAttributesOfType (entity , entityType ))
@@ -263,14 +282,14 @@ public static <T, I> long countForeignKeyReferences(EntityManager entityManager,
263
282
}
264
283
265
284
private static <E > Class <?> getJavaType (Attribute <? super E , ?> attribute ) {
266
- return ( attribute instanceof PluralAttribute )
285
+ return attribute instanceof PluralAttribute
267
286
? ((PluralAttribute <?, ?, ?>) attribute ).getElementType ().getJavaType ()
268
287
: attribute .getJavaType ();
269
288
}
270
289
271
290
@ SuppressWarnings ("unchecked" )
272
291
private static <E , T , I > Long countReferencesTo (EntityManager entityManager , Attribute <E , ?> attribute , SingularAttribute <? super T , I > idAttribute , I id ) {
273
- CriteriaBuilder criteriaBuilder = entityManager .getCriteriaBuilder ();
292
+ var criteriaBuilder = entityManager .getCriteriaBuilder ();
274
293
CriteriaQuery <Long > query = criteriaBuilder .createQuery (Long .class );
275
294
Root <E > root = query .from (attribute .getDeclaringType ().getJavaType ());
276
295
Join <E , T > join ;
@@ -381,10 +400,10 @@ public static boolean isEnumeratedByOrdinal(Path<?> path) {
381
400
Bindable <?> model = path .getModel ();
382
401
383
402
if (model instanceof Attribute ) {
384
- Member member = ((Attribute <?, ?>) model ).getJavaMember ();
403
+ var member = ((Attribute <?, ?>) model ).getJavaMember ();
385
404
386
405
if (member instanceof AnnotatedElement ) {
387
- Enumerated enumerated = ((AnnotatedElement ) member ).getAnnotation (Enumerated .class );
406
+ var enumerated = ((AnnotatedElement ) member ).getAnnotation (Enumerated .class );
388
407
389
408
if (enumerated != null ) {
390
409
return enumerated .value () == EnumType .ORDINAL ;
0 commit comments