001package org.hl7.fhir.utilities.graphql;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.hl7.fhir.utilities.graphql.Argument.ArgumentListStatus;
007
008import com.google.gson.JsonArray;
009import com.google.gson.JsonElement;
010import com.google.gson.JsonObject;
011import com.google.gson.JsonPrimitive;
012
013public class Argument {
014  public enum ArgumentListStatus {NOT_SPECIFIED, SINGLETON, REPEATING}
015  String name;
016  private List<Value> values = new ArrayList<Value>();
017  ArgumentListStatus listStatus;
018  public Argument() {
019    super();
020  }
021  public Argument(String name, Value value) {
022    super();
023    this.name = name;
024    this.values.add(value);
025  }
026  public Argument(String name, JsonElement json) throws EGraphQLException {
027    super();
028    this.name = name;
029    valuesFromNode(json);
030  }
031  public String getName() {
032    return name;
033  }
034  public void setName(String name) {
035    this.name = name;
036  }
037  public ArgumentListStatus getListStatus() {
038    return listStatus;
039  }
040  public void setListStatus(ArgumentListStatus listStatus) {
041    this.listStatus = listStatus;
042  }
043  public List<Value> getValues() {
044    return values;
045  }
046  public void addValue(Value value){ 
047    values.add(value);
048  }
049
050  public boolean hasValue(String value) {
051    for (Value v : values ) 
052      if (v.isValue(value)) 
053        return true;
054    return false;
055  }
056
057  public void valuesFromNode(JsonElement json) throws EGraphQLException {
058    if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isString())
059      values.add(new StringValue(((JsonPrimitive)json).getAsString()));
060    else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isNumber())
061      values.add(new NumberValue(((JsonPrimitive)json).getAsString()));
062    else if (json instanceof JsonPrimitive && ((JsonPrimitive) json).isBoolean())
063      values.add(new NameValue(((JsonPrimitive)json).getAsBoolean()));
064    else if (json instanceof JsonObject)
065      values.add(new ObjectValue((JsonObject) json));
066    else if (json instanceof JsonArray) {
067      for (JsonElement v : (JsonArray) json)
068        valuesFromNode(v);
069    } else
070      throw new EGraphQLException("Unexpected JSON type for \""+name+"\": "+json.getClass().getName());
071  }
072
073  public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine {
074    b.append("\"");
075    for (char ch : name.toCharArray()) {
076      if (ch == '"') b.append("\"");
077      else if (ch == '\\') b.append("\\");
078      else if (ch == '\r') b.append("\\r");
079      else if (ch == '\n') b.append("\\n");
080      else if (ch == '\t') b.append("\\t");
081      else if (ch < 32)
082        b.append("\\u"+Integer.toHexString(ch));
083      else
084        b.append(ch);
085    }
086    b.append("\":");
087    if (listStatus == ArgumentListStatus.REPEATING) {
088      b.append("[");
089      boolean first = true;
090      for (Value v : values) {
091        if (first) first = false; else b.append(",");
092        v.write(b, indent);
093      }
094      b.append("]");
095    } else {
096      if (values.size() > 1)
097        throw new EGraphQLException("Internal error: non list \""+name+"\" has "+Integer.toString(values.size())+" values");
098      if (values.size() == 0)
099        b.append("null");
100      else
101        values.get(0).write(b, indent);
102    }
103  }
104}