Inserting and updating a row take a very similar approach, issuing a single call to insert or update a DFEntityDataProvider with an array mapping the field to the new value based on the primary key (usually ID). This is not carried out on the entity itself, but on the data provider. During an update, fields that are not updated will be left alone. The scriptlet below identifies fields, builds a sample array, and then inserts the row based on the id.
The API call for inserting a row into the DFEntityDataProvider is:
DFUpdateInfo insert(Map<String,Object> values,
Map<String,Object> insertOptions,
DFEnvironmentRW env)
The scriptlet below identifies the DFEntityDataProvider for the DFEntity. It also selects all row IDs fro the full result set on the entity. This code only inserts the string 'Some code' and 'Second column' into the table, but it demonstrates how more complex operations could be used to update other information. A full scripts which demonstrate the usage is MicroSpecies Table Populator.
import com.im.commons.progress.*
def ety = dataTree.rootVertex.entity
def ids = parentVS.ids
def codeFld = ety.fields.items.find { it.name == 'Code' }
def columnFld = ety.fields.items.find { it.name == 'Second Columns' }
def code
lock = schema.lockable.obtainLock('Updating')
def envRW = EnvUtils.createDefaultEnvironmentRW(lock, 'Updating', true)
try {
ids.each { id ->
// Define the values for each field
code = 'Some code'
moreCol = 'Second column'
def vals = [(codeFld.id):code]
vals.putAt(columnFld.id,moreCol)
// To insert the new row
edp.insert(vals, null, envRW)
}finally{
lock?.release()
envRW?.feedback.finish()
}
Updating a row is a little more complex. The array of fields to update must be in the form of a DFUpdateDescription. The API call for this is:
Map<DFUpdateDescription,DFUpdateResult> update(List<DFUpdateDescription> updateDescriptors,
DFUndoConfig undoConfig,
DFEnvironmentRW env)
The DFUpdateDescription is constructured from a map of fields:values, just as above. However, this map is then passed
into the DFUpdateDescription constructor to provide a format necessary for update. The example below
demonstrates the same functionality as above, but in an update fashion, not an insert fashion.
Both the insert and update method can be found on the DFEntityDataProvider
page. A full scripts which demonstrate this usage are Table Standardizer
import com.im.commons.progress.*
import com.im.df.api.support.*
def ety = dataTree.rootVertex.entity
def ids = parentVS.ids
def codeFld = ety.fields.items.find { it.name == 'Code' }
def columnFld = ety.fields.items.find { it.name == 'Second Columns' }
def code
lock = schema.lockable.obtainLock('Updating')
def envRW = EnvUtils.createDefaultEnvironmentRW(lock, 'Updating', true)
try {
ids.each { id ->
// Define the values for each field
code = 'Some code'
moreCol = 'Second column'
def vals = [(codeFld.id):code]
vals.putAt(columnFld.id,moreCol)
// Create the DFUpdateDescription and update the DFEntityDataProvider
DFUpdateDescription ud = DFUpdateDescription.create(ety, id, vals)
submitList = Collections.singletonList(ud)
edp.update(submitList, DFUndoConfig.OFF, envRW)
}finally{
lock?.release()
envRW?.feedback.finish()
}