Skip to content

Commit

Permalink
create an utility class for annotation features
Browse files Browse the repository at this point in the history
Signed-off-by: Hakan <ozler.hakan@gmail.com>
  • Loading branch information
ozlerhakan committed May 1, 2020
1 parent 2abca12 commit 2f442d1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 45 deletions.
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ As of 2.7.0, we can observe each cell format of a given excel file. Assume that
|12/31/2020 12.00 AM
|===

We can get all list of cell formats using `PoijiLogCellFormat` with `PoijiOptions`:
We can get all the list of cell formats using `PoijiLogCellFormat` with `PoijiOptions`:

----
PoijiLogCellFormat log = new PoijiLogCellFormat();
Expand All @@ -641,19 +641,19 @@ Hmm, It looks like we did not achieve the correct date format when we get the da

----
List<InternalCellFormat> formats = log.formats();
InternalCellFormat cell00 = formats.get(0);
InternalCellFormat cell10 = formats.get(1);
cell00.getFormatString()
cell10.getFormatString()
// mm:ss.0
cell00.getFormatIndex()
cell10.getFormatIndex()
// 47
----

Now that we know the reason of why we don't see the expected date value, it's because the default format of the date cell is the `mm:ss.0` format with a given index 47, we need to change the default format of index (i.e. `47`). This format was automatically assigned to the cell having a number, but almost certainly with a special style or format. Note that this option should be used for debugging purpose only.

=== Feature 11

Using 2.7.0, we can change the default format of a cell using `PoijiNumberFormat`. Recall `feature 10`, we are unable to see the correct cell format besides the excel file uses another format which we do not want to.
Using 2.7.0, we can change the default format of a cell using `PoijiNumberFormat`. Recall `feature 10`, we are unable to see the correct cell format what's more the excel file uses another format which we do not want to.

|===
|Date
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/com/poiji/bind/Poiji.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package com.poiji.bind;

import com.poiji.annotation.ExcelCellName;
import com.poiji.bind.mapping.UnmarshallerHelper;
import com.poiji.exception.HeaderMissingException;
import com.poiji.exception.IllegalCastException;
import com.poiji.exception.InvalidExcelFileExtension;
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
import com.poiji.util.Files;
import com.poiji.util.ReflectUtil;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static com.poiji.util.PoijiConstants.XLSX_EXTENSION;
import static com.poiji.util.PoijiConstants.XLS_EXTENSION;
Expand Down Expand Up @@ -241,33 +234,4 @@ private static Unmarshaller deserializer(final InputStream inputStream, PoijiExc
}
}

/**
* Validate that all headers specified via @ExcelCellName annotations are present in the list of header names.
* <p>
* Validation is only performed if it is set in the PoijiOptions
*
* @throws HeaderMissingException if one or more headers are missing
*/
public static <T> void validateMandatoryNameColumns(PoijiOptions options,
Class<T> modelType,
Collection<String> headerNames) {
if (options.getNamedHeaderMandatory()) {
Collection<ExcelCellName> excelCellNames = ReflectUtil.findRecursivePoijiAnnotations(modelType,
ExcelCellName.class);

BiPredicate<String, String> comparator = options.getCaseInsensitive()
? String::equalsIgnoreCase
: String::equals;

Set<String> missingHeaders = excelCellNames.stream()
.filter(excelCellName -> headerNames.stream()
.noneMatch(title -> comparator.test(excelCellName.value(), title)))
.map(ExcelCellName::value)
.collect(Collectors.toSet());

if (!missingHeaders.isEmpty()) {
throw new HeaderMissingException("Some headers are missing in the sheet: " + missingHeaders);
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.poiji.annotation.ExcelCellRange;
import com.poiji.annotation.ExcelRow;
import com.poiji.annotation.ExcelUnknownCells;
import com.poiji.bind.Poiji;
import com.poiji.bind.Unmarshaller;
import com.poiji.config.Casting;
import com.poiji.exception.IllegalCastException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
Expand Down Expand Up @@ -68,7 +68,7 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
int maxPhysicalNumberOfRows = sheet.getPhysicalNumberOfRows() + 1 - skip;

loadColumnTitles(sheet, maxPhysicalNumberOfRows);
Poiji.validateMandatoryNameColumns(options, type, columnIndexPerTitle.keySet());
AnnotationUtil.validateMandatoryNameColumns(options, type, columnIndexPerTitle.keySet());

for (Row currentRow : sheet) {
if (!skip(currentRow, skip) && !isRowEmpty(currentRow)) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.poiji.annotation.ExcelCellRange;
import com.poiji.annotation.ExcelRow;
import com.poiji.annotation.ExcelUnknownCells;
import com.poiji.bind.Poiji;
import com.poiji.config.Casting;
import com.poiji.exception.IllegalCastException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
Expand Down Expand Up @@ -246,6 +246,6 @@ public void headerFooter(String text, boolean isHeader, String tagName) {

@Override
public void endSheet() {
Poiji.validateMandatoryNameColumns(options, type, columnIndexPerTitle.keySet());
AnnotationUtil.validateMandatoryNameColumns(options, type, columnIndexPerTitle.keySet());
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/poiji/util/AnnotationUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.poiji.util;

import com.poiji.annotation.ExcelCellName;
import com.poiji.exception.HeaderMissingException;
import com.poiji.option.PoijiOptions;

import java.util.Collection;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;

/**
* Created by hakan on 2.05.2020
*/
public class AnnotationUtil {


/**
* Validate that all headers specified via @ExcelCellName annotations are present in the list of header names.
* <p>
* Validation is only performed if it is set in the PoijiOptions
*
* @throws HeaderMissingException if one or more headers are missing
*/
public static <T> void validateMandatoryNameColumns(PoijiOptions options,
Class<T> modelType,
Collection<String> headerNames) {
if (options.getNamedHeaderMandatory()) {
Collection<ExcelCellName> excelCellNames = ReflectUtil.findRecursivePoijiAnnotations(modelType,
ExcelCellName.class);

BiPredicate<String, String> comparator = options.getCaseInsensitive()
? String::equalsIgnoreCase
: String::equals;

Set<String> missingHeaders = excelCellNames.stream()
.filter(excelCellName -> headerNames.stream()
.noneMatch(title -> comparator.test(excelCellName.value(), title)))
.map(ExcelCellName::value)
.collect(Collectors.toSet());

if (!missingHeaders.isEmpty()) {
throw new HeaderMissingException("Some headers are missing in the sheet: " + missingHeaders);
}
}
}
}

0 comments on commit 2f442d1

Please sign in to comment.