001package org.hl7.fhir.utilities.graphql;
002
003/*-
004 * #%L
005 * org.hl7.fhir.utilities
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map.Entry;
027
028import org.hl7.fhir.exceptions.FHIRException;
029import org.hl7.fhir.utilities.Utilities;
030import org.hl7.fhir.utilities.graphql.Argument.ArgumentListStatus;
031
032import com.google.gson.JsonElement;
033import com.google.gson.JsonObject;
034
035public class ObjectValue extends Value {
036  private List<Argument> fields = new ArrayList<Argument>();
037
038  public ObjectValue() {
039    super();
040  }
041
042  public ObjectValue(JsonObject json) throws EGraphQLException {
043    super();
044    for (Entry<String, JsonElement> n : json.entrySet()) 
045      fields.add(new Argument(n.getKey(), n.getValue()));      
046  }
047
048  public List<Argument> getFields() {
049    return fields;
050  }
051
052  public Argument addField(String name, ArgumentListStatus listStatus) throws FHIRException {
053    Argument result = null;
054    for (Argument t : fields)
055      if ((t.name.equals(name)))
056        result = t;
057    if (result == null) {
058      result = new Argument();
059      result.setName(name);
060      result.setListStatus(listStatus);
061      fields.add(result);
062    } else if (result.getListStatus() == ArgumentListStatus.SINGLETON)
063        throw new FHIRException("Error: Attempt to make '+name+' into a repeating field when it is constrained by @singleton");
064    else
065      result.setListStatus(ArgumentListStatus.REPEATING);
066    return result;
067  }
068
069  /**
070   * Write the output using the system default line separator (as defined in {@link System#lineSeparator}
071   * @param b The StringBuilder to populate
072   * @param indent The indent level, or <code>-1</code> for no indent
073   */
074  public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine {
075    write(b, indent, System.lineSeparator());
076  }
077
078  public String getValue() {
079    return null;
080  }
081
082  /**
083   * Write the output using the system default line separator (as defined in {@link System#lineSeparator}
084   * @param b The StringBuilder to populate
085   * @param indent The indent level, or <code>-1</code> for no indent
086   * @param lineSeparator The line separator
087   */
088  public void write(StringBuilder b, Integer indent, String lineSeparator) throws EGraphQLException, EGraphEngine {
089
090    // Write the GraphQL output
091    b.append("{");
092    String s = "";
093    String se = "";
094    if ((indent > -1))
095    {
096      se = lineSeparator + Utilities.padLeft("",' ', indent*2);
097      indent++;
098      s = lineSeparator + Utilities.padLeft("",' ', indent*2);
099    }
100    boolean first = true;
101    for (Argument a : fields) {
102      if (first) first = false; else b.append(",");
103      b.append(s);
104      a.write(b, indent);
105    }
106    b.append(se);
107    b.append("}");
108  }
109}