Skip to content

Commit 74fdb34

Browse files
add support for return only ids in process and case instance query
1 parent c72f396 commit 74fdb34

16 files changed

Lines changed: 357 additions & 4 deletions

File tree

‎modules/flowable-cmmn-api/src/main/java/org/flowable/cmmn/api/runtime/CaseInstanceQuery.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ public interface CaseInstanceQuery extends Query<CaseInstanceQuery, CaseInstance
269269
*/
270270
CaseInstanceQuery withLocalizationFallback();
271271

272+
/**
273+
* Return only the id value of the case instances, to reduce any additional instance data to be returned.
274+
*/
275+
CaseInstanceQuery returnIdsOnly();
276+
272277
CaseInstanceQuery orderByCaseInstanceId();
273278
CaseInstanceQuery orderByCaseDefinitionKey();
274279
CaseInstanceQuery orderByCaseDefinitionId();

‎modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/persistence/entity/CaseInstanceEntityManager.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public interface CaseInstanceEntityManager extends EntityManager<CaseInstanceEnt
3737

3838
List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQuery query);
3939

40+
List<CaseInstance> findIdsByCriteria(CaseInstanceQuery query);
41+
4042
long countByCriteria(CaseInstanceQuery query);
4143

4244
void delete(String caseInstanceId, boolean cascade, String deleteReason);

‎modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/persistence/entity/CaseInstanceEntityManagerImpl.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQuery query) {
9191
return dataManager.findWithVariablesByCriteria((CaseInstanceQueryImpl) query);
9292
}
9393

94+
@Override
95+
public List<CaseInstance> findIdsByCriteria(CaseInstanceQuery query) {
96+
return dataManager.findIdsByCriteria((CaseInstanceQueryImpl) query);
97+
}
98+
9499
@Override
95100
public long countByCriteria(CaseInstanceQuery query) {
96101
return dataManager.countByCriteria((CaseInstanceQueryImpl) query);

‎modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/persistence/entity/data/CaseInstanceDataManager.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public interface CaseInstanceDataManager extends DataManager<CaseInstanceEntity>
3838

3939
List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQueryImpl query);
4040

41+
List<CaseInstance> findIdsByCriteria(CaseInstanceQueryImpl query);
42+
4143
long countByCriteria(CaseInstanceQueryImpl query);
4244

4345
void updateLockTime(String caseInstanceId, Date lockDate, String lockOwner, Date expirationTime);

‎modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/persistence/entity/data/impl/MybatisCaseInstanceDataManagerImpl.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ public List<CaseInstance> findWithVariablesByCriteria(CaseInstanceQueryImpl quer
179179
return getDbSqlSession().selectListNoCacheLoadAndStore("selectCaseInstanceWithVariablesByQueryCriteria", query, getManagedEntityClass());
180180
}
181181

182+
@SuppressWarnings("unchecked")
183+
@Override
184+
public List<CaseInstance> findIdsByCriteria(CaseInstanceQueryImpl query) {
185+
setSafeInValueLists(query);
186+
return getDbSqlSession().selectListNoCacheLoadAndStore("selectCaseInstanceIdsByQueryCriteria", query, getManagedEntityClass());
187+
}
188+
182189
@Override
183190
public long countByCriteria(CaseInstanceQueryImpl query) {
184191
setSafeInValueLists(query);

‎modules/flowable-cmmn-engine/src/main/java/org/flowable/cmmn/engine/impl/runtime/CaseInstanceQueryImpl.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public class CaseInstanceQueryImpl extends AbstractVariableQueryImpl<CaseInstanc
113113

114114
protected String locale;
115115
protected boolean withLocalizationFallback;
116+
protected boolean returnIdsOnly;
116117

117118
public CaseInstanceQueryImpl() {
118119
}
@@ -1084,6 +1085,12 @@ public CaseInstanceQuery withLocalizationFallback() {
10841085
return this;
10851086
}
10861087

1088+
@Override
1089+
public CaseInstanceQuery returnIdsOnly() {
1090+
this.returnIdsOnly = true;
1091+
return this;
1092+
}
1093+
10871094
// results ////////////////////////////////////////////////////
10881095

10891096
@Override
@@ -1096,13 +1103,16 @@ public long executeCount(CommandContext commandContext) {
10961103
public List<CaseInstance> executeList(CommandContext commandContext) {
10971104
ensureVariablesInitialized();
10981105
List<CaseInstance> caseInstances = null;
1099-
if (this.isIncludeCaseVariables()) {
1106+
if (returnIdsOnly) {
1107+
caseInstances = cmmnEngineConfiguration.getCaseInstanceEntityManager().findIdsByCriteria(this);
1108+
} else if (this.isIncludeCaseVariables()) {
11001109
caseInstances = cmmnEngineConfiguration.getCaseInstanceEntityManager().findWithVariablesByCriteria(this);
11011110
} else {
11021111
caseInstances = cmmnEngineConfiguration.getCaseInstanceEntityManager().findByCriteria(this);
11031112
}
11041113

1105-
if (cmmnEngineConfiguration.getCaseLocalizationManager() != null) {
1114+
// Id only results have no name to localize
1115+
if (!returnIdsOnly && cmmnEngineConfiguration.getCaseLocalizationManager() != null) {
11061116
for (CaseInstance caseInstance : caseInstances) {
11071117
cmmnEngineConfiguration.getCaseLocalizationManager().localize(caseInstance, locale, withLocalizationFallback);
11081118
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@
213213
<result property="caseDefinitionDeploymentId" column="CaseDefinitionDeploymentId" jdbcType="VARCHAR" />
214214
</resultMap>
215215

216+
<resultMap id="caseInstanceIdResultMap" type="org.flowable.cmmn.engine.impl.persistence.entity.CaseInstanceEntityImpl">
217+
<id property="id" column="ID_" jdbcType="VARCHAR" />
218+
</resultMap>
219+
216220
<select id="selectCaseInstance" parameterType="string" resultMap="caseInstanceResultMap">
217221
select RES.*,
218222
CASE_DEF.KEY_ as CaseDefinitionKey,
@@ -323,6 +327,18 @@
323327
<if test="needsPaging">${limitAfter}</if>
324328
</select>
325329

330+
<!-- same as selectCaseInstancesByQueryCriteria, but only selecting the id -->
331+
<select id="selectCaseInstanceIdsByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.runtime.CaseInstanceQueryImpl" resultMap="caseInstanceIdResultMap">
332+
<if test="needsPaging">${limitBefore}</if>
333+
select RES.ID_ <if test="needsPaging">${limitBetween}</if>
334+
from ${prefix}ACT_CMMN_RU_CASE_INST RES
335+
<!-- Doing an inner join on the definition table is OK. It is an N:1 relationship -->
336+
inner join ${prefix}ACT_CMMN_CASEDEF CASE_DEF on RES.CASE_DEF_ID_ = CASE_DEF.ID_
337+
<include refid="commonSelectCaseInstancesByQueryCriteriaSql" />
338+
${orderBy}
339+
<if test="needsPaging">${limitAfter}</if>
340+
</select>
341+
326342
<select id="selectCaseInstanceCountByQueryCriteria" parameterType="org.flowable.cmmn.engine.impl.runtime.CaseInstanceQueryImpl" resultType="long">
327343
select count(distinct RES.ID_) from ${prefix}ACT_CMMN_RU_CASE_INST RES
328344
<!-- Doing an inner join on the definition table is OK. It is an N:1 relationship -->

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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.time.LocalDate;
2121
import java.time.LocalDateTime;
2222
import java.time.temporal.ChronoUnit;
23+
import java.util.ArrayList;
2324
import java.util.Calendar;
2425
import java.util.Collections;
2526
import java.util.GregorianCalendar;
@@ -2273,4 +2274,130 @@ public void testQueryByParentScopeIds() {
22732274
.extracting(CaseInstance::getId)
22742275
.containsExactlyInAnyOrder(oneTaskCase2PlanItemInstance.getReferenceId());
22752276
}
2277+
2278+
@Test
2279+
public void getCaseInstanceIdsOnly() {
2280+
CaseInstance caseInstance1 = cmmnRuntimeService.createCaseInstanceBuilder()
2281+
.caseDefinitionKey("oneTaskCase")
2282+
.businessKey("returnIdsOnly1")
2283+
.start();
2284+
CaseInstance caseInstance2 = cmmnRuntimeService.createCaseInstanceBuilder()
2285+
.caseDefinitionKey("oneTaskCase")
2286+
.businessKey("returnIdsOnly2")
2287+
.start();
2288+
2289+
List<CaseInstance> caseInstances = cmmnRuntimeService.createCaseInstanceQuery().returnIdsOnly().list();
2290+
assertThat(caseInstances)
2291+
.extracting(CaseInstance::getId)
2292+
.contains(caseInstance1.getId(), caseInstance2.getId())
2293+
.containsExactlyInAnyOrderElementsOf(cmmnRuntimeService.createCaseInstanceQuery().list().stream().map(CaseInstance::getId).toList());
2294+
assertThat(caseInstances)
2295+
.allSatisfy(caseInstance -> {
2296+
assertThat(caseInstance.getId()).isNotNull();
2297+
assertThat(caseInstance.getCaseDefinitionId()).isNull();
2298+
assertThat(caseInstance.getCaseDefinitionKey()).isNull();
2299+
assertThat(caseInstance.getBusinessKey()).isNull();
2300+
assertThat(caseInstance.getStartTime()).isNull();
2301+
});
2302+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().returnIdsOnly().count()).isEqualTo(caseInstances.size());
2303+
2304+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().caseDefinitionKey("oneTaskCase").returnIdsOnly().list())
2305+
.extracting(CaseInstance::getId, CaseInstance::getCaseDefinitionId)
2306+
.containsExactlyInAnyOrder(
2307+
tuple(caseInstance1.getId(), null),
2308+
tuple(caseInstance2.getId(), null)
2309+
);
2310+
2311+
CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceQuery().caseInstanceBusinessKey("returnIdsOnly2").returnIdsOnly().singleResult();
2312+
assertThat(caseInstance.getId()).isEqualTo(caseInstance2.getId());
2313+
assertThat(caseInstance.getCaseDefinitionId()).isNull();
2314+
2315+
caseInstance = cmmnRuntimeService.createCaseInstanceQuery().caseInstanceBusinessKey("returnIdsOnly2").singleResult();
2316+
assertThat(caseInstance.getId()).isEqualTo(caseInstance2.getId());
2317+
assertThat(caseInstance.getCaseDefinitionId()).isNotNull();
2318+
assertThat(caseInstance.getStartTime()).isNotNull();
2319+
2320+
caseInstance = cmmnRuntimeService.createCaseInstanceQuery()
2321+
.or()
2322+
.caseInstanceBusinessKey("returnIdsOnly1")
2323+
.caseInstanceId("undefined")
2324+
.endOr()
2325+
.returnIdsOnly()
2326+
.singleResult();
2327+
assertThat(caseInstance.getId()).isEqualTo(caseInstance1.getId());
2328+
assertThat(caseInstance.getCaseDefinitionId()).isNull();
2329+
2330+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().caseDefinitionKey("invalid").returnIdsOnly().singleResult()).isNull();
2331+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().caseDefinitionKey("invalid").returnIdsOnly().list()).isEmpty();
2332+
}
2333+
2334+
@Test
2335+
public void getCaseInstanceIdsOnlyByVariableValue() {
2336+
CaseInstance caseInstance1 = cmmnRuntimeService.createCaseInstanceBuilder()
2337+
.caseDefinitionKey("oneTaskCase")
2338+
.variable("returnIdsOnlyVar", "match")
2339+
.start();
2340+
cmmnRuntimeService.createCaseInstanceBuilder()
2341+
.caseDefinitionKey("oneTaskCase")
2342+
.variable("returnIdsOnlyVar", "noMatch")
2343+
.start();
2344+
CaseInstance caseInstance3 = cmmnRuntimeService.createCaseInstanceBuilder()
2345+
.caseDefinitionKey("oneTaskCase")
2346+
.variable("returnIdsOnlyVar", "match")
2347+
.start();
2348+
2349+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().variableValueEquals("returnIdsOnlyVar", "match").returnIdsOnly().list())
2350+
.extracting(CaseInstance::getId, CaseInstance::getCaseDefinitionId)
2351+
.containsExactlyInAnyOrder(
2352+
tuple(caseInstance1.getId(), null),
2353+
tuple(caseInstance3.getId(), null)
2354+
);
2355+
2356+
// includeCaseVariables is ignored when only returning ids
2357+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().variableValueEquals("returnIdsOnlyVar", "match").includeCaseVariables()
2358+
.returnIdsOnly().list())
2359+
.extracting(CaseInstance::getId, CaseInstance::getCaseVariables)
2360+
.containsExactlyInAnyOrder(
2361+
tuple(caseInstance1.getId(), Collections.emptyMap()),
2362+
tuple(caseInstance3.getId(), Collections.emptyMap())
2363+
);
2364+
}
2365+
2366+
@Test
2367+
public void getCaseInstanceIdsOnlyOrderedAndPaged() {
2368+
for (int i = 0; i < 4; i++) {
2369+
cmmnRuntimeService.createCaseInstanceBuilder()
2370+
.caseDefinitionKey("oneTaskCase")
2371+
.start();
2372+
}
2373+
2374+
List<String> expectedAscIds = cmmnRuntimeService.createCaseInstanceQuery().orderByCaseInstanceId().asc().list().stream()
2375+
.map(CaseInstance::getId)
2376+
.toList();
2377+
assertThat(expectedAscIds).hasSizeGreaterThanOrEqualTo(5);
2378+
2379+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().orderByCaseInstanceId().asc().returnIdsOnly().list())
2380+
.extracting(CaseInstance::getId)
2381+
.containsExactlyElementsOf(expectedAscIds);
2382+
2383+
List<String> pagedIds = new ArrayList<>();
2384+
for (int firstResult = 0; firstResult < expectedAscIds.size(); firstResult += 2) {
2385+
List<CaseInstance> page = cmmnRuntimeService.createCaseInstanceQuery().orderByCaseInstanceId().asc().returnIdsOnly().listPage(firstResult, 2);
2386+
assertThat(page).hasSizeLessThanOrEqualTo(2);
2387+
assertThat(page).extracting(CaseInstance::getCaseDefinitionId).containsOnlyNulls();
2388+
page.forEach(caseInstance -> pagedIds.add(caseInstance.getId()));
2389+
}
2390+
assertThat(pagedIds).containsExactlyElementsOf(expectedAscIds);
2391+
2392+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().orderByCaseInstanceId().desc().returnIdsOnly().listPage(1, 3))
2393+
.extracting(CaseInstance::getId)
2394+
.containsExactlyElementsOf(cmmnRuntimeService.createCaseInstanceQuery().orderByCaseInstanceId().desc().listPage(1, 3).stream()
2395+
.map(CaseInstance::getId).toList());
2396+
2397+
// Ordering by a column of the joined case definition
2398+
assertThat(cmmnRuntimeService.createCaseInstanceQuery().orderByCaseDefinitionKey().asc().orderByCaseInstanceId().asc().returnIdsOnly().listPage(1, 3))
2399+
.extracting(CaseInstance::getId)
2400+
.containsExactlyElementsOf(cmmnRuntimeService.createCaseInstanceQuery().orderByCaseDefinitionKey().asc().orderByCaseInstanceId().asc()
2401+
.listPage(1, 3).stream().map(CaseInstance::getId).toList());
2402+
}
22762403
}

‎modules/flowable-engine/src/main/java/org/flowable/engine/impl/ProcessInstanceQueryImpl.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public class ProcessInstanceQueryImpl extends AbstractVariableQueryImpl<ProcessI
108108
protected String referenceType;
109109
protected String locale;
110110
protected boolean withLocalizationFallback;
111+
protected boolean returnIdsOnly;
111112

112113
protected String tenantId;
113114
protected String tenantIdLike;
@@ -1000,6 +1001,12 @@ public ProcessInstanceQuery withLocalizationFallback() {
10001001
return this;
10011002
}
10021003

1004+
@Override
1005+
public ProcessInstanceQuery returnIdsOnly() {
1006+
this.returnIdsOnly = true;
1007+
return this;
1008+
}
1009+
10031010
@Override
10041011
public ProcessInstanceQuery startedBefore(Date beforeTime) {
10051012
if (inOrStatement) {
@@ -1081,13 +1088,17 @@ public List<ProcessInstance> executeList(CommandContext commandContext) {
10811088
processEngineConfiguration.getProcessInstanceQueryInterceptor().beforeProcessInstanceQueryExecute(this);
10821089
}
10831090

1084-
if (includeProcessVariables) {
1091+
if (returnIdsOnly) {
1092+
processInstances = processEngineConfiguration.getExecutionEntityManager().findProcessInstanceIdsByQueryCriteria(this);
1093+
} else if (includeProcessVariables) {
10851094
processInstances = processEngineConfiguration.getExecutionEntityManager().findProcessInstanceAndVariablesByQueryCriteria(this);
10861095
} else {
10871096
processInstances = processEngineConfiguration.getExecutionEntityManager().findProcessInstanceByQueryCriteria(this);
10881097
}
10891098

1090-
if (processEngineConfiguration.getPerformanceSettings().isEnableLocalization() && processEngineConfiguration.getInternalProcessLocalizationManager() != null) {
1099+
// Id only results have no name or description to localize
1100+
if (!returnIdsOnly && processEngineConfiguration.getPerformanceSettings().isEnableLocalization()
1101+
&& processEngineConfiguration.getInternalProcessLocalizationManager() != null) {
10911102
for (ProcessInstance processInstance : processInstances) {
10921103
processEngineConfiguration.getInternalProcessLocalizationManager().localize(processInstance, locale, withLocalizationFallback);
10931104
}

‎modules/flowable-engine/src/main/java/org/flowable/engine/impl/persistence/entity/ExecutionEntityManager.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ ExecutionEntity createSubprocessInstance(ProcessDefinition processDefinition, Ex
6262

6363
List<ProcessInstance> findProcessInstanceAndVariablesByQueryCriteria(ProcessInstanceQueryImpl executionQuery);
6464

65+
List<ProcessInstance> findProcessInstanceIdsByQueryCriteria(ProcessInstanceQueryImpl executionQuery);
66+
6567
Collection<ExecutionEntity> findInactiveExecutionsByProcessInstanceId(String processInstanceId);
6668

6769
Collection<ExecutionEntity> findInactiveExecutionsByActivityIdAndProcessInstanceId(String activityId, String processInstanceId);

0 commit comments

Comments
 (0)