Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions exporter-addon/src/main/java/org/vaadin/haijian/FileBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.vaadin.data.provider.DataCommunicator;
import com.vaadin.data.provider.Query;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;

public abstract class FileBuilder<T> {
private static final String TMP_FILE_NAME = "tmp";
Expand All @@ -36,16 +37,15 @@ public abstract class FileBuilder<T> {
FileBuilder(Grid<T> grid, ExporterOption option) {
this.grid = Objects.requireNonNull(grid);
this.option = Objects.requireNonNull(option);

columns = grid.getColumns().stream().filter(this::isExportable).collect(Collectors.toList());
columns = grid.getColumns().stream().filter(this::isColumnExportable).collect(Collectors.toList());
if (columns.isEmpty()) {
throw new ExporterException(
"No exportable column found, did you remember to set property name as the key for column");
}

}

private boolean isExportable(Grid.Column column) {
private boolean isColumnExportable(Grid.Column column) {
if (column.isHidden()) {
return false;
}
Expand All @@ -58,20 +58,6 @@ private boolean isExportable(Grid.Column column) {
return false;
}

if (option.isUseItemProperties()) {
if (propertySet == null) {
return false;
}

if (!propertySet.getProperty(column.getId()).isPresent()) {
return false;
}
}

if (!option.getColumnOption(column.getId()).getValueProviderFunction().isPresent()) {
return false;
}

return true;
}

Expand Down Expand Up @@ -104,23 +90,19 @@ protected void resetContent() {

private void buildHeaderRow() {
columns.forEach(column -> {
if (option.isUseItemProperties()) {
Optional<PropertyDefinition<T, ?>> propertyDefinition = propertySet.getProperty(column.getId());
if (getColumnOption(column).getHeaderProviderFunction().isPresent()) {
onNewCell();
buildColumnHeaderCell(getColumnOption(column).getHeaderProviderFunction().get().apply(column.getId()));
} else {
Optional<PropertyDefinition<T, ?>> propertyDefinition = propertySet.getProperty(column.getId());
if (propertyDefinition.isPresent()) {
onNewCell();
buildColumnHeaderCell(getColumnHeader(propertyDefinition.get()));
} else {
LoggerFactory.getLogger(this.getClass())
.warn(String.format("Column id %s is a property which cannot be found", column.getId()));
}
} else {
ColumnOption columnOption = option.getColumnOption(column.getId());
columnOption.getHeaderProviderFunction().ifPresent(headerProvider -> {
String header = headerProvider.apply(column.getId());
onNewCell();
buildCell(header);
});
}
}
});

headerRowBuilt = true;
Expand Down Expand Up @@ -165,19 +147,18 @@ private void buildRows() {

@SuppressWarnings("unchecked")
private void buildRow(T item) {
if (option.isUseItemProperties() && propertySet == null) {
if (propertySet == null) {
propertySet = (PropertySet<T>) BeanPropertySet.get(item.getClass());
columns = columns.stream().filter(this::isExportable).collect(Collectors.toList());
}

if (!headerRowBuilt) {
buildHeaderRow();
onNewRow();
}
columns.forEach(column -> {
ColumnOption columnOption = option.getColumnOption(column.getId());
Optional<ValueProvider<Object, Object>> getterFunction = columnOption.getValueProviderFunction();
if (getterFunction.isPresent()) {
Object value = getterFunction.get().apply(item);
Optional<ValueProvider<Object, Object>> optionalGetter = getColumnOption(column).getValueProviderFunction();
if (optionalGetter.isPresent()) {
Object value = optionalGetter.get().apply(item);
onNewCell();
buildCell(value);
} else {
Expand All @@ -187,12 +168,16 @@ private void buildRow(T item) {
buildCell(propertyDefinition.get().getGetter().apply(item));
} else {
throw new ExporterException(
"Column id: " + column.getId() + " is a property which cannot be found");
"Property for column id: " + column.getId() + " cannot be found");
}
}
});
}

private ColumnOption getColumnOption(Column column) {
return option.getColumnOption(column.getId());
}

void onNewRow() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
import java.util.HashMap;
import java.util.Map;

import com.vaadin.ui.Grid.Column;

public class ExporterOption {
private Map<String, ColumnOption> columnOptions;
private boolean useItemProperties;

/**
* If does not exist empty {@linkplain ColumnOption option} will be created for given ID.
*
* @param columnKey use column {@link Column#getId() ID}
*/
public ColumnOption getColumnOption(String columnKey) {
getColumnOptions().putIfAbsent(columnKey, new ColumnOption());
return getColumnOptions().get(columnKey);
}

/**
* @return Column mapped by {@link Column#getId() ID}
*/
private Map<String, ColumnOption> getColumnOptions() {
if (columnOptions == null) {
columnOptions = new HashMap<>();
}
return columnOptions;
}

public boolean isUseItemProperties() {
return useItemProperties;
}

public void setUseItemProperties(boolean useItemProperties) {
this.useItemProperties = useItemProperties;
}
}
1 change: 1 addition & 0 deletions exporter-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
4 changes: 4 additions & 0 deletions exporter-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-server</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.vaadin.addons.demo;
package org.vaadin.haijian.demo;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
import org.vaadin.addons.demo.helpers.GridDemoViewCreator;

import javax.servlet.annotation.WebServlet;

import org.vaadin.haijian.demo.helpers.GridDemoViewCreator;

@Theme("valo")
@Title("Exporter Add-on Demo")
@SuppressWarnings("serial")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.vaadin.addons.demo.helpers;
package org.vaadin.haijian.demo.helpers;

public class AgeGroup {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.vaadin.addons.demo.helpers;
package org.vaadin.haijian.demo.helpers;

import com.vaadin.data.ValueProvider;
import com.vaadin.data.provider.CallbackDataProvider;
import com.vaadin.data.provider.ConfigurableFilterDataProvider;
import com.vaadin.data.provider.DataProvider;
Expand Down Expand Up @@ -69,7 +70,8 @@ private static Component createGridDemo(boolean lazyLoading) {
ExporterOption exporterOption = new ExporterOption();
exporterOption.getColumnOption("name").columnName("My Name").toUpperCase();
exporterOption.getColumnOption("email").columnName("My Email");
exporterOption.getColumnOption("age").toUpperCase();
exporterOption.getColumnOption("age").headerProviderFunction(value -> "Current " + value + " x10");
exporterOption.getColumnOption("age").valueProviderFunction(source -> {return (int) ((Person)source).getAge() * 10;});

StreamResource excelStreamResourceWithCustomHeader = new StreamResource((StreamResource.StreamSource) () -> Exporter.exportAsExcel(grid, exporterOption), "my-excel-with-cutom-header.xlsx");
FileDownloader excelFileDownloaderWithCustomHeader = new FileDownloader(excelStreamResourceWithCustomHeader);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.vaadin.addons.demo.helpers;
package org.vaadin.haijian.demo.helpers;

import java.time.LocalDate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.vaadin.addons.demo.helpers;
package org.vaadin.haijian.demo.helpers;

import com.vaadin.data.provider.QuerySortOrder;
import com.vaadin.shared.data.sort.SortDirection;
Expand Down