Skip to content

Commit

Permalink
Merge 673974b into 12283d9
Browse files Browse the repository at this point in the history
  • Loading branch information
vaa25 committed Feb 8, 2020
2 parents 12283d9 + 673974b commit c28073c
Show file tree
Hide file tree
Showing 27 changed files with 1,153 additions and 58 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/poiji/annotation/ExcelCellName.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,31 @@
@Documented
public @interface ExcelCellName {

int ABSENT_ORDER = -1;

/**
* Specifies the column name where the corresponding value is mapped from the excel data
*
* @return column name
*/
String value();

/**
* Specifies the column order in saved file
*
* @return column order
*/
int order() default ABSENT_ORDER;

/**
* Delimeter for column multiname.
* <p>
* Example: @ExcelCellName(value = "id,identifier", delimerer = ",")
* reading: column with name 'id' will be mapped into field, or if no column 'id',
* then column 'identifier' will be mapped into field.
* writing: field will be saved into column 'id'
*
* @return delimeter for column multiname.
*/
String delimeter() default "";
}
20 changes: 14 additions & 6 deletions src/main/java/com/poiji/bind/Poiji.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
import com.poiji.save.FileSaverFactory;
import com.poiji.util.Files;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -207,17 +207,25 @@ public static synchronized <T> List<T> fromExcel(final InputStream inputStream,
* language access control and the underlying field is either inaccessible or final.
* @see Poiji#fromExcel(File, Class)
*/
public static synchronized <T> void fromExcel(final InputStream inputStream,
final PoijiExcelType excelType,
final Class<T> type,
final PoijiOptions options,
final Consumer<? super T> consumer) {
public static synchronized <T> void fromExcel(final InputStream inputStream, final PoijiExcelType excelType,
final Class<T> type, final PoijiOptions options, final Consumer<? super T> consumer
) {
Objects.requireNonNull(excelType);

final Unmarshaller unmarshaller = deserializer(inputStream, excelType, options);
unmarshaller.unmarshal(type, consumer);
}

public static <T> void toExcel(final File file, final Class<T> clazz, final List<T> data) {
toExcel(file, clazz, data, PoijiOptionsBuilder.settings().build());
}

public static <T> void toExcel(
final File file, final Class<T> clazz, final List<T> data, final PoijiOptions options
) {
new FileSaverFactory<>(clazz, options).toFile(file).save(data);
}

private static Unmarshaller deserializer(final File file, final PoijiOptions options) {
final PoijiFile<?> poijiFile = new PoijiFile<>(file);

Expand Down
35 changes: 24 additions & 11 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
import com.poiji.exception.IllegalCastException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.ReflectUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -28,6 +21,12 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import static java.lang.String.valueOf;

Expand Down Expand Up @@ -196,15 +195,29 @@ private Integer getFieldColumn(final Field field) {
} else {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
column = columnIndexPerTitle.get(titleName);
column = getFieldColumnFromExcelCellName(excelCellName);
}
}
return column;
}

private Integer getFieldColumnFromExcelCellName(final ExcelCellName excelCellName) {
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
if (excelCellName.delimeter().isEmpty()) {
return columnIndexPerTitle.get(titleName);
} else {
final String[] possibleTitles = titleName.split(excelCellName.delimeter());
for (final String possibleTitle : possibleTitles) {
if (columnIndexPerTitle.containsKey(possibleTitle)) {
return columnIndexPerTitle.get(possibleTitle);
}
}
}
return null;
}

private <T> void constructTypeValue(Row currentRow, T instance, Field field, int column) {
Cell cell = currentRow.getCell(column);

Expand Down
34 changes: 23 additions & 11 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import com.poiji.exception.IllegalCastException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.ReflectUtil;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

import static java.lang.String.valueOf;

Expand Down Expand Up @@ -169,14 +168,10 @@ private boolean setValue(Field field, int column, String content, Object ins) {
} else {
ExcelCellName excelCellName = field.getAnnotation(ExcelCellName.class);
if (excelCellName != null) {
Class<?> fieldType = field.getType();
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
final Integer titleColumn = columnIndexPerTitle.get(titleName);
final Integer titleColumn = getFieldColumnFromExcelCellName(excelCellName);
//Fix both columns mapped to name passing this condition below
if (titleColumn != null && titleColumn == column) {
Object o = casting.castValue(fieldType, content, options);
if (titleColumn != null && titleColumn.equals(column)) {
Object o = casting.castValue(field.getType(), content, options);
setFieldData(field, o, ins);
return true;
}
Expand All @@ -185,6 +180,23 @@ private boolean setValue(Field field, int column, String content, Object ins) {
return false;
}

private Integer getFieldColumnFromExcelCellName(final ExcelCellName excelCellName) {
final String titleName = options.getCaseInsensitive()
? excelCellName.value().toLowerCase()
: excelCellName.value();
if (excelCellName.delimeter().isEmpty()) {
return columnIndexPerTitle.get(titleName);
} else {
final String[] possibleTitles = titleName.split(excelCellName.delimeter());
for (final String possibleTitle : possibleTitles) {
if (columnIndexPerTitle.containsKey(possibleTitle)) {
return columnIndexPerTitle.get(possibleTitle);
}
}
}
return null;
}

private void setFieldData(Field field, Object o, Object instance) {
try {
field.setAccessible(true);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/poiji/bind/mapping/SheetNameExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import com.poiji.annotation.ExcelSheet;
import com.poiji.option.PoijiOptions;

import java.util.Optional;

/**
* Utility class to extract the sheet name.
*/
class SheetNameExtractor {
public class SheetNameExtractor {
/**
* Extracts the sheet name from either the annotated value {@link ExcelSheet} from the model class or from the sheet name set
* in the Poiji Options. Poiji first looks at {@link ExcelSheet} then {@link PoijiOptions}.
* @param type The class instance of the object model.
*
* @param type The class instance of the object model.
* @param options The Poiji options.
* @param <T> The type of the object model.
* @param <T> The type of the object model.
* @return an Optional sheet name
*/
public static <T> Optional<String> getSheetName(Class<T> type, PoijiOptions options) {
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/poiji/config/DefaultCasting.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.poiji.config;

import com.poiji.option.PoijiOptions;
import com.poiji.parser.Parsers;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -13,9 +15,6 @@
import java.util.Date;
import java.util.List;

import com.poiji.option.PoijiOptions;
import com.poiji.parser.Parsers;

/**
* Created by hakan on 22/01/2017.
*/
Expand Down Expand Up @@ -222,9 +221,24 @@ public Object castValue(Class<?> fieldType, String rawValue, int row, int col, P
} else if (fieldType == Float.class) {
o = floatValue(value, sheetName, row, col, options);

} else if (fieldType == boolean.class || fieldType == Boolean.class) {
} else if (fieldType == boolean.class) {
o = Boolean.valueOf(value);

} else if (fieldType == Boolean.class) {
o = value.isEmpty() ? options.preferNullOverDefault() ? null : false : Boolean.valueOf(value);

} else if (fieldType == byte.class) {
o = Byte.valueOf(value);

} else if (fieldType == Byte.class) {
o = value.isEmpty() ? options.preferNullOverDefault() ? null : (byte) 0 : Byte.valueOf(value);

} else if (fieldType == short.class) {
o = Short.valueOf(value);

} else if (fieldType == Short.class) {
o = value.isEmpty() ? options.preferNullOverDefault() ? null : (short) 0 : Short.valueOf(value);

} else if (fieldType == Date.class) {
o = dateValue(value, sheetName, row, col, options);

Expand Down
Loading

0 comments on commit c28073c

Please sign in to comment.