1.7.2. Contract Editor Controller
Go to the Controller tab and replace its content with the following code:
package com.company.demo.web.contract;
import com.company.demo.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.AbstractEditor;
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());
}
}
Press OK to save the changes.
Let’s examine the controller code in details.
The ProcActionsFrame
is the frame that displays process actions available at the moment. During the frame initialization, a related ProcInstance
object search is performed based on two parameters (process code and entity instance). If the ProcInstance
object is not found then a new instance is created and the frame displays a start process button. If the related process instance is found then an uncompleted process task for the current user is searched and the buttons for process task actions are displayed. 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 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