44 */
55package org .hibernate .processor .annotation ;
66
7+
78import javax .lang .model .element .ExecutableElement ;
89
10+ import java .util .Set ;
11+
12+ import static java .lang .Character .toUpperCase ;
913import static org .hibernate .processor .util .Constants .UNI ;
1014
1115public 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 ("\t try {\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 ( "\t event.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 ( "\t event.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 \t return " )
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 ( "\t return " )
156+ .append ( parameterName )
157+ .append ( ";\n " );
158+ }
159+ }
160+
105161 private void delegateCall (StringBuilder declaration ) {
106162 if ( isReactive () ) {
107163 declaration
0 commit comments