set an outputstream as table containerDataSource

Hi to everyone,
I must upload a csv file from desktop into a table ui (before) and in several db entities (after). Don’t ask for the reason of this horrible procedure, but I have to do it. I was inspired from http://demo.vaadin.com/sampler/#UploadWithProgressMonitoring and now, in the class that implements Receiver I must upload the output stream in a UI Table and so in different db tables.


public class LineBreakCounter implements Receiver {

	private String fileName;
    private String mtype;

    private int counter;
    private int total;
    private boolean sleep;
	private File file;

    /**
     * return an OutputStream that simply counts lineends
     */
    public OutputStream receiveUpload(String filename, String MIMEType) {
        counter = 0;
        total = 0;
        fileName = filename;
        mtype = MIMEType;         
        return new OutputStream() {
            private static final int searchedByte = '\n';

            @Override
            public void write(int b) throws IOException {
                total++;
                if (b == searchedByte) {
                    counter++;
                }
                if (sleep && total % 1000 == 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        };
        

I don’t know how to do this. Any suggestion?

Hi,

I’d separate out the problems, myself : I think the main problem is “How to create a container/items from a CSV Stream” . The rest is just a case of linking bits together.

Is the CSV structure hard coded (i.e. same columns for every file) or varialbe (each file has a completely different set of columns)?
Do you already have a functions/objects to read a CSV format?
Are you trying to populate an already existing container and/or item, or can you create any-old item or container?

Cheers,

Charles.

Hi Charles,
the CSV file structure could be different depending on the database table to manage. I already wrote a CSV reader class (is a simple class that read a file), but with the upload widget of vaadin I saw that is useless. Moreover, the container I have to populate will have the same CSV file number and format columns, and this will change depending on the file layout. I’m developing a CRUD application, but my project manager doesn’t want that I provides the possibility to insert directly from UI a row into the database table, so he thought (but this is bad programming according to me) that is better write a batch load procedure to insert data into db tables. So, now I have this problem… :frowning:

Hi,

I’ve knocked together a simple demonstration application showing how upload and parse CSV file into a Vaadin Container/Table. I used the Apache 2 licensed
open-csv library
to read the CSV 'cos life is too short to write your own when you don’t have to.

Source code is contained in this simple
one-class gist on github

Of course, Sod’s law dictates that there is a
more succinct approach to this already on the forum
Ah well, mines a little more complete (even with no error handling)

HTH,

Cheers,

Charles.

(Data in screen shot from
UK Home Office site
, FWIW)
12525.png

Thanks Charles, you’ve been very helpful. Great!!!