onBeforeShow Event

Hello, how I can make column generator generate value with onBeforeShow event? Here is what I have done but it doesn’t work when I add new “ElementVariable” to the table:

@Install(to = "elementVariablesTable.value", subject = "columnGenerator")
    private Component elementVariablesTableValueColumnGenerator(DataGrid.ColumnGeneratorEvent<ElementVariable> columnGeneratorEvent) {
        addBeforeShowListener(this::onBeforeShow);
        return generateVariableValueColumn(columnGeneratorEvent.getItem());
    }

private Component generateVariableValueColumn(ElementVariable elementVariable) {
        switch (elementVariable.getDecriminatorValue()) {
            case "DATE":
                DateField<LocalDateTime> date = uiComponents.create(DateField.NAME);

                EntityManager entityManager = entityManagerFactory.createEntityManager();
                TypedQuery<VariableDate> query = entityManager.createQuery(
                        "select d from VariableDate d where d.id =:id", VariableDate.class)
                        .setParameter("id", elementVariable.getId());
                VariableDate variableDate = query.getSingleResult();

                date.setValue(variableDate.getLocalDateTime());
                return date;
            case "BOOLEAN":
                CheckBox checkBox = uiComponents.create(CheckBox.NAME);

                EntityManager entityManager2 = entityManagerFactory.createEntityManager();
                TypedQuery<VariableBoolean> query2 = entityManager2.createQuery(
                        "select b from VariableBoolean b where b.id =:id", VariableBoolean.class)
                        .setParameter("id", elementVariable.getId());
                VariableBoolean variableBoolean = query2.getSingleResult();

                checkBox.setValue(variableBoolean.getVariableBoolean());
                return checkBox;
            case "STRING":
                Label<String> textField = uiComponents.create(Label.NAME);

                EntityManager entityManager3 = entityManagerFactory.createEntityManager();
                TypedQuery<VariableString> query3 = entityManager3.createQuery(
                        "select s from VariableString s where s.id =:id", VariableString.class)
                        .setParameter("id", elementVariable.getId());
                VariableString variableString = query3.getSingleResult();

                textField.setValue(variableString.getValue());
                return textField;
        }
        return null;
    }

Hi,

You can’t call addBeforeShowListener(this::onBeforeShow) in the column generator, because it’s a screen event. Column generator is called when a table needs to pain a table cell. If you describe your problem in more details, I’d try to suggest a suitable solution.

Gleb

JMIXPR

During creating new variable its saving to the database but my query and column generator runs before the variable is added to the database. I need this function to work after saving entity to database if you know what I mean because query don’t see the new entity.

Could you please attach a small demo project that demonstrates the issue? Unfortunately, it’s still unclear what you’re trying to achieve.

Regards,
Gleb

What I need is to create the variable in database directly after commiting Variable Editor instead of commiting “OK” in Element Editor.

I think I know what’s wrong. I need to merge newly created entity which is saved to dataContext with actual database table items and it will work. How I can do that?

Found similar problem on CUBA forum and that’s what I need(I suppose?):

if (dbnoteAct != null) {
                facts_provDc.getMutableItems().clear();
                for (FacturaProv facturaProv : dbnoteAct.getFacturesProveidor()) {
                    facts_provDc.getMutableItems().add(dataContext.merge(facturaProv));
                }
            } 

I’ll try to use it same way

Made a solution :wink: Works fine for me, have a look.

@Subscribe
    public void onBeforeShow(BeforeShowEvent event) {
        elementVariablesTable.addGeneratedColumn("value", entity -> {
            switch (entity.getDecriminatorValue()) {
                case "DATE":
                    DateField<LocalDateTime> date = uiComponents.create(DateField.NAME);

                    try {
                        VariableDate variableDate = dataManager.loadValue(
                                "select d from VariableDate d where d.id =:id", VariableDate.class)
                                .parameter("id", entity.getId())
                                .one();
                        date.setValue(variableDate.getLocalDateTime());
                    } catch (IllegalStateException e) {
                        VariableDate variableDate = dataContext.find(VariableDate.class, entity.getId());
                        date.setValue(variableDate.getLocalDateTime());
                    }

                    return date;
                case "BOOLEAN":
                    CheckBox checkBox = uiComponents.create(CheckBox.NAME);

                    try {
                        VariableBoolean variableBoolean = dataManager.loadValue(
                                "select b from VariableBoolean b where b.id =:id", VariableBoolean.class)
                                .parameter("id", entity.getId())
                                .one();
                        checkBox.setValue(variableBoolean.getVariableBoolean());
                    } catch (IllegalStateException e) {
                        VariableBoolean variableBoolean = dataContext.find(VariableBoolean.class, entity.getId());
                        checkBox.setValue(variableBoolean.getVariableBoolean());
                    }

                    return checkBox;
                case "STRING":
                    Label<String> textField = uiComponents.create(Label.NAME);

                    try {
                        VariableString variableString = dataManager.loadValue(
                                "select s from VariableString s where s.id =:id", VariableString.class)
                                .parameter("id", entity.getId())
                                .one();

                        textField.setValue(variableString.getValue());
                    } catch (IllegalStateException e) {
                        VariableString variableString = dataContext.find(VariableString.class, entity.getId());
                        textField.setValue(variableString.getValue());
                    }
                    return textField;
            }
            return null;
        });

        elementVariablesTable.getColumn("name").setCaption("Name");
        elementVariablesTable.getColumn("value").setCaption("Value");
    }