From 82cfaff77915118b1c9660ccf2cb236e3055cb6b Mon Sep 17 00:00:00 2001 From: Payam Meyer Date: Wed, 25 Sep 2024 09:42:08 -0400 Subject: [PATCH] Added append methods that accept custom styler. --- .../target/spreadsheet/SpreadsheetTarget.java | 66 +++++++++++++------ .../spreadsheet/SpreadsheetTargetConfig.java | 66 +++++++++++-------- .../target/spreadsheet/excel/ExcelTarget.java | 4 +- .../spreadsheet/excel/ExcelTargetConfig.java | 13 +++- .../excel/cell/BoldCellHandler.java | 30 +++++++++ .../zulia/data/test/DataTargetExamples.java | 6 +- 6 files changed, 133 insertions(+), 52 deletions(-) create mode 100644 zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/cell/BoldCellHandler.java diff --git a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTarget.java b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTarget.java index 30d28d69..df4da4b5 100644 --- a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTarget.java +++ b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTarget.java @@ -7,41 +7,69 @@ import java.util.Date; public abstract class SpreadsheetTarget> implements AutoCloseable { - + private final SpreadsheetTargetConfig dataConfig; - + public SpreadsheetTarget(SpreadsheetTargetConfig dataConfig) { this.dataConfig = dataConfig; } - + public void appendValue(Collection value) { dataConfig.getCollectionHandler().writeType(generateReference(), value); } - + public void appendValue(Boolean value) { dataConfig.getBooleanTypeHandler().writeType(generateReference(), value); } - + public void appendValue(Date value) { dataConfig.getDateTypeHandler().writeType(generateReference(), value); } - + public void appendValue(Number value) { dataConfig.getNumberTypeHandler().writeType(generateReference(), value); } - + public void appendValue(String value) { dataConfig.getStringTypeHandler().writeType(generateReference(), value); } - + public void appendValue(Link link) { dataConfig.getLinkTypeHandler().writeType(generateReference(), link); } - + public void appendGenericValue(Object o) { dataConfig.getDefaultTypeHandler().writeType(generateReference(), o); } - + + public void appendValue(SpreadsheetTypeHandler> spreadsheetTypeHandler, Collection value) { + spreadsheetTypeHandler.writeType(generateReference(), value); + } + + public void appendValue(SpreadsheetTypeHandler spreadsheetTypeHandler, Boolean value) { + spreadsheetTypeHandler.writeType(generateReference(), value); + } + + public void appendValue(SpreadsheetTypeHandler spreadsheetTypeHandler, Date value) { + spreadsheetTypeHandler.writeType(generateReference(), value); + } + + public void appendValue(SpreadsheetTypeHandler spreadsheetTypeHandler, Number value) { + spreadsheetTypeHandler.writeType(generateReference(), value); + } + + public void appendValue(SpreadsheetTypeHandler spreadsheetTypeHandler, String value) { + spreadsheetTypeHandler.writeType(generateReference(), value); + } + + public void appendValue(SpreadsheetTypeHandler spreadsheetTypeHandler, Link value) { + spreadsheetTypeHandler.writeType(generateReference(), value); + } + + public void appendGenericValue(SpreadsheetTypeHandler spreadsheetTypeHandler, Object o) { + spreadsheetTypeHandler.writeType(generateReference(), o); + } + protected void writeHeaders(Collection headers) { SpreadsheetTypeHandler headerCellHandler = dataConfig.getHeaderHandler(); for (String header : headers) { @@ -49,13 +77,13 @@ protected void writeHeaders(Collection headers) { } finishRow(); } - + protected abstract R generateReference(); - + public void appendLink(String label, String href) { appendValue(new Link(label, href)); } - + public void appendValue(Object o) { switch (o) { case Collection collection -> appendValue(collection); @@ -67,35 +95,35 @@ public void appendValue(Object o) { case null, default -> appendGenericValue(o); } } - + public void appendValues(String... values) { for (String value : values) { appendValue(value); } } - + public void writeRow(String... values) { for (String value : values) { appendValue(value); } finishRow(); } - + public void writeRow(Object... values) { for (Object value : values) { appendValue(value); } finishRow(); } - + public void writeRow(Collection values) { for (Object value : values) { appendValue(value); } finishRow(); } - + public abstract void finishRow(); - + public abstract void close() throws IOException; } diff --git a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTargetConfig.java b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTargetConfig.java index 326cc8e9..b8c8eddb 100644 --- a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTargetConfig.java +++ b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/SpreadsheetTargetConfig.java @@ -9,13 +9,13 @@ import java.util.Date; public abstract class SpreadsheetTargetConfig> { - + private final DataOutputStream dataStream; - + private Collection headers; - + private DelimitedListHandler delimitedListHandler = new DefaultDelimitedListHandler(';'); - + private SpreadsheetTypeHandler stringTypeHandler; private SpreadsheetTypeHandler dateTypeHandler; private SpreadsheetTypeHandler numberTypeHandler; @@ -24,110 +24,120 @@ public abstract class SpreadsheetTargetConfig linkTypeHandler; private SpreadsheetTypeHandler defaultTypeHandler; private SpreadsheetTypeHandler headerHandler; - + private SpreadsheetTypeHandler boldHandler; + public SpreadsheetTargetConfig(DataOutputStream dataStream) { this.dataStream = dataStream; withListDelimiter(';'); } - + public DataOutputStream getDataStream() { return dataStream; } - + protected abstract S getSelf(); - + public S withListDelimiter(char listDelimiter) { this.delimitedListHandler = new DefaultDelimitedListHandler(listDelimiter); return getSelf(); } - + public S withDelimitedListHandler(DelimitedListHandler delimitedListHandler) { this.delimitedListHandler = delimitedListHandler; return getSelf(); } - + public S withHeaders(Collection headers) { this.headers = headers; return getSelf(); } - + public Collection getHeaders() { return headers; } - + public DelimitedListHandler getDelimitedListHandler() { return delimitedListHandler; } - + public SpreadsheetTypeHandler getStringTypeHandler() { return stringTypeHandler; } - + public S withStringHandler(SpreadsheetTypeHandler stringTypeHandler) { this.stringTypeHandler = stringTypeHandler; return getSelf(); } - + public SpreadsheetTypeHandler getDateTypeHandler() { return dateTypeHandler; } - + public S withDateTypeHandler(SpreadsheetTypeHandler dateTypeHandler) { this.dateTypeHandler = dateTypeHandler; return getSelf(); } - + public SpreadsheetTypeHandler getNumberTypeHandler() { return numberTypeHandler; } - + public S withNumberTypeHandler(SpreadsheetTypeHandler numberTypeHandler) { this.numberTypeHandler = numberTypeHandler; return getSelf(); } - + public SpreadsheetTypeHandler getBooleanTypeHandler() { return booleanTypeHandler; } - + public S withBooleanTypeHandler(SpreadsheetTypeHandler booleanTypeHandler) { this.booleanTypeHandler = booleanTypeHandler; return getSelf(); } - + public SpreadsheetTypeHandler> getCollectionHandler() { return collectionHandler; } - + public S withCollectionHandler(SpreadsheetTypeHandler> collectionHandler) { this.collectionHandler = collectionHandler; return getSelf(); } - + public SpreadsheetTypeHandler getLinkTypeHandler() { return linkTypeHandler; } - + public S withLinkTypeHandler(SpreadsheetTypeHandler linkTypeHandler) { this.linkTypeHandler = linkTypeHandler; return getSelf(); } - + public SpreadsheetTypeHandler getDefaultTypeHandler() { return defaultTypeHandler; } - + public S withDefaultTypeHandler(SpreadsheetTypeHandler defaultTypeHandler) { this.defaultTypeHandler = defaultTypeHandler; return getSelf(); } - + public SpreadsheetTypeHandler getHeaderHandler() { return headerHandler; } - + public S withHeaderHandler(SpreadsheetTypeHandler headerHandler) { this.headerHandler = headerHandler; return getSelf(); } + + public SpreadsheetTypeHandler getBoldHandler() { + return boldHandler; + } + + public S withBoldHandler(SpreadsheetTypeHandler boldHandler) { + this.boldHandler = boldHandler; + return getSelf(); + } } diff --git a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTarget.java b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTarget.java index beb51a4b..b6860b4e 100644 --- a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTarget.java +++ b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTarget.java @@ -64,7 +64,7 @@ protected CellReference generateReference() { SXSSFCell cell = getNextCell(); return new CellReference(workbookHelper, cell); } - + public SXSSFCell getNextCell() { if (this.row == null) { this.row = this.sheet.createRow(this.rowIdx); @@ -79,7 +79,7 @@ public void finishRow() { colIdx = 0; row = null; } - + public void newSheet(String sheetName) { newSheet(sheetName, null); } diff --git a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTargetConfig.java b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTargetConfig.java index 9b300531..1a8898c1 100644 --- a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTargetConfig.java +++ b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/ExcelTargetConfig.java @@ -2,7 +2,16 @@ import io.zulia.data.output.DataOutputStream; import io.zulia.data.target.spreadsheet.SpreadsheetTargetConfig; -import io.zulia.data.target.spreadsheet.excel.cell.*; +import io.zulia.data.target.spreadsheet.excel.cell.BoldCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.BooleanCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.CellReference; +import io.zulia.data.target.spreadsheet.excel.cell.CollectionCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.DateCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.DefaultCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.HeaderCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.LinkCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.NumberCellHandler; +import io.zulia.data.target.spreadsheet.excel.cell.StringCellHandler; public class ExcelTargetConfig extends SpreadsheetTargetConfig { @@ -22,7 +31,7 @@ private ExcelTargetConfig(DataOutputStream dataStream) { withBooleanTypeHandler(new BooleanCellHandler()); withCollectionHandler(new CollectionCellHandler(this)); withHeaderHandler(new HeaderCellHandler()); - + withBoldHandler(new BoldCellHandler()); } public ExcelTargetConfig getSelf() { diff --git a/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/cell/BoldCellHandler.java b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/cell/BoldCellHandler.java new file mode 100644 index 00000000..8d307908 --- /dev/null +++ b/zulia-data/src/main/java/io/zulia/data/target/spreadsheet/excel/cell/BoldCellHandler.java @@ -0,0 +1,30 @@ +package io.zulia.data.target.spreadsheet.excel.cell; + +import io.zulia.data.target.spreadsheet.SpreadsheetTypeHandler; +import io.zulia.data.target.spreadsheet.excel.WorkbookHelper; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.xssf.streaming.SXSSFCell; + +public class BoldCellHandler implements SpreadsheetTypeHandler { + + @Override + public void writeType(CellReference reference, String value) { + SXSSFCell cell = reference.cell(); + if (value != null) { + WorkbookHelper workbookHelper = reference.workbookHelper(); + CellStyle linkStyle = workbookHelper.createOrGetStyle("boldStyle", style -> { + Font boldFont = workbookHelper.createOrGetFont("boldFont", font -> { + font.setBold(true); + font.setFontHeightInPoints((short) 10); + }); + style.setFont(boldFont); + }); + cell.setCellStyle(linkStyle); + cell.setCellValue(value); + } + else { + cell.setBlank(); + } + } +} \ No newline at end of file diff --git a/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java b/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java index 9049b0c0..ef5a3c7e 100644 --- a/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java +++ b/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java @@ -48,7 +48,7 @@ public void excelTarget() throws IOException { excelTarget.finishRow(); excelTarget.appendValue(2); - excelTarget.appendValue("Tigers"); + excelTarget.appendValue(excelTargetConfig.getBoldHandler(), "Tigers"); excelTarget.finishRow(); excelTarget.appendValue(3); @@ -87,4 +87,8 @@ public void writeSampleRecords(SpreadsheetTarget spreadsheetTarget) { spreadsheetTarget.appendValue(new Date()); spreadsheetTarget.finishRow(); } + + public static void main(String[] args) throws IOException { + new DataTargetExamples().excelTarget(); + } }