1515
1616public class LifecycleMethod extends AbstractAnnotatedMethod {
1717 private final String entity ;
18+ private final String actualEntity ;
1819 private final String methodName ;
1920 private final String parameterName ;
2021 private final String operationName ;
@@ -33,6 +34,7 @@ public LifecycleMethod(
3334 AnnotationMetaEntity annotationMetaEntity ,
3435 ExecutableElement method ,
3536 String entity ,
37+ String actualEntity ,
3638 String methodName ,
3739 String parameterName ,
3840 String sessionName ,
@@ -44,6 +46,7 @@ public LifecycleMethod(
4446 boolean hasGeneratedId ) {
4547 super (annotationMetaEntity , method , sessionName , sessionType );
4648 this .entity = entity ;
49+ this .actualEntity = actualEntity ;
4750 this .methodName = methodName ;
4851 this .parameterName = parameterName ;
4952 this .operationName = operationName ;
@@ -74,7 +77,7 @@ public String getAttributeDeclarationString() {
7477 StringBuilder declaration = new StringBuilder ();
7578 preamble (declaration );
7679 nullCheck (declaration , parameterName );
77- preEvent (declaration );
80+ fireEvents (declaration , "Pre" );
7881 if ( !isReactive () ) {
7982 declaration .append ( "\t try {\n " );
8083 }
@@ -88,53 +91,71 @@ public String getAttributeDeclarationString() {
8891 declaration
8992 .append ( ";\n " );
9093 }
91- postEvent (declaration );
94+ fireEvents (declaration , "Post" );
9295 returnArgument (declaration );
9396 declaration .append ("}" );
9497 return declaration .toString ();
9598 }
9699
97- private void postEvent (StringBuilder declaration ) {
100+ private void fireEvents (StringBuilder declaration , String prefix ) {
98101 if ( annotationMetaEntity .getContext ().isDataEventPackageAvailable ()
99102 && annotationMetaEntity .getContext ().addDependentAnnotation ()
100103 && eventTypes .contains ( operationName )
101- && !isReactive () ) {
102- final String postEventType = "Post" + capitalize ( operationName ) + "Event" ;
103- annotationMetaEntity .importType ( "jakarta.data.event." + postEventType );
104+ && !isReactive ()) {
105+ final String entityName = iterateEvents ( declaration );
106+ fireEvent ( declaration , entityName , prefix + capitalize ( operationName ) + "Event" );
107+ endIterateEvents ( declaration );
108+ }
109+ }
110+
111+ private void fireEvent (StringBuilder declaration , String entityName , String eventType ) {
112+ annotationMetaEntity .importType ( "jakarta.data.event.LifecycleEvent" );
113+ annotationMetaEntity .importType ( "jakarta.enterprise.util.TypeLiteral" );
114+ annotationMetaEntity .importType ( "jakarta.enterprise.event.Event" );
115+ annotationMetaEntity .importType ( "jakarta.inject.Inject" );
116+ annotationMetaEntity .importType ( "jakarta.data.event." + eventType );
117+ if (parameterKind != ParameterKind .NORMAL ) {
118+ declaration .append ( "\t " );
119+ }
120+ declaration
121+ .append ( "\t if (event != null) {\n " );
122+ if (parameterKind != ParameterKind .NORMAL ) {
123+ declaration .append ( "\t " );
124+ }
125+ declaration
126+ .append ( "\t \t event.select(new TypeLiteral<" )
127+ .append ( eventType )
128+ .append ( "<" )
129+ .append ( annotationMetaEntity .importType ( actualEntity ) )
130+ .append ( ">>(){})\n \t \t \t \t .fire(new " )
131+ .append ( eventType )
132+ .append ( "<>(" )
133+ .append ( entityName )
134+ .append ( "));\n " );
135+ if (parameterKind != ParameterKind .NORMAL ) {
136+ declaration .append ( "\t " );
137+ }
138+ declaration
139+ .append ("\t }\n " );
140+ }
141+
142+ private void endIterateEvents (StringBuilder declaration ) {
143+ if (parameterKind != ParameterKind .NORMAL ) {
104144 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 " );
145+ .append ( "\t }\n " );
114146 }
115147 }
116148
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" );
149+ private String iterateEvents (StringBuilder declaration ) {
150+ if (parameterKind != ParameterKind .NORMAL ) {
128151 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 ( "<>(" )
152+ .append ( "\t for (var _entity : " )
136153 .append ( parameterName )
137- .append ( "));\n " );
154+ .append (") {\n " );
155+ return "_entity" ;
156+ }
157+ else {
158+ return parameterName ;
138159 }
139160 }
140161
0 commit comments