Directory

← Back

list-file-upload

CSV-, Excel- and Text-File Upload helper

Author

Rating

Popularity

<100

This addon is used to process a CSV, Excel or text file. To execute a clean processing, the corresponding variable (= Excel cell) must be annotated in the corresponding domain class (Excel -> Java) with the fileUpload annotation. The FileUpload annotation must be passed to a FileUploadProcessor. This serves to carry out a test on the corresponding, e.g. Excel cell. Further in the code examples.

How to use it

Official releases of this add-on are available at Vaadin Directory. For Maven instructions, download and reviews, go to https://vaadin.com/directory#!addon/list-file-upload

How it works

You must annotate variables that are to be set during the file upload with the annotation @FileUpload().

Example:

public class DemoObject {
    @FileUpload(header = "first-name", converter = FirstNameDemoObjectUploadProcessor.class)
    private String firstName;

    @FileUpload(header = "last-name", converter = LastNameDemoObjectUploadProcessor.class)
    private String lastName;

    @FileUpload(header = "email", converter = EmailDemoObjectUploadProcessor.class)
    private String email;
    
    [...]
}

File cell processor example:

public class FirstNameDemoObjectUploadProcessor implements FileCellProcessor<DemoObject> {
    @Override
    public void process(DemoObject data, Cell cell) throws ContentValidationException {
        String value = cell.getStringValue();

        if (StringUtils.isNotBlank(value)) {
            data.setFirstName(value);
        } else {
            throw new ContentValidationException("Field First-Name is required");
        }
    }
}

Simple

In the Vaadin UI, you must initialize the SimpleFileUploadProcessor and specify what should happen in case of a fault-free or incorrect file upload. The SimpleFileUploadProcessor must be passed to the upload element (from Vaadin) as succeededListener.

Example:

SimpleFileUploadProcessor<DemoObject> uploadProcessor = new SimpleFileUploadProcessor<>(DemoObject.class,
    (event, items) -> {
        gridSimpleItems.addAll(items);
        demoSimpleGrid.getDataProvider().refreshAll();
    }, (event, errorItems) -> {
        gridSimpleErrorItems.addAll(errorItems);
        importErrorSimpleGrid.getDataProvider().refreshAll();
    });

Upload upload = new Upload("Choose file...", uploadProcessor);
upload.addSucceededListener(uploadProcessor);
rootLayout.addComponent(upload);

Stream

In the Vaadin UI, you must initialize the StreamFileUploadProcessor and specify what should happen in case of a fault-free or incorrect file upload. The StreamFileUploadProcessor must be passed to the upload element (from Vaadin) as succeededListener.

Example:

StreamFileUploadProcessor<DemoObject> uploadProcessor = new StreamFileUploadProcessor<>(DemoObject.class,
    item -> {
        gridStreamItems.add(item);
        demoStreamGrid.getDataProvider().refreshAll();
    }, error -> {
        gridStreamErrorItems.add(error);
        importErrorStreamGrid.getDataProvider().refreshAll();
    }, event -> Notification.show("Upload completed!"));

Upload upload = new Upload("Choose file...", uploadProcessor);
upload.addSucceededListener(uploadProcessor);
rootLayout.addComponent(upload);

Try it with the demo

git clone git@github.com:loefflefarn/list-file-upload.git
mvn clean install
cd list-file-upload-demo
mvn jetty:run

To see the demo, navigate to http://localhost:8080/

Contributing

Issues for this add-on are tracked here. All bug reports and feature requests are welcome.

Sample code

public class DemoObject {
    @FileUpload(header = "first-name", converter = FirstNameDemoObjectUploadProcessor.class)
    private String firstName;

    @FileUpload(header = "last-name", converter = LastNameDemoObjectUploadProcessor.class)
    private String lastName;

    @FileUpload(header = "email", converter = EmailDemoObjectUploadProcessor.class)
    private String email;
    
    [...]
}
public class FirstNameDemoObjectUploadProcessor implements FileCellProcessor<DemoObject> {
    @Override
    public void process(DemoObject data, Cell cell) throws ContentValidationException {
        String value = cell.getStringValue();

        if (StringUtils.isNotBlank(value)) {
            data.setFirstName(value);
        } else {
            throw new ContentValidationException("Field First-Name is required");
        }
    }
}
    private final VerticalLayout rootLayout = new VerticalLayout();

    private final transient List<DemoObject> gridItems = new ArrayList<>();

    private final Grid<DemoObject> demoGrid = new Grid<>(DemoObject.class);

    private final transient List<ImportError> gridErrorItems = new ArrayList<>();

    private final Grid<ImportError> importErrorGrid = new Grid<>(ImportError.class);

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        initUploadButton();
        initDemoGrid();
        initImportErrorGrid();

        setContent(rootLayout);
    }

    private void initUploadButton() {
        SimpleFileUploadProcessor<DemoObject> uploadProcessor = new SimpleFileUploadProcessor<>(DemoObject.class,
                (event, items) -> {
                    gridItems.addAll(items);
                    demoGrid.getDataProvider().refreshAll();
                }, (event, errorItems) -> {
                    gridErrorItems.addAll(errorItems);
                    importErrorGrid.getDataProvider().refreshAll();
                });

        Upload upload = new Upload("Choose file...", uploadProcessor);
        upload.addSucceededListener(uploadProcessor);
        rootLayout.addComponent(upload);
    }

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Released
2017-08-22
Maturity
EXPERIMENTAL
License
MIT License

Compatibility

Framework
Vaadin 8.0+
Browser
Browser Independent

list-file-upload - Vaadin Add-on Directory

CSV-, Excel- and Text-File Upload helper list-file-upload - Vaadin Add-on Directory
This addon is used to process a CSV, Excel or text file. To execute a clean processing, the corresponding variable (= Excel cell) must be annotated in the corresponding domain class (Excel -> Java) with the fileUpload annotation. The FileUpload annotation must be passed to a FileUploadProcessor. This serves to carry out a test on the corresponding, e.g. Excel cell. Further in the code examples. ## How to use it Official releases of this add-on are available at Vaadin Directory. For Maven instructions, download and reviews, go to https://vaadin.com/directory#!addon/list-file-upload ## How it works You must annotate variables that are to be set during the file upload with the annotation `@FileUpload()`. Example: ```java public class DemoObject { @FileUpload(header = "first-name", converter = FirstNameDemoObjectUploadProcessor.class) private String firstName; @FileUpload(header = "last-name", converter = LastNameDemoObjectUploadProcessor.class) private String lastName; @FileUpload(header = "email", converter = EmailDemoObjectUploadProcessor.class) private String email; [...] } ``` File cell processor example: ```java public class FirstNameDemoObjectUploadProcessor implements FileCellProcessor { @Override public void process(DemoObject data, Cell cell) throws ContentValidationException { String value = cell.getStringValue(); if (StringUtils.isNotBlank(value)) { data.setFirstName(value); } else { throw new ContentValidationException("Field First-Name is required"); } } } ``` ### Simple In the Vaadin UI, you must initialize the `SimpleFileUploadProcessor` and specify what should happen in case of a fault-free or incorrect file upload. The `SimpleFileUploadProcessor` must be passed to the upload element (from Vaadin) as` succeededListener`. Example: ```java SimpleFileUploadProcessor uploadProcessor = new SimpleFileUploadProcessor<>(DemoObject.class, (event, items) -> { gridSimpleItems.addAll(items); demoSimpleGrid.getDataProvider().refreshAll(); }, (event, errorItems) -> { gridSimpleErrorItems.addAll(errorItems); importErrorSimpleGrid.getDataProvider().refreshAll(); }); Upload upload = new Upload("Choose file...", uploadProcessor); upload.addSucceededListener(uploadProcessor); rootLayout.addComponent(upload); ``` ### Stream In the Vaadin UI, you must initialize the `StreamFileUploadProcessor` and specify what should happen in case of a fault-free or incorrect file upload. The `StreamFileUploadProcessor` must be passed to the upload element (from Vaadin) as` succeededListener`. Example: ```java StreamFileUploadProcessor uploadProcessor = new StreamFileUploadProcessor<>(DemoObject.class, item -> { gridStreamItems.add(item); demoStreamGrid.getDataProvider().refreshAll(); }, error -> { gridStreamErrorItems.add(error); importErrorStreamGrid.getDataProvider().refreshAll(); }, event -> Notification.show("Upload completed!")); Upload upload = new Upload("Choose file...", uploadProcessor); upload.addSucceededListener(uploadProcessor); rootLayout.addComponent(upload); ``` ## Try it with the demo ```` git clone git@github.com:loefflefarn/list-file-upload.git mvn clean install cd list-file-upload-demo mvn jetty:run ```` To see the demo, navigate to http://localhost:8080/ ## Contributing Issues for this add-on are tracked [here](https://github.com/loefflefarn/list-file-upload/issues). All bug reports and feature requests are welcome.
Online