Executing code on every row of a table separately

Hey!

I am wondering about the best way to have my code calculate values for all rows at once. The problem I am facing is that i do not know where to start out and what functions to use for this.

Currently this code calculates the difference between todays date and an old date, giving back the amount of days it has been since this old date. Then it compares this date difference with an amount of dates the user has set. If it is more or equal, a boolean will be set to true. If its less the boolean will be false.

This current code only does this for one row, but I am wondering if i could do this for all rows at once by having it loop through the code for every row once or something in that direction.

The code for one row:

public void onCalcDate (Component source) {

        onderhoudsregelsDs.refresh();

        Onderhoudsregel selectedOnderhoudsregel = onderhoudsregelsDs.getItem();
        if (selectedOnderhoudsregel == null) {
            showNotification("Selecteer een onderhoudsregel!");
            return;
        }

        Date input = selectedOnderhoudsregel.getLaatstUitgevoerd();
        LocalDate oud = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate huidig = LocalDate.now();

        long tussen = Math.abs(oud.getDayOfYear() - huidig.getDayOfYear());
        long tussenDagen = TimeUnit.DAYS.convert(tussen, TimeUnit.DAYS);

        int tussenDagenConverted = (int) tussenDagen;

        selectedOnderhoudsregel.setDagenGeleden(tussenDagenConverted);

        onderhoudsregelsDs.refresh();

        if (tussenDagenConverted >= selectedOnderhoudsregel.getPeriodeInDagen()){
            selectedOnderhoudsregel.setActief(true);
        }
        if (tussenDagenConverted < selectedOnderhoudsregel.getPeriodeInDagen()){
            selectedOnderhoudsregel.setActief(false);
        }

        dataManager.commit(selectedOnderhoudsregel);
        onderhoudsregelsDs.refresh();

        showNotification("Regel afgerond!", NotificationType.TRAY);
    }

Hello, @timmothyquist

The metaproperties mechanism will allow you to solve the problem. The main idea is to create some read-only attribute, mark it with @MetaProperty annotation and write your business logic to field getter.

An example can be found here: GitHub.

Few related topics:

Regards,
Daniil

Thanks a lot! I will be trying this out right away