Skip to content

Commit

Permalink
Provide useful error output when there are duplicate headers
Browse files Browse the repository at this point in the history
  • Loading branch information
QuincyCantu committed Nov 13, 2024
1 parent 1ff1880 commit 42cda5c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.swing.JPanel;
Expand Down Expand Up @@ -44,6 +43,14 @@ public void setHeaderOptions(String[] headerOptions) {
this.headerOptions = Arrays.stream(headerOptions)
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toSet()).stream().sorted(Comparator.comparing(String::new, String.CASE_INSENSITIVE_ORDER)).toArray(String[]::new);

List<String> headerList = List.of(headerOptions);
for (int i =0; i < this.headerOptions.length; i++) {
if (Collections.frequency(headerList, headerOptions[i]) > 1){
throw new IllegalArgumentException("Duplicate header option: " + headerOptions[i]);
}
}

this.headerOptions = ArrayUtils.addFirst(this.headerOptions, null);
updateHeaderOptions(this.headerOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ yield postProcessStream(
saveAction,
successAction
);
} catch (IOException e) {
} catch (IOException | IllegalArgumentException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
yield Collections.emptyList();
}
Expand Down Expand Up @@ -285,28 +285,29 @@ private boolean isProcessableRow(MapWithRowNumber rowNumber) {
);
}

private List<ObjectWithRowError<O>> postProcessStream(Stream<ObjectWithRowError<O>> stream, Function<ObjectWithRowError<O>, ObjectWithRowError<O>> saveAction, Runnable successAction) {
private List<ObjectWithRowError<O>> postProcessStream(Stream<ObjectWithRowError<O>> stream, Function<ObjectWithRowError<O>, ObjectWithRowError<O>> saveAction, Runnable successAction)
throws IllegalArgumentException {
List<ObjectWithRowError<O>> exceptions = new ArrayList<>(0);
stream.peek(o -> {
Throwable exception = o.throwable();
if (exception != null) {
if (exception.getSuppressed().length == 0) {
exceptions.add(new ObjectWithRowError<>(o.object(), o.row(), exception));
} else {
exceptions.addAll(
Arrays.stream(exception.getSuppressed())
.map(throwable -> new ObjectWithRowError<>(o.object(), o.row(), throwable))
.toList()
);
}
}
}).filter(o -> Objects.nonNull(o.object()) && Objects.isNull(o.throwable()))
.map(saveAction)
.filter(objectWithRowConversionException -> Objects.nonNull(objectWithRowConversionException.throwable()))
.forEach(exceptions::add);
Throwable exception = o.throwable();
if (exception != null) {
if (exception.getSuppressed().length == 0) {
exceptions.add(new ObjectWithRowError<>(o.object(), o.row(), exception));
} else {
exceptions.addAll(
Arrays.stream(exception.getSuppressed())
.map(throwable -> new ObjectWithRowError<>(o.object(), o.row(), throwable))
.toList()
);
}
}
}).filter(o -> Objects.nonNull(o.object()) && Objects.isNull(o.throwable()))
.map(saveAction)
.filter(objectWithRowConversionException -> Objects.nonNull(objectWithRowConversionException.throwable()))
.forEach(exceptions::add);

successAction.run();

return exceptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -23,7 +24,7 @@ public class ExcelReader {
* @return Stream of map with row number objects
* @throws IOException thrown in case of error reading from Excel file
*/
public static Stream<MapWithRowNumber> read(InputStream inputStream, int sheetIndex) throws IOException {
public static Stream<MapWithRowNumber> read(InputStream inputStream, int sheetIndex) throws IOException, IllegalArgumentException {
try (ReadableWorkbook workbook = new ReadableWorkbook(inputStream)) {
return workbook.getSheet(sheetIndex).map(
sheet -> {
Expand All @@ -46,6 +47,11 @@ public static Stream<MapWithRowNumber> read(InputStream inputStream, int sheetIn
}

private static MapWithRowNumber rowToPropertyMap(Row row, List<String> headers) {
for (int i =0; i < headers.size(); i++) {
if (Collections.frequency(headers, headers.get(i)) > 1){
throw new IllegalArgumentException("Duplicate header option: " + headers.get(i));
}
}
return new MapWithRowNumber(
IntStream.range(0, headers.size()).boxed().collect(Collectors.toMap(
headers::get,
Expand Down

0 comments on commit 42cda5c

Please sign in to comment.