0
votes

Good day, community. I am new to Java and Vaadin in particular. Please explain to me why addSucceededListener() is triggered earlier (when loading a page) than I download a file?

You can see the discussion on the Vaadin forum here.

Just a fish text that would have omitted the editor for publication -_-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla non finibus massa. Maecenas ut metus sit amet odio luctus facilisis. Mauris vulputate enim nec nibh mollis, in tincidunt orci posuere. Curabitur vitae tincidunt velit, congue sagittis turpis. Duis sollicitudin in metus non pulvinar. Pellentesque quis justo sit amet dui tristique vestibulum commodo et risus. Suspendisse a commodo lectus, a hendrerit massa. Donec sagittis commodo purus, ut mollis risus ultricies id. Curabitur lacinia mi vitae luctus tristique. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Proin at varius justo. Vestibulum posuere tristique metus, ac commodo elit consectetur id.

@Route("")
public class UploadView extends VerticalLayout {
    @Autowired
    PictureRepository pictureRepository;

    @Autowired
    GoogleCloudAiAssistantRecognition googleCloudAiAssistantRecognition;

    public UploadView() {
        this.pictureRepository = pictureRepository;
        this.googleCloudAiAssistantRecognition = googleCloudAiAssistantRecognition;

        MenuBar menuBar = new MenuBar();
        menuBar.addThemeVariants(MenuBarVariant.LUMO_PRIMARY);

        MenuItem resultsItem = menuBar.addItem("Get Results");
        MenuItem tagItem = menuBar.addItem("Edit Tag");

        resultsItem.addClickListener(e -> UI.getCurrent().navigate("ai-result"));
        tagItem.addClickListener(e -> UI.getCurrent().navigate("edit-tag"));

        add(menuBar, new H1("Assistant recognition by Google Vision"));

        MemoryBuffer buffer = new MemoryBuffer();
        Upload upload = new Upload(buffer);
        upload.setAcceptedFileTypes("image/jpeg", "image/png", "image/gif");
        upload.setMaxFileSize(10485760);
        Div output = new Div();

        upload.addSucceededListener(event -> {
                InputStream inputStream = buffer.getInputStream();
                byte[] bytes = new byte[0];
                try {
                    bytes = IOUtils.toByteArray(inputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                add(new H1(event.getMIMEType()));
                Component component = createComponent(event.getMIMEType(), event.getFileName(), inputStream);
                showOutput(event.getFileName(), component, output);

                PictureModel pictureModel = new PictureModel(event.getFileName(), event.getMIMEType(), bytes);
                Long picId = pictureRepository.save(pictureModel).getId();

                List<TagModel> tags = null;
                try {
                    tags = googleCloudAiAssistantRecognition.detect(picId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                pictureModel.setTagModel(tags);
        });

        /*upload.getElement().addEventListener("upload-success", e -> {
            System.out.println(e.getEventData());
        });*/

        add(upload, output);
    }
1

1 Answers

0
votes

I suspect you may have a misunderstanding. It adds the succeeded-listener, but the listener-code is not executed yet.

You also seem to use the bytes variable that is always going to be empty because you read the inputStream of an empty buffer (outside of the succeededListener).

Please try this code and then, if your "problem" still exists, try to reformulate your problem.

public UploadView() throws IOException {
    add(new H1("Assistant recognition by Google Vision"));

    MemoryBuffer buffer = new MemoryBuffer();
    Upload upload = new Upload(buffer);
    upload.setAcceptedFileTypes("image/jpeg", "image/png", "image/gif");
    upload.setMaxFileSize(10485760);
    Div output = new Div();

    // move this INTO the succeededListener, because otherwise the buffer is empty
    //InputStream inputStream = buffer.getInputStream();
    //byte[] bytes = IOUtils.toByteArray(inputStream);

    upload.addSucceededListener(event -> {
        InputStream inputStream = buffer.getInputStream();
        byte[] bytes = IOUtils.toByteArray(inputStream);

        add(new H1(event.getMIMEType()));
        Component component = createComponent(event.getMIMEType(), event.getFileName(), inputStream);
        showOutput(event.getFileName(), component, output);

        PictureModel pictureModel = new PictureModel(event.getFileName(), event.getMIMEType(), bytes);
        Long picId = pictureRepository.save(pictureModel).getId();

        List<TagModel> tags = null;
        try {
            tags = googleCloudAiAssistantRecognition.detect(picId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pictureModel.setTagModel(tags);
        pictureRepository.save(pictureModel);
    });

    add(upload, output);
}