Possible bug in onBeforeCommitChanges for editedEntity

Hi all

In my understanding, the event onBeforeCommitChanges takes place before the action (command) windowCommitAndClose. I use the onBeforeCommitChanges event to verify and modify values for the given editedEntity depending on the choosen dialog option of the user. The user is given the opportunity to cancel the operation (saving changed or new data).

But it seems that the provided entries in the edit form are saved to the database regardless whether the user chooses option “yes, I want to save” or “no, I do not wan’t to save”. I experimented and found out, that obviously the action command takes place BEFORE the event onBeforeCommitChanges.

My code is very simple and similar to the sample provided for this usecase.

thx for you feedback.

best regards,

Michael

Hi,

Could you please attach a small demo project that reproduces the problem? If not possible, please specify the following:

  1. Do you have any custom code in the editor screen? if so, please attach a code snippet
  2. What entity relations do you have? Composition one-to-many, association o-t-m, m-t-o etc.

Regards,
Gleb

Hello Gleb, thx for answering and sorry for my late reply.

In the editor screen controller the only custom code is a call to a kieServer processing rules.

public void onBeforeCommitChanges(BeforeCommitChangesEvent event) {
//even without calling dataManager.save(getEditedEntity()); the value for StamGUID is saved.
        User user = (User) currentAuthentication.getUser();
        getEditedEntity().setStammGUID(user);

        dialogs.createOptionDialog()
                .withCaption("Bestätigung")
                .withMessage("Möchten Sie die Änderungen speichern?")
                .withActions(
                        new DialogAction(DialogAction.Type.YES).withHandler(e -> {
                            // Speichern der Änderungen, wenn der Benutzer auf "Ja" klickt
                            // keine Aktion benötigt, da dies die Standardeinstellung ist
                            try{
                                boolean bSpeichern = false;
                                //not included in this snippet is the code which runs the kiesession and updates the editedEntity with getEditedEntity.setParticularPropertyName(retrievedValue)

                                    //if everything runs fine then I force the save. Without calling the save-Method, the property values set within the .withAction part of the code are not saved. But the values for properties bound the screen are saved.
                                    if (bSpeichern){
                                        dataManager.save(getEditedEntity());
                                    }
                                }
                            }catch (Exception exception){
                                System.out.println("Fehler bei onBeforeCommitChanges: " + exception.getMessage());
                                System.out.println(exception.getStackTrace().toString());
                                event.preventCommit();
                            }
                        }),
                        new DialogAction(DialogAction.Type.NO).withHandler(e -> {
                            // verhindern Sie das Speichern der Änderungen, wenn der Benutzer auf "Nein" klickt
                            event.preventCommit();
                        })
                )

                .show();
    }

I hope, this compressed code snippet gives you a picture of the structure of my code.

Entity relation is manytoone.

Hope this helps.

thx in advance

Michael

Hi,

event.preventCommit(); must be called at the end of the method, instead of in the handler of NO actions, because displaying a dialog doesn’t prevent other code from executing.

As an example, take a look at io.jmix.ui.screen.StandardEditor#preventUnsavedChanges which stops screen closing and resumes closing if certain actions are clicked.

Regards,
Gleb

Thank you!