Skip to content

Commit 5a1a2ef

Browse files
authored
Improve HistoricPlanItemInstanceQuery (#4241)
1 parent 269f602 commit 5a1a2ef

4 files changed

Lines changed: 142 additions & 18 deletions

File tree

‎modules/flowable-cmmn-api/src/main/java/org/flowable/cmmn/api/history/HistoricPlanItemInstanceQuery.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public interface HistoricPlanItemInstanceQuery extends Query<HistoricPlanItemIns
9898

9999
/**
100100
* Begin an OR statement. Make sure you invoke the endOr method at the end of your OR statement.
101-
* Only one OR statement is allowed, for the second call to this method an exception will be thrown.
101+
* OR statements cannot be nested: calling or() again before endOr() throws an exception. Sequential
102+
* OR statements (or() ... endOr() ... or() ... endOr()) are allowed and are AND-ed together.
102103
*/
103104
HistoricPlanItemInstanceQuery or();
104105

‎modules/flowable-cmmn-engine/src/main/resources/org/flowable/cmmn/db/mapping/entity/HistoricPlanItemInstance.xml‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@
402402
RES.ID_ = #{planItemInstanceId, jdbcType=VARCHAR}
403403
</if>
404404
<if test="planItemInstanceName != null">
405-
RES.NAME_ = #{planItemInstanceName, jdbcType=NVARCHAR}
405+
and RES.NAME_ = #{planItemInstanceName, jdbcType=NVARCHAR}
406406
</if>
407407
<if test="state != null">
408408
and RES.STATE_ = #{state, jdbcType=VARCHAR}
@@ -441,7 +441,7 @@
441441
<if test="planItemDefinitionType != null">
442442
and RES.ITEM_DEFINITION_TYPE_ = #{planItemDefinitionType, jdbcType=VARCHAR}
443443
</if>
444-
<if test="planItemDefinitionTypes != null">
444+
<if test="planItemDefinitionTypes != null and !planItemDefinitionTypes.empty">
445445
and RES.ITEM_DEFINITION_TYPE_ in <foreach item="planItemDefinitionType" index="index" collection="planItemDefinitionTypes" open="(" separator="," close=")">#{planItemDefinitionType, jdbcType=VARCHAR}</foreach>
446446
</if>
447447
<if test="createdBefore != null">
@@ -623,7 +623,17 @@
623623
or RES.CASE_INST_ID_ = #{orQueryObject.caseInstanceId, jdbcType=VARCHAR}
624624
</if>
625625
<if test="orQueryObject.caseInstanceIds != null and !orQueryObject.caseInstanceIds.empty">
626-
or RES.CASE_INST_ID_ in <foreach item="caseInstanceId" index="index" collection="orQueryObject.caseInstanceIds" open="(" separator="," close=")">#{caseInstanceId, jdbcType=VARCHAR}</foreach>
626+
or (
627+
<foreach item="caseInstanceIdListItem" index="groupIndex" collection="orQueryObject.safeCaseInstanceIds">
628+
<if test="groupIndex &gt; 0">
629+
or
630+
</if>
631+
RES.CASE_INST_ID_ IN
632+
<foreach item="caseInstanceId" index="index" collection="caseInstanceIdListItem" open="(" separator="," close=")">
633+
#{caseInstanceId, jdbcType=VARCHAR}
634+
</foreach>
635+
</foreach>
636+
)
627637
</if>
628638
<if test="orQueryObject.stageInstanceId != null">
629639
or RES.STAGE_INST_ID_ = #{orQueryObject.stageInstanceId, jdbcType=VARCHAR}

‎modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/history/HistoricPlanItemInstanceQueryTest.java‎

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Date;
2222
import java.util.List;
2323
import java.util.Set;
24+
import java.util.function.UnaryOperator;
2425

2526
import org.flowable.cmmn.api.history.HistoricPlanItemInstance;
2627
import org.flowable.cmmn.api.history.HistoricPlanItemInstanceQuery;
@@ -203,6 +204,16 @@ public void testByAssignee() {
203204
.list();
204205
assertThat(planItemInstances).hasSize(0);
205206

207+
// or(): assignee "gonzo" OR bogus case definition id => the same 2 assigned human tasks
208+
planItemInstances = cmmnHistoryService.createHistoricPlanItemInstanceQuery()
209+
.or()
210+
.planItemInstanceAssignee("gonzo")
211+
.planItemInstanceCaseDefinitionId("undefinedId")
212+
.endOr()
213+
.list();
214+
assertThat(planItemInstances)
215+
.extracting(HistoricPlanItemInstance::getAssignee)
216+
.containsExactlyInAnyOrder("gonzo", "gonzo");
206217
}
207218

208219
@Test
@@ -233,6 +244,16 @@ public void testByCompletedBy() {
233244
.list();
234245
assertThat(planItemInstances).hasSize(0);
235246

247+
// or(): completedBy "kermit" OR bogus case definition id => the same 3 completed human tasks
248+
planItemInstances = cmmnHistoryService.createHistoricPlanItemInstanceQuery()
249+
.or()
250+
.planItemInstanceCompletedBy("kermit")
251+
.planItemInstanceCaseDefinitionId("undefinedId")
252+
.endOr()
253+
.list();
254+
assertThat(planItemInstances)
255+
.extracting(HistoricPlanItemInstance::getCompletedBy)
256+
.containsExactlyInAnyOrder("kermit", "kermit", "kermit");
236257
}
237258

238259
@Test
@@ -389,6 +410,28 @@ public void testByStarted() {
389410

390411
// started and notStarted partition the full result set
391412
assertThat(started.size() + notStarted.size()).isEqualTo(all.size());
413+
414+
// or(): started OR bogus case definition id => only A
415+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
416+
.planItemInstanceCaseInstanceId(caseInstance.getId())
417+
.or()
418+
.started()
419+
.planItemInstanceCaseDefinitionId("undefinedId")
420+
.endOr()
421+
.list())
422+
.extracting(HistoricPlanItemInstance::getName)
423+
.containsExactly("A");
424+
425+
// or(): notStarted OR bogus case definition id => only B
426+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
427+
.planItemInstanceCaseInstanceId(caseInstance.getId())
428+
.or()
429+
.notStarted()
430+
.planItemInstanceCaseDefinitionId("undefinedId")
431+
.endOr()
432+
.list())
433+
.extracting(HistoricPlanItemInstance::getName)
434+
.containsExactly("B");
392435
}
393436
}
394437

@@ -403,7 +446,9 @@ public void testOrQueryUnionWithBogusCondition() {
403446
.planItemInstanceCaseDefinitionId("undefinedId")
404447
.planItemInstanceCaseInstanceId(caseInstanceIds.get(0))
405448
.endOr()
406-
.list()).hasSize(4);
449+
.list())
450+
.extracting(HistoricPlanItemInstance::getName)
451+
.containsExactlyInAnyOrder("Stage one", "Stage two", "A", "B");
407452

408453
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
409454
.or()
@@ -419,13 +464,17 @@ public void testOrQueryUnionAcrossColumns() {
419464
List<String> caseInstanceIds = startInstances(2);
420465

421466
if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
422-
// (all 4 plan items of case 0) OR (the 2 stages of every case) => 4 (case 0) + 2 (stages of case 1) = 6, overlap deduped
467+
// (all 4 plan items of case 0) OR (the 2 stages of every case) => 4 (case 0) + 2 (stages of case 1) = 6.
468+
// A single WHERE with OR yields each row once by construction (no DISTINCT), so case 0's 2 stages
469+
// are not double-counted despite matching both legs.
423470
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
424471
.or()
425472
.planItemInstanceCaseInstanceId(caseInstanceIds.get(0))
426473
.planItemInstanceDefinitionType(PlanItemDefinitionType.STAGE)
427474
.endOr()
428-
.list()).hasSize(6);
475+
.list())
476+
.extracting(HistoricPlanItemInstance::getName)
477+
.containsExactlyInAnyOrder("Stage one", "Stage two", "A", "B", "Stage one", "Stage two");
429478
}
430479
}
431480

@@ -440,7 +489,9 @@ public void testOrQueryWithCaseInstanceIds() {
440489
.planItemInstanceCaseInstanceIds(Set.of(caseInstanceIds.get(0), caseInstanceIds.get(1)))
441490
.planItemInstanceCaseDefinitionId("undefinedId")
442491
.endOr()
443-
.list()).hasSize(8);
492+
.list())
493+
.extracting(HistoricPlanItemInstance::getName)
494+
.containsExactlyInAnyOrder("Stage one", "Stage two", "A", "B", "Stage one", "Stage two", "A", "B");
444495
}
445496
}
446497

@@ -449,25 +500,26 @@ public void testOuterAndWithOr() {
449500
startInstances(2);
450501

451502
if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
452-
// type == HUMAN_TASK AND (bogus OR real case definition id) => the 2 human tasks per case = 4
503+
// type == HUMAN_TASK AND (name "A" OR elementId "planItem3" i.e. "Stage one").
504+
// "Stage one" is a STAGE, so the outer AND must strip it, leaving only the 2 "A" human tasks.
505+
// This fails both if the OR block is deleted (would return only "A" from the name leg = still 2,
506+
// but the elementId leg proves the OR participates) and if OR is mutated to AND.
453507
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
454508
.planItemInstanceDefinitionType(PlanItemDefinitionType.HUMAN_TASK)
455509
.or()
456-
.planItemInstanceCaseInstanceId("undefinedId")
457-
.planItemInstanceCaseDefinitionId(caseDefinitionId)
510+
.planItemInstanceName("A")
511+
.planItemInstanceElementId("planItem3")
458512
.endOr()
459-
.list()).hasSize(4);
513+
.list())
514+
.extracting(HistoricPlanItemInstance::getName)
515+
.containsExactlyInAnyOrder("A", "A");
460516
}
461517
}
462518

463519
@Test
464520
public void testOrQueryCoversManyParameters() {
465521
List<String> caseInstanceIds = startInstances(2);
466522

467-
if (!CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
468-
return;
469-
}
470-
471523
String firstCaseInstanceId = caseInstanceIds.get(0);
472524

473525
// Resolve a couple of concrete ids from the first case instance to drive exact-match OR branches
@@ -519,15 +571,20 @@ public void testOrQueryUnionAcrossDistinctColumns() {
519571
.planItemInstanceName("A")
520572
.planItemInstanceElementId("planItem3")
521573
.endOr()
522-
.list()).hasSize(4);
574+
.list())
575+
.extracting(HistoricPlanItemInstance::getName)
576+
.containsExactlyInAnyOrder("A", "Stage one", "A", "Stage one");
523577
}
524578
}
525579

526580
/**
527581
* Runs {@code or(filter OR bogusCaseDefinitionId).endOr()} and asserts both list() and count() return expectedSize.
528582
* Pairing the real filter with a non-matching one isolates the real filter's OR branch.
529583
*/
530-
private void assertOr(int expectedSize, java.util.function.UnaryOperator<HistoricPlanItemInstanceQuery> filter) {
584+
private void assertOr(int expectedSize, UnaryOperator<HistoricPlanItemInstanceQuery> filter) {
585+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
586+
.planItemInstanceCaseDefinitionId("undefinedId").list()).isEmpty();
587+
531588
HistoricPlanItemInstanceQuery listQuery = cmmnHistoryService.createHistoricPlanItemInstanceQuery().or();
532589
filter.apply(listQuery).planItemInstanceCaseDefinitionId("undefinedId").endOr();
533590
assertThat(listQuery.list()).hasSize(expectedSize);
@@ -551,6 +608,48 @@ public void testEndOrThrowsWhenNotInOrStatement() {
551608
.hasMessage("endOr() can only be called after calling or()");
552609
}
553610

611+
@Test
612+
public void testByTenantId() {
613+
// Deploy the same case with a tenant and start a single instance in it. The @BeforeEach deployment has no tenant,
614+
// so the only tenant-scoped plan items are the 4 belonging to this case instance.
615+
addDeploymentForAutoCleanup(cmmnRepositoryService.createDeployment()
616+
.addClasspathResource("org/flowable/cmmn/test/history/HistoricPlanItemInstanceQueryTest.testQuery.cmmn")
617+
.tenantId("flowable")
618+
.deploy());
619+
cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testQuery").tenantId("flowable").start();
620+
621+
if (CmmnHistoryTestHelper.isHistoryLevelAtLeast(HistoryLevel.ACTIVITY, cmmnEngineConfiguration)) {
622+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
623+
.planItemInstanceTenantId("flowable").list()).hasSize(4);
624+
625+
// or(): exact tenant id OR bogus case definition id => the 4 tenant plan items
626+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
627+
.or()
628+
.planItemInstanceTenantId("flowable")
629+
.planItemInstanceCaseDefinitionId("undefinedId")
630+
.endOr()
631+
.list())
632+
.extracting(HistoricPlanItemInstance::getName)
633+
.containsExactlyInAnyOrder("Stage one", "Stage two", "A", "B");
634+
635+
// or(): tenant id like OR bogus case definition id => the same 4 tenant plan items
636+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
637+
.or()
638+
.planItemInstanceTenantIdLike("flow%")
639+
.planItemInstanceCaseDefinitionId("undefinedId")
640+
.endOr()
641+
.list())
642+
.extracting(HistoricPlanItemInstance::getName)
643+
.containsExactlyInAnyOrder("Stage one", "Stage two", "A", "B");
644+
645+
// a non-matching tenant id alone matches nothing, so the OR branches above are genuinely discriminating
646+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
647+
.planItemInstanceTenantId("nonexisting").list()).isEmpty();
648+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
649+
.planItemInstanceTenantIdLike("nonexisting%").list()).isEmpty();
650+
}
651+
}
652+
554653
private List<String> startInstances(int numberOfInstances) {
555654
List<String> caseInstanceIds = new ArrayList<>();
556655
for (int i = 0; i < numberOfInstances; i++) {

‎modules/flowable-cmmn-engine/src/test/java/org/flowable/cmmn/test/runtime/CasePageTaskTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ public void testIdentityLinks() {
217217
planItemInstance = cmmnRuntimeService.createPlanItemInstanceQuery().involvedUser("johndoe").involvedGroups(nonMatchingGroups).singleResult();
218218
assertThat(planItemInstance.getName()).isEqualTo("Case Page Task One");
219219

220+
HistoricPlanItemInstance historicByUser = cmmnHistoryService.createHistoricPlanItemInstanceQuery()
221+
.or().planItemInstanceCaseInstanceId("undefinedId").involvedUser("janedoe").endOr()
222+
.singleResult();
223+
assertThat(historicByUser.getName()).isEqualTo("Case Page Task One");
224+
225+
HistoricPlanItemInstance historicByGroup = cmmnHistoryService.createHistoricPlanItemInstanceQuery()
226+
.or().planItemInstanceCaseInstanceId("undefinedId").involvedGroups(groups).endOr()
227+
.singleResult();
228+
assertThat(historicByGroup.getName()).isEqualTo("Case Page Task One");
229+
230+
assertThat(cmmnHistoryService.createHistoricPlanItemInstanceQuery()
231+
.or().planItemInstanceCaseInstanceId("undefinedId").involvedUser("nonexisting").endOr()
232+
.list()).isEmpty();
233+
220234
List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
221235
.caseInstanceId(caseInstance.getId())
222236
.planItemInstanceState(PlanItemInstanceState.ACTIVE)

0 commit comments

Comments
 (0)