Skip to content

Commit a31dbef

Browse files
committed
prototype impl of Jakarta Data events
1 parent fe9a3cc commit a31dbef

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/ClassWriter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ private static StringBuffer generateBody(Metamodel entity, Context context) {
110110

111111
pw.println();
112112

113+
if ( context.addDependentAnnotation() && entity.isImplementation() ) {
114+
pw.println("\t@Inject private Event<? super LifecycleEvent<?>> event;\n");
115+
}
116+
113117
for ( MetaAttribute metaMember : members ) {
114118
if ( metaMember.hasTypedAttribute() ) {
115119
metaMember.getAttributeDeclarationString().lines()

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/LifecycleMethod.java

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
*/
55
package org.hibernate.processor.annotation;
66

7+
78
import javax.lang.model.element.ExecutableElement;
89

10+
import java.util.Set;
11+
12+
import static java.lang.Character.toUpperCase;
913
import static org.hibernate.processor.util.Constants.UNI;
1014

1115
public class LifecycleMethod extends AbstractAnnotatedMethod {
@@ -52,14 +56,21 @@ public boolean hasStringAttribute() {
5256
return false;
5357
}
5458

59+
private String capitalize(String string) {
60+
return toUpperCase(string.charAt(0)) + string.substring(1);
61+
}
62+
63+
static final Set<String> eventTypes = Set.of("insert", "update", "delete");
64+
5565
@Override
5666
public String getAttributeDeclarationString() {
5767
StringBuilder declaration = new StringBuilder();
5868
preamble(declaration);
5969
nullCheck(declaration);
70+
preEvent( declaration );
6071
declaration.append("\ttry {\n");
6172
delegateCall(declaration);
62-
returnArgument(declaration);
73+
returnArgumentReactively(declaration);
6374
declaration.append("\t}\n");
6475
if ( operationName.equals("insert") ) {
6576
convertException(declaration,
@@ -74,34 +85,79 @@ public String getAttributeDeclarationString() {
7485
convertException(declaration,
7586
"jakarta.persistence.PersistenceException",
7687
"jakarta.data.exceptions.DataException");
88+
postEvent( declaration );
89+
returnArgument(declaration);
7790
declaration.append("}");
7891
return declaration.toString();
7992
}
8093

81-
private void returnArgument(StringBuilder declaration) {
82-
if ( returnArgument ) {
83-
if ( isReactive() ) {
94+
private void postEvent(StringBuilder declaration) {
95+
if ( annotationMetaEntity.getContext().addDependentAnnotation()
96+
&& eventTypes.contains( operationName )
97+
&& !isReactive() ) {
98+
final String postEventType = "Post" + capitalize( operationName ) + "Event";
99+
annotationMetaEntity.importType( "jakarta.data.event." + postEventType );
100+
declaration
101+
.append( "\tevent.select(new TypeLiteral<" )
102+
.append( postEventType )
103+
.append( "<" )
104+
.append( annotationMetaEntity.importType( entity ) )
105+
.append( ">>(){}).fire(new " )
106+
.append( postEventType )
107+
.append( "<>(" )
108+
.append( parameterName )
109+
.append( "));\n" );
110+
}
111+
}
112+
113+
private void preEvent(StringBuilder declaration) {
114+
if ( annotationMetaEntity.getContext().addDependentAnnotation()
115+
&& eventTypes.contains( operationName )
116+
&& !isReactive()) {
117+
final String preEventType = "Pre" + capitalize( operationName ) + "Event";
118+
annotationMetaEntity.importType( "jakarta.data.event." + preEventType );
119+
annotationMetaEntity.importType( "jakarta.data.event.LifecycleEvent" );
120+
annotationMetaEntity.importType( "jakarta.enterprise.util.TypeLiteral" );
121+
annotationMetaEntity.importType( "jakarta.enterprise.event.Event" );
122+
annotationMetaEntity.importType( "jakarta.inject.Inject" );
123+
declaration
124+
.append( "\tevent.select(new TypeLiteral<" )
125+
.append( preEventType )
126+
.append( "<" )
127+
.append( annotationMetaEntity.importType( entity ) )
128+
.append( ">>(){}).fire(new " )
129+
.append( preEventType )
130+
.append( "<>(" )
131+
.append( parameterName )
132+
.append( "));\n" );
133+
}
134+
}
135+
136+
private void returnArgumentReactively(StringBuilder declaration) {
137+
if ( isReactive() ) {
138+
if ( returnArgument ) {
84139
declaration
85-
.append(".replaceWith(")
86-
.append(parameterName)
87-
.append(")");
140+
.append(".replaceWith(")
141+
.append(parameterName)
142+
.append(")")
143+
.append(";\n");
88144
}
89145
else {
90-
declaration
91-
.append("\t\treturn ")
92-
.append(parameterName);
93-
}
94-
declaration
95-
.append(";\n");
96-
}
97-
else {
98-
if ( isReactive() ) {
99146
declaration
100147
.append(";\n");
101148
}
102149
}
103150
}
104151

152+
private void returnArgument(StringBuilder declaration) {
153+
if ( returnArgument && !isReactive() ) {
154+
declaration
155+
.append( "\treturn " )
156+
.append( parameterName )
157+
.append( ";\n" );
158+
}
159+
}
160+
105161
private void delegateCall(StringBuilder declaration) {
106162
if ( isReactive() ) {
107163
declaration

0 commit comments

Comments
 (0)