77
88import javax .lang .model .element .ExecutableElement ;
99
10+ import java .util .Set ;
11+
12+ import static java .lang .Character .toUpperCase ;
1013import static org .hibernate .processor .util .Constants .LIST ;
1114import static org .hibernate .processor .util .Constants .UNI ;
1215
@@ -60,32 +63,102 @@ public boolean hasStringAttribute() {
6063 return false ;
6164 }
6265
66+ private String capitalize (String string ) {
67+ return toUpperCase (string .charAt (0 )) + string .substring (1 );
68+ }
69+
70+ static final Set <String > eventTypes = Set .of ("insert" , "update" , "delete" );
71+
6372 @ Override
6473 public String getAttributeDeclarationString () {
6574 StringBuilder declaration = new StringBuilder ();
6675 preamble (declaration );
6776 nullCheck (declaration , parameterName );
77+ preEvent (declaration );
6878 if ( !isReactive () ) {
6979 declaration .append ( "\t try {\n " );
7080 }
7181 delegateCall (declaration );
72- returnArgument (declaration );
82+ returnArgumentReactively (declaration );
7383 if ( !isReactive () ) {
74- if ( returnArgument ) {
75- declaration
76- .append ( ";\n " );
77- }
7884 declaration .append ( "\t }\n " );
7985 }
8086 convertExceptions ( declaration );
8187 if ( isReactive () ) {
8288 declaration
8389 .append ( ";\n " );
8490 }
91+ postEvent (declaration );
92+ returnArgument (declaration );
8593 declaration .append ("}" );
8694 return declaration .toString ();
8795 }
8896
97+ private void postEvent (StringBuilder declaration ) {
98+ if ( annotationMetaEntity .getContext ().isDataEventPackageAvailable ()
99+ && annotationMetaEntity .getContext ().addDependentAnnotation ()
100+ && eventTypes .contains ( operationName )
101+ && !isReactive () ) {
102+ final String postEventType = "Post" + capitalize ( operationName ) + "Event" ;
103+ annotationMetaEntity .importType ( "jakarta.data.event." + postEventType );
104+ declaration
105+ .append ( "\t event.select(new TypeLiteral<" )
106+ .append ( postEventType )
107+ .append ( "<" )
108+ .append ( annotationMetaEntity .importType ( entity ) )
109+ .append ( ">>(){})\n \t \t \t .fire(new " )
110+ .append ( postEventType )
111+ .append ( "<>(" )
112+ .append ( parameterName )
113+ .append ( "));\n " );
114+ }
115+ }
116+
117+ private void preEvent (StringBuilder declaration ) {
118+ if ( annotationMetaEntity .getContext ().isDataEventPackageAvailable ()
119+ && annotationMetaEntity .getContext ().addDependentAnnotation ()
120+ && eventTypes .contains ( operationName )
121+ && !isReactive ()) {
122+ final String preEventType = "Pre" + capitalize ( operationName ) + "Event" ;
123+ annotationMetaEntity .importType ( "jakarta.data.event." + preEventType );
124+ annotationMetaEntity .importType ( "jakarta.data.event.LifecycleEvent" );
125+ annotationMetaEntity .importType ( "jakarta.enterprise.util.TypeLiteral" );
126+ annotationMetaEntity .importType ( "jakarta.enterprise.event.Event" );
127+ annotationMetaEntity .importType ( "jakarta.inject.Inject" );
128+ declaration
129+ .append ( "\t event.select(new TypeLiteral<" )
130+ .append ( preEventType )
131+ .append ( "<" )
132+ .append ( annotationMetaEntity .importType ( entity ) )
133+ .append ( ">>(){})\n \t \t \t .fire(new " )
134+ .append ( preEventType )
135+ .append ( "<>(" )
136+ .append ( parameterName )
137+ .append ( "));\n " );
138+ }
139+ }
140+
141+ private void returnArgument (StringBuilder declaration ) {
142+ if ( returnArgument && !isReactive () ) {
143+ declaration
144+ .append ( "\t return " )
145+ .append ( parameterName )
146+ .append ( ";\n " );
147+ }
148+ }
149+
150+ private void returnArgumentReactively (StringBuilder declaration ) {
151+ if ( isReactive () ) {
152+ if ( returnArgument ) {
153+ declaration
154+ .append ( "\n \t \t \t .replaceWith(" )
155+ .append (parameterName )
156+ .append (")" );
157+ }
158+ }
159+ }
160+
161+
89162 private void convertExceptions (StringBuilder declaration ) {
90163 if ( operationName .equals ("insert" ) ) {
91164 handle ( declaration ,
@@ -102,23 +175,6 @@ private void convertExceptions(StringBuilder declaration) {
102175 "jakarta.data.exceptions.DataException" );
103176 }
104177
105- private void returnArgument (StringBuilder declaration ) {
106- if ( returnArgument ) {
107- if ( isReactive () ) {
108- declaration
109- .append ( "\n \t \t \t " )
110- .append (".replaceWith(" )
111- .append (parameterName )
112- .append (")" );
113- }
114- else {
115- declaration
116- .append ("\t \t return " )
117- .append (parameterName );
118- }
119- }
120- }
121-
122178 private void delegateCall (StringBuilder declaration ) {
123179 if ( isReactive () ) {
124180 // TODO: handle the case of an iterable parameter
@@ -179,7 +235,7 @@ private void argument(StringBuilder declaration) {
179235 if ( isReactive () ) {
180236 declaration
181237 .append ("All" )
182- .append ("(" )
238+ .append ("((Object[]) " )
183239 .append (parameterName )
184240 .append (")" );
185241 }
0 commit comments