Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Getting generated key from the server after insertion

    Hi,

    I have a master form bound to a data source that can fetch, create, update, delete datas, and other dependant forms bound to their data sources too.

    My question is about creation, which is called from masterForm.saveData(): server-side, a key is generated after the creation, and so far I return an XML structure like this:
    <result code="1"><key>186</key></result>
    My intention is to get the generated key and call the saveData() method of the dependant forms after setting the key.

    I try to read the generated key with a DSCallback() but unfortunately, the response is a DSResponse and although it is mentionned here http://forums.smartclient.com/showth...=generated+key that we can get the generated ID, I don't know how to with SmartGWT: the only method available is response.getData() that return a ListGridRecord[] which is empty, and the other Object rawData is simply null.

    So ,where is my ID and how to get it ?
    Thanks for your help !
    Last edited by ppoulard; 17 May 2009, 23:42.

    #2
    Finally, I found the solution: we have to act on the master data source:

    -first, we create a new data source that reflects the XML structure returned by the server <result code="1"><key>186</key></result>:
    Code:
    public class Result extends DataSource {
        private static final Result RESULT = new Result();
        public static Result getInstance() {
            return RESULT;
        }
        private Result() {
            DataSourceIntegerField key = new DataSourceIntegerField("key");
            key.setValueXPath("key");
            setFields(key);
        }
    }
    -then, we bind it to the "insert" operation of the master data source:
    Code:
            .../...
            OperationBinding insert = new OperationBinding();
            insert.setDataURL("some/path/for/insertion.xml");
            insert.setOperationType(DSOperationType.ADD);
            insert.setDataProtocol(DSProtocol.POSTPARAMS);
            insert.setRecordXPath("/result");
            insert.setResponseDataSchema(Result.getInstance());
            .../...
    -when saving the master form, we just have to check if our ID was set, and take it from the result DS when necessary:
    Code:
           this.form.saveData(
                new DSCallback() {
                    public void execute(DSResponse response, Object rawData,
                            DSRequest request) {
                        if (id == null) {
                            ListGridRecord[] records = response.getData();
                            if (records != null && records.length > 0) {
                                id = records[0].getAttributeAsString("key");
                                // update this id to other dependant forms                           
                            } // else something wrong occurs :(
                        }
                        // call saveData() for all dependant forms
                    }
                }
            );
    Last edited by ppoulard; 7 May 2009, 04:18.

    Comment

    Working...
    X