package com.sforce.async; import com.sforce.ws.parser.XmlOutputStream; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public final class SObject { private static final int MAX_DEPTH = 5; private final HashMap fields = new HashMap<>(); private final HashMap fkRefs = new HashMap<>(); private int maxDepth; public SObject() { this.maxDepth = 5; } public SObject(int maxDepth) { this.maxDepth = (maxDepth < 5) ? 5 : maxDepth; } public int getMaxDepth() { return this.maxDepth; } public void setMaxDepth(int maxDepth) { this.maxDepth = maxDepth; } public Set getFieldNames() { return Collections.unmodifiableSet(this.fields.keySet()); } public String getField(String name) { return this.fields.get(name); } public void setField(String name, String value) { this.fields.put(name, value); } public void setFieldReference(String name, SObject ref) { if (ref == this) throw new IllegalStateException( "Foreign Key SObject Reference is pointing to the same SObject"); this.fkRefs.put(name, ref); } public Map getFieldReferences() { return Collections.unmodifiableMap(this.fkRefs); } public void write(XmlOutputStream out) throws IOException { write(out, 0); } public void write(XmlOutputStream out, int depth) throws IOException { if (depth > this.maxDepth) throw new IllegalStateException( "foreign key reference exceeded the maximum allowed depth of " + this.maxDepth); out.writeStartTag("http://www.force.com/2009/06/asyncapi/dataload", "sObject"); for (Map.Entry entry : this.fields.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); out.writeStringElement("http://www.force.com/2009/06/asyncapi/dataload", name, value); } for (Map.Entry entry : this.fkRefs.entrySet()) { String relationshipName = entry.getKey(); SObject ref = entry.getValue(); out.writeStartTag("http://www.force.com/2009/06/asyncapi/dataload", relationshipName); ref.write(out, depth++); out.writeEndTag("http://www.force.com/2009/06/asyncapi/dataload", relationshipName); } out.writeEndTag("http://www.force.com/2009/06/asyncapi/dataload", "sObject"); } }