From f8268442d1f02eac861a57df219b8ca6b8aae3dd Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Fri, 3 Sep 2021 17:31:05 +1000 Subject: [PATCH] fix duplicate sheet name problem generating spreadsheets --- .../CanonicalSpreadsheetGenerator.java | 2 +- .../CodeSystemSpreadsheetGenerator.java | 74 +++++++++---------- .../ConceptMapSpreadsheetGenerator.java | 2 +- .../spreadsheets/SpreadsheetGenerator.java | 16 ++++ ...ructureDefinitionSpreadsheetGenerator.java | 3 +- .../ValueSetSpreadsheetGenerator.java | 8 +- 6 files changed, 61 insertions(+), 44 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CanonicalSpreadsheetGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CanonicalSpreadsheetGenerator.java index 8145fc7d2..4a5cd4e22 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CanonicalSpreadsheetGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CanonicalSpreadsheetGenerator.java @@ -21,7 +21,7 @@ public class CanonicalSpreadsheetGenerator extends SpreadsheetGenerator { } protected Sheet renderCanonicalResource(CanonicalResource cr) { - Sheet sheet = wb.createSheet("Metadata"); + Sheet sheet = makeSheet("Metadata"); Row headerRow = sheet.createRow(0); addCell(headerRow, 0, "Property", styles.get("header")); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CodeSystemSpreadsheetGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CodeSystemSpreadsheetGenerator.java index 78556a167..9097fc8dc 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CodeSystemSpreadsheetGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/CodeSystemSpreadsheetGenerator.java @@ -68,7 +68,7 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato } private void addFilters(List filters) { - Sheet sheet = wb.createSheet("Filters"); + Sheet sheet = makeSheet("Filters"); addHeaders(sheet, "Code", "Description", "Operators", "Value"); for (CodeSystemFilterComponent f : filters) { CommaSeparatedStringBuilder cs = new CommaSeparatedStringBuilder(); @@ -80,7 +80,7 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato } private void addProperties(List properties) { - Sheet sheet = wb.createSheet("Properties"); + Sheet sheet = makeSheet("Properties"); addHeaders(sheet, "Code", "Uri", "Description", "Type"); for (PropertyComponent p : properties) { addRow(sheet, p.getCode(), p.getUri(), p.getDescription(), p.getTypeElement().asStringValue()); @@ -88,7 +88,7 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato } private void addConcepts(List concepts) { - Sheet sheet = wb.createSheet("Concepts"); + Sheet sheet = makeSheet("Concepts"); addHeaders(sheet, "Level", "Code", "Display", "Definition"); //todo: properties and designations addConcepts(sheet, 1, concepts); } @@ -103,7 +103,7 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato } private void genExpansionParams(List params) { - Sheet sheet = wb.createSheet("Expansion Parameters"); + Sheet sheet = makeSheet("Expansion Parameters"); addHeaders(sheet, "Parameter", "Value"); for (ValueSetExpansionParameterComponent p : params) { addRow(sheet, p.getName(), dr.display(p.getValue())); @@ -111,7 +111,7 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato } private void genExpansion(List list) { - Sheet sheet = wb.createSheet("Expansion"); + Sheet sheet = makeSheet("Expansion"); addHeaders(sheet, "Level", "System", "version", "Code", "Display", "Abstract", "Inactive"); genExpansionEntry(1, list, sheet); } @@ -129,12 +129,12 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato return value ? "" : "false"; } - private void genInclude(ValueSet vs, ConceptSetComponent inc, String mode) { - if (inc.hasSystem()) { - genIncludeSystem(vs, inc, mode); - } else { - genIncludeValueSets(vs, inc, mode); - } +// private void genInclude(ValueSet vs, ConceptSetComponent inc, String mode) { +// if (inc.hasSystem()) { +// genIncludeSystem(vs, inc, mode); +// } else { +// genIncludeValueSets(vs, inc, mode); +// } // String subname = inc.hasSystem() ? : "ValueSets"; // // @@ -152,33 +152,33 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato // processElement(sheet, sd, child); // } // configureSheet(sheet, sd); - } +// } - private void genIncludeValueSets(ValueSet vs, ConceptSetComponent inc, String mode) { - Sheet sheet = wb.createSheet(mode+" ValueSets"); - addValueSets(sheet, inc.getValueSet()); - configureSheet(sheet); - } - - private void genIncludeSystem(ValueSet vs, ConceptSetComponent inc, String mode) { - Sheet sheet = wb.createSheet(mode+" from "+dr.displaySystem(inc.getSystem())); - if (inc.hasValueSet()) { - addValueSets(sheet, inc.getValueSet()); - } - if (inc.hasFilter()) { - addFilters(sheet, inc.getFilter()); - } - if (inc.hasConcept()) { - addConcepts(sheet, inc.getConcept()); - } - if (!inc.hasConcept() && !inc.hasFilter()) { - addAllCodes(sheet); - } - addRow(sheet, "", ""); - addRow(sheet, "System URI", inc.getSystem()); - - configureSheet(sheet); - } +// private void genIncludeValueSets(ValueSet vs, ConceptSetComponent inc, String mode) { +// Sheet sheet = makeSheet(mode+" ValueSets"); +// addValueSets(sheet, inc.getValueSet()); +// configureSheet(sheet); +// } +// +// private void genIncludeSystem(ValueSet vs, ConceptSetComponent inc, String mode) { +// Sheet sheet = makeSheet(mode+" from "+dr.displaySystem(inc.getSystem())); +// if (inc.hasValueSet()) { +// addValueSets(sheet, inc.getValueSet()); +// } +// if (inc.hasFilter()) { +// addFilters(sheet, inc.getFilter()); +// } +// if (inc.hasConcept()) { +// addConcepts(sheet, inc.getConcept()); +// } +// if (!inc.hasConcept() && !inc.hasFilter()) { +// addAllCodes(sheet); +// } +// addRow(sheet, "", ""); +// addRow(sheet, "System URI", inc.getSystem()); +// +// configureSheet(sheet); +// } private void addAllCodes(Sheet sheet) { addHeaders(sheet, "Codes"); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ConceptMapSpreadsheetGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ConceptMapSpreadsheetGenerator.java index d30849373..64cb58ad6 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ConceptMapSpreadsheetGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ConceptMapSpreadsheetGenerator.java @@ -38,7 +38,7 @@ public class ConceptMapSpreadsheetGenerator extends CanonicalSpreadsheetGenerato } private void renderGroup(ConceptMapGroupComponent grp, int i) { - Sheet sheet = wb.createSheet("Mapping Table "+Integer.toString(i)); + Sheet sheet = makeSheet("Mapping Table "+Integer.toString(i)); addHeaders(sheet, "Source", "Display", "Relationship", "Target", "Display"); addRow(sheet, grp.getSource(), "", "", grp.getTarget(), ""); for (SourceElementComponent s : grp.getElement()) { diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/SpreadsheetGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/SpreadsheetGenerator.java index 3eddf8fb4..1e62d3416 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/SpreadsheetGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/SpreadsheetGenerator.java @@ -2,7 +2,9 @@ package org.hl7.fhir.r5.renderers.spreadsheets; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.BorderStyle; @@ -58,6 +60,7 @@ public class SpreadsheetGenerator { protected Map styles; protected DataRenderer dr; + private List sheetNames = new ArrayList<>(); public SpreadsheetGenerator(IWorkerContext context) { super(); @@ -72,6 +75,19 @@ public class SpreadsheetGenerator { outStream.close(); } + protected Sheet makeSheet(String name) { + String s = name; + if (sheetNames.contains(s)) { + int i = 1; + do { + i++; + s = name+" "+Integer.toString(i); + } while (sheetNames.contains(s)); + } + sheetNames.add(s); + return wb.createSheet(s); + } + private static Map createStyles(Workbook wb){ Map styles = new HashMap<>(); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/StructureDefinitionSpreadsheetGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/StructureDefinitionSpreadsheetGenerator.java index 09d06ad0b..1d10c3f97 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/StructureDefinitionSpreadsheetGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/StructureDefinitionSpreadsheetGenerator.java @@ -96,7 +96,7 @@ public class StructureDefinitionSpreadsheetGenerator extends CanonicalSpreadshee throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT)); } addStructureDefinitionMetadata(renderCanonicalResource(sd), sd); - Sheet sheet = wb.createSheet("Elements"); + Sheet sheet = makeSheet("Elements"); Row headerRow = sheet.createRow(0); for (int i = 0; i < titles.length; i++) { @@ -115,6 +115,7 @@ public class StructureDefinitionSpreadsheetGenerator extends CanonicalSpreadshee return this; } + private void addStructureDefinitionMetadata(Sheet sheet, StructureDefinition sd) { for (Coding k : sd.getKeyword()) { addMetadataRow(sheet, "Keyword", dr.display(k)); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ValueSetSpreadsheetGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ValueSetSpreadsheetGenerator.java index a72bdb93a..dec177b1a 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ValueSetSpreadsheetGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/spreadsheets/ValueSetSpreadsheetGenerator.java @@ -56,7 +56,7 @@ public class ValueSetSpreadsheetGenerator extends CanonicalSpreadsheetGenerator } private void genExpansionParams(List params) { - Sheet sheet = wb.createSheet("Expansion Parameters"); + Sheet sheet = makeSheet("Expansion Parameters"); addHeaders(sheet, "Parameter", "Value"); for (ValueSetExpansionParameterComponent p : params) { addRow(sheet, p.getName(), dr.display(p.getValue())); @@ -64,7 +64,7 @@ public class ValueSetSpreadsheetGenerator extends CanonicalSpreadsheetGenerator } private void genExpansion(List list) { - Sheet sheet = wb.createSheet("Expansion"); + Sheet sheet = makeSheet("Expansion"); addHeaders(sheet, "Level", "System", "version", "Code", "Display", "Abstract", "Inactive"); genExpansionEntry(1, list, sheet); } @@ -108,13 +108,13 @@ public class ValueSetSpreadsheetGenerator extends CanonicalSpreadsheetGenerator } private void genIncludeValueSets(ValueSet vs, ConceptSetComponent inc, String mode) { - Sheet sheet = wb.createSheet(mode+" ValueSets"); + Sheet sheet = makeSheet(mode+" ValueSets"); addValueSets(sheet, inc.getValueSet()); configureSheet(sheet); } private void genIncludeSystem(ValueSet vs, ConceptSetComponent inc, String mode) { - Sheet sheet = wb.createSheet(mode+" from "+dr.displaySystem(inc.getSystem())); + Sheet sheet = makeSheet(mode+" from "+dr.displaySystem(inc.getSystem())); if (inc.hasValueSet()) { addValueSets(sheet, inc.getValueSet()); }