2.7.2. Contract Editor Controller

Open the ContractEdit screen controller and replace its content with the following code:

package com.company.bpmdemo.web.screens.contract;

import com.haulmont.bpm.entity.ProcAttachment;
import com.haulmont.bpm.gui.procactionsfragment.ProcActionsFragment;
import com.haulmont.cuba.gui.app.core.file.FileDownloadHelper;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.model.CollectionLoader;
import com.haulmont.cuba.gui.model.InstanceContainer;
import com.haulmont.cuba.gui.model.InstanceLoader;
import com.haulmont.cuba.gui.screen.*;
import com.company.bpmdemo.entity.Contract;

import javax.inject.Inject;

@UiController("bpmdemo_Contract.edit")
@UiDescriptor("contract-edit.xml")
@EditedEntityContainer("contractDc")
public class ContractEdit extends StandardEditor<Contract> {
    private static final String PROCESS_CODE = "contractApproval";

    @Inject
    private CollectionLoader<ProcAttachment> procAttachmentsDl;

    @Inject
    private InstanceContainer<Contract> contractDc;

    @Inject
    protected ProcActionsFragment procActionsFragment;

    @Inject
    private Table<ProcAttachment> attachmentsTable;

    @Inject
    private InstanceLoader<Contract> contractDl;

    @Subscribe
    private void onBeforeShow(BeforeShowEvent event) {
        contractDl.load();
        procAttachmentsDl.setParameter("entityId",contractDc.getItem().getId());
        procAttachmentsDl.load();
        procActionsFragment.initializer()
                .standard()
                .init(PROCESS_CODE, getEditedEntity());

        FileDownloadHelper.initGeneratedColumn(attachmentsTable, "file");
    }
}

Let’s examine the controller code in details.

The ProcessActionsFragment is the fragment that displays process actions available to the user at the moment. On initialization, the fragment 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 fragment displays the start process button. If the related process instance is found then the fragment searches for an uncompleted process task for the current user and displays buttons for process task actions. See ProcActionsFragment for details.

In the contract editor, the process actions fragment initialization is performed in the onBeforeShow() by the invocation of the init(PROCESS_CODE, getEditedEntity()). The PROCESS_CODE stores a process code (contractApproval – we saw this value during the model deployment, see Process Model Deployment). The second argument getEditedEntity() is the current edited contract.

The standard() initialization does the following:

  • initializes standard process actions predicates that will be invoked before the process is started and before process tasks are completed. The predicates commit the entity editor.

  • initializes standard listeners that will show notifications like "Process started" or "Task completed" and refresh the procActionsFragment after the process is started or process task is completed.