Problem with hidden PK's and Forms

Hi

I took the simple Master-Detail example and added an Add functionality. I changed the DataSource to work with my backend REST service (see code below).

Btw: I’m using 1.0b3 from the Maven repository (fetched sunday the 26th of April).

How ever I have a problem with the Primarykey when ADDING to the DataSource - It want it to be my serverside generated key, which I will provide the entity with when calling Add on the server. On the server I return the changed entity with its PK value filled (as I understand this is how SmartGWT works with ADDs, please correct me if I’m wrong).

However since the DynamicForm doesn’t show the clientUuid (the PK field) visually things “fail” when adding to the DataSource unless I provide a PK value on the client side when adding, which I would rather avoid.

Here’s briefly what happens: I click the Add button, enter some data, press Save and the entity gets added to the DataSource (the ListGrid updates) and things look good.
I then click the entity in the ListGrid to edit it again, change a value and press Save and perform the UPDATE. Now I have TWO entities in the ListGrid.

If I provide a clientside value for clientUuid (the PK) when calling form.editNewRecord(), like this:

addButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                Map<Object, Object> map = new HashMap<Object, Object>();
                map.put("clientUuid", UUID.uuid());
                form.editNewRecord(map);
            }
        });

then everything works as expected and an add and following update and I only have one Entity in the ListGrid. I’ve set the PK field in the DataSource to not required to indicate (as I understand it), that the PK field will receive it’s value on the serverside.

Is this a bug or am I doing something wrong?

Here’s what is sent back and forth when the PK value isn’t set on the client side (the Dynamicform clearly doesn’t send the clientUuid value on update):
ADD (HTTP - Post):

<VehicleDS>
    <licensePlateNumber>plate1</licensePlateNumber>
    <name>plate2</name>
    <startMileage>123</startMileage>
</VehicleDS>

ADD (HTTP - Response)

<VehicleDS>
  <clientUuid>8ee42132-54b1-4f97-844f-9838dbfc4a83</clientUuid>
  <licensePlateNumber>plate1</licensePlateNumber>
  <name>plate2</name>
  <startMileage>123</startMileage>
</VehicleDS>

UPDATE (HTTP - Post):

<VehicleDS>
    <licensePlateNumber>Xplate1</licensePlateNumber>
    <name>plate2</name>
    <startMileage>123</startMileage>
</VehicleDS>

UPDATE (HTTP - Response)

<VehicleDS>
  <clientUuid>a0d9f47f-e54a-4a0d-bef7-14eea0438bb6</clientUuid>
  <licensePlateNumber>Xplate1</licensePlateNumber>
  <name>plate2</name>
  <startMileage>123</startMileage>
</VehicleDS>

Datasource definition

public class VehicleDS extends DataSource {
    private static VehicleDS instance = null;

    public VehicleDS(java.lang.String id) {
        setID(id);

        DataSourceTextField pkField = new DataSourceTextField("clientUuid",
                "clientUuid");
        pkField.setPrimaryKey(true);
        pkField.setHidden(true);
        pkField.setRequired(false);

        DataSourceTextField licensePlateNumberField =
                new DataSourceTextField("licensePlateNumber",
                        "licensePlateNumber");
        DataSourceTextField nameField = new DataSourceTextField("name",
                "name");
        DataSourceFloatField startMileageField = new DataSourceFloatField("startMileage",
                "startMileage");
        setFields(pkField,
                licensePlateNumberField, nameField, startMileageField);
        setRecordXPath("/List/VehicleDS");
        setDataURL("/rest/vehicle");

        OperationBinding addOperation = new OperationBinding(DSOperationType.ADD,
                "/rest/vehicle");
        addOperation.setDataProtocol(DSProtocol.POSTXML);

        OperationBinding updateOperation = new OperationBinding(DSOperationType.UPDATE,
                "/rest/vehicle");
        updateOperation.setDataProtocol(DSProtocol.POSTXML);

        OperationBinding fetchOperation = new OperationBinding(DSOperationType.FETCH,
                "/rest/vehicle");
        fetchOperation.setDataProtocol(DSProtocol.GETPARAMS);

        OperationBinding removeOperation = new OperationBinding(DSOperationType.REMOVE,
                "/rest/vehicle");
        removeOperation.setDataProtocol(DSProtocol.GETPARAMS);
        setOperationBindings(addOperation, updateOperation, fetchOperation,
                removeOperation);
    }

    public static VehicleDS getInstance() {
        if (instance == null) {
            instance = new VehicleDS("VehicleDS");
        }

        return instance;
    }
}

UI code:

        VLayout layout = new VLayout(15);

        Label label = new Label();
        label.setHeight(10);
        label.setWidth100();
        label.setContents("Intro....");
        layout.addMember(label);

        final DataSource dataSource = 
                VehicleDS.getInstance();

        final DynamicForm form = new DynamicForm();
        form.setIsGroup(true);
        form.setGroupTitle("Update");
        form.setNumCols(4);
        form.setDataSource(dataSource);


        final ListGrid listGrid = new ListGrid();
        listGrid.setWidth100();
        listGrid.setHeight(200);
        listGrid.setDataSource(dataSource);
        listGrid.setAutoFetchData(true);
        listGrid.addRecordClickHandler(new RecordClickHandler() {
            public void onRecordClick(RecordClickEvent event) {
                form.reset();
                form.editSelectedData(listGrid);
            }
        });

        layout.addMember(listGrid);
        layout.addMember(form);

        IButton addButton = new IButton("Add");
        addButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                //Map<Object, Object> map = new HashMap<Object, Object>();
                //map.put("clientUuid", UUID.uuid());
                //form.editNewRecord(map);
                form.editNewRecord();
            }
        });
        layout.addMember(addButton);
        IButton saveButton = new IButton("Save");
        saveButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                form.saveData();
                listGrid.saveAllEdits();
            }
        });
        layout.addMember(saveButton);

/Jeppe

Your understanding of the correct response messages is correct, however note that you should be calling editRecord() to explicitly tell the form that the record is pre-existing. You currently call editSelectedData().