1.7.2. Contract Editor Controller

Go to the Controller tab and replace its content with the following code:

package com.company.bpmdemo.web.contract;

import com.haulmont.cuba.gui.components.AbstractEditor;
import com.company.bpmdemo.entity.Contract;
import com.haulmont.bpm.entity.ProcAttachment;
import com.haulmont.bpm.gui.procactions.ProcActionsFrame;
import com.haulmont.cuba.gui.app.core.file.FileDownloadHelper;
import com.haulmont.cuba.gui.components.Table;

import javax.inject.Inject;

public class ContractEdit extends AbstractEditor<Contract> {
    private static final String PROCESS_CODE = "contractApproval";

    @Inject
    private ProcActionsFrame procActionsFrame;

    @Inject
    private Table<ProcAttachment> attachmentsTable;

    @Override
    protected void postInit() {
        FileDownloadHelper.initGeneratedColumn(attachmentsTable, "file");
        initProcActionsFrame();
    }

    private void initProcActionsFrame() {
        procActionsFrame.initializer()
                .setBeforeStartProcessPredicate(this::commit)
                .setAfterStartProcessListener(() -> {
                    showNotification(getMessage("processStarted"), NotificationType.HUMANIZED);
                    close(COMMIT_ACTION_ID);
                })
                .setBeforeCompleteTaskPredicate(this::commit)
                .setAfterCompleteTaskListener(() -> {
                    showNotification(getMessage("taskCompleted"), NotificationType.HUMANIZED);
                    close(COMMIT_ACTION_ID);
                })
                .init(PROCESS_CODE, getItem());
    }
}

Click OK to save the changes.

Let’s examine the controller code in details.

The ProcActionsFrame is the frame that displays process actions available to the user at the moment. On initialization, the frame searches for a related ProcInstance object by two parameters: process code and entity instance. If the ProcInstance object is not found then a new instance is created and the frame displays the start process button. If the related process instance is found then the frame searches for an uncompleted process task for the current user and displays buttons for process task actions. See ProcActionsFrame for details.

In the contract editor, the process actions frame initialization is performed in the initProcActionsFrame() method. The main part of the method is the invocation of the init(PROCESS_CODE, getItem()). The PROCESS_CODE stores a process code (contractApproval - we saw this value during the model deployment, see Process Model Deployment). The second argument getItem() is the current contract.

We have also defined an additional business logic that is related to process actions:

  • setBeforeStartProcessPredicate adds the check that is performed before the process is started. An editor commit is performed and if the commit fails the process won’t start.

  • setAfterStartProcessListener shows the notification and closes the contract editor after the process start.

  • setBeforeCompleteTaskPredicate invokes the editor commit before the process action. If the commit fails the action won’t be performed.

  • setAfterCompleteTaskListener shows the notification and closes the contract editor after the process action is performed.