Skip to content

Commit

Permalink
Implement export functionality alejandro-du#21
Browse files Browse the repository at this point in the history
added base support for exporters
added base support for excel exporter
  • Loading branch information
dmicol committed Nov 4, 2017
1 parent fe6f18d commit f37bb28
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>

<build>
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/org/vaadin/crudui/crud/AbstractCrud.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.vaadin.crudui.crud;

import com.vaadin.ui.Composite;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.vaadin.crudui.form.CrudFormFactory;
import org.vaadin.crudui.layout.CrudLayout;
import org.vaadin.crudui.support.BeanExcelBuilder;
import org.vaadin.crudui.support.ExcelOnDemandStreamResource;

import java.util.Collections;
import com.vaadin.server.Resource;
import com.vaadin.ui.Composite;

/**
* @author Alejandro Duarte
Expand All @@ -17,6 +24,7 @@ public abstract class AbstractCrud<T> extends Composite implements Crud<T> {
protected AddOperationListener<T> addOperation = t -> null;
protected UpdateOperationListener<T> updateOperation = t -> null;
protected DeleteOperationListener<T> deleteOperation = t -> { };
protected Map<String, Resource> exportOperations = new HashMap<>();

protected CrudLayout crudLayout;
protected CrudFormFactory<T> crudFormFactory;
Expand All @@ -25,6 +33,13 @@ public AbstractCrud(Class<T> domainType, CrudLayout crudLayout, CrudFormFactory<
this.domainType = domainType;
this.crudLayout = crudLayout;
this.crudFormFactory = crudFormFactory;
exportOperations.put("EXCEL", new ExcelOnDemandStreamResource() {

@Override
protected XSSFWorkbook getWorkbook() {
return new BeanExcelBuilder<T>(domainType).createExcelDocument(findAllOperation.findAll());
}
});

if (crudListener != null) {
setCrudListener(crudListener);
Expand Down Expand Up @@ -89,4 +104,15 @@ public void setCrudListener(CrudListener<T> crudListener) {
setFindAllOperation(crudListener::findAll);
}

public void addExporter(String name, Resource exporter) {
exportOperations.put(name, exporter);
}

public void removeExporter(String name) {
exportOperations.remove(name);
}

public Resource getExporter(String name) {
return exportOperations.get(name);
}
}
38 changes: 29 additions & 9 deletions src/main/java/org/vaadin/crudui/crud/impl/GridCrud.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.vaadin.crudui.crud.impl;

import com.vaadin.data.provider.Query;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Notification;
import java.util.Collection;
import java.util.LinkedHashMap;

import org.vaadin.crudui.crud.AbstractCrud;
import org.vaadin.crudui.crud.CrudListener;
import org.vaadin.crudui.crud.CrudOperation;
Expand All @@ -15,7 +12,14 @@
import org.vaadin.crudui.layout.CrudLayout;
import org.vaadin.crudui.layout.impl.WindowBasedCrudLayout;

import java.util.Collection;
import com.vaadin.data.provider.Query;
import com.vaadin.icons.VaadinIcons;
import com.vaadin.server.FileDownloader;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Notification;

/**
* @author Alejandro Duarte
Expand All @@ -32,6 +36,7 @@ public class GridCrud<T> extends AbstractCrud<T> {
protected Button deleteButton;
protected Grid<T> grid;

protected LinkedHashMap<String, Button> exporterButtons = new LinkedHashMap<>();
protected Collection<T> items;

public GridCrud(Class<T> domainType) {
Expand Down Expand Up @@ -64,7 +69,7 @@ protected void initLayout() {
findAllButton.setDescription("Refresh list");
findAllButton.setIcon(VaadinIcons.REFRESH);
crudLayout.addToolbarComponent(findAllButton);

addButton = new Button("", e -> addButtonClicked());
addButton.setDescription("Add");
addButton.setIcon(VaadinIcons.PLUS);
Expand All @@ -84,6 +89,10 @@ protected void initLayout() {
grid.setSizeFull();
grid.addSelectionListener(e -> gridSelectionChanged());
crudLayout.setMainComponent(grid);

Button btn = new Button(FontAwesome.FILE_EXCEL_O.getHtml());
btn.setCaptionAsHtml(true);
addExporterMenu("EXCEL", btn);

updateButtons();
}
Expand Down Expand Up @@ -249,9 +258,20 @@ public void setRowCountCaption(String rowCountCaption) {
public void setSavedMessage(String savedMessage) {
this.savedMessage = savedMessage;
}

public void setDeletedMessage(String deletedMessage) {
this.deletedMessage = deletedMessage;
}

public Button getExporterMenu(String name) {
return exporterButtons.get(name);
}

public GridCrud<T> addExporterMenu(String name, Button exporterButton) {
exporterButtons.put(name, exporterButton);
new FileDownloader(getExporter(name)).extend(exporterButton);
crudLayout.addToolbarComponent(exporterButton);

return this;
}
}
Loading

2 comments on commit f37bb28

@bonifacechacha
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be submitted as a pull request to the main crudui repo?

@alejandro-du
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be happy to merge it.

Please sign in to comment.