datai/docs/reference-code/force-wsc-57.0.0/com/sforce/async/BatchRequest.java

70 lines
2.5 KiB
Java

package com.sforce.async;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.parser.PullParserException;
import com.sforce.ws.parser.XmlInputStream;
import com.sforce.ws.parser.XmlOutputStream;
import com.sforce.ws.transport.Transport;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class BatchRequest {
private XmlOutputStream xmlStream;
private Transport transport;
public BatchRequest(Transport transport, OutputStream out) throws IOException {
this.transport = transport;
this.xmlStream = new AsyncXmlOutputStream(out, false);
this.xmlStream.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
this.xmlStream.writeStartTag("http://www.force.com/2009/06/asyncapi/dataload", "sObjects");
}
public void addSObject(SObject object) throws AsyncApiException {
try {
object.write(this.xmlStream);
} catch (IOException e) {
throw new AsyncApiException("Failed to add SObject", AsyncExceptionCode.ClientInputError, e);
}
}
public void addSObjects(SObject[] objects) throws AsyncApiException {
byte b;
int i;
SObject[] arrayOfSObject;
for (i = (arrayOfSObject = objects).length, b = 0; b < i; ) {
SObject object = arrayOfSObject[b];
addSObject(object);
b++;
}
}
public BatchInfo completeRequest() throws AsyncApiException {
try {
this.xmlStream.writeEndTag("http://www.force.com/2009/06/asyncapi/dataload", "sObjects");
this.xmlStream.endDocument();
this.xmlStream.close();
InputStream in = this.transport.getContent();
if (this.transport.isSuccessful())
return loadBatchInfo(in);
BulkConnection.parseAndThrowException(in, ContentType.XML);
} catch (IOException e) {
throw new AsyncApiException("Failed to complete request", AsyncExceptionCode.ClientInputError, e);
} catch (PullParserException e) {
throw new AsyncApiException("Failed to complete request", AsyncExceptionCode.ClientInputError, e);
} catch (ConnectionException e) {
throw new AsyncApiException("Failed to complete request", AsyncExceptionCode.ClientInputError, e);
}
return null;
}
static BatchInfo loadBatchInfo(InputStream in) throws PullParserException, IOException, ConnectionException {
BatchInfo info = new BatchInfo();
XmlInputStream xin = new XmlInputStream();
xin.setInput(in, "UTF-8");
info.load(xin, BulkConnection.typeMapper);
return info;
}
}