Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X
This code snippet creates then deletes a data item to illustrate CRUD technique.
Parameter: model_number
import com.axeda.drm.sdk.Context
import com.axeda.drm.sdk.device.ModelFinder
import com.axeda.drm.sdk.device.Model
import com.axeda.drm.sdk.device.DeviceFinder
import com.axeda.drm.sdk.data.CurrentDataFinder
import com.axeda.drm.sdk.device.Device
import com.axeda.drm.sdk.data.HistoricalDataFinder
import groovy.xml.MarkupBuilder
import com.axeda.drm.sdk.device.DataItem
import com.axeda.drm.services.device.DataItemType
/*
* DeleteDataItem.groovy
*
* Delete a data item.
*
* @param model_number - (REQ):Str name of the model.
*
* @author Sara Streeter <sstreeter@axeda.com>
*/
def response = [:]
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
try {
// getUserContext is supported as of release 6.1.5 and higher
final def CONTEXT = Context.getUserContext()
// find the model
def modelFinder = new ModelFinder(CONTEXT)
modelFinder.setName(parameters.model_name)
Model model = modelFinder.findOne()
// throw exception if no model found
if (!model) {
throw new Exception("No model found for ${parameters.model_name}.")
}
// Add a dummy data item
DataItem dataitem = new DataItem(CONTEXT, model, DataItemType.STRING, "MyDataItem");
dataitem.store();
// find the data items on the model
model.dataItems.each{
logger.info(it.name)
if (it.name=="MyDataItem"){
it.delete()
}
}
} catch (def ex) {
xml.Response() {
Fault {
Code('Groovy Exception')
Message(ex.getMessage())
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
Detail(sw.toString())
}
}
}
return ['Content-Type': 'text/xml', 'Content': writer.toString()]