Open Documentation Menu

CRUD

The d.ecs forms engine can analyse the values of a data source and divide them into the three sections:

  • Create: Values added within d.ecs forms

  • Update: Values changed within d.ecs forms

  • Delete: Values deleted within d.ecs forms

This is done with reference to comparison operations to the last load- or save process. The section Create combines everything that was added to the form. Update only includes changes, and so on.

To use this mechanism the following three methods must be implemented in the server-side (Groovy) script instead of the saveData-method:

  • processCreate

  • processUpdate

  • processDelete

Apart from this, these methods behave just as the saveData-method. The difference is only the pre-filtering into the three sections cretae, update and delete. These methods are only called, if a respective Create, Update or Delete has been performed since creating the form instance or since the latest saving of the form (form.instance.save()).

The following sample script requires a data source (object model) with the properties name, firstname, address, zip, city und nr (each of the type multiple values), an according database table and a matching database alias (myDB).

CRUD data source

def loadData(def ds) {
        def query="SELECT name,vorname,adresse,plz,ort,nr FROM forms.KUNDEN"
        ds.engine.sql.executeAndStore( "myDB", query)
        ds.log.info("Loaded "+ds.data.getTable().size()+" Customers")
        return null
}

def processUpdate(def ds) {
        def query="""UPDATE forms.KUNDEN
                                SET name=?, vorname=?, adresse=?, plz=?, ort=?
                                WHERE nr=?"""

        for (row in ds.data.getTable()) {
                def nr=row.getValue("nr")
                if (nr==null)
                        throw new Exception("Missing nr in $row for processUpdate")

                def name=row.getValue("name")
                def vorname=row.getValue("vorname")
                def adresse=row.getValue("adresse")
                def plz=row.getValue("plz")
                def ort=row.getValue("ort")
                def params = [name,vorname,adresse,plz,ort,nr]

                ds.log.info("Updating Customer "+params)
                ds.engine.sql.executeUpdate("myDB",query, params)
        }
        return null
}

def processCreate(def ds) {
        def query="INSERT INTO forms.KUNDEN VALUES(?,?,?,?,?,?)"

        for (row in ds.data.getTable()) {
                def nr=row.getValue("nr")
                if (nr==null)
                        throw new Exception("Missing nr in $row for processCreate")

                def name=row.getValue("name")
                def vorname=row.getValue("vorname")
                def adresse=row.getValue("adresse")
                def plz=row.getValue("plz")
                def ort=row.getValue("ort")
                def params = [name,vorname,adresse,plz,ort,nr]

                ds.log.info("Creating Customer "+params)
                ds.engine.sql.executeInsert("myDB",query,params)
        }
        return null
}

def processDelete(def ds) {
        def query="DELETE FROM forms.KUNDEN WHERE nr=?"
        for (row in ds.data.getTable()) {
                def nr = row.getValue("nr")
                if (nr==null)
                        throw new Exception("Missing nr in $row for processDelete")

                ds.log.info("Deleting Customer "+nr)
                ds.engine.sql.execute("myDB",query,[nr])
        }
        return null
}