001package org.hl7.fhir.r5.renderers.spreadsheets;
002
003import java.io.FileOutputStream;
004
005import org.apache.poi.ss.usermodel.Sheet;
006import org.hl7.fhir.r5.context.IWorkerContext;
007import org.hl7.fhir.r5.context.SimpleWorkerContext;
008import org.hl7.fhir.r5.model.ConceptMap;
009import org.hl7.fhir.r5.model.ConceptMap.ConceptMapGroupComponent;
010import org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent;
011import org.hl7.fhir.r5.model.ConceptMap.TargetElementComponent;
012
013public class ConceptMapSpreadsheetGenerator extends CanonicalSpreadsheetGenerator {
014
015  
016  public ConceptMapSpreadsheetGenerator(IWorkerContext context) {
017    super(context);
018  }
019
020  public boolean canGenerate(ConceptMap cm) {
021    return true;
022  }
023
024  public ConceptMapSpreadsheetGenerator renderConceptMap(ConceptMap cm) {
025    addConceptMapMetadata(renderCanonicalResource(cm, false), cm);
026    int i = 0;
027    for (ConceptMapGroupComponent grp : cm.getGroup()) {
028      renderGroup(grp, i);
029      i++;
030    }
031    return this;
032  }
033
034  private void addConceptMapMetadata(Sheet sheet, ConceptMap cm) {
035    if (cm.hasSourceScope()) {
036      addMetadataRow(sheet, "Source", cm.getSourceScope().primitiveValue());
037    }
038    if (cm.hasTargetScope()) {
039      addMetadataRow(sheet, "Target", cm.getTargetScope().primitiveValue());
040    }    
041  }
042
043  private void renderGroup(ConceptMapGroupComponent grp, int i) {
044    Sheet sheet = makeSheet("Mapping Table "+Integer.toString(i));
045    addHeaders(sheet, "Source", "Display", "Relationship", "Target", "Display");
046    addRow(sheet, grp.getSource(), "", "", grp.getTarget(), "");
047    for (SourceElementComponent s : grp.getElement()) {
048      for (TargetElementComponent t : s.getTarget()) {
049        addRow(sheet, s.getCode(), s.getDisplay(), t.getRelationshipElement().asStringValue(), t.getCode(), t.getDisplay());        
050      }
051    }    
052  }
053
054}