Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexing order of Headers at annotations to write #53

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
String value() default "";

int index() default -1;

/**
* Setting this to <code>false</code> will enable the null check on the Column values, to ensure
* non-null values for the field.
Expand Down
71 changes: 61 additions & 10 deletions src/main/java/io/github/millij/poi/util/Spreadsheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -58,26 +59,28 @@ public static Map<String, String> getPropertyToColumnNameMap(Class<?> beanType)
// Fields
Field[] fields = beanType.getDeclaredFields();
for (Field f : fields) {
String fieldName = f.getName();
mapping.put(fieldName, fieldName);
if (!f.isAnnotationPresent(SheetColumn.class)) {
continue;
}

final String fieldName = f.getName();
SheetColumn ec = f.getAnnotation(SheetColumn.class);
if (ec != null && StringUtils.isNotEmpty(ec.value())) {
mapping.put(fieldName, ec.value());
if (ec != null) {
mapping.put(fieldName, StringUtils.isBlank(ec.value()) ? fieldName : ec.value());
}
}

// Methods
Method[] methods = beanType.getDeclaredMethods();
for (Method m : methods) {
String fieldName = Beans.getFieldName(m);
if (!mapping.containsKey(fieldName)) {
mapping.put(fieldName, fieldName);
if (!m.isAnnotationPresent(SheetColumn.class)) {
continue;
}

final String fieldName = Beans.getFieldName(m);
SheetColumn ec = m.getAnnotation(SheetColumn.class);
if (ec != null && StringUtils.isNotEmpty(ec.value())) {
mapping.put(fieldName, ec.value());
if (ec != null) {
mapping.put(fieldName, StringUtils.isBlank(ec.value()) ? fieldName : ec.value());
}
}

Expand All @@ -103,12 +106,60 @@ public static List<String> getColumnNames(Class<?> beanType) {
// Bean Property to Column Mapping
final Map<String, String> propToColumnMap = getPropertyToColumnNameMap(beanType);

final Map<Integer, String> indexToPropMap = getIndexToPropertyMap(beanType);

if (propToColumnMap.size() == indexToPropMap.size()) {

final Set<Integer> indexSet = indexToPropMap.keySet();
List<Integer> indexList = new ArrayList<Integer>(indexSet);
Collections.sort(indexList);

List<String> indexedColumns = new ArrayList<String>();
for (Integer index : indexList) {
indexedColumns.add(propToColumnMap.get(indexToPropMap.get(index)));
if (index == -1) {
LOGGER.info("Writing One field : '{}' at first column as no index specified",
propToColumnMap.get(indexToPropMap.get(index)));
}
}
return indexedColumns;
}


LOGGER.info("Failed to write headers in partially indexed order. Proceeded to write in random order");
final ArrayList<String> columnNames = new ArrayList<>(propToColumnMap.values());
return columnNames;
}



public static Map<Integer, String> getIndexToPropertyMap(Class<?> beanClz) {

Map<Integer, String> indexToPropMap = new HashMap<Integer, String>();

Field[] fields = beanClz.getDeclaredFields();
for (Field f : fields) {
if (!f.isAnnotationPresent(SheetColumn.class)) {
continue;
}
final String fieldName = f.getName();
SheetColumn ec = f.getDeclaredAnnotation(SheetColumn.class);
indexToPropMap.put(ec.index(), fieldName);
}

Method[] methods = beanClz.getDeclaredMethods();
for (Method m : methods) {
if (!m.isAnnotationPresent(SheetColumn.class)) {
continue;
}
final String fieldName = Beans.getFieldName(m);
SheetColumn ec = m.getDeclaredAnnotation(SheetColumn.class);
indexToPropMap.put(ec.index(), fieldName);
}

return indexToPropMap;
}



// Read from Bean : as Row Data
// ------------------------------------------------------------------------
Expand Down
98 changes: 98 additions & 0 deletions src/test/java/io/github/millij/bean/EmployeeIndexed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.github.millij.bean;

import io.github.millij.poi.ss.model.annotations.Sheet;
import io.github.millij.poi.ss.model.annotations.SheetColumn;


@Sheet
public class EmployeeIndexed {
private String id;
private String name;

@SheetColumn(value = "Age",index = 2)
private Integer age;

@SheetColumn(value = "Gender", index = 3)
private String gender;

@SheetColumn(value = "Height (mts)", index = 4)
private Double height;

@SheetColumn(value = "Address", index = 5)
private String address;


public EmployeeIndexed() {
// Default
}

public EmployeeIndexed(String id, String name, Integer age, String gender, Double height, String address) {
super();

this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
this.address = address;

}



@SheetColumn(value = "ID", index = 0)
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@SheetColumn(value = "Name", index = 1)
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public Double getHeight() {
return height;
}

public void setHeight(Double height) {
this.height = height;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Emp_indexed [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", height=" + height
+ ", address=" + address + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.millij.poi.ss.writer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.millij.bean.Employee;
import io.github.millij.bean.EmployeeIndexed;
import io.github.millij.poi.ss.model.annotations.SheetColumn;
import io.github.millij.poi.ss.writer.SpreadsheetWriter;
import io.github.millij.poi.ss.writer.SpreadsheetWriterTest;


public class IndexedHeadersWriterTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SpreadsheetWriterTest.class);

private final String _path_test_output = "test-cases/output/";



@Test
public void test_write_xlsx_single_sheet() throws IOException {
final String filepath_output_file = _path_test_output.concat("indexed_header_writer_sample.xlsx");

// Excel Writer
LOGGER.info("test_write_xlsx_single_sheet :: Writing to file - {}", filepath_output_file);
SpreadsheetWriter gew = new SpreadsheetWriter(filepath_output_file);

// Employees
List<EmployeeIndexed> employees = new ArrayList<EmployeeIndexed>();
employees.add(new EmployeeIndexed("1", "foo", 12, "MALE", 1.68, "Chennai"));
employees.add(new EmployeeIndexed("2", "bar", 24, "MALE", 1.98, "Banglore"));
employees.add(new EmployeeIndexed("3", "foo bar", 10, "FEMALE", 2.0, "Kolkata"));

// Write
gew.addSheet(EmployeeIndexed.class, employees);
gew.write();
}

}