Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git issue 4578 taskid save for variables #4745

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.batch.Batch;
import org.camunda.bpm.engine.delegate.DelegateExecution;
Expand Down Expand Up @@ -65,6 +64,7 @@
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ExternalTaskEntity;
import org.camunda.bpm.engine.impl.persistence.entity.HistoricJobLogEventEntity;
import org.camunda.bpm.engine.impl.persistence.entity.HistoricVariableInstanceEntity;
import org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity;
import org.camunda.bpm.engine.impl.persistence.entity.JobEntity;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
Expand Down Expand Up @@ -422,6 +422,7 @@ protected void initHistoricIncidentEvent(HistoricIncidentEventEntity evt, Incide
protected HistoryEvent createHistoricVariableEvent(VariableInstanceEntity variableInstance, VariableScope sourceVariableScope, HistoryEventType eventType) {
String scopeActivityInstanceId = null;
String sourceActivityInstanceId = null;
String taskId = null;

if(variableInstance.getExecutionId() != null) {
ExecutionEntity scopeExecution = Context.getCommandContext()
Expand Down Expand Up @@ -449,6 +450,13 @@ else if (variableInstance.getCaseExecutionId() != null) {
} else if (sourceVariableScope instanceof TaskEntity) {
sourceExecution = ((TaskEntity) sourceVariableScope).getExecution();
if (sourceExecution != null) {

//this block when executed for task listener variables, gets task id from source execution
List<TaskEntity> taskEntityList = sourceExecution.getTasks();
if(taskEntityList!=null && !taskEntityList.isEmpty()){
taskId = taskEntityList.get(0).getId();
}

sourceActivityInstanceId = sourceExecution.getActivityInstanceId();
}
else {
Expand Down Expand Up @@ -476,6 +484,11 @@ else if (sourceVariableScope instanceof CaseExecutionEntity) {
// set source activity instance id
evt.setActivityInstanceId(sourceActivityInstanceId);

// set task id for task listener variables
if(taskId!=null && evt.getTaskId()==null) {
evt.setTaskId(taskId);
}

// mark initial variables on process start
if (sourceExecution != null && sourceExecution.isProcessInstanceStarting()
&& HistoryEventTypes.VARIABLE_INSTANCE_CREATE.equals(eventType)) {
Expand Down Expand Up @@ -727,6 +740,23 @@ public HistoryEvent createActivityInstanceUpdateEvt(DelegateExecution execution,
if(task != null) {
evt.setTaskId(task.getId());
evt.setTaskAssignee(task.getAssignee());

/*
* this code is required for input variables of the task as the historic variable instance is created first
* and then task entity is created
* get historic variable instances from the cache as the transaction is still not committed
*/
List <HistoricVariableInstanceEntity> cachedHistoricVariableInstances = Context.getCommandContext().getDbEntityManager().getCachedEntitiesByType(HistoricVariableInstanceEntity.class);
String executionActivityInstanceId = executionEntity.getActivityInstanceId();
for (HistoricVariableInstanceEntity historicVariableInstance : cachedHistoricVariableInstances) {
String historicActivityInstanceId = historicVariableInstance.getActivityInstanceId();
//update task id for historic variable instances only specific to that task
if(executionActivityInstanceId!=null && historicActivityInstanceId!=null) {
if (historicActivityInstanceId.equals(executionActivityInstanceId)) {
historicVariableInstance.setTaskId(task.getId());
}
}
}
}

return evt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void testSetVariableLocalOnUserTask() {

HistoricVariableInstance variable = query.singleResult();
assertNotNull(variable);
assertEquals(task.getId(),variable.getTaskId());

// the variable is in the task scope
assertEquals(taskExecution.getActivityInstanceId(), variable.getActivityInstanceId());
Expand Down Expand Up @@ -118,12 +120,14 @@ public void testSetVariableOnProcessIntanceStartAndSetVariableLocalOnUserTask()
assertEquals("testValue", firstVar.getValue());
// the variable is in the process instance scope
assertEquals(pi.getId(), firstVar.getActivityInstanceId());
assertNull(firstVar.getTaskId());

HistoricVariableInstance secondVar = result.get(1);
assertEquals("testVar", secondVar.getVariableName());
assertEquals("anotherTestValue", secondVar.getValue());
// the variable is in the task scope
assertEquals(taskExecution.getActivityInstanceId(), secondVar.getActivityInstanceId());
assertEquals(task.getId(),secondVar.getTaskId());

taskService.complete(task.getId());
testRule.assertProcessEnded(pi.getId());
Expand All @@ -145,6 +149,7 @@ public void testSetVariableOnUserTaskInsideSubProcess() {
HistoricVariableInstance variable = query.singleResult();
// the variable is in the process instance scope
assertEquals(pi.getId(), variable.getActivityInstanceId());
assertEquals(task.getId(),variable.getTaskId());

taskService.complete(task.getId());
testRule.assertProcessEnded(pi.getId());
Expand All @@ -161,6 +166,7 @@ public void testSetVariableOnServiceTaskInsideSubProcess() {
HistoricVariableInstance variable = query.singleResult();
// the variable is in the process instance scope
assertEquals(pi.getId(), variable.getActivityInstanceId());
assertNull(variable.getTaskId());

testRule.assertProcessEnded(pi.getId());
}
Expand Down Expand Up @@ -205,6 +211,7 @@ public void testSetVariableLocalOnTaskInsideParallelBranch() {
HistoricVariableInstance variable = query.singleResult();
// the variable is in the user task scope
assertEquals(taskExecution.getActivityInstanceId(), variable.getActivityInstanceId());
assertEquals(task.getId(),variable.getTaskId());

taskService.complete(task.getId());

Expand All @@ -227,6 +234,7 @@ public void testSetVariableOnTaskInsideParallelBranch() {
HistoricVariableInstance variable = query.singleResult();
// the variable is in the process instance scope
assertEquals(pi.getId(), variable.getActivityInstanceId());
assertEquals(task.getId(),variable.getTaskId());

taskService.complete(task.getId());

Expand All @@ -244,6 +252,7 @@ public void testSetVariableOnServiceTaskInsideParallelBranch() {
HistoricVariableInstance variable = query.singleResult();
// the variable is in the process instance scope
assertEquals(pi.getId(), variable.getActivityInstanceId());
assertNull(variable.getTaskId());

testRule.assertProcessEnded(pi.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,24 @@ public void testBinaryFetchingEnabled() {
taskService.deleteTask(newTask.getId(), true);
}

@Test
public void testTaskIdInHistoricVariableInstance() {

Task newTask = taskService.newTask();
taskService.saveTask(newTask);

String variableName = "varName";
taskService.setVariable(newTask.getId(), variableName, "varValue");

HistoricVariableInstance variableInstance = historyService.createHistoricVariableInstanceQuery()
.variableName(variableName)
.singleResult();

assertEquals(newTask.getId(),variableInstance.getTaskId());

taskService.deleteTask(newTask.getId(), true);
}

@Test
public void testBinaryFetchingDisabled() {

Expand Down Expand Up @@ -798,6 +816,7 @@ public void testDisableCustomObjectDeserializationNativeQuery() {

for (HistoricVariableInstance variableInstance : variableInstances) {
assertNull(variableInstance.getErrorMessage());
assertEquals(newTask.getId(),variableInstance.getTaskId());

ObjectValue typedValue = (ObjectValue) variableInstance.getTypedValue();
assertNotNull(typedValue);
Expand Down Expand Up @@ -829,6 +848,7 @@ public void testErrorMessage() {
.singleResult();

assertNull(variableInstance.getValue());
assertEquals(newTask.getId(),variableInstance.getTaskId());
assertNotNull(variableInstance.getErrorMessage());

taskService.deleteTask(newTask.getId(), true);
Expand Down Expand Up @@ -1315,6 +1335,7 @@ public void testTaskVariableUpdateOrder() {
.createHistoricVariableInstanceQuery()
.singleResult();
assertNotNull(variable);
assertEquals(taskId, variable.getTaskId());

String variableInstanceId = variable.getId();

Expand Down Expand Up @@ -1349,6 +1370,7 @@ public void testTaskVariableUpdateOrder() {
.createHistoricVariableInstanceQuery()
.singleResult();
assertNotNull(variable);
assertEquals(taskId, variable.getTaskId());

if (isFullHistoryEnabled()) {

Expand Down Expand Up @@ -1557,6 +1579,7 @@ public void testSetSameVariableUpdateOrder() {
.createHistoricVariableInstanceQuery()
.singleResult();
assertNotNull(variable);
assertEquals(taskId,variable.getTaskId());

String variableInstanceId = variable.getId();

Expand Down Expand Up @@ -1626,6 +1649,8 @@ public void testProcessDefinitionProperty() {
assertNotNull(instance.getProcessDefinitionKey());
assertEquals(key, instance.getProcessDefinitionKey());

assertEquals(taskId,instance.getTaskId());

assertNotNull(instance.getProcessDefinitionId());
assertEquals(processInstance.getProcessDefinitionId(), instance.getProcessDefinitionId());

Expand Down Expand Up @@ -1673,6 +1698,7 @@ public void testCaseDefinitionProperty() {

// then (2)
assertCaseVariable(key, caseInstance, instance);
assertEquals(taskId,instance.getTaskId());

// when (3)
instance = historyService
Expand Down Expand Up @@ -2312,6 +2338,7 @@ public void testSetDifferentStates() {
createdCounter += 1;
} else if (variable.getName().equals("bar")) {
Assert.assertEquals(HistoricVariableInstance.STATE_DELETED, variable.getState());
assertEquals(task.getId(),variable.getTaskId());
deletedCounter += 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ public void testQueryDeleteVariableHistoryOperationOnTaskOfRunningInstance() {
historyService.deleteHistoricVariableInstance(variableInstanceId);

// then
verifyHistoricVariableOperationAsserts(1, UserOperationLogEntry.OPERATION_TYPE_DELETE_HISTORY);
verifyHistoricVariableOperationAssertsWithTaskId(1, UserOperationLogEntry.OPERATION_TYPE_DELETE_HISTORY);
verifySingleVariableOperationPropertyChange("name", "testVariable", UserOperationLogEntry.OPERATION_TYPE_DELETE_HISTORY);
}

Expand All @@ -1427,7 +1427,7 @@ public void testQueryDeleteVariableHistoryOperationOnTaskOfHistoricInstance() {
historyService.deleteHistoricVariableInstance(variableInstanceId);

// then
verifyHistoricVariableOperationAsserts(1, UserOperationLogEntry.OPERATION_TYPE_DELETE_HISTORY);
verifyHistoricVariableOperationAssertsWithTaskId(1, UserOperationLogEntry.OPERATION_TYPE_DELETE_HISTORY);
verifySingleVariableOperationPropertyChange("name", "testVariable", UserOperationLogEntry.OPERATION_TYPE_DELETE_HISTORY);
}

Expand Down Expand Up @@ -1814,6 +1814,31 @@ private void verifyHistoricVariableOperationAsserts(int countAssertValue, String
}
}

private void verifyHistoricVariableOperationAssertsWithTaskId(int countAssertValue, String operationType) {
String deploymentId = repositoryService.createDeploymentQuery().singleResult().getId();
UserOperationLogQuery logQuery = query().entityType(EntityTypes.VARIABLE).operationType(operationType);
assertEquals(countAssertValue, logQuery.count());

if(countAssertValue > 1) {
List<UserOperationLogEntry> logEntryList = logQuery.list();

for (UserOperationLogEntry logEntry : logEntryList) {
assertEquals(process.getProcessDefinitionId(), logEntry.getProcessDefinitionId());
assertEquals(process.getProcessInstanceId(), logEntry.getProcessInstanceId());
assertEquals(deploymentId, logEntry.getDeploymentId());
assertNotNull(logEntry.getTaskId());
assertEquals(UserOperationLogEntry.CATEGORY_OPERATOR, logEntry.getCategory());
}
} else {
UserOperationLogEntry logEntry = logQuery.singleResult();
assertEquals(process.getProcessDefinitionId(), logEntry.getProcessDefinitionId());
assertEquals(process.getProcessInstanceId(), logEntry.getProcessInstanceId());
assertEquals(deploymentId, logEntry.getDeploymentId());
assertNotNull(logEntry.getTaskId());
assertEquals(UserOperationLogEntry.CATEGORY_OPERATOR, logEntry.getCategory());
}
}

private void verifySingleVariableOperationPropertyChange(String property, String newValue, String operationType) {
UserOperationLogQuery logQuery = query().entityType(EntityTypes.VARIABLE).operationType(operationType);
assertEquals(1, logQuery.count());
Expand Down
Loading