Skip to content

Commit

Permalink
fix: Do not generate row when all pivot values are empty (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: Romuald Rousseau <[email protected]>
  • Loading branch information
RomualdRousseau and Romuald Rousseau authored Nov 15, 2024
1 parent 9c220ae commit 893a68c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ public DataFrame getRows() throws IOException {
public void startElement(final String uri, final String localName, final String name,
final Attributes attributes) {
if ("row".equals(name)) {
assert (attributes.getValue("r") != null) : "Row malformed without ref";
this.fillMissingRows(Integer.valueOf(attributes.getValue("r")) - 1);
this.row = new ArrayList<String>();
this.prevCell = null;
this.currCell = null;
if (attributes.getValue("r") != null) {
if (attributes.getValue("r") != null) {
this.fillMissingRows(Integer.valueOf(attributes.getValue("r")) - 1);
}
this.row = new ArrayList<String>();
this.prevCell = null;
this.currCell = null;
}
} else if ("c".equals(name)) {
assert (attributes.getValue("r") != null) : "Cell malformed without ref";
this.prevCell = this.currCell;
this.currCell = new Cell();
this.currCell.address = new CellAddress(attributes.getValue("r"));
this.currCell.address = (attributes.getValue("r") != null)
? new CellAddress(attributes.getValue("r"))
: CellAddress.A1;
this.currCell.type = this.getCellTypeFromString(attributes.getValue("t"));
this.currCell.style = this.getCellStyleFromString(attributes.getValue("s"));
} else if ("v".equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.romualdrousseau.archery.intelli;

import java.util.List;
import java.util.Optional;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -127,57 +128,69 @@ private List<Row> buildRowsForOneRow(final BaseTableGraph graph, final DataTable
}

if (pivot == null) {
newRows.add(buildOneRowWithoutPivot(graph, orgTable, orgRow, rowGroup));
buildOneRowWithoutPivot(graph, orgTable, orgRow, rowGroup).ifPresent(newRows::add);
} else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE_AND_VALUE) {
for (final var value : pivot.getEntryValues()) {
newRows.add(buildOneRowWithPivotTypeAndValue(graph, orgTable, orgRow, pivot, value, rowGroup));
}
pivot.getEntryValues()
.forEach(x -> buildOneRowWithPivotTypeAndValue(graph, orgTable, orgRow, pivot, x, rowGroup)
.ifPresent(newRows::add));
} else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE) {
for (final var pivotEntry : pivot.getEntries()) {
if (orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) {
newRows.add(buildOneRowWithPivotAndType(graph, orgTable, orgRow, pivotEntry, rowGroup));
}
}
pivot.getEntries()
.forEach(x -> buildOneRowWithPivotAndType(graph, orgTable, orgRow, x, rowGroup)
.ifPresent(newRows::add));
} else {
for (final var pivotEntry : pivot.getEntries()) {
if (orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) {
newRows.add(buildOneRowWithPivot(graph, orgTable, orgRow, pivotEntry, rowGroup));
}
}
pivot.getEntries()
.forEach(x -> buildOneRowWithPivot(graph, orgTable, orgRow, x, rowGroup)
.ifPresent(newRows::add));
}
return newRows;
}

private Row buildOneRowWithoutPivot(final BaseTableGraph graph, final DataTable orgTable,
private Optional<Row> buildOneRowWithoutPivot(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final RowGroup rowGroup) {
final var newRow = new Row(this.tmpHeaders.size());
for (final var abstractHeader : this.tmpHeaders) {
final var orgHeaders = orgTable.findAllHeaders(abstractHeader);
this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow);
}
return newRow;
return Optional.of(newRow);
}

private Row buildOneRowWithPivot(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final PivotEntry pivotEntry, final RowGroup rowGroup) {
private Optional<Row> buildOneRowWithPivotTypeAndValue(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final PivotKeyHeader pivot, final String value, final RowGroup rowGroup) {
final var newRow = new Row(this.tmpHeaders.size());
boolean hasPivotedValues = false;
for (final var abstractHeader : this.tmpHeaders) {
final var orgHeaders = orgTable.findAllHeaders(abstractHeader);
if (abstractHeader instanceof PivotKeyHeader) {
if (orgHeaders.size() > 0) {
newRow.set(abstractHeader.getColumnIndex() + 0, pivotEntry.getCell().getValue());
newRow.set(abstractHeader.getColumnIndex() + 1,
orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).getValue());
newRow.set(abstractHeader.getColumnIndex(), value);
int i = 1;
for (final var typeValue : pivot.getEntryTypes()) {
final var ci = abstractHeader.getColumnIndex() + i;
hasPivotedValues |= pivot.getEntries().stream()
.filter(x -> x.getValue().equals(value) && x.getTypeValue().equals(typeValue))
.findFirst()
.map(x -> {
newRow.set(ci, orgRow.getCellAt(x.getCell().getColumnIndex()).getValue());
return orgRow.getCellAt(x.getCell().getColumnIndex()).hasValue();
})
.orElse(false);

i++;
}
}
} else {
this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow);
}
}
return newRow;
return hasPivotedValues ? Optional.of(newRow) : Optional.empty();
}

private Row buildOneRowWithPivotAndType(final BaseTableGraph graph, final DataTable orgTable,
private Optional<Row> buildOneRowWithPivotAndType(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final PivotEntry pivotEntry, final RowGroup rowGroup) {
if (!orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) {
return Optional.empty();
}
final var newRow = new Row(this.tmpHeaders.size());
for (final var abstractHeader : this.tmpHeaders) {
final var orgHeaders = orgTable.findAllHeaders(abstractHeader);
Expand All @@ -192,33 +205,28 @@ private Row buildOneRowWithPivotAndType(final BaseTableGraph graph, final DataTa
this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow);
}
}
return newRow;
return Optional.of(newRow);
}

private Row buildOneRowWithPivotTypeAndValue(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final PivotKeyHeader pivot, final String value, final RowGroup rowGroup) {
private Optional<Row> buildOneRowWithPivot(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final PivotEntry pivotEntry, final RowGroup rowGroup) {
if (!orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).hasValue()) {
return Optional.empty();
}
final var newRow = new Row(this.tmpHeaders.size());
for (final var abstractHeader : this.tmpHeaders) {
final var orgHeaders = orgTable.findAllHeaders(abstractHeader);
if (abstractHeader instanceof PivotKeyHeader) {
if (orgHeaders.size() > 0) {
newRow.set(abstractHeader.getColumnIndex(), value);
int i = 1;
for (final var typeValue : pivot.getEntryTypes()) {
final var ci = abstractHeader.getColumnIndex() + i;
pivot.getEntries().stream()
.filter(x -> x.getValue().equals(value) && x.getTypeValue().equals(typeValue))
.findFirst()
.ifPresent(
x -> newRow.set(ci, orgRow.getCellAt(x.getCell().getColumnIndex()).getValue()));
i++;
}
newRow.set(abstractHeader.getColumnIndex() + 0, pivotEntry.getCell().getValue());
newRow.set(abstractHeader.getColumnIndex() + 1,
orgRow.getCellAt(pivotEntry.getCell().getColumnIndex()).getValue());
}
} else {
this.generateCells(graph, orgHeaders, abstractHeader, rowGroup, orgRow, newRow);
}
}
return newRow;
return Optional.of(newRow);
}

private void generateCells(final BaseTableGraph graph, final List<BaseHeader> orgHeaders,
Expand All @@ -244,7 +252,8 @@ private void generateCells(final BaseTableGraph graph, final List<BaseHeader> or
}
}

private void generateCell(final BaseHeader abstractHeader, final String prevValue, final String currValue, final Row newRow) {
private void generateCell(final BaseHeader abstractHeader, final String prevValue, final String currValue,
final Row newRow) {
abstractHeader.setColumnEmpty(abstractHeader.isColumnEmpty() && StringUtils.isFastBlank(currValue));
if (prevValue == null) {
newRow.set(abstractHeader.getColumnIndex(), currValue);
Expand Down

0 comments on commit 893a68c

Please sign in to comment.