001package org.hl7.fhir.r5.utils.sql;
002
003import java.util.List;
004
005import org.hl7.fhir.exceptions.FHIRException;
006import org.hl7.fhir.r5.model.Base;
007import org.hl7.fhir.r5.utils.sql.Validator.TrueFalseOrUnknown;
008import org.hl7.fhir.utilities.json.model.JsonArray;
009import org.hl7.fhir.utilities.json.model.JsonBoolean;
010import org.hl7.fhir.utilities.json.model.JsonElement;
011import org.hl7.fhir.utilities.json.model.JsonNull;
012import org.hl7.fhir.utilities.json.model.JsonNumber;
013import org.hl7.fhir.utilities.json.model.JsonObject;
014import org.hl7.fhir.utilities.json.model.JsonString;
015
016public class StorageJson implements Storage {
017
018  private String name; 
019  private JsonArray rows;
020  
021  @Override
022  public TrueFalseOrUnknown supportsArrays() {
023    return TrueFalseOrUnknown.TRUE;
024  }
025
026  @Override
027  public Store createStore(String name, List<Column> columns) {
028    this.name = name;
029    this.rows = new JsonArray();
030    return new Store(name); // we're not doing anything with this
031  }
032
033  @Override
034  public void addRow(Store store, List<Cell> cells) {
035    JsonObject row = new JsonObject();
036    rows.add(row);
037    for (Cell cell : cells) {
038      if (cell.getColumn().isColl() || cell.getValues().size() > 1) {
039        JsonArray arr = new JsonArray();
040        row.add(cell.getColumn().getName(), arr);   
041        for (Value value : cell.getValues()) {
042          arr.add(makeJsonNode(value));
043        } 
044      } else if (cell.getValues().size() == 0) {
045        row.add(cell.getColumn().getName(), new JsonNull());
046      } else {
047        row.add(cell.getColumn().getName(), makeJsonNode(cell.getValues().get(0)));
048      }
049    }
050  }
051
052  private JsonElement makeJsonNode(Value value) {
053    if (value == null) {
054      return new JsonNull();
055    } else if (value.getValueInt() != null) {
056      return new JsonNumber(value.getValueInt().intValue());
057    }
058    if (value.getValueBoolean() != null) {
059      return new JsonBoolean(value.getValueBoolean().booleanValue());
060    }
061    if (value.getValueDecimal() != null) {
062      return new JsonNumber(value.getValueDecimal().toPlainString());
063    }
064    return new JsonString(value.getValueString());
065  }
066
067  @Override
068  public void finish(Store store) {
069    // nothing
070  }
071
072  public String getName() {
073    return name;
074  }
075
076  public JsonArray getRows() {
077    return rows;
078  }
079
080  @Override
081  public TrueFalseOrUnknown supportsComplexTypes() {
082    return TrueFalseOrUnknown.TRUE;
083  }
084
085  @Override
086  public TrueFalseOrUnknown needsName() {
087    return TrueFalseOrUnknown.FALSE;
088  }
089
090  @Override
091  public String getKeyForSourceResource(Base res) {
092    return res.fhirType()+"/"+res.getIdBase();
093  }
094
095  @Override
096  public String getKeyForTargetResource(Base res) {
097    return res.fhirType()+"/"+res.getIdBase();
098  }
099
100}