Hi,
I'm having a problem with a multi-user environment. I have a form, which
has an edit button. When a user presses the edit button, I write to the
database that this current user is editing the form. So when another
user comes along and wants to edit it, he gets the message that the form
cannot be edited because it's locked by another user. This works fine
when user A loads the form, presses the edit button and after that user
B comes along. However, if the process is: user A comes along and loads
the form, user B loads the same form, then user A presses the edit
button and after that user B presses it. Then I get an unexpected
runtimeexception and the message:
WicketMessage: Submit Button topBar:edit (path=arfForm:topBar:edit) is
not visible
Now this obviously has to do with the way I'm determining whether or not
this button should be visible. What I did is extend the Button class and
override the isVisible method. Here I check whether there is no editor,
if so, the button should be visible so the current user can become the
editor. However, since in this situation, user B became the editor while
this user had already loaded the form, the button is no longer valid for
user A but he already has it on his screen. I thought to check this in
the onSubmit method and retrieve the persisted object and check if
nothing had changed in the meantime, but apparently Wicket checks
whether this button should have been visible in the first place. And
since I use LoadableDetachableModel as the underlying model (arfModel)
the updated object is retrieved and Wicket detects that the isVisible
method returns false and throws an error that I'm trying to call the
onSubmit method.
Does anyone know of an elegant solution for this problem? Please see
below for my implementation of the edit button.
Thanks, Jonck
add(new Button("edit") {
@Override
public void onSubmit() {
String username =
((AppAuthenticatedWebSession)getSession()).getUsername();
ARF currentArf = arfModel.getObject();
ARF persistedArf = arfService.load(currentArf.getId());
if (persistedArf.getEditor() == null ||
persistedArf.getEditor().equals(username)) {
currentArf.setEditor(username);
arfService.save(currentArf);
}
}
@Override
public boolean isVisible() {
String editor = arfModel.getObject().getEditor();
if (editor == null) {
return true;
}
return false;
}
});