@@ -81,18 +81,19 @@ public Long migrateTransactionally(HistoricIncident c7Incident) {
8181 if (shouldMigrate (c7IncidentId , HISTORY_INCIDENT )) {
8282 HistoryMigratorLogs .migratingHistoricIncident (c7IncidentId );
8383 var c7ProcessInstance = findProcessInstanceByC7Id (c7Incident .getProcessInstanceId ());
84+ Long processInstanceKey = null ;
85+ Long flowNodeInstanceKey = null ;
8486
8587 var builder = new Builder ();
8688 var processDefinitionKey = findProcessDefinitionKey (c7Incident .getProcessDefinitionId ());
8789 builder .processDefinitionKey (processDefinitionKey );
8890 if (c7ProcessInstance != null ) {
89- var processInstanceKey = c7ProcessInstance .processInstanceKey ();
91+ processInstanceKey = c7ProcessInstance .processInstanceKey ();
9092 builder .processInstanceKey (processInstanceKey );
9193 if (processInstanceKey != null ) {
92- var flowNodeInstanceKey = findFlowNodeInstanceKey (c7Incident .getActivityId (), c7Incident .getProcessInstanceId ());
94+ flowNodeInstanceKey = findFlowNodeInstanceKey (c7Incident .getActivityId (), c7Incident .getProcessInstanceId ());
9395 builder .flowNodeInstanceKey (flowNodeInstanceKey );
94- // .jobKey(jobDefinitionKey) // TODO when jobs are migrated
95-
96+ // .jobKey(jobDefinitionKey) // TODO when jobs are migrated
9697
9798 String c7RootProcessInstanceId = c7Incident .getRootProcessInstanceId ();
9899 if (c7RootProcessInstanceId != null && isMigrated (c7RootProcessInstanceId , HISTORY_PROCESS_INSTANCE )) {
@@ -101,32 +102,34 @@ public Long migrateTransactionally(HistoricIncident c7Incident) {
101102 builder .rootProcessInstanceKey (rootProcessInstance .processInstanceKey ());
102103 }
103104 }
105+ builder .treePath (generateTreePath (processInstanceKey , flowNodeInstanceKey ));
104106 }
105107 }
106108
107109 IncidentDbModel dbModel = convert (C7Entity .of (c7Incident ), builder );
108110
109- if (dbModel .processDefinitionKey () == null ) {
110- throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_PROCESS_DEFINITION );
111- }
111+ if (dbModel .processDefinitionKey () == null ) {
112+ throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_PROCESS_DEFINITION );
113+ }
112114
113- if (dbModel .processInstanceKey () == null ) {
115+ if (dbModel .processInstanceKey () == null ) {
114116 throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_PROCESS_INSTANCE );
115- }
117+ }
116118
117- if (dbModel .rootProcessInstanceKey () == null ) {
118- throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_ROOT_PROCESS_INSTANCE );
119- }
119+ if (dbModel .rootProcessInstanceKey () == null ) {
120+ throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_ROOT_PROCESS_INSTANCE );
121+ }
120122
121- if (dbModel .flowNodeInstanceKey () == null ) {
122- if (!c7Client .hasWaitingExecution (c7Incident .getProcessInstanceId (), c7Incident .getActivityId ())) { // Activities on async before waiting state will not have a flow node instance key, but should not be skipped
123- throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_FLOW_NODE );
123+ if (dbModel .flowNodeInstanceKey () == null ) {
124+ if (!c7Client .hasWaitingExecution (c7Incident .getProcessInstanceId (), c7Incident .getActivityId ())) {
125+ // Activities on async before waiting state will not have a flow node instance key, but should not be skipped
126+ throw new EntitySkippedException (c7Incident , SKIP_REASON_MISSING_FLOW_NODE );
127+ }
124128 }
125- }
126129
127- if (dbModel .jobKey () == null ) {
128- // throw new EntitySkippedException(c7Incident, SKIP_REASON_MISSING_JOB_REFERENCE); // TODO when jobs are migrated
129- }
130+ if (dbModel .jobKey () == null ) {
131+ // throw new EntitySkippedException(c7Incident, SKIP_REASON_MISSING_JOB_REFERENCE); // TODO when jobs are migrated
132+ }
130133 c8Client .insertIncident (dbModel );
131134
132135 return dbModel .incidentKey ();
@@ -141,11 +144,9 @@ protected Long findFlowNodeInstanceKey(String activityId, String processInstance
141144 return null ;
142145 }
143146
144- List <FlowNodeInstanceDbModel > flowNodes = c8Client .searchFlowNodeInstances (
145- FlowNodeInstanceDbQuery .of (builder -> builder .filter (
146- FlowNodeInstanceFilter .of (filter -> filter .flowNodeIds (activityId ).processInstanceKeys (processInstanceKey ))
147- ))
148- );
147+ List <FlowNodeInstanceDbModel > flowNodes = c8Client .searchFlowNodeInstances (FlowNodeInstanceDbQuery .of (
148+ builder -> builder .filter (FlowNodeInstanceFilter .of (
149+ filter -> filter .flowNodeIds (activityId ).processInstanceKeys (processInstanceKey )))));
149150
150151 if (!flowNodes .isEmpty ()) {
151152 return flowNodes .getFirst ().flowNodeInstanceKey ();
@@ -154,5 +155,17 @@ protected Long findFlowNodeInstanceKey(String activityId, String processInstance
154155 }
155156 }
156157
158+ /**
159+ * Generates a tree path for incidents in the format: processInstanceKey/elementInstanceKey (if exists)
160+ *
161+ * @param processInstanceKey the process instance key
162+ * @param elementInstanceKey the flow node instance key
163+ * @return the tree path string
164+ */
165+ public static String generateTreePath (Long processInstanceKey , Long elementInstanceKey ) {
166+ return elementInstanceKey == null ?
167+ "PI_" + processInstanceKey .toString () :
168+ "PI_" + processInstanceKey + "/FNI_" + elementInstanceKey ;
169+ }
157170}
158171
0 commit comments