Upsert by Batches (SAP IS Adapter)
To upsert a larger data set, a loop is required to upsert all records if the number of records is larger than the batch size. In this example, the records to be upserted are converted from CSV to XML and to the required JSON batch upsert request messages and are temporarily stored in the Datastore.
import com.sap.gateway.ip.core.customdev.util.Message
import com.sap.esb.datastore.DataStore
import com.sap.esb.datastore.Data
import org.osgi.framework.*
import groovy.json.*
boolean isArray(object) {
object != null && object.getClass().isArray()
}
def calculateBatchCount(int recordCount, int batchSize) {
def batchCount = (int) (recordCount / batchSize)
def hasRemaining = recordCount % batchSize
if (hasRemaining > 0) {
batchCount = batchCount + 1
}
return batchCount
}
Message processData(Message message) {
def body = message.getBody(String.class)
def arrayField = message.getProperty("arrayField")
def batchSize = (message.getProperty("batchSize")?:"200") as int
def results = [:]
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(body)
def batchCount = calculateBatchCount(json.get(arrayField)?.size()?:0, batchSize)
(1..batchCount)?.each{
def start = (batchCount-1)*batchSize
def end = batchCount*batchSize - 1
if(end > json.get(arrayField)?.size()-1){
end = json.get(arrayField)?.size() - 1
}
results.put(it as String, json.get(arrayField)[start..end])
}
message.setBody(JsonOutput.toJson(results))
message.setProperty("batchCount", batchCount as String)
return message
}
In the loop, get the current batch of records by below script and send the request to the Pricefx integration adapter until all batches are processed.
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.*
def processData(Message message) {
//Body
def body = message.getBody(String.class);
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(body)
def pageNumber = message.getProperty("pageNumber")?:"1"
if (json.get(pageNumber)){
message.setBody(JsonOutput.toJson(json.get(pageNumber)))
message.setProperty("isEmpty", "false")
} else {
message.setBody(null)
message.setProperty("isEmpty", "true")
}
return message
}