Announcement

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

    Ok to answer my own question - should have read this thread more closely - looks like the object needs to be cast in the GwtRpcDataSource, ie, something like:

    Code:
     protected void copyValues(T from, ListGridRecord to) {
        	DataSourceField[] fields = getFields();
    
        	for (DataSourceField field : fields) {
        		String name = field.getName();
        		Object value = getValueOf(from, name);
        		if (value instanceof Integer){
        			to.setAttribute(name, ((Integer) value).intValue());
        		}
        		else if (value instanceof Date){
        			to.setAttribute(name, (Date) value);
        		}
        		else {
        			to.setAttribute(name, value);
        		}
        	}
    }
    Anyway, keep up the excellent work!

    jd

    Comment


      Can author add the GwtRpcDataSource into SmartGWT?
      User will be easy to follow up the latest version.
      Thanks.

      Comment


        It works! Great! I'll see if I can put it to good use.

        Comment


          Adding (updating) muiltiple records with GWT-RPC

          We have adopted the GWT-RPC code for our project, and it works great. I really like it. Here is a fragment of code from our adopted GWT-RPC implementation class :

          Code:
          		service.add(getRecordType(),getValuesAsMap(new ListGridRecord(data)),new AsyncCallback<HashMap>() {
          			public void onFailure(Throwable caught) {
          				response.setStatus(RPCResponse.STATUS_FAILURE);
          				processResponse(requestId,response);
          			}
          			public void onSuccess(HashMap result) {
          				switch(type.getDataSourceType()) {
          					case GRID:
          						ListGridRecord[] list = new ListGridRecord[1];
          						ListGridRecord newRec = new ListGridRecord();
          						copyValues(result,newRec);
          						list[0] = newRec;
          						response.setData(list);
          						break;
          					case TREE:
          						TreeNode[] tree = new TreeNode[1];
          						TreeNode node = new TreeNode();
          						copyValues(result,node);
          						tree[0] = node;
          						response.setData(tree);
          						break;
          					default:
          						SC.logWarn("unknown datasource type: " + recordType.getDataSourceType());							
          				}
          				processResponse(requestId,response);
          			}
          		});
          and here lies my question. Notice the code:

          Code:
          						ListGridRecord[] list = new ListGridRecord[1];
          						ListGridRecord newRec = new ListGridRecord();
          						copyValues(result,newRec);
          						list[0] = newRec;
          						response.setData(list);
          						break;
          It creates a one-record-long array of Records to be returned via setData() to the grid. The GWT-RPC behavior is that whatever is in that record, will be added as a new record (or, replace the record with same key in case of update) to the grid.

          I wonder if it would be possible to add multiple records in one shot, and have them be added to the grid by creating & returning an array of not just one record, like above, but of several records ? Are there any things to know or be aware of when implementing such a solution ? Would that approach work for the update() function as well ?

          Did anyone successfully implement that approach ?

          D.

          Comment


            TestAdvDataSource

            i tried TestAdv one, i tried listGrid.fetchData(form.getValuesAsCriteria());

            but it will show all data instead of what i defined as in the criteria..

            any idea why?

            Comment


              showing error messages

              hi all,

              thank you for the great solution.

              i am currently using the restDataSource and when an error occurs on a specific record update, my server returns an error msg per record.

              thanks to the great way in which smart handles errors,the grid displays those messages automatically.

              how do i achieve the same with the rpc data source?

              thanks,
              shay

              Comment


                using an exception object

                after reading the code further , i believe the solution might be to throw an exception on error , and put the error messages in the exception object.

                in the onFailure parse them into response.data.

                is that the correct approach?

                Thanks,
                Shay

                Comment


                  Originally posted by lanceweber
                  I just wanted to point out that with the nifty use of generics you can only have to implement this class once. We've got an initial implementation up and running very nicely by using a Resource interface that bridges between Models and SmartGWT constructs.

                  Here's a snippet, just enough to get you started thinking about it and yes, this requires the you have a pretty good handle on coding with generics.

                  Code:
                  public interface HasSmartGWTResources<M extends DTObjectModel> {
                  	List<DataSourceField> getDataSourceFields();
                  
                  	M newModel();
                  	ListGridRecord toListGridRecord(M model);
                  	M toModel(ListGridRecord record);
                  
                  	DvTreeNode toTreeNode(M model, String parentid);
                  	List<DvTreeNode> toTreeNodeWithChildren(M model, String parentid);
                  }
                  
                  }
                  Just out of curiosity, could you tell us more about the DvTreeNode class? Is it a kind of DTObjectModel?

                  These generics aren't half as scary looking as the ninja generics in GWT's source code. ;)

                  Comment


                    Does anyone have an example on how to populate a TreeGrid using the GwtRpcDataSource?

                    I have tried to build an array of TreeNodes in the executeFetch method and set it as return data, but the result was somehow glitchy. The leaf nodes (on the lowest level) were falsely displayed with a plus button to expand their contents, but logically they have no children. When I click the plus button, all records in the TreeGrid disappear.

                    Comment


                      Another question, does anyone have an example for the "executeRemove" method?

                      How is this supposed to work, I mean, how is the displayed record removed from the connected data control? By deleting data on the server and then refreshing data?

                      I have a read-only dataSource connected to a ListGrid and I want to remove records from this list grid. I do not need to contact the server, I just want to delete the corresponding ListGridRecord in presentation tier. How can I achieve this? ListGrid.removeData() does not seem to work, maybe because the ListGrid is connected to a DataSource. An obvious solution would be the usage of disconnected data, but unfortunately, filtering does not work for me then.

                      Comment


                        Originally posted by jballh
                        Does anyone have an example on how to populate a TreeGrid using the GwtRpcDataSource?

                        I have tried to build an array of TreeNodes in the executeFetch method and set it as return data, but the result was somehow glitchy. The leaf nodes (on the lowest level) were falsely displayed with a plus button to expand their contents, but logically they have no children. When I click the plus button, all records in the TreeGrid disappear.

                        You need to be careful how you setup your children nodes ie: make sure you don't reference the children attribute for the child nodes, only for the parent nodes.

                        But in order to work out what your issue is completely you would need to post your code.

                        Comment


                          Hello Charles,

                          Thank you very much. I have already solved the problem. Sorry for not updating my post regarding that issue. First, I used setIsFolder(true) on folder nodes and setIsFolder(false) on leaf nodes. To eliminate the "plus" icon on empty nodes, I had to manually invoke setChildren() on the nodes in question, supplying an empty array of records, like so:

                          Code:
                              if ( !hasChildNodes )
                                setChildren( new TreeNode[0] );

                          Comment


                            Hi, could you give us, more detailed example? What is this DTObjectModel. Pleas provide some simple usage :).

                            Originally posted by lanceweber
                            I just wanted to point out that with the nifty use of generics you can only have to implement this class once. We've got an initial implementation up and running very nicely by using a Resource interface that bridges between Models and SmartGWT constructs.

                            Here's a snippet, just enough to get you started thinking about it and yes, this requires the you have a pretty good handle on coding with generics.

                            Code:
                            public interface HasSmartGWTResources<M extends DTObjectModel> {
                            	List<DataSourceField> getDataSourceFields();
                            
                            	M newModel();
                            	ListGridRecord toListGridRecord(M model);
                            	M toModel(ListGridRecord record);
                            
                            	DvTreeNode toTreeNode(M model, String parentid);
                            	List<DvTreeNode> toTreeNodeWithChildren(M model, String parentid);
                            }
                            
                            
                            public interface DataService<M extends DTObjectModel> extends RemoteService {
                            	List<M> fetch(int startIndex, int count);
                            	M add(M model);
                            	M update(M model);
                            	void remove(M model);
                            }
                            
                            
                            public interface DataServiceAsync<M extends DTObjectModel> {
                            	void fetch(int startIndex, int count, AsyncCallback<List<M>> async);
                            	void add(M model, AsyncCallback<M> async);
                            	void update(M model, AsyncCallback<M> async);
                            	void remove(M model, AsyncCallback<M> async);
                            }
                            
                            
                            public class SmartGWTDataSource<M extends DTObjectModel, R extends HasSmartGWTResources<M>, S extends DataService<M>> extends DataSource {
                            	private HasSmartGWTResources<M> resource;
                            	private DataServiceAsync<M> service;
                            	
                                /**
                                 * Creates new data source which communicates with server by GWT RPC.
                                 * It is normal server side SmartClient data source with data protocol
                                 * set to <code>DSProtocol.CLIENTCUSTOM</code> ("clientCustom" - natively
                                 * supported by SmartClient but should be added to smartGWT) and with data
                                 * format <code>DSDataFormat.CUSTOM</code>.
                                 */
                            	@SuppressWarnings("unchecked")
                            	public SmartGWTDataSource(HasSmartGWTResources<M> resource, Class<S> serviceClass) {
                            		super();
                                    setDataProtocol (DSProtocol.CLIENTCUSTOM);
                                    setDataFormat (DSDataFormat.CUSTOM);
                                    setClientOnly (false);
                            		this.resource = resource;
                            		service = (DataServiceAsync<M>) GWT.create(serviceClass);
                            		for (DataSourceField field: resource.getDataSourceFields()) {
                            			addField(field);
                            		}
                            		GWT.log("SourcebookDataSource() initialized.", null);
                            	}
                            
                            .
                            .
                            .
                            }

                            Comment


                              mnenchev, I agree. This looks very interesting, but also a little confusing at first look. I would also be very interested in a small example using these interfaces too see what this is about.

                              Comment


                                how to call add record

                                hi all,
                                I'm newbie with smartgwt
                                can anybody post small code snippet how to add record to listgrid.

                                I have listgrid and bellow it is add record button
                                add record button opens new window for filling parameters for record.
                                how to add record now ?
                                is it correct if i create ListGridRecord class instance and fill its attributes and
                                call dataSource.addData(listGridRecord); ?
                                if yes it didn't not work i get javascript errors:
                                "(TypeError): null has no properties"

                                is it correct if new window call addNewMyObject(My obj ...) mehtod
                                and after success adding destroy window and fetch data again ?

                                what is correct design pattern ???

                                ____________________
                                Rgards,
                                Paata Lominadze
                                Magticom LTD.

                                Comment

                                Working...
                                X