list-file-upload - Vaadin Add-on Directory
CSV-, Excel- and Text-File Upload helperThis 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.
Author HomepageIssue Tracker
Source Code