2.5.2.2. Browser Controller

Open the Controller tab and replace its contents with the following code:

package com.sample.library.gui.ebook;

import java.util.Map;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.components.actions.EditAction;

import javax.inject.Inject;

public class EBookBrowse extends AbstractLookup {

    @Inject
    protected Table eBookTable;

    @Override
    public void init(Map<String, Object> params) {
        eBookTable.addAction(new CreateAction(eBookTable) {
            @Override
            protected void afterCommit(Entity entity) {
                eBookTable.getDatasource().refresh();
            }
        });
        eBookTable.addAction(new EditAction(eBookTable) {
            @Override
            protected void afterCommit(Entity entity) {
                eBookTable.getDatasource().refresh();
            }
        });
    }
}

The standard CreateAction and EditAction are added to the eBookTable table in the init() method with an overridden afterCommit() method that is responsible for refreshing the table datasource. This allows displaying the altered state of the card in the table immediately when the changes are saved and the process moves on to the next stage.

The CreateAction and EditAction standard actions do not reload the datasource after the opened editor is committed; they receive the saved entity instance from the Middleware and simply substitute it in the datasource for the original.

In our case, it is necessary to reload the entity from the database because the process is launched in a separate transaction after saving the edited EBook instance, and the state of the card is changed at the same time. In other words, the EBook instance, which has not yet been changed by the process, is returned after the editor is committed to the datasource. It is necessary to reload the entity from the database in order to display these changes.