Welcome back to our practical Wicket tutorial series. I apologize for long break after last post (shame on me as it was published more than 1,5 month ago) but I had many unplanned things with higher priority: Wicket demo appplication  on 33rdDegree conference to prepare, a conference itself and most time-consuming one, I damaged knee ligament during football match and after that everyday for three weeks I was driving to the hospital for rehabilitation so day became too short to write something here. But now everything seems to be ok and I have some spare time to continue Wicket tutorial subject.

Today we will add two new features: editing and removing locations from our application. Currently we don’t have any items connected with locations so deleting shouldn’t be too complicated. Later on, when we add some items, we will have to rework this functionality but now it shouldn’t bother us too much.

Editing locations

To edit location we should first add link to the LocationsPage redirecting user to edit page:

In the corresponding Java class we add link to edit page:

Careful reader might notice that we have new class here, AddEditLocationPage. But it’s just old renamed AddLocationPage after some refactoring to reuse it for both creating new and editing existing locations. As in the code listing above, we are passing Id of location to the constructor to inform which object we are going to edit.

Refactoring AddLocationPage to AddEditLocationPage

Apart from renaming both Java and HTML files we have to introduce some major changes to this Java class.

First we add constructor taking id as a parameter:

setDefaultModel will enable other components from the page to use loaded location as a source of rendered data. After this, to  be consistent we change constructor for new locations too:

After these changes initGui() method needs some tweaks because it should use data from default page model and not from String name field:

This code listing needs some explanation:

In (1) we are changing model of Form to use default model from parent page so we could load data from existing or new (just created) location.

Then in (2) extracted method createLabelFieldWithValidation() is invoked and inside it (5) input field component is created and (more important)  proper validator is assigned to this input field (6).

The way how validator is created (7) depends on whether we edit existing location and our unique name check should not create an error if the same location with the same name is found (8) or we just create new location and any existing location with the same name should prevent application from creating a new one. In this method we use helper method (9) extracting Location from page model.

Ok. that’s everything we have to do to add edit feature. We now can edit and add new locations as shown on the screen shot below.

Removing locations

To start adding this feature we place another link in the LocationsPage HTML file (next to Edit one) and add link to Java class:

And because we know that in the future logic for removing location will be more complicated, it will be easier to change it if we create remove location link as a separate class:

We are almost done. The last thing we need to change is method in service class responsible for removing entity.  Without this change we will be trying to delete detached entity so we should use removeSafely() method instead of remove():

which is implemented in AbstractDAO class:

After this small change application is ready for tests and we can check that everything works just fine.


That’s all for today. Latest source code is available here next to both changesets to view and analyse  (editing location and removing location).  Thank you for you time and as always, any comments, suggestions, etc are welcome 🙂

Next episode will be released in a week or two maximum, so stay tuned!