Table widget without database DataContainer

Hi,

I currently have two entities that inherit from the same class, say B and C inheriting from class A. I am currently using joined inheritance which means for of the classes A, B and C, I have the corresponding tables in the Database. B and C have different columns.

Here are the two questions I have:

  1. Displaying the information
    I would like to have one table to show both B and C. However the rendering of B and C is specific.

mockup

In the table above B.C1 means table B, attribute C1. C.C3 means table C, attribute C3.

How can I extend the Table to show rows containing B differently than rows of C?

  1. Edition
    My plan is to have two buttons “Add A” and “Add B”, when the user chooses to create a new A, I create the corresponding form at runtime with the required fields of A. The same approach for B.

Do you think this is a good approach?

Thanks all for your help.

Best,
Samy

Hi,

Since both entities have the same parent, JPQL query that loads parent entity - loads all children as well. But in this case, you have no access to the specific attributes of child entities. I can suggest implementing a LoadDelegate that loads children entities separately and add them to a single collection:

<data readOnly="true">
    <collection id="customersDc"
                class="com.company.demo.entity.Customer"
                view="_local">
        <loader id="customersDl"/>
    </collection>
</data>
@Install(to = "customersDl", target = Target.DATA_LOADER)
private List<Customer> customersDlLoadDelegate(LoadContext<Customer> loadContext) {
    List<Individual> individuals = dataManager.loadList(
            LoadContext.create(Individual.class)
                    .setQuery(LoadContext.createQuery("select e from demo_Individual e"))
    );

    List<Company> companies = dataManager.loadList(
            LoadContext.create(Company.class)
                    .setQuery(LoadContext.createQuery("select e from demo_Company e"))
    );

    return ListUtils.union(individuals, companies);
}

In order to show specific attributes, you need to create generated columns:

@Subscribe
public void onInit(InitEvent event) {
    customersDataGrid.addGeneratedColumn("age", new DataGrid.ColumnGenerator<Customer, Integer>() {
        @Override
        public Integer getValue(DataGrid.ColumnGeneratorEvent<Customer> event) {
            return (event.getItem() instanceof Individual) ? ((Individual) event.getItem()).getAge() : null;
        }

        @Override
        public Class<Integer> getType() {
            return Integer.class;
        }
    });

    customersDataGrid.addGeneratedColumn("address", new DataGrid.ColumnGenerator<Customer, String>() {
        @Override
        public String getValue(DataGrid.ColumnGeneratorEvent<Customer> event) {
            return (event.getItem() instanceof Company) ? ((Company) event.getItem()).getAddress() : null;
        }

        @Override
        public Class<String> getType() {
            return String.class;
        }
    });
}

In order to create a specific entity, you need to subscribe to ActionPerformedEvent of a corresponding CreateAction and provide a custom implementation, e.g.:

@Subscribe("customersDataGrid.createCompany")
public void onCustomersTableCreateCompany(Action.ActionPerformedEvent event) {
    openEditor(Company.class);
}

@Subscribe("customersDataGrid.createIndividual")
public void onCustomersTableCreateIndividual(Action.ActionPerformedEvent event) {
    openEditor(Individual.class);
}

@SuppressWarnings("unchecked")
protected <E extends Customer> void openEditor(Class<E> entityClass) {
    StandardEditor<E> editor = (StandardEditor<E>) screenBuilders.editor(entityClass, this)
            .newEntity()
            .build();
    editor.addAfterCloseListener(afterCloseEvent -> {
        if (WINDOW_COMMIT_AND_CLOSE_ACTION.equals(afterCloseEvent.getCloseAction())) {
            E committedEntity = editor.getEditedEntity();
            customersDc.getMutableItems().add(0, committedEntity);

            customersDataGrid.focus();
        }
    });
    editor.show();
}

55

inheritance-table.zip (86.5 KB)

Regards,
Gleb

Hi Gleb,

Awesome that solved my problem of showing different entity types in one table.

Thanks for the help.

Regards,
Samy