Version: 22.10
Loading Reference Data using the Groovy Service Invoke REST API
The example Groovy Service below loads a product catalog CSV file into a client property named "Product Catalog" belonging to the "Maguire" organization.
Fluent Groovy Script
The REST client makes a multipart post request with the CSV file uploaded using the request parameter name products
. The script gets the request FileItem
object params.products
, and converts it's byte data into text using the UTF-8 charset.
The script then loads the products CSV data into the database using a PropertyBuilder
. This service creates or updates any existing property value with the same name.
import com.avoka.core.groovy.GroovyLogger as logger
import com.avoka.tm.svc.*
import com.avoka.tm.vo.*
import javax.servlet.http.*
import org.apache.commons.fileupload.FileItem
class FluentGroovyService {
/*
* Perform a groovy service invocation
*
* return: the result object
*/
Object invoke(SvcDef svcDef, HttpServletRequest request, User user, Map params) {
// Get the products file upload file item
FileItem fileItem = (FileItem) params.products
logger.info "loading file: " + fileItem.name
// Convert the fileItem binary data to CSV text
String productsCSV = new String(fileItem.get(), "UTF-8")
// Create or update the Organizations "Product Catalog" property
new PropertyBuilder()
.setName("Product Catalog")
.setValue(productsCSV)
.setClientCode(svcDef.clientCode)
.build()
String msg = "Imported '" + fileItem.name + "' file into 'Product Catalog' organization property"
logger.info msg
return msg
}
}
Response
When the script executes, it returns a 200 OK
response like this:
Imported 'product-catalog-q1.csv' file into 'Product Catalog' organization property