Implementing the create and update operations

The create CRUD operation starts when the user clicks the add button. Similarly, the update CRUD operation starts when the user clicks the update button. We need the following infrastructure code:

...
private void
initBehavior() {
...
add.addClickListener(e -> showAddWindow());
edit.addClickListener(e -> showEditWindow());
}

private void showAddWindow() {
UserFormWindow window = new UserFormWindow("Add", new User());
getUI().addWindow(window);
}

private void showEditWindow() {
UserFormWindow window = new UserFormWindow("Edit", grid.asSingleSelect().getValue());
getUI().addWindow(window);
}

When any of the buttons is clicked, we show a UserFormindow (implemented shortly). For the add button, we pass a new User instance. For the update button, we pass the User instance selected in the Grid. We can implement UserWindow as an inner class inside CustomCrud. We'll omit the details of the layout configuration, and focus on the data binding part. Let's start with the following:

private class UserFormWindow extends Window { // inner to CustomCrud

private TextField firstName = new TextField("First name");
private TextField lastName = new TextField("Last name");
private TextField email = new TextField("Email");
private PasswordField password = new PasswordField("Password");
private CheckBoxGroup<Role> roles = new CheckBoxGroup<>("Roles", RoleRepository.findAll());
private ComboBox<Role> mainRole = new ComboBox<>("Main Role", RoleRepository.findAll());
private CheckBox blocked = new CheckBox("Blocked");

private Button cancel = new Button("Cancel");
private Button save = new Button("Save", VaadinIcons.CHECK);

public UserFormWindow(String caption, User user) {
initLayout(caption);
initBehavior(user);
}

private void initLayout(String caption) {
...
}

private void initBehavior(User user) {
}
}

All the input fields in the form are members of the UserFormWindow class, and are added to some sort of layout in the initLayout method (not shown). The initBehaviour method should configure the data binding between the User instance and the input fields. It also should add behavior to the cancel and save buttons. Let's think about what's required before we start coding:

  • We need data-binding. In the Vaadin Framework, that usually means using a Binder.
  • We need to bind the fields in the UserFormWindow class to the fields in the User class.
  • We need to make sure that the input fields show the correct values initially.
  • We need to make sure that the values in the input fields are written in the User instance when the save button is clicked.
  • We need to make sure no values are written in the User instance when the cancel button is clicked.

Now, we can start coding:

private void initBehavior(User user) { // inside UserFormWindow
Binder<User> binder = new Binder<>(User.class);
binder.bindInstanceFields(this);
binder.readBean(user);
}

Two important things happen in the previous code: one) all the Java fields that are also input fields in the UserFormWindow class are bound to the Java fields in the User class (with the bindIntanceFields call); and two), all the values in the Java fields of the User class are set to the corresponding input fields in the UserFormWindow class (with the readBean call).

Finally, the following code adds the behavior to the buttons:

private void initBehavior(User user) { // inside UserFormWindow
...

cancel.addClickListener(e -> close());
save.addClickListener(e -> {
try {
binder.writeBean(user);
UserRepository.save(user);
close();
refresh();
Notification.show("User saved");

} catch (ValidationException ex) {
Notification.show("Please fix the errors and try again");
}
});
}

The listener on the cancel button only has to call Window.close() (inherited). The listener on the save button calls writeBean in order to write the values in the input fields in the user instance.

Notice that writeBean throws a ValidationException. There are no validations at the moment, though. Adding the JavaBean Validation constraints we have in the User class is as simple as changing the Binder implementation:

private void initBehavior(User user) { // inside UserFormWindow
BeanValidationBinder<User> binder = new BeanValidationBinder<>(User.class);
...
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset