001package org.hl7.fhir.utilities.graphql;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.util.ArrayList;
035import java.util.List;
036import java.util.Map.Entry;
037
038import org.hl7.fhir.exceptions.FHIRException;
039import org.hl7.fhir.utilities.Utilities;
040import org.hl7.fhir.utilities.graphql.Argument.ArgumentListStatus;
041
042import com.google.gson.JsonElement;
043import com.google.gson.JsonObject;
044
045public class ObjectValue extends Value {
046  private List<Argument> fields = new ArrayList<Argument>();
047
048  public ObjectValue() {
049    super();
050  }
051
052  public ObjectValue(JsonObject json) throws EGraphQLException {
053    super();
054    for (Entry<String, JsonElement> n : json.entrySet()) 
055      fields.add(new Argument(n.getKey(), n.getValue()));      
056  }
057
058  public List<Argument> getFields() {
059    return fields;
060  }
061
062  public Argument addField(String name, ArgumentListStatus listStatus) throws FHIRException {
063    Argument result = null;
064    for (Argument t : fields)
065      if ((t.name.equals(name)))
066        result = t;
067    if (result == null) {
068      result = new Argument();
069      result.setName(name);
070      result.setListStatus(listStatus);
071      fields.add(result);
072    } else if (result.getListStatus() == ArgumentListStatus.SINGLETON)
073        throw new FHIRException("Error: Attempt to make '+name+' into a repeating field when it is constrained by @singleton");
074    else
075      result.setListStatus(ArgumentListStatus.REPEATING);
076    return result;
077  }
078
079  /**
080   * Write the output using the system default line separator (as defined in {@link System#lineSeparator}
081   * @param b The StringBuilder to populate
082   * @param indent The indent level, or <code>-1</code> for no indent
083   */
084  public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine {
085    write(b, indent, System.lineSeparator());
086  }
087
088  public String getValue() {
089    return null;
090  }
091
092  /**
093   * Write the output using the system default line separator (as defined in {@link System#lineSeparator}
094   * @param b The StringBuilder to populate
095   * @param indent The indent level, or <code>-1</code> for no indent
096   * @param lineSeparator The line separator
097   */
098  public void write(StringBuilder b, Integer indent, String lineSeparator) throws EGraphQLException, EGraphEngine {
099
100    // Write the GraphQL output
101    b.append("{");
102    String s = "";
103    String se = "";
104    if ((indent > -1))
105    {
106      se = lineSeparator + Utilities.padLeft("",' ', indent*2);
107      indent++;
108      s = lineSeparator + Utilities.padLeft("",' ', indent*2);
109    }
110    boolean first = true;
111    for (Argument a : fields) {
112      if (first) first = false; else b.append(",");
113      b.append(s);
114      a.write(b, indent);
115    }
116    b.append(se);
117    b.append("}");
118  }
119}