File Upload

Hi Guys,

I have one concern with upload component… It disable all my console logs the second time the upload event fired. No output from System.out.print/ln, printStackTrace() and Logger (java.util.logging.Logger). It affects the entire project. Is this a bug or am missing something?

Im using Vaadin 13.0.5

		MemoryBuffer buffer = new MemoryBuffer();
        Upload upload = new Upload(buffer);
        
        upload.addStartedListener(event -> {
            //TODO);
        });
        
        upload.addProgressListener(event -> {
            //TODO
        });
        
        upload.addFailedListener(event -> {
            //TODO
        });
        
        upload.addSucceededListener(event -> {
            //TODO
        });
        
        upload.addFinishedListener(event -> {
            /TODO
        });

Has anyone experience this as well?

Hm, I tried to recreate this with 13.0.5 but failed to do so. System.out works like a charm even after multiple uploads. Do you use an IDE or the console?

Ronny Edler:
Hm, I tried to recreate this with 13.0.5 but failed to do so. System.out works like a charm even after multiple uploads. Do you use an IDE or the console?

Hi Ron, Im using Netbeans 8.2 IDE

No issues there either (tested with 8.1). Do you execute any code in the upload listeners?

Also, you don’t use Spring, do you?

Ronny Edler:
Also, you don’t use Spring, do you?

Hi Ron, Im using Spring Boot…

here’s my full source code for the upload…

Dialog uploadFile(){
        Dialog dialog = new Dialog();
        
        MemoryBuffer buffer = new MemoryBuffer();
//        FileBuffer fileBuffer = new FileBuffer();
        Upload upload = new Upload(buffer);
        
        upload.addStartedListener(event -> {
            //TODO);
        });
        
        upload.addProgressListener(event -> {
            //TODO
        });
        
        upload.addFailedListener(event -> {
            //TODO
        });
        
        upload.addSucceededListener(event -> {
			System.out.print("CLOSED!");
            dialog.close();
        });
        
        upload.addFinishedListener(event -> {
            members.clear();
            processFileUpload(buffer.getInputStream());
            try {
                buffer.getInputStream().close();
            } catch (IOException ex) {
                Logger.getLogger(MembersAccount.class.getName()).log(Level.SEVERE, null, ex);
                new ErrorNotificationDialog(this.getClass().getName(), ex).open();
            } finally {
                try {
                    buffer.getInputStream().close();
                } catch (IOException ex) {
                    Logger.getLogger(MembersAccount.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
            Button save = new Button("SAVE", new Icon(VaadinIcon.SAFE));
            test = true;
            if(test){
                Dialog log = new Dialog();
                
                newMembersGrid.popolateGrid(getMembers());
                newMembersGrid.addComponentColumn(item -> {
                    Button del = new Button(new Icon(VaadinIcon.TRASH), listener -> {
                        newMembersGrid.removeFromGrid(getMembers(), item);
                        Notification.show("members: "+getMembers().isEmpty(), 2000, Notification.Position.MIDDLE);
                        if(getMembers().isEmpty()){
                            save.setEnabled(false);
                        }
                    });
                    del.addClassName("grid-button-background");
                    
                    return del;
                });
                
                save.addClickListener(listener -> {
                    for(Member m : members){
                        if(!ms.upload(m)){
                            Notification.show("CANNOT ADD "+m.getFirstname().toUpperCase()+" "+m.getLastname().toUpperCase(), 3000, Notification.Position.MIDDLE);
                            return;
                        }
                    }
                    membersGrid.setItems(ms.findAll());
                    log.close();
                });
                
                VerticalLayout components = new VerticalLayout(newMembersGrid, save);
                components.setHorizontalComponentAlignment(Alignment.END, save);
                
                log.add(components);
                log.open();
            }
        });
        
        dialog.add(upload);
        
        return dialog;
    }
    
	//I have copied this code somewhere in converting csv to java object
    void processFileUpload(InputStream stream) {
        Pattern pattern = Pattern.compile(",");
        try (BufferedReader in = new BufferedReader(new InputStreamReader(stream))) {
            members = in.lines().skip(1).map(line -> {
                String[] x = pattern.split(line);
                return new Member(x[0]
, x[1]
, x[2]
, x[3]
);
            }).collect(Collectors.toList());
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            mapper.writeValue(System.out, members);
        } catch (IOException ex) {
            Logger.getLogger(MembersAccount.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

Also, after the first fire event occur, I cant enable/disable the button as well…

Not sure your application logic properly handles Dialog’s behaviour.
From the API doc: “Close - Note: This method also removes the dialog component from the DOM after closing it, unless you have added the component manually.”
One of the log.close(); calls might be the culprit.

Ronny Edler:
Not sure your application logic properly handles Dialog’s behaviour.
From the API doc: “Close - Note: This method also removes the dialog component from the DOM after closing it, unless you have added the component manually.”
One of the log.close(); calls might be the culprit.

Hi Ron, really appreciate your help… Without pointing out that the upload component works fine, I would not have solve this problem and might considered it as bug since I have posted an issue in github and nobody has answered it. LOL

The culprit was not the “log.close” call, but this class,

	ObjectMapper mapper = new ObjectMapper();
	mapper.enable(SerializationFeature.INDENT_OUTPUT);
	mapper.writeValue(System.out, members);

I have to remove everything, and test it individually until I have found the problem. When I copied the source code and successfully converted csv into an object, I did not bother to review it. LOL

Been trying to solve this for a month… LOL

Glad to hear.
You might accidentally close System.out in your method. :slight_smile:

Ronny Edler:
Glad to hear.
You might accidentally close System.out in your method. :slight_smile:

Yeah… LOL

Thanks Ron.