AnsweredAssumed Answered

TaskQuery#taskVariableValueLike

Question asked by tomo1 on Feb 1, 2012
Latest reply on Feb 6, 2012 by tomo1
I am an engineer living in Japan.
I am using Activiti for our application.

My application needs to create query using 'like' operation for task variables and process variables to find tasks.

Are there any solution to make 'like' query for using Activiti API?

I looked into API documents but I could not find good solution.

I try to add new interfaces. These interfaces look like,


    TaskQuery TaskQuery#taskVariableValueLike(String variableName, String variableValue);
    TaskQuery TaskQuery#processVariableValueLike(String variableName, String variableValue);

And, I try to implement new interfaces.
Following are those code.

TaskQuery

package org.activiti.engine.task;

public interface TaskQuery extends Query<TaskQuery, Task>{
     :
    TaskQuery TaskQuery#taskVariableValueLike(String variableName, String variableValue);
    TaskQuery TaskQuery#processVariableValueLike(String variableName, String variableValue);
     :

TaskQueryImpl

package org.activiti.engine.impl;

public class TaskQueryImpl extends AbstractQuery<TaskQuery, Task> implements TaskQuery {

  :

  public TaskQuery taskVariableValueLike(String variableName, String variableValue) {
    variables.add(new TaskQueryVariableValue(variableName, variableValue, QueryOperator.LIKE, true));
    return this;
  }
 
  public TaskQuery processVariableValueLike(String variableName, String variableValue) {
    variables.add(new TaskQueryVariableValue(variableName, variableValue, QueryOperator.LIKE, false));
    return this;
  }
  :

activiti-engine/src/main/resources/org/activiti/db/mapping/entity/Task.xml


   :
      <foreach item="var" collection="variables" index="index">
        <if test="!var.taskVariable">
          <!– When process instance variable is queried for, taskId should be null –>
          and A${index}.TASK_ID_ is null
        </if>
        and A${index}.NAME_= #{var.name}
        <if test="!var.type.equals('null')">
          and A${index}.TYPE_ = #{var.type}
        </if>
        <!– Variable value –>
        <if test="var.textValue != null &amp;&amp; var.longValue == null &amp;&amp; var.doubleValue == null">
          <if test="var.operator.equals('EQUALS')">
            and A${index}.TEXT_ =  #{var.textValue}
          </if>
          <if test="var.operator.equals('LIKE')">
            and A${index}.TEXT_ like  #{var.textValue}
          </if>
        </if>
        <if test="var.textValue2 != null">
          <if test="var.operator.equals('EQUALS')">
            and A${index}.TEXT2_ = #{var.textValue2}
          </if>
          <if test="var.operator.equals('LIKE')">
            and A${index}.TEXT2_ like #{var.textValue2}
          </if>
        </if>
        <if test="var.longValue != null">
          and A${index}.LONG_ = #{var.longValue}
        </if>
        <if test="var.doubleValue != null">
          and A${index}.DOUBLE_ = #{var.doubleValue}
        </if>
        <!– Null variable type –>
        <if test="var.textValue == null &amp;&amp; var.textValue2 == null &amp;&amp; var.longValue == null &amp;&amp; var.doubleValue == null">
          and A${index}.TEXT_ is null and A${index}.TEXT2_ is null and A${index}.LONG_ is null and A${index}.DOUBLE_ is null and A${index}.BYTEARRAY_ID_ is null
        </if>
      </foreach>

    </where>
  </sql>

</mapper>

TaskQueryTest

package org.activiti.engine.test.api.task;
  :
public class TaskQueryTest extends PluggableActivitiTestCase {
  :
  @Deployment(resources={"org/activiti/engine/test/api/task/TaskQueryTest.testTaskVariableValueEquals.bpmn20.xml"})
  public void testTaskVariableValueLike() throws Exception {
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess");
    Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
   
    // No task should be found for an unexisting var
    assertEquals(0, taskService.createTaskQuery().taskVariableValueEquals("unexistingVar", "value").count());
   
    // Create a map with a variable for all default types
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("longVar", 928374L);
    variables.put("shortVar", (short) 123);
    variables.put("integerVar", 1234);
    variables.put("stringVar", "stringValue");
    variables.put("booleanVar", true);
    Date date = Calendar.getInstance().getTime();
    variables.put("dateVar", date);
    variables.put("nullVar", null);
   
    taskService.setVariablesLocal(task.getId(), variables);
   
    // Test query matches
    assertEquals(1, taskService.createTaskQuery().taskVariableValueLike("stringVar", "stringValue").count());
    assertEquals(1, taskService.createTaskQuery().taskVariableValueLike("stringVar", "s%").count());
    assertEquals(1, taskService.createTaskQuery().taskVariableValueLike("stringVar", "string%").count());
    assertEquals(1, taskService.createTaskQuery().taskVariableValueLike("stringVar", "%").count());
   
    // Test query for other values on existing variables
    assertEquals(0, taskService.createTaskQuery().taskVariableValueLike("stringVar", "999").count());
  }
 
  @Deployment(resources={"org/activiti/engine/test/api/task/TaskQueryTest.testProcessVariableValueEquals.bpmn20.xml"})
  public void testProcessVariableValueLike() throws Exception {
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("longVar", 928374L);
    variables.put("shortVar", (short) 123);
    variables.put("integerVar", 1234);
    variables.put("stringVar", "stringValue");
    variables.put("booleanVar", true);
    Date date = Calendar.getInstance().getTime();
    variables.put("dateVar", date);
    variables.put("nullVar", null);
   
    // Start process-instance with all types of variables
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("oneTaskProcess", variables);
   
    // Test query matches
    assertEquals(1, taskService.createTaskQuery().processVariableValueLike("stringVar", "stringValue").count());
    assertEquals(1, taskService.createTaskQuery().processVariableValueLike("stringVar", "s%").count());
    assertEquals(1, taskService.createTaskQuery().processVariableValueLike("stringVar", "str%").count());
    assertEquals(1, taskService.createTaskQuery().processVariableValueLike("stringVar", "string%").count());
   
    // Test query for other values on existing variables
    assertEquals(0, taskService.createTaskQuery().taskVariableValueLike("stringVar", "999").count());
   
    // Test querying for task variables don't match the process-variables
    assertEquals(0, taskService.createTaskQuery().taskVariableValueLike("stringVar", "stringValue").count());
   
  }

Outcomes