Dec 07

GWT-Ext Store to SmartGWT DataSource

Tag: Ext, Gwt, Java, Programming, smartgwtAbhijeet Maharana @ 2:18 am

GWT-Ext Store instances are all over my code. And I am populating them by various means. While migrating from GWT-Ext to SmartGWT, converting Stores to DataSources will probably take up a lot of time. I was wondering if I could migrate and test only the grids first and deal with the stores later. Code below could help do just that. It creates a DataSource from a Store. You can use this data source to populate SmartGWT grids and when the grids are working fine, replace the stores completely. Isn’t exactly a life saver but nevertheless. Have tested it only on a trivial store.

It doesn’t matter but assume that the RecordDef used to create the Store looks like this:

RecordDef recordDef = new RecordDef(new FieldDef[] {
	new StringFieldDef("company"), 
	new FloatFieldDef("price"),
	new FloatFieldDef("change"), 
	new FloatFieldDef("pctChange"),
	new DateFieldDef("lastChanged", "n/j h:ia"),
	new StringFieldDef("symbol"), 
	new StringFieldDef("industry") 
});

Create a DataSource with the same fields:

private DataSource createDataSource(Store store){
	DataSource dataSource = new DataSource();
	dataSource.setClientOnly(true);
 
	String[] fields = store.getFields();
	for(String field:fields) {
		DataSourceField smartgwtField = new DataSourceField();
		smartgwtField.setName(field);
		dataSource.addField(smartgwtField);
	}
	return dataSource;
}

Add a method that copies records from the Store to the data source created above and call it in the Store’s onLoad handler

public void copyStore(Store store, DataSource dataSource) {
	Record[] records = store.getRecords();
	String[] fields = store.getFields();
 
	for(Record gwtextRecord:records) {
		ListGridRecord smartgwtRecord = new ListGridRecord();
		for(String field:fields) {
			smartgwtRecord.setAttribute(field, gwtextRecord.getAsString(field));
		}
		dataSource.addData(smartgwtRecord);
	}
}

For heavy stores, this code will probably punish the browser and column data types are ignored as well but it is meant only for the dev environment until all components have been replaced eventually.

PS: If you need it, here is a small sample with GWT-Ext and SmartGWT grids being populated from the same RPC method. You will need to add gwtext.jar and smartgwt.jar and use Cypal studio plugin or add compile/run scripts.

12 Responses to “GWT-Ext Store to SmartGWT DataSource”

  1. dellsoft says:

    how to use gwt-rpc json data for smartgwt?
    thanks

  2. Abhijeet Maharana says:

    Haven’t tried this but I assume you can use JSONParser to convert the JSON string returned by RPC to a JSON object. Then populate the DataSource as done in the sample project.

    http://www.gwtapps.com/doc/html/com.google.gwt.json.client.JSONParser.html

  3. Suzuki says:

    Is there way to clear a DataSource, to refresh content from server? I am using GWT-RPC + Hibernate on server side. Till now I didn’t find any simple way to use DataSource with Hibernate, someone did ?

  4. Suzuki says:

    It seems that I will answer myself :) It it enough to call grid.setData(new ListGridRecord[]{});

  5. xaxera says:

    Is there a way to submit changes in the store to the database and how?
    I am using GWT Ext and still haven’t migrated to smart GWT because of the incompatibility between those 2 libs.
    My question is how to submit changes in the store or datasource to the database!

  6. Abhijeet Maharana says:

    Store..getModifiedRecords() combined with RPC?
    I am not sure if Store provides any direct way to integrate with the backend such that modified data is automatically saved.

  7. xaxera says:

    Maybe there isn’t!I dealt with the problem by getting all records using store.getRecords() and by manually persisting and updating them!

  8. xaxera says:

    I have another problem:
    I want to load store from xml to a combobox and use it as suggestion
    Could You give one very simple example how to do that?

    My Code is:

    HttpProxy dataProxy = new HttpProxy(“data/alerts.xml”);
    final String resultTpl = “{SerialKey}”;
    RecordDef alertsRecordDef =
    new RecordDef(new FieldDef[] { new IntegerFieldDef(“SerialKey”,”SerialKey”)});
    XmlReader xmlreader = new XmlReader(“Alert”,alertsRecordDef);
    xmlreader.setRecord(“Alert”);

    Store store = new Store(dataProxy, xmlreader);
    store.load();
    Record[] records = store.getRecords();
    int recordNum = records.length;
    MessageBox.alert(recordNum+”");
    ComboBox cb = new ComboBox();
    cb.setStore(store);
    cb.setDisplayField(“SerialKey”);
    cb.setId(“SerialKey”);
    cb.setTypeAhead(false);
    cb.setLoadingText(“Searching…”);
    cb.setFieldLabel(“SerialKey”);
    cb.setWidth(130);
    cb.setTpl(resultTpl);
    cb.setPageSize(10);
    cb.setHideTrigger(false);
    cb.setMode(ComboBox.REMOTE);
    cb.setLazyRender(true);

    cb.setTitle(“Alerts”);
    //cb.setHideLabel(true);
    cb.setItemSelector(“div.search-item”);

    RootPanel.get().add(cb);

    this code gives me 0 records!
    Please help!

  9. Abhijeet Maharana says:

    Hi xaxera, I wont be able to test this code. Try posting your query in the gwt-ext forums at http://gwt-ext.com/forum. I am positive someone there must have an explanation.

    Regards,
    Abhijeet.

  10. Kaushal says:

    I’m working on Java application in which I’ve to use either ‘GWT-Ext’ or ‘Smart GWT’.

    I’m totally new face to Java World & i don’t know , which tool I’ve to use for developing an application.

    I read somewhere that GWT-Ext would require some kind of license & it’s old compare to ‘Smart GWT’.

    I would require your opinion for this. Can you please tell me that what should I use? ‘GWT-Ext’ or ‘Smart GWT’ ?

    Your suggestion would be appreciated.

  11. Abhijeet Maharana says:

    Hi Kaushal, the answer to this really depends on what you are trying to achieve and your target environment. There are many variables that need to be considered before making a decision. I recommend that you check both the forums out. You will be able to take a balanced decision after following the related discussions on the forums.

  12. Emanuel says:

    Hello. I’m working on an application with gwt-ext. Now I’m trying to copy the store from the grid to a smartGwt DataSource and then into a smartGwt ListGrid.

    I tryed to use the way, Abhijeet is showing above.

    This is my code:

    DataSource dataSource = new DataSource();
    dataSource.setClientOnly(true);
    Store store = reportGrid.getStore(); //gets the store from GWT EXT grid
    String [] fields = store.getFields();
    Record[] records = store.getRecords();

    for(String field:fields){
    DataSourceField smartgwtField = new DataSourceField();
    smartgwtField.setName(field);
    dataSource.addField(smartgwtField);
    }

    for(Record gwtRecord : records){
    ListGridRecord smrtRecord = new ListGridRecord();
    for(String field : fields){
    smrtRecord.setAttribute(field, gwtRecord.getAsString(field));
    }
    dataSource.addData(smrtRecord);
    }

    ListGrid printGrid = new ListGrid();
    printGrid.setHeight(Window.getClientHeight()-50);
    printGrid.setDataSource(dataSource);

    SectionStack printStack = new SectionStack();
    printStack.setVisibilityMode(VisibilityMode.MULTIPLE);
    printStack.setWidth(Window.getClientWidth()-50);
    printStack.setHeight(Window.getClientHeight()-50);

    SectionStackSection costAreaSection = new SectionStackSection(“CostArea”);
    costAreaSection.setExpanded(true);
    costAreaSection.addItem(printGrid);
    printStack.addSection(costAreaSection);

    VLayout printContainer = new VLayout(10);
    printContainer.addMember(printStack);
    printContainer.draw();

    So I don’t know why it won’t work.

Leave a Reply