Create multiple entities with only one submit

Hello,
I am doing a sample application that has a table with a list of dates.
In the edit screen I can add a range of dates, so I have two datefields (start and end) and a windowcommitandclose button.
I need to know the “cuba way” to override the standard behaviour to do a for loop to insert all entities.
In the controller I have:

@Subscribe("commitAndCloseBtn")
    public void onCommitAndCloseBtnClick(Button.ClickEvent event) {
        for (LocalDate d=    bookingDateField.getValue();d.isBefore(bookingDateEndField.getValue().plusDays(1));d=d.plusDays(1))              {
        System.out.println(d);
    }
}

Obviously I need to replace println with jpa code to save entity. But it does not seem to me a good idea to put business logic in Controller and anyway I do not know what do inject.

Thanks for any hint,
Mario

Hi,

I would suggest installing a custom delegate for the commit() method in the screen’s DataContext and implement the saving algorithm there instead of handling button events.

Ok I have done this:

@Subscribe
public void onInit(InitEvent event) {
    dataContext.setCommitDelegate(commitContext -> {
        for (LocalDate d= bookingDateField.getValue().plusDays(1);d.isBefore(bookingDateEndField.getValue().plusDays(1));d=d.plusDays(1)) { Booking b = dataContext.create(Booking.class);
            b.setBookingDate(d);
            commitContext.addInstanceToCommit(b);
        }
        return dataManager.commit(commitContext);
    });
}

It works (I must skip first entity because it is already included) but then the groupTable is not refreshed: I see only first entity added.
Can I force the refresh?
Thanks,
Mario

Hi,

It looks OK, but it would be better to generate a commit delegate:

    @Install(target = Target.DATA_CONTEXT)
    private Set<Entity> commitDelegate(CommitContext commitContext) {
        //Insert your code here
    }

You can do it in Studio by clicking on “Generate Handler” in the tool panel of the screen controller code editor and select Data context handlers -> Commit delegate. It’ll make your code look cleaner.

To refresh the data, just inject the data loader and invoke its load() method.

Hi @belyaev
I created a commitDelegate with @Install(target = Target.DATA_CONTEXT). It never runs into my method when clicking OK.
Do I have to change my commitAndCloseBtn?

 <button id="commitAndCloseBtn" action="windowCommitAndClose"/>

Thanks! Reini

Hi @reinhard.unger

Could you provide a bit more details, please? A small example would be great. Could you create a simple app to reproduce the issue and attach it to the thread, please?

My mistake was that I have not set an empty entity to the embedded fragment.
I have an entity Employer which has a composition of an Address entity.
In the employer-edit I want include a fragment for modifying the address.
If the employer has no address I must create one at onAfterShow.
Then it reaches my Commit Delegate method when i click OK in employer-edit.
What’s the best way to define that it should not validate the address if it is empty?
I tried:

public class Employer {
@Valid
@...
private Address address;

But it validates with errors because the address is empty. It should only validate if something in address is filled. Shouldn’t I create an empty address on begin?
Thank you!

Is Address a mandatory field? What is the validation error text? BTW you can read this article about validation, maybe you need something different from validation on the screen