updates for all-profiles csv and xlsx

This commit is contained in:
Grahame Grieve 2022-12-06 16:21:33 +11:00
parent cdfc93052b
commit 3ee52938d6
8 changed files with 121 additions and 31 deletions

View File

@ -6087,18 +6087,28 @@ public class ProfileUtilities extends TranslatingUtilities {
}
// generate a CSV representation of the structure definition
public void generateCsvs(OutputStream dest, StructureDefinition structure, boolean asXml) throws IOException, DefinitionException, Exception {
public void generateCsv(OutputStream dest, StructureDefinition structure, boolean asXml) throws IOException, DefinitionException, Exception {
if (!structure.hasSnapshot())
throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT));
CSVWriter csv = new CSVWriter(dest, structure, asXml);
for (ElementDefinition child : structure.getSnapshot().getElement()) {
csv.processElement(child);
csv.processElement(null, child);
}
csv.dump();
}
// generate a CSV representation of the structure definition
public void addToCSV(CSVWriter csv, StructureDefinition structure) throws IOException, DefinitionException, Exception {
if (!structure.hasSnapshot())
throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT));
for (ElementDefinition child : structure.getSnapshot().getElement()) {
csv.processElement(structure, child);
}
}
private class Slicer extends ElementDefinitionSlicingComponent {
String criteria = "";

View File

@ -20,13 +20,16 @@ public class CanonicalSpreadsheetGenerator extends SpreadsheetGenerator {
super(context);
}
protected Sheet renderCanonicalResource(CanonicalResource cr) {
Sheet sheet = makeSheet("Metadata");
protected Sheet renderCanonicalResource(CanonicalResource cr, boolean forMultiple) {
Sheet sheet = forMultiple && hasSheet("Metadata") ? getSheet("Metadata") : makeSheet("Metadata");
Row headerRow = sheet.createRow(0);
Row headerRow = sheet.createRow(forMultiple ? sheet.getLastRowNum()+1 : 0);
addCell(headerRow, 0, "Property", styles.get("header"));
addCell(headerRow, 1, "Value", styles.get("header"));
if (forMultiple) {
addMetadataRow(sheet, "ID", cr.getId());
}
addMetadataRow(sheet, "URL", cr.getUrl());
for (Identifier id : cr.getIdentifier()) {
addMetadataRow(sheet, "Identifier", dr.display(id));

View File

@ -42,7 +42,7 @@ public class CodeSystemSpreadsheetGenerator extends CanonicalSpreadsheetGenerato
if (cs == null) {
System.out.println("no code system!");
}
addCodeSystemMetadata(renderCanonicalResource(cs), cs);
addCodeSystemMetadata(renderCanonicalResource(cs, false), cs);
if (cs.hasProperty()) {
addProperties(cs.getProperty());

View File

@ -22,7 +22,7 @@ public class ConceptMapSpreadsheetGenerator extends CanonicalSpreadsheetGenerato
}
public ConceptMapSpreadsheetGenerator renderConceptMap(ConceptMap cm) {
addConceptMapMetadata(renderCanonicalResource(cm), cm);
addConceptMapMetadata(renderCanonicalResource(cm, false), cm);
int i = 0;
for (ConceptMapGroupComponent grp : cm.getGroup()) {
renderGroup(grp, i);

View File

@ -65,7 +65,7 @@ public class SpreadsheetGenerator {
protected Map<String, CellStyle> styles;
protected DataRenderer dr;
private List<String> sheetNames = new ArrayList<>();
private Map<String, Sheet> sheetNames = new HashMap<>();
public SpreadsheetGenerator(IWorkerContext context) {
super();
@ -80,20 +80,37 @@ public class SpreadsheetGenerator {
outStream.close();
}
protected boolean hasSheet(String name) {
if (name.length() > MAX_SENSITIVE_SHEET_NAME_LEN - 2) {
name = name.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN - 2);
}
String s = fixSheetNameChars(name);
return sheetNames.containsKey(s);
}
protected Sheet getSheet(String name) {
if (name.length() > MAX_SENSITIVE_SHEET_NAME_LEN - 2) {
name = name.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN - 2);
}
String s = fixSheetNameChars(name);
return sheetNames.get(s);
}
protected Sheet makeSheet(String name) {
if (name.length() > MAX_SENSITIVE_SHEET_NAME_LEN - 2) {
name = name.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN - 2);
}
String s = fixSheetNameChars(name);
if (sheetNames.contains(s)) {
if (sheetNames.containsKey(s)) {
int i = 1;
do {
i++;
s = name+" "+Integer.toString(i);
} while (sheetNames.contains(s));
} while (sheetNames.containsKey(s));
}
sheetNames.add(s);
return wb.createSheet(s);
Sheet res = wb.createSheet(s);
sheetNames.put(s, res);
return res;
}
private String fixSheetNameChars(String name) {

View File

@ -38,6 +38,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.checkerframework.common.reflection.qual.ForName;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
@ -88,28 +89,34 @@ public class StructureDefinitionSpreadsheetGenerator extends CanonicalSpreadshee
this.hideMustSupportFalse = hideMustSupportFalse;
}
public StructureDefinitionSpreadsheetGenerator renderStructureDefinition(StructureDefinition sd) throws Exception {
public StructureDefinitionSpreadsheetGenerator renderStructureDefinition(StructureDefinition sd, boolean forMultiple) throws Exception {
if (sd == null) {
System.out.println("no structure!");
}
if (!sd.hasSnapshot()) {
throw new DefinitionException(context.formatMessage(I18nConstants.NEEDS_A_SNAPSHOT));
}
addStructureDefinitionMetadata(renderCanonicalResource(sd), sd);
Sheet sheet = makeSheet("Elements");
addStructureDefinitionMetadata(renderCanonicalResource(sd, forMultiple), sd);
Sheet sheet = forMultiple && hasSheet("Elements") ? getSheet("Elements") : makeSheet("Elements");
Row headerRow = sheet.createRow(0);
for (int i = 0; i < titles.length; i++) {
addCell(headerRow, i, titles[i], styles.get("header"));
if (sheet.getLastRowNum() == 0) {
Row headerRow = sheet.createRow(0);
int coffset = forMultiple ? 1 : 0;
for (int i = 0; i < titles.length; i++) {
if (forMultiple) {
addCell(headerRow, 0, "ID", styles.get("header"));
}
addCell(headerRow, i+coffset, titles[i], styles.get("header"));
}
int i = titles.length - 1;
for (StructureDefinitionMappingComponent map : sd.getMapping()) {
i++;
addCell(headerRow, i+coffset, "Mapping: " + map.getName(), styles.get("header"));
}
}
int i = titles.length - 1;
for (StructureDefinitionMappingComponent map : sd.getMapping()) {
i++;
addCell(headerRow, i, "Mapping: " + map.getName(), styles.get("header"));
}
for (ElementDefinition child : sd.getSnapshot().getElement()) {
processElement(sheet, sd, child);
processElement(sheet, sd, child, forMultiple);
}
configureSheet(sheet, sd);
return this;
@ -136,9 +143,12 @@ public class StructureDefinitionSpreadsheetGenerator extends CanonicalSpreadshee
}
public void processElement(Sheet sheet, StructureDefinition sd, ElementDefinition ed) throws Exception {
public void processElement(Sheet sheet, StructureDefinition sd, ElementDefinition ed, boolean forMultiple) throws Exception {
Row row = sheet.createRow(sheet.getLastRowNum()+1);
int i = 0;
if (forMultiple) {
addCell(row, i++, sd.getId(), styles.get("body"));
}
addCell(row, i++, ed.getPath(), styles.get("body"));
addCell(row, i++, ed.getSliceName());
addCell(row, i++, itemList(ed.getAlias()));

View File

@ -35,7 +35,7 @@ public class ValueSetSpreadsheetGenerator extends CanonicalSpreadsheetGenerator
if (vs == null) {
System.out.println("no valueset!");
}
addValueSetMetadata(renderCanonicalResource(vs), vs);
addValueSetMetadata(renderCanonicalResource(vs, false), vs);
for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
genInclude(vs, inc, "Include");
}

View File

@ -150,6 +150,51 @@ public class CSVWriter extends TextStreamWriter {
}
}
public CSVWriter(OutputStream out, boolean asXml) throws UnsupportedEncodingException {
super(out);
this.asXml = asXml;
this.def = null;
CSVLine header = new CSVLine();
lines.add(header);
header.addString("Profile"); //
header.addString("Id"); //
header.addString("Path"); //A
header.addString("Slice Name"); //B
header.addString("Alias(s)"); //C
header.addString("Label"); //D
header.addString("Min"); //E
header.addString("Max"); //F
header.addString("Must Support?"); //G
header.addString("Is Modifier?"); //H
header.addString("Is Summary?"); //I
header.addString("Type(s)"); //J
header.addString("Short"); //K
header.addString("Definition"); //L
header.addString("Comments"); //M
header.addString("Requirements"); //N
header.addString("Default Value"); //O
header.addString("Meaning When Missing"); //P
header.addString("Fixed Value"); //Q
header.addString("Pattern"); //R
header.addString("Example"); //S
header.addString("Minimum Value"); //T
header.addString("Maximum Value"); //U
header.addString("Maximum Length"); //V
header.addString("Binding Strength"); //W
header.addString("Binding Description"); //X
header.addString("Binding Value Set"); //Y
header.addString("Code"); //Z
header.addString("Slicing Discriminator");//AA
header.addString("Slicing Description"); //AB
header.addString("Slicing Ordered"); //AC
header.addString("Slicing Rules"); //AD
header.addString("Base Path"); //AE
header.addString("Base Min"); //AF
header.addString("Base Max"); //AG
header.addString("Condition(s)"); //AH
header.addString("Constraint(s)"); //AI
}
/* private void findMapKeys(StructureDefinition def, List<StructureDefinitionMappingComponent> maps, IWorkerContext context) {
maps.addAll(def.getMapping());
if (def.getBaseDefinition()!=null) {
@ -158,9 +203,12 @@ public class CSVWriter extends TextStreamWriter {
}
}*/
public void processElement(ElementDefinition ed) throws Exception {
public void processElement(StructureDefinition sd, ElementDefinition ed) throws Exception {
CSVLine line = new CSVLine();
lines.add(line);
if (def == null) {
line.addString(sd.getId());
}
line.addString(ed.getId());
line.addString(ed.getPath());
line.addString(ed.getSliceName());
@ -218,10 +266,12 @@ public class CSVWriter extends TextStreamWriter {
}
line.addString(itemList(ed.getCondition()));
line.addString(itemList(ed.getConstraint()));
for (StructureDefinitionMappingComponent mapKey : def.getMapping()) {
for (ElementDefinitionMappingComponent map : ed.getMapping()) {
if (map.getIdentity().equals(mapKey.getIdentity()))
line.addString(map.getMap());
if (def != null) {
for (StructureDefinitionMappingComponent mapKey : def.getMapping()) {
for (ElementDefinitionMappingComponent map : ed.getMapping()) {
if (map.getIdentity().equals(mapKey.getIdentity()))
line.addString(map.getMap());
}
}
}
}