-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
GroupsPerOperation.java
122 lines (98 loc) · 3.97 KB
/
GroupsPerOperation.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
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.cfg.beanvalidation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.validation.groups.Default;
import org.hibernate.HibernateException;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.internal.util.collections.CollectionHelper;
/**
* @author Emmanuel Bernard
*/
public class GroupsPerOperation {
private static final String JPA_GROUP_PREFIX = "javax.persistence.validation.group.";
private static final String HIBERNATE_GROUP_PREFIX = "org.hibernate.validator.group.";
private static final Class<?>[] DEFAULT_GROUPS = new Class<?>[] { Default.class };
private static final Class<?>[] EMPTY_GROUPS = new Class<?>[] { };
private Map<Operation, Class<?>[]> groupsPerOperation = CollectionHelper.mapOfSize( 4 );
private GroupsPerOperation() {
}
public static GroupsPerOperation from(Map settings, ClassLoaderAccess classLoaderAccess) {
GroupsPerOperation groupsPerOperation = new GroupsPerOperation();
applyOperationGrouping( groupsPerOperation, Operation.INSERT, settings, classLoaderAccess );
applyOperationGrouping( groupsPerOperation, Operation.UPDATE, settings, classLoaderAccess );
applyOperationGrouping( groupsPerOperation, Operation.DELETE, settings, classLoaderAccess );
applyOperationGrouping( groupsPerOperation, Operation.DDL, settings, classLoaderAccess );
return groupsPerOperation;
}
private static void applyOperationGrouping(
GroupsPerOperation groupsPerOperation,
Operation operation,
Map settings,
ClassLoaderAccess classLoaderAccess) {
groupsPerOperation.groupsPerOperation.put(
operation,
buildGroupsForOperation( operation, settings, classLoaderAccess )
);
}
public static Class<?>[] buildGroupsForOperation(Operation operation, Map settings, ClassLoaderAccess classLoaderAccess) {
final Object property = settings.get( operation.getGroupPropertyName() );
if ( property == null ) {
return operation == Operation.DELETE ? EMPTY_GROUPS : DEFAULT_GROUPS;
}
if ( property instanceof Class<?>[] ) {
return (Class<?>[]) property;
}
if ( property instanceof String ) {
String stringProperty = (String) property;
String[] groupNames = stringProperty.split( "," );
if ( groupNames.length == 1 && groupNames[0].isEmpty() ) {
return EMPTY_GROUPS;
}
List<Class<?>> groupsList = new ArrayList<>(groupNames.length);
for (String groupName : groupNames) {
String cleanedGroupName = groupName.trim();
if ( cleanedGroupName.length() > 0) {
try {
groupsList.add( classLoaderAccess.classForName( cleanedGroupName ) );
}
catch ( ClassLoadingException e ) {
throw new HibernateException( "Unable to load class " + cleanedGroupName, e );
}
}
}
return groupsList.toArray( new Class<?>[groupsList.size()] );
}
//null is bad and excluded by instanceof => exception is raised
throw new HibernateException( JPA_GROUP_PREFIX + operation.getGroupPropertyName() + " is of unknown type: String or Class<?>[] only");
}
public Class<?>[] get(Operation operation) {
return groupsPerOperation.get( operation );
}
public static enum Operation {
INSERT("persist", JPA_GROUP_PREFIX + "pre-persist"),
UPDATE("update", JPA_GROUP_PREFIX + "pre-update"),
DELETE("remove", JPA_GROUP_PREFIX + "pre-remove"),
DDL("ddl", HIBERNATE_GROUP_PREFIX + "ddl");
private String exposedName;
private String groupPropertyName;
Operation(String exposedName, String groupProperty) {
this.exposedName = exposedName;
this.groupPropertyName = groupProperty;
}
public String getName() {
return exposedName;
}
public String getGroupPropertyName() {
return groupPropertyName;
}
}
}