1414
1515import java .util .List ;
1616
17- import org .apache .commons .lang3 .StringUtils ;
18- import org .flowable .bpmn .constants .BpmnXMLConstants ;
1917import org .flowable .bpmn .model .BpmnModel ;
20- import org .flowable .bpmn .model .EventDefinition ;
21- import org .flowable .bpmn .model .EventRegistryEventDefinition ;
22- import org .flowable .bpmn .model .Message ;
23- import org .flowable .bpmn .model .MessageEventDefinition ;
24- import org .flowable .bpmn .model .SignalEventDefinition ;
18+ import org .flowable .bpmn .model .FlowElement ;
2519import org .flowable .bpmn .model .StartEvent ;
26- import org .flowable .bpmn .model .TimerEventDefinition ;
2720import org .flowable .common .engine .api .delegate .event .FlowableEngineEventType ;
2821import org .flowable .common .engine .api .delegate .event .FlowableEventDispatcher ;
29- import org .flowable .common .engine .api .scope .ScopeTypes ;
3022import org .flowable .common .engine .impl .interceptor .CommandContext ;
3123import org .flowable .common .engine .impl .util .CollectionUtil ;
3224import org .flowable .engine .ProcessEngineConfiguration ;
3325import org .flowable .engine .delegate .event .impl .FlowableEventBuilder ;
3426import org .flowable .engine .impl .ProcessDefinitionQueryImpl ;
27+ import org .flowable .engine .impl .bpmn .behavior .ProcessLevelStartEventActivityBehavior ;
28+ import org .flowable .engine .impl .bpmn .behavior .ProcessLevelStartEventDeployContext ;
3529import org .flowable .engine .impl .cfg .ProcessEngineConfigurationImpl ;
3630import org .flowable .engine .impl .context .Context ;
37- import org .flowable .engine .impl .event .EventDefinitionExpressionUtil ;
38- import org .flowable .engine .impl .jobexecutor .TimerEventHandler ;
3931import org .flowable .engine .impl .jobexecutor .TimerStartEventJobHandler ;
4032import org .flowable .engine .impl .persistence .entity .ProcessDefinitionEntity ;
4133import org .flowable .engine .impl .persistence .entity .ProcessDefinitionEntityManager ;
42- import org .flowable .engine .impl .util .CorrelationUtil ;
43- import org .flowable .engine .impl .util .CountingEntityUtil ;
4434import org .flowable .engine .impl .util .ProcessDefinitionUtil ;
45- import org .flowable .engine .impl .util .TimerUtil ;
4635import org .flowable .engine .repository .ProcessDefinition ;
47- import org .flowable .eventsubscription .api .EventSubscription ;
48- import org .flowable .eventsubscription .api .EventSubscriptionBuilder ;
49- import org .flowable .eventsubscription .service .EventSubscriptionService ;
50- import org .flowable .eventsubscription .service .impl .persistence .entity .MessageEventSubscriptionEntity ;
51- import org .flowable .eventsubscription .service .impl .persistence .entity .SignalEventSubscriptionEntity ;
5236import org .flowable .job .service .TimerJobService ;
5337import org .flowable .job .service .impl .persistence .entity .TimerJobEntity ;
5438
@@ -71,10 +55,8 @@ public void deleteDefinitionForDeployment(ProcessDefinition processDefinition, S
7155
7256 removeTimerStartJobs (processDefinition );
7357
74- // If previous process definition version has a timer/signal/message start event, it must be added
75- // Only if the currently deleted process definition is the latest version,
76- // we fall back to the previous timer/signal/message start event
77-
58+ // If the deleted process definition was the latest version, restore the previous version's
59+ // timer / message / signal / event-registry start events.
7860 restorePreviousStartEventsIfNeeded (processDefinition );
7961 }
8062
@@ -109,110 +91,31 @@ protected void removeTimerStartJobs(ProcessDefinition processDefinition) {
10991
11092 protected void restorePreviousStartEventsIfNeeded (ProcessDefinition processDefinition ) {
11193 ProcessDefinitionEntity latestProcessDefinition = findLatestProcessDefinition (processDefinition );
112- if (latestProcessDefinition != null && processDefinition .getId ().equals (latestProcessDefinition .getId ())) {
113-
114- // Try to find a previous version (it could be some versions are missing due to deletions)
115- ProcessDefinition previousProcessDefinition = findNewLatestProcessDefinitionAfterRemovalOf (processDefinition );
116- if (previousProcessDefinition != null ) {
117-
118- BpmnModel bpmnModel = ProcessDefinitionUtil .getBpmnModel (previousProcessDefinition .getId ());
119- org .flowable .bpmn .model .Process previousProcess = ProcessDefinitionUtil .getProcess (previousProcessDefinition .getId ());
120- if (CollectionUtil .isNotEmpty (previousProcess .getFlowElements ())) {
121-
122- List <StartEvent > startEvents = previousProcess .findFlowElementsOfType (StartEvent .class );
123-
124- if (CollectionUtil .isNotEmpty (startEvents )) {
125- for (StartEvent startEvent : startEvents ) {
126-
127- if (CollectionUtil .isNotEmpty (startEvent .getEventDefinitions ())) {
128- EventDefinition eventDefinition = startEvent .getEventDefinitions ().get (0 );
129- if (eventDefinition instanceof TimerEventDefinition ) {
130- restoreTimerStartEvent (previousProcessDefinition , startEvent , eventDefinition );
131- } else if (eventDefinition instanceof SignalEventDefinition ) {
132- restoreSignalStartEvent (previousProcessDefinition , bpmnModel , startEvent , eventDefinition );
133- } else if (eventDefinition instanceof MessageEventDefinition ) {
134- restoreMessageStartEvent (previousProcessDefinition , bpmnModel , startEvent , eventDefinition );
135- } else if (eventDefinition instanceof EventRegistryEventDefinition eventRegistryEventDefinition
136- && StringUtils .isNotEmpty (eventRegistryEventDefinition .getEventDefinitionKey ())) {
137- restoreEventRegistryStartEvent (previousProcessDefinition , bpmnModel , startEvent , eventRegistryEventDefinition .getEventDefinitionKey ());
138- }
139- }
140-
141- }
142- }
143-
144- }
145-
146- }
94+ if (latestProcessDefinition == null || !processDefinition .getId ().equals (latestProcessDefinition .getId ())) {
95+ return ;
14796 }
148- }
14997
150- protected void restoreTimerStartEvent (ProcessDefinition previousProcessDefinition , StartEvent startEvent , EventDefinition eventDefinition ) {
151- TimerEventDefinition timerEventDefinition = (TimerEventDefinition ) eventDefinition ;
152-
153- TimerJobEntity timerJob = TimerUtil .createTimerEntityForTimerEventDefinition (timerEventDefinition , startEvent ,
154- false , previousProcessDefinition , TimerStartEventJobHandler .TYPE , TimerEventHandler .createConfiguration (startEvent .getId (),
155- timerEventDefinition .getEndDate (), timerEventDefinition .getCalendarName ()));
156-
157- engineConfiguration .getJobServiceConfiguration ().getTimerJobService ().scheduleTimerJob (timerJob );
158- }
159-
160- protected void restoreSignalStartEvent (ProcessDefinition previousProcessDefinition , BpmnModel bpmnModel , StartEvent startEvent , EventDefinition eventDefinition ) {
161- CommandContext commandContext = Context .getCommandContext ();
162- SignalEventDefinition signalEventDefinition = (SignalEventDefinition ) eventDefinition ;
163- SignalEventSubscriptionEntity subscriptionEntity = engineConfiguration .getEventSubscriptionServiceConfiguration ().getEventSubscriptionService ().createSignalEventSubscription ();
164-
165- String eventName = EventDefinitionExpressionUtil .determineSignalName (commandContext , signalEventDefinition , bpmnModel , previousProcessDefinition );
166- subscriptionEntity .setEventName (eventName );
167- subscriptionEntity .setActivityId (startEvent .getId ());
168- subscriptionEntity .setProcessDefinitionId (previousProcessDefinition .getId ());
169- if (previousProcessDefinition .getTenantId () != null ) {
170- subscriptionEntity .setTenantId (previousProcessDefinition .getTenantId ());
171- }
172-
173- engineConfiguration .getEventSubscriptionServiceConfiguration ().getEventSubscriptionService ().insertEventSubscription (subscriptionEntity );
174- CountingEntityUtil .handleInsertEventSubscriptionEntityCount (subscriptionEntity );
175- }
176-
177- protected void restoreMessageStartEvent (ProcessDefinition previousProcessDefinition , BpmnModel bpmnModel , StartEvent startEvent , EventDefinition eventDefinition ) {
178- MessageEventDefinition messageEventDefinition = (MessageEventDefinition ) eventDefinition ;
179- if (bpmnModel .containsMessageId (messageEventDefinition .getMessageRef ())) {
180- Message message = bpmnModel .getMessage (messageEventDefinition .getMessageRef ());
181- messageEventDefinition .setMessageRef (message .getName ());
98+ // Try to find a previous version (it could be some versions are missing due to deletions)
99+ ProcessDefinition previousProcessDefinition = findNewLatestProcessDefinitionAfterRemovalOf (processDefinition );
100+ if (previousProcessDefinition == null ) {
101+ return ;
182102 }
183103
184- CommandContext commandContext = Context .getCommandContext ();
185- MessageEventSubscriptionEntity newSubscription = engineConfiguration .getEventSubscriptionServiceConfiguration ().getEventSubscriptionService ().createMessageEventSubscription ();
186- String messageName = EventDefinitionExpressionUtil .determineMessageName (commandContext , messageEventDefinition , previousProcessDefinition );
187- newSubscription .setEventName (messageName );
188- newSubscription .setActivityId (startEvent .getId ());
189- newSubscription .setConfiguration (previousProcessDefinition .getId ());
190- newSubscription .setProcessDefinitionId (previousProcessDefinition .getId ());
191-
192- if (previousProcessDefinition .getTenantId () != null ) {
193- newSubscription .setTenantId (previousProcessDefinition .getTenantId ());
104+ org .flowable .bpmn .model .Process previousProcess = ProcessDefinitionUtil .getProcess (previousProcessDefinition .getId ());
105+ if (CollectionUtil .isEmpty (previousProcess .getFlowElements ())) {
106+ return ;
194107 }
195108
196- engineConfiguration .getEventSubscriptionServiceConfiguration ().getEventSubscriptionService ().insertEventSubscription (newSubscription );
197- CountingEntityUtil .handleInsertEventSubscriptionEntityCount (newSubscription );
198- }
199-
200- protected void restoreEventRegistryStartEvent (ProcessDefinition previousProcessDefinition , BpmnModel bpmnModel , StartEvent startEvent , String eventDefinitionKey ) {
109+ BpmnModel bpmnModel = ProcessDefinitionUtil .getBpmnModel (previousProcessDefinition .getId ());
201110 CommandContext commandContext = Context .getCommandContext ();
202- EventSubscriptionService eventSubscriptionService = engineConfiguration .getEventSubscriptionServiceConfiguration ().getEventSubscriptionService ();
203- EventSubscriptionBuilder eventSubscriptionBuilder = eventSubscriptionService .createEventSubscriptionBuilder ()
204- .eventType (eventDefinitionKey )
205- .activityId (startEvent .getId ())
206- .processDefinitionId (previousProcessDefinition .getId ())
207- .scopeType (ScopeTypes .BPMN )
208- .configuration (CorrelationUtil .getCorrelationKey (BpmnXMLConstants .ELEMENT_EVENT_CORRELATION_PARAMETER , commandContext , startEvent , null ));
209-
210- if (previousProcessDefinition .getTenantId () != null ) {
211- eventSubscriptionBuilder .tenantId (previousProcessDefinition .getTenantId ());
111+ ProcessDefinitionEntity previousProcessDefinitionEntity = (ProcessDefinitionEntity ) previousProcessDefinition ;
112+ for (FlowElement flowElement : previousProcess .getFlowElements ()) {
113+ if (flowElement instanceof StartEvent startEvent
114+ && startEvent .getBehavior () instanceof ProcessLevelStartEventActivityBehavior behavior ) {
115+ behavior .deploy (new ProcessLevelStartEventDeployContext (previousProcessDefinitionEntity ,
116+ previousProcess , bpmnModel , startEvent , engineConfiguration , commandContext , true ));
117+ }
212118 }
213-
214- EventSubscription eventSubscription = eventSubscriptionBuilder .create ();
215- CountingEntityUtil .handleInsertEventSubscriptionEntityCount (eventSubscription );
216119 }
217120
218121 protected ProcessDefinitionEntity findLatestProcessDefinition (ProcessDefinition processDefinition ) {
0 commit comments