How do i override functions in a Vaadin 12 project? ValueProvider, SelectionListener and ComponentEventListener

58 views Asked by At

This will be quite a bit of code as I don't know what will be important. I was trying to recreated the basic UI Alejandro made in my tutorial session with him a few months ago, substituting a table in my database for the one he used. The errors I'm getting all seem related to overriding Vaadin Flow functions. I know that replaces the behavior of the Super method. IntelliJ opens the relevant Super method when I click on the errors, which I'm assuming it wants me to edit to solve the problem, but I have no idea how to do that.

I was going to paste a link to the code but the forum told me to just place it here.

Customer.java

package com.dbproject.storeui;

import java.time.LocalDate;

public class Customer {
    private Long id;
    private String lastname;
    private String firstname;
    private String email;
    private String password;
    private String phone;
    private String street;
    private String city;
    private String st;
    private int zip;
    private LocalDate dob;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getSt() {
        return st;
    }

    public void setSt(String st) {
        this.st = st;
    }

    public int getZip() {
        return zip;
    }

    public void setZip(int zip) {
        this.zip = zip;
    }

    public LocalDate getDob() {
        return dob;
    }

    public void setDob(LocalDate dob) {
        this.dob = dob;
    }
}

CustomerRepository.java

package com.dbproject.storeui;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CustomerMapper {

    @Select("SELECT * FROM customer ORDER BY id")
    List<Customer> findAll();

    @Update("UPDATE customer" +
            "SET lastname=#{lastname}, firstname=#{firstname}, email=#{email}, password=#{password}, phone=#{phone}, street=#{street}, city=#{city}, st=${st}, zip=#{zip}, dob=#{dob}" +
            "WHERE id=#{id}")
    void update(Customer customer);

    @Insert("INSERT INTO customer(lastname, firstname, email, password, phone, street, city, st, zip, dob) VALUES(#{lastname}, #{firstname}, #{email}, #{password}, #{phone}, #{street}, #{city}, #{st}, #{zip}, #{dob})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void create(Customer customer);
}

CustomerView.java (the UI class)

package com.dbproject.storeui;

import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.router.Route;

@Route("")
public class CustomerView extends Composite<VerticalLayout> {

    private final CustomerMapper customerMapper;

    private Grid<Customer> grid = new Grid<>();
    private TextField lastname = new TextField("Last Name");
    private TextField firstname = new TextField("First Name");
    private Button save = new Button("Save", VaadinIcon.CHECK.create());
    private Button create = new Button("New", VaadinIcon.PLUS.create());
    private VerticalLayout form = new VerticalLayout(lastname, firstname, save);

    private Binder<Customer> binder = new Binder<>(Customer.class);
    private Customer customer;

    public CustomerView(CustomerMapper customerMapper) {

        this.customerMapper = customerMapper;

        grid.addColumn(Customer::getLastname).setHeader("Last Name");
        grid.addColumn(Customer::getFirstname).setHeader("First Name");
        grid.addSelectionListener(event -> setCustomer(grid.asSingleSelect().getValue()));
        updateGrid();

        save.addClickListener(event -> saveClicked());
        create.addClickListener(event -> createClicked());

        getContent().add(grid, create, form);
        binder.bindInstanceFields(this);
        binder.setBean(null);
    }

    private void createClicked() {
        grid.asSingleSelect().clear();
        setCustomer(new Customer());
    }

    private void saveClicked() {
        binder.readBean(customer);
        if (customer.getId() == null) {
            customerMapper.create(customer);
        } else {
            customerMapper.update(customer);
        }
        updateGrid();
        Notification.show("Saved!");
    }

    private void setCustomer(Customer customer) {
        this.customer = customer;
        form.setEnabled(customer != null);
        binder.setBean(customer);
    }

    private void updateGrid() {
        grid.setItems(customerMapper.findAll());
    }
}
0

There are 0 answers