Skip to content

Commit

Permalink
feat: Add a new header type and process it in IntelliTable
Browse files Browse the repository at this point in the history
  • Loading branch information
Romuald Rousseau committed Nov 20, 2024
1 parent 893a68c commit 9deb89f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.github.romualdrousseau.archery.commons.python;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class PythonSimpleDateFormat extends SimpleDateFormat {

private final Locale locale;

public PythonSimpleDateFormat() {
super();
this("", Locale.US);
}

public PythonSimpleDateFormat(final String pattern) {
super(PythonSimpleDateFormat.toJava(pattern));
}

public PythonSimpleDateFormat(final String pattern, DateFormatSymbols formatSymbols) {
super(PythonSimpleDateFormat.toJava(pattern), formatSymbols);
this(pattern, PythonSimpleDateFormat.toJavaLocale(pattern));
}

public PythonSimpleDateFormat(final String pattern, Locale locale) {
super(PythonSimpleDateFormat.toJava(pattern), locale);
this.locale = locale;
}

public static String toPython(final String javaPattern) {
Expand Down Expand Up @@ -68,7 +66,6 @@ public static String toJava(final String pythonPattern) {
.replaceAll("%w", "u")
.replaceAll("%u", "u")
.replaceAll("%U", "ww")
.replaceAll("%V", "ww")
.replaceAll("%H", "HH")
.replaceAll("%-H", "H")
.replaceAll("%I", "hh")
Expand All @@ -78,4 +75,16 @@ public static String toJava(final String pythonPattern) {
.replaceAll("%S", "ss")
.replaceAll("%-S", "s");
}

public static Locale toJavaLocale(final String pythonPattern) {
if (pythonPattern.contains("%w") || pythonPattern.contains("%W")) {
return Locale.UK;
} else {
return Locale.US;
}
}

public Locale getLocale() {
return this.locale;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.romualdrousseau.archery.header;

import com.github.romualdrousseau.archery.base.BaseCell;
import com.github.romualdrousseau.archery.base.BaseHeader;
import com.github.romualdrousseau.archery.base.BaseTable;

public class DataTableTypeHeader extends DataTableHeader {

public DataTableTypeHeader(final BaseHeader parent) {
super(parent);
}

public DataTableTypeHeader(final BaseTable table, final BaseCell cell) {
super(table, cell);
}

@Override
public BaseHeader clone() {
return new DataTableTypeHeader(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public PivotEntry(final BaseCell cell, final String pivotEntityName) {
this.typeValue = cell.getSheet().getDocument().getModel().toEntityName(cell.getValue(), pivotEntityName);
}

private PivotEntry(final PivotEntry pivotEntry) {
this.cell = pivotEntry.cell;
this.pivotValue = pivotEntry.pivotValue;
this.typeValue = pivotEntry.typeValue;
}

public BaseCell getCell() {
return this.cell;
}
Expand All @@ -23,8 +29,13 @@ public String getTypeValue() {
return this.typeValue;
}

public void setTypeValue(final String typeValue) {
public PivotEntry setTypeValue(final String typeValue) {
this.typeValue = typeValue;
return this;
}

public PivotEntry clone() {
return new PivotEntry(this);
}

private final BaseCell cell;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public void updateValueName(final String newName) {
this.valueName = newName;
}

public PivotValueHeader getPivotValue() {
return new PivotValueHeader(this, this.valueName);
}

public PivotTypeHeader getPivotType() {
return new PivotTypeHeader(this, this.valueName);
}

public PivotValueHeader getPivotValue() {
return new PivotValueHeader(this, this.valueName);
}

public void addEntry(final BaseCell entry) {
this.entries.add(new PivotEntry(entry, this.getPivotEntityAsString().get()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.HashSet;

import com.github.romualdrousseau.archery.PivotOption;
import com.github.romualdrousseau.archery.base.BaseHeader;
Expand All @@ -13,6 +15,7 @@
import com.github.romualdrousseau.archery.base.DataTable;
import com.github.romualdrousseau.archery.base.BaseRow;
import com.github.romualdrousseau.archery.base.RowGroup;
import com.github.romualdrousseau.archery.header.DataTableTypeHeader;
import com.github.romualdrousseau.archery.header.PivotEntry;
import com.github.romualdrousseau.archery.header.PivotKeyHeader;
import com.github.romualdrousseau.archery.commons.collections.DataFrame;
Expand All @@ -24,6 +27,11 @@ public class IntelliTable extends DataTable {

private static final int BATCH_SIZE = 10000;

private final ArrayList<BaseHeader> tmpHeaders = new ArrayList<>();
private final DataFrameWriter writer;
private final DataFrame rows;
private final Set<String> typesValues = new HashSet<>();

public IntelliTable(final BaseSheet sheet, final BaseTableGraph root, final boolean headerAutoNameEnabled) {
super(sheet);

Expand All @@ -35,9 +43,11 @@ public IntelliTable(final BaseSheet sheet, final BaseTableGraph root, final bool

try {
this.writer = new DataFrameWriter(BATCH_SIZE, this.tmpHeaders.size());
final var pivot = this.findPivotHeader();
final var pivotHeader = this.findPivotHeader();
final var typeHeader = this.findDataTableTypeHeader();
root.parseIf(
e -> this.buildRowsForOneTable((BaseTableGraph) e, (DataTable) e.getTable(), pivot),
e -> this.buildRowsForOneTable((BaseTableGraph) e, (DataTable) e.getTable(), pivotHeader,
typeHeader),
e -> e instanceof BaseTableGraph && e.getTable() instanceof DataTable);
this.setLoadCompleted(true);
this.rows = this.writer.getDataFrame();
Expand Down Expand Up @@ -83,9 +93,8 @@ private void addTmpHeader(final BaseHeader header) {
final var pivot = (PivotKeyHeader) header;
if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE_AND_VALUE) {
this.addHeaderIntoTmpHeaders(pivot.clone(), false);
for (final String typeValue : pivot.getEntryTypes()) {
this.addHeaderIntoTmpHeaders(pivot.getPivotType().clone().setName(typeValue), false);
}
pivot.getEntryTypes()
.forEach(x -> this.addHeaderIntoTmpHeaders(pivot.getPivotType().clone().setName(x), false));
} else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE) {
this.addHeaderIntoTmpHeaders(pivot.clone(), false);
this.addHeaderIntoTmpHeaders(pivot.getPivotType().clone(), false);
Expand All @@ -100,18 +109,20 @@ private void addTmpHeader(final BaseHeader header) {
}

private void buildRowsForOneTable(final BaseTableGraph graph, final DataTable orgTable,
final PivotKeyHeader pivot) {
final PivotKeyHeader pivotHeader, final DataTableTypeHeader typeHeader) {
if (orgTable.getNumberOfRowGroups() == 0) {
for (final var orgRow : orgTable.rows()) {
final var newRows = buildRowsForOneRow(graph, orgTable, (BaseRow) orgRow, pivot, null);
final var newRows = buildRowsForOneRow(graph, orgTable, (BaseRow) orgRow, pivotHeader, typeHeader,
null);
this.addRows(newRows);
}
} else {
for (final RowGroup rowGroup : orgTable.rowGroups()) {
for (int i = 0; i < rowGroup.getNumberOfRows(); i++) {
if (rowGroup.getRow() + i < orgTable.getNumberOfRows()) {
final var orgRow = orgTable.getRowAt(rowGroup.getRow() + i);
final var newRows = buildRowsForOneRow(graph, orgTable, orgRow, pivot, rowGroup);
final var newRows = buildRowsForOneRow(graph, orgTable, orgRow, pivotHeader, typeHeader,
rowGroup);
this.addRows(newRows);
}
}
Expand All @@ -121,25 +132,27 @@ private void buildRowsForOneTable(final BaseTableGraph graph, final DataTable or

private List<Row> buildRowsForOneRow(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow,
final PivotKeyHeader pivot, final RowGroup rowGroup) {
final PivotKeyHeader pivotHeader, final DataTableTypeHeader typeHeader, final RowGroup rowGroup) {
final var newRows = new ArrayList<Row>();
if (orgRow.isIgnored()) {
return newRows;
}

if (pivot == null) {
buildOneRowWithoutPivot(graph, orgTable, orgRow, rowGroup).ifPresent(newRows::add);
if (pivotHeader == null) {
this.buildOneRowWithoutPivot(graph, orgTable, orgRow, rowGroup).ifPresent(newRows::add);
} else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE_AND_VALUE) {
pivot.getEntryValues()
.forEach(x -> buildOneRowWithPivotTypeAndValue(graph, orgTable, orgRow, pivot, x, rowGroup)
final var typeValue = this.findTypeValue(orgRow, pivotHeader, typeHeader);
pivotHeader.getEntryValues()
.forEach(x -> this
.buildOneRowWithPivotTypeAndValue(graph, orgTable, orgRow, pivotHeader, x, typeValue,
rowGroup)
.ifPresent(newRows::add));
} else if (this.getSheet().getPivotOption() == PivotOption.WITH_TYPE) {
pivot.getEntries()
.forEach(x -> buildOneRowWithPivotAndType(graph, orgTable, orgRow, x, rowGroup)
pivotHeader.getEntries()
.forEach(x -> this.buildOneRowWithPivotAndType(graph, orgTable, orgRow, x, rowGroup)
.ifPresent(newRows::add));
} else {
pivot.getEntries()
.forEach(x -> buildOneRowWithPivot(graph, orgTable, orgRow, x, rowGroup)
pivotHeader.getEntries()
.forEach(x -> this.buildOneRowWithPivot(graph, orgTable, orgRow, x, rowGroup)
.ifPresent(newRows::add));
}
return newRows;
Expand All @@ -156,7 +169,8 @@ private Optional<Row> buildOneRowWithoutPivot(final BaseTableGraph graph, final
}

private Optional<Row> buildOneRowWithPivotTypeAndValue(final BaseTableGraph graph, final DataTable orgTable,
final BaseRow orgRow, final PivotKeyHeader pivot, final String value, final RowGroup rowGroup) {
final BaseRow orgRow, final PivotKeyHeader pivotHeader, final String value, final String typeValue,
final RowGroup rowGroup) {
final var newRow = new Row(this.tmpHeaders.size());
boolean hasPivotedValues = false;
for (final var abstractHeader : this.tmpHeaders) {
Expand All @@ -165,17 +179,18 @@ private Optional<Row> buildOneRowWithPivotTypeAndValue(final BaseTableGraph grap
if (orgHeaders.size() > 0) {
newRow.set(abstractHeader.getColumnIndex(), value);
int i = 1;
for (final var typeValue : pivot.getEntryTypes()) {
for (final var tv : pivotHeader.getEntryTypes()) {
final var ci = abstractHeader.getColumnIndex() + i;
hasPivotedValues |= pivot.getEntries().stream()
.filter(x -> x.getValue().equals(value) && x.getTypeValue().equals(typeValue))
hasPivotedValues |= pivotHeader.getEntries().stream()
.filter(x -> x.getValue().equals(value)
&& (typeValue != null && tv.equals(typeValue)
|| typeValue == null && 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++;
}
}
Expand Down Expand Up @@ -271,13 +286,42 @@ private PivotKeyHeader findPivotHeader() {
return null;
}

private DataTableTypeHeader findDataTableTypeHeader() {
for (final var header : this.tmpHeaders) {
if (header instanceof DataTableTypeHeader) {
return (DataTableTypeHeader) header;
}
}
return null;
}
private String findTypeValue(final BaseRow orgRow, final PivotKeyHeader pivotHeader,
final DataTableTypeHeader typeHeader) {
if (typeHeader == null) {
return null;
}

final var typeValue = typeHeader.getCellAtRow(orgRow).getValue();
if (this.typesValues.contains(typeValue)) {
return typeValue;
}

this.typesValues.add(typeValue);

final var entries = new ArrayList<PivotEntry>();
this.typesValues.forEach(x -> {
pivotHeader.getEntries().stream().map(y -> y.clone().setTypeValue(x)).forEach(entries::add);
});
pivotHeader.getEntries().clear();
pivotHeader.getEntries().addAll(entries);

return typeValue;
}
private void addHeaderIntoTmpHeaders(final BaseHeader newHeader, final boolean columnEmpty) {
newHeader.setTable(this);
newHeader.setColumnIndex(this.tmpHeaders.size());
newHeader.setColumnEmpty(columnEmpty);
this.tmpHeaders.add(newHeader);
}

private void addRows(final List<Row> newRows) {
newRows.forEach(row -> {
try {
Expand All @@ -287,8 +331,4 @@ private void addRows(final List<Row> newRows) {
}
});
}

private final ArrayList<BaseHeader> tmpHeaders = new ArrayList<>();
private final DataFrameWriter writer;
private final DataFrame rows;
}

0 comments on commit 9deb89f

Please sign in to comment.