001package org.hl7.fhir.utilities.graphql; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.Map.Entry; 006 007import org.hl7.fhir.exceptions.FHIRException; 008import org.hl7.fhir.utilities.Utilities; 009import org.hl7.fhir.utilities.graphql.Argument.ArgumentListStatus; 010 011import com.google.gson.JsonElement; 012import com.google.gson.JsonObject; 013 014public class ObjectValue extends Value { 015 private List<Argument> fields = new ArrayList<Argument>(); 016 017 public ObjectValue() { 018 super(); 019 } 020 021 public ObjectValue(JsonObject json) throws EGraphQLException { 022 super(); 023 for (Entry<String, JsonElement> n : json.entrySet()) 024 fields.add(new Argument(n.getKey(), n.getValue())); 025 } 026 027 public List<Argument> getFields() { 028 return fields; 029 } 030 031 public Argument addField(String name, ArgumentListStatus listStatus) throws FHIRException { 032 Argument result = null; 033 for (Argument t : fields) 034 if ((t.name.equals(name))) 035 result = t; 036 if (result == null) { 037 result = new Argument(); 038 result.setName(name); 039 result.setListStatus(listStatus); 040 fields.add(result); 041 } else if (result.getListStatus() == ArgumentListStatus.SINGLETON) 042 throw new FHIRException("Error: Attempt to make '+name+' into a repeating field when it is constrained by @singleton"); 043 else 044 result.setListStatus(ArgumentListStatus.REPEATING); 045 return result; 046 } 047 048 /** 049 * Write the output using the system default line separator (as defined in {@link System#lineSeparator} 050 * @param b The StringBuilder to populate 051 * @param indent The indent level, or <code>-1</code> for no indent 052 */ 053 public void write(StringBuilder b, int indent) throws EGraphQLException, EGraphEngine { 054 write(b, indent, System.lineSeparator()); 055 } 056 057 public String getValue() { 058 return null; 059 } 060 061 /** 062 * Write the output using the system default line separator (as defined in {@link System#lineSeparator} 063 * @param b The StringBuilder to populate 064 * @param indent The indent level, or <code>-1</code> for no indent 065 * @param lineSeparator The line separator 066 */ 067 public void write(StringBuilder b, Integer indent, String lineSeparator) throws EGraphQLException, EGraphEngine { 068 b.append("{"); 069 String s = ""; 070 String se = ""; 071 if ((indent > -1)) 072 { 073 se = lineSeparator + Utilities.padLeft("",' ', indent*2); 074 indent++; 075 s = lineSeparator + Utilities.padLeft("",' ', indent*2); 076 } 077 boolean first = true; 078 for (Argument a : fields) { 079 if (first) first = false; else b.append(","); 080 b.append(s); 081 a.write(b, indent); 082 } 083 b.append(se); 084 b.append("}"); 085 086 } 087}