Question

To expand my question, you could say that I want to program in SmartGWT instead of programming into SmartGWT ( http://msmvps.com/blogs/jon_skeet/archive/2008/04/23/programming-quot-in-quot-a-language-vs-programming-quot-into-quot-a-language.aspx ).

I have a 2 column ListGrid, populated with data from the 5 column database table. I don't use a DataSource (more on that later), instead I get the data from the async service and populate it on success like this predmetiGrid.setData(PredmetRecord.convertToContractRecordArray(result)); . The user can edit the data and press the Save button to save it. The way I have implemented the save is:

// repeat this for each edited field 
for (int i=0; i < predmetiGrid.getAllEditRows().length; i++){

        int editedRowIndex = predmetiGrid.getAllEditRows()[i];

        // for each edite get the full record

        PredmetRecord editedRecord = (PredmetRecord)predmetiGrid.getRecord(editedRowIndex);

        // create a new DomainObject - Predmet, and set the ID from the 
        // Row so I have the ID to use for update later

        Integer predmetID = editedRecord.getAttributeAsInt("predmetID");
        Predmet predmet = new Predmet(predmetID);

        // fill Predmet object with either the edited value, or the
        // original value (if only one of the fields was changed and not both)

        String editedNazivPredmeta = (String)predmetiGrid.getEditValues(editedRecord).get("nazivPredmeta");
        boolean isNazivChanged = editedNazivPredmeta != null;
        if (!isNazivChanged){
            editedNazivPredmeta = editedRecord.getAttribute("nazivPredmeta");
        } 
        predmet.setNazivPredmeta(editedNazivPredmeta);

        String editedOpisPredmeta = (String) predmetiGrid.getEditValues(editedRecord).get("opisPredmeta");
        boolean isOpisChanged = editedOpisPredmeta != null;
        if (!isOpisChanged){
            editedOpisPredmeta = editedRecord.getAttribute("opisPredmeta");
        }
        predmet.setOpisPredmeta(editedOpisPredmeta);

        predmetiList.add(predmet);

    }

In another method I call the async service:

public void updatePredmeti(List<Predmet> predmeti) throws RpcException, IllegalArgumentException {

    for (int i=0; i<predmeti.size();i++){
        JdbcPredgledPredmetaDAO.getInstance().updatePredmet(predmeti.get(i));
    }

}

Now there are a few problems with this implementation. The most obvious ones are:

a) I'm not using a Datasource conected with the ListGrid. I don't use it because I don't understand how to use it in my case since the examples are written either for an XML DataSource or for the SmartGWT Pro (or higher) integrated server.

b) The async method needs to have a rollback mechanism if one of the inserts fail, though there could be a smarter implementation of this (e.g. do all inserts in one transaction).

c) I'm "hacking" to get and update the data instead of using object methods/properties but this is, currently, the best I got form the JavaDoc; I'd prefer to see best practice way to write this and learn

I'm using SmartGWT LGPL 3.0, Tomcat 7.0, Java 1.6

Was it helpful?

Solution

You can use a custom Datasource. DataSource.setDataFormat(DSDataFormat.CUSTOM). With this setting the DataSource will not handle the response, instead you have to parse it with transformResponse().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top