How to expand jBPM to realize "abnormal" requirements

If the requirements are "abnormal", we can try to extend jBPM, which is easy to be extended, such as event listener, custom activity, Java code activity, etc.

Suppose we need to customize the query of the task here, and use multiple attributes as the query criteria.

In order to implement this function in the current task service, we can extend the jbm4 command service to realize the command of custom query task:

public class CustomTaskQueryCommand implements Command<List<Task>> {

    /**
     * Task priority
     */
    private int taskPriority;

    /**
     * Task description
     */
    private String taskDescLike;

    /**
     * Task start time
     */
    private Date taskCreateFrom;

    /**
     * Task end time
     */
    private Date taskCreateTo;

    /**
     * Get filter parameters (conditions) with constructor
     *
     * @param taskPriority priority
     * @param taskDescLike describe
     * @param taskCreateFrom Start time of creation time condition
     * @param taskCreateTo End time of creation time condition
     */
    public CustomTaskQueryCommand(int taskPriority, String taskDescLike, Date taskCreateFrom, Date taskCreateTo) {
        this.taskPriority = taskPriority;
        this.taskDescLike = taskDescLike;
        this.taskCreateFrom = taskCreateFrom;
        this.taskCreateTo = taskCreateTo;
    }

    @Override
    public List<Task> execute(Environment environment) throws Exception {
        Session session = environment.get(Session.class);//Get hibernate session

        Criteria criteria = session.createCriteria(TaskImpl.class);
        //See jbpm.task.hbm.xml for task mapping attribute names

        if (taskPriority != 0) {
            criteria.add(Restrictions.eq("priority", taskPriority));
        }
        if (taskDescLike != null) {
            criteria.add(Restrictions.like("description", "%" + taskDescLike + "%"));
        }
        if (taskCreateFrom != null && taskCreateTo != null) {
            criteria.add(Restrictions.between("createTime", taskCreateFrom, taskCreateTo));
        }

        //Execution query
        List<?> list = criteria.list();
        return (List<Task>) list;
    }
}

CustomTaskQueryCommand implements the Command interface, and in the execute method, obtains the Hibernate Session object through the Environment object. Next, you can filter and query the data with multiple conditions O(∩∩) O haha~

Unit test:

public class CustomTaskQueryCommandTest extends JbpmTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        //Create tasks and set priorities and descriptions
        create("production", Priority.HIGH, "Production tasks");
        create("Packing", Priority.LOW, "Packaging task");
        create("transport", Priority.HIGHEST, "Transport task");
    }

    @Test
    public void test(){
        Calendar from=Calendar.getInstance();
        from.set(2018,1,1);
        Calendar to=Calendar.getInstance();
        to.set(2018, 5, 30);

        Command<List<Task>> command=new CustomTaskQueryCommand(Priority.HIGHEST,
                "transport",from.getTime(),to.getTime());

        List<Task> tasks= Configuration.getProcessEngine().execute(command);
        assertEquals(1, tasks.size());
        final Task task = tasks.get(0);
        assertEquals("transport", task.getName());
        assertEquals("Transport task",task.getDescription());
    }

    /**
     * Create tasks
     *
     * @param name     Task name
     * @param priority priority
     * @param desc     describe
     * @return
     */
    private void create(String name, int priority, String desc) {
        Task task = taskService.newTask();
        task.setName(name);
        task.setPriority(priority);
        task.setDescription(desc);
        taskService.saveTask(task);
    }
}

In unit test, we first create several test tasks in the setUp() method, then instantiate the custom command class in the test method, and execute the command from the ProcessEngine object obtained from Configuration.getProcessEngine().

Is it easy to extend jBPM~

Keywords: Session Hibernate Java xml

Added by Jennie on Fri, 20 Mar 2020 18:37:42 +0200