Search code examples
javaspring-bootvaadin

Session Attribute issue Vaadin 24


I have trouble understading how VaadinSessions and UI works.

I have my class that takes the id and other information of SampleClient and use it as a URL:

@Route(value = "URL-of-page/:sampleClientID?", layout = MainLayout.class)
@RolesAllowed("USER")
public class DocumentazioneEntita extends Div implements BeforeEnterObserver {
    
    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        this.client = (SampleClient) VaadinSession.getCurrent().getAttribute("client");
        System.out.println("client inside override " + client);
        if (this.client == null) {
            Notification.show("No client selected", 3000, Notification.Position.BOTTOM_START);
            navigateToMain(event);
            return;
        }
        this.idClienteString = (String) VaadinSession.getCurrent().getAttribute("idCliente");
        System.out.println("ID CLIENT STRING: " + idClienteString);
        Optional<Long> sampleClientId = event.getRouteParameters().get(SAMPLECLIENT_ID).map(Long::parseLong);
        if (sampleClientId.isPresent()) {
            Optional<SampleClient> sampleClientFromBackend = sampleClientService.get(sampleClientId.get());
            if (sampleClientFromBackend.isPresent()) {
                populateForm(sampleClientFromBackend.get());
            } else {
                Notification.show(
                        String.format("Client not found, ID = %d", sampleClientId.get()),
                        3000, Notification.Position.BOTTOM_START);
                navigateToMain(event);
                return;
            }
            refreshGridData();
        } 
    }
    private Long idCliente = (Long) VaadinSession.getCurrent().getAttribute("IdCliente");
  
    
    private SampleCoursesService sampleCoursesService;
    private SampleContactsService sampleContactsService;
    private SampleClientService sampleClientService;
    private SampleContacts contact;
    private SampleClient client;
    
    private String SAMPLECLIENT_ID = "sampleClientID";


    private Checkbox showArchivedCheckbox; // Define showArchivedCheckbox as a field
    private Grid<SampleContacts> dispositiviTable = new Grid<>(SampleContacts.class);; // Define dispositiviTable as a field
    private List<SampleContacts> contactsList; // Define contactsList as a field

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

    private GenerateReport generateReport;
    public String idClienteString;
    

        public DocumentazioneEntita(SampleCoursesService sampleCoursesService, 
                                    SampleContactsService sampleContactsService, 
                                    SampleClientService sampleClientService, GenerateReport generateReport) {
            this.sampleCoursesService = sampleCoursesService;
            this.sampleContactsService = sampleContactsService;
            this.sampleClientService = sampleClientService;
            this.generateReport = generateReport;
            
            setId("documentazione-entita-view");
            setSizeFull();
            System.out.println("ID CLIENTE: " + idCliente); 
            System.out.println("ID CLIENTE STRING: sotto autowired " + idClienteString);

To better understand the situation i started debugging printing in the console the data i have. Tthis is the output of my terminal:

ID CLIENTE: 1
ID CLIENTE STRING: sotto autowired null

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, [email protected], idCliente=37, indirizzoCliente=789 Main St, insolvente=false, localitaCliente=New York, nomeCliente=John Smith, noteCliente=null, provinciaCliente=null, pivaCliente=1.23456789E8, sdiCliente=SDI123, telefonoCliente=1234567890]       
ID CLIENTE STRING: 37

So inside the @Overridei have my data outside of it i do not. why is the reason for that?
I wanted to be able to use the data of my SampleClient in my page freely but i can't figure it out why it just stays inside the before enter class.

Is worth noticing that in the previous page i got all the necessary information through Vaadin UI:

   private void navigateToEntityDocumentation(SampleClient client) {
        System.out.println("go to: " + client);
        if (client != null) {
            Long idCliente = client.getIdCliente();
            
            if (idCliente != null) {
                
                String idClienteString = idCliente.toString();
                UI.getCurrent().navigate("documentazione-entita/" + idClienteString);
                UI.getCurrent().getSession().setAttribute("idCliente", idClienteString);
                UI.getCurrent().getSession().setAttribute("client", client); 
            } else {
                // Log that idCliente is null
                System.out.println("Cliente not found " + client);
                UI.getCurrent().navigate("#");
            }
        } else {
            // Log that client is null
            System.out.println("Client does not exists");
            UI.getCurrent().navigate("#");
        }
    }

This is what my System.out.println("go to: " + client); outputs in the terminal, as a proof that i have a data flow from page to page.

go to: SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, [email protected], idCliente=37, indirizzoCliente=789 Main St, insolvente=false, localitaCliente=New York, nomeCliente=John Smith, noteCliente=null, provinciaCliente=null, pivaCliente=1.23456789E8, sdiCliente=SDI123, telefonoCliente=1234567890]

Solution

  • As pointed out in a comment, the problem here has nothing to do with VaadinSession.

    Instead, the problem is that one part of the code uses "IdCliente" as the session attribute name and another part of the code uses "idCliente". It's recommended to use a constant for magic string values like this to avoid these types of mistakes.