Announcement

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

    Sending modified datasource back to server via GWT-RPC

    Hey together,

    sorry to bother you, but I am kind of new to all this and a bit confused. I do not know how to get my modified DataSource and its records (those which were modified) to send them back to my server via GWT-RPC.

    I managed to receive data via GWT-RPC. This works fine. I realized it using this thread: http://forums.smartclient.com/showthread.php?t=3538

    I used this Master Detail example (http://www.smartclient.com/smartgwt/..._master_detail) and show my received data in the grid. A click on a record shows the data in the form.

    Now, what steps do I have to take if the user has modified the data in the form and hits the Save button? How to I get my modified DataSource and its records? I put notes in the code below to show where I am stuck.

    This is my class PersonPanel (its like the Master Detail example):
    Code:
    public class PersonPanel extends Canvas
    {
    
    	public PersonPanel()
    	{
    		VLayout layout = new VLayout(15);
    
    		Label label = new Label();
    		label.setHeight(10);
    		label.setWidth100();
    		label.setContents("Persons");
    		layout.addMember(label);
    
    		final DataSource dataSource = PersonDS.getInstance();
    
    		final DynamicForm form = new DynamicForm();
    		form.setIsGroup(true);
    		form.setGroupTitle("Update");
    		form.setNumCols(3);
    		form.setDataSource(dataSource);
    
    		final ListGrid listGrid = new ListGrid();
    		listGrid.setWidth(200);
    		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 button = new IButton("Save");
    		button.addClickHandler(new ClickHandler() {
    			public void onClick(ClickEvent event)
    			{
    				form.saveData();
    				
    // - - - - - This is where I don not know what to do - - - - - -
    			 
    // Do I have to call a method in my DataSource object (PersonDS)
    // If yes, how do I get my DataSource records?
    // How do I know which record has changed / was modified?
    				
    				
    			}
    		});
    		
    		layout.addMember(button);
    		layout.draw();
    	}
    
    }
    My class PersonDS which extends DataSource
    Code:
    public class PersonDS extends DataSource
    {
    
    	private PersonServiceAsync personService;
    
    	private static PersonDS instance = null;
    
    	public static PersonDS getInstance()
    	{
    		if (instance == null) {
    			instance = new PersonDS("personeDS");
    		}
    		return instance;
    	}
    
    	public PersonDS(String identifier)
    	{
    		setID(identifier);
    		setClientOnly(true);
    
    		DataSourceIntegerField id = new DataSourceIntegerField("id", "Id");
    		DataSourceTextField firstName = new DataSourceTextField("firstName",
    			"First Name");
    		DataSourceTextField lastName = new DataSourceTextField("lastName",
    			"Last Name");
    
    		setFields(id, firstName, lastName);
    		loadData();
    	}
    
    	private void loadData()
    	{
    		if (personService == null) {
    			personService = (PersonServiceAsync) GWT
    				.create(PersonService.class);
    		}
    
    		personService.getData(new AsyncCallback<List<Person>>() {
    
    			public void onFailure(Throwable caught)
    			{
    				System.out.println("Problem getting data from server");
    				caught.printStackTrace();
    			}
    
    			public void onSuccess(List<Person> result)
    			{
    				// Map Part-Entities to ListGridRecords
    				for (Person person : result) {
    					addData(new PersonLGR(person.getId(),
    						person.getFirstName(),
    						person.getLastName()));
    				}
    
    				// Print received data
    				System.out.println("Persons received from remote.....");
    				System.out.println("Number of received persons: " + result
    					.size());
    				for (Person p : result) {
    					System.out.println(p.getId() + "/"
    						+ p.getFirstName()
    						+ "/"
    						+ p.getLastName());
    				}
    
    			}
    
    		});
    	}
    
    // - - - - - This is where I don not know what to do - - - - - -
    	public void sendModifiedDataToServer()
    	{
    		// ???????
    		// I think I have to do something like this
    		// ListGridRecords[] modifiedRecords = this.getRecords(); <-- getRecords does not exist.
    		// for(ListGridRecords lgr : modifiedRecords) {
    		//     PersonLGR personLGR = (PersonLGR) lgr;
    		//     .....
    	}
    
    }
    My class PersonLGR which extend ListGridRecord
    Code:
    public class PersonLGR extends ListGridRecord
    {
    	
    	public PersonLGR() {
    		
    	}
    	
    	public PersonLGR(int id, String firstName, String lastName){
    		setAttribute("id", id);
    		setAttribute("firstName", firstName);
    		setAttribute("lastName", lastName);
    	}
    
    	public int getId() {
    		return getAttributeAsInt("id");
    	}
    
    	public String getFirstName() {
    		return getAttributeAsString("firstName");
    	}
    
    	public String getLastName() {
    		return getAttributeAsString("lastName");
    	}
    
    }

    Thanks in advance for your help.

    Syd

    #2
    You have to use transformResponse.

    This is an example from my CustomDataSource class:
    In the executeXXX methods there is the actual comunication with GWT service.

    Code:
    	@Override
    	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
    		if (request.getOperationType() != null) {
    			Log.debug(getID() + " execute operation " + request.getOperationType());
    			ListGridRecord[] lstRec = response.getData();
    			switch (request.getOperationType()) {
    				case ADD:
    					executeAdd(lstRec, true);
    					break;
    				case FETCH:
    					executeFetch();
    					break;
    				case REMOVE:
    					executeRemove(lstRec);
    					break;
    				case UPDATE:
    					executeAdd(lstRec, false);
    					break;
    
    				default:
    					break;
    			}
    		}
    		super.transformResponse(response, request, data);
    	}

    Comment


      #3
      @uberto,

      thank a lot for your quick help!! This works excelent!

      @All

      setPrimaryKey(true) is missing in my code above.

      Comment


        #4
        Hi Syd.
        can you post your DataSource class after the changes ?
        Im doing something similar, but im still didnt understood how to get and send the modified ListRecords to server.

        Tks
        Last edited by mrquejo; 17 Feb 2009, 16:10.

        Comment


          #5
          I am using the setTestData to load from server and getTestData to process changes and send them back to server.

          Comment


            #6
            @mrquejo

            Here is my code. To keep it short, I have implemented UPDATE only.

            Code:
            public class PersonDSRPC extends DataSource
            {
            
            	private PersonServiceAsync personService;
            
            	private static PersonDSRPC instance = null;
            
            	public static PersonDSRPC getInstance()
            	{
            		if (instance == null) {
            			instance = new PersonDSRPC("personeDS");
            		}
            		return instance;
            	}
            
            	public PersonDSRPC(String identifier)
            	{
            		setID(identifier);
            		setClientOnly(true);
            
            		DataSourceIntegerField id = new DataSourceIntegerField("id", "Id");
            		DataSourceTextField firstName = new DataSourceTextField("firstName",
            			"First Name");
            		DataSourceTextField lastName = new DataSourceTextField("lastName",
            			"Last Name");
            
            		id.setPrimaryKey(true);
            		setFields(id, firstName, lastName);
            		loadData();
            	}
            
            	private void loadData()
            	{
            		if (personService == null) {
            			personService = (PersonServiceAsync) GWT
            				.create(PersonService.class);
            		}
            
            		personService.getData(new AsyncCallback<List<Person>>() {
            
            			public void onFailure(Throwable caught)
            			{
            				System.out.println("Problem getting data from server");
            				caught.printStackTrace();
            			}
            
            			public void onSuccess(List<Person> result)
            			{
            				// Map Part-Entities to ListGridRecords
            				for (Person person : result) {
            					addData(new PersonLGR(person.getId(),
            						person.getFirstName(),
            						person.getLastName()));
            				}
            
            				// Print received data
            				System.out.println("Persons received from remote.....");
            				System.out.println("Number of received persons: " + result
            					.size());
            				for (Person p : result) {
            					System.out.println(p.getId() + "/"
            						+ p.getFirstName()
            						+ "/"
            						+ p.getLastName());
            				}
            
            			}
            
            		});
            	}
            
            	@Override
            	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
            		if (request.getOperationType() != null) {
            			System.out.println(getID() + " execute operation " + request.getOperationType());
            			PersonLGR[] records = (PersonLGR[]) response.getData();
            			switch (request.getOperationType()) {
            				case ADD:
            					//executeAdd(lstRec, true);
            					break;
            				case FETCH:
            					//executeFetch();
            					break;
            				case REMOVE:
            					//executeRemove(lstRec);
            					break;
            				case UPDATE:
            					// Map records to entities
            					Person[] persons = new Person[records.length];
            					for(int i = 0; i < records.length; i++) {
            						persons[i] = new Person(
            							records[i].getId(), 
            							records[i].getFirstName(), 
            							records[i].getLastName());
            					}
            					
            					if (personService == null) {
            						personService = (PersonServiceAsync) GWT
            							.create(PersonService.class);
            					}
            
            					personService.updateData(persons, new AsyncCallback<String>() {
            
            						public void onFailure(Throwable caught)
            						{
            							System.out.println("Problem getting data from server");
            							caught.printStackTrace();
            						}
            
            						public void onSuccess(String result)
            						{
            							// result contains string
            							System.out.println(result); 
            						}
            
            					});
            
            					break;
            
            				default:
            					break;
            			}
            		}
            		super.transformResponse(response, request, data);
            	}
            
            }

            Comment


              #7
              Tks a lot Syd.
              Now i can send my data back to server \o/

              Comment


                #8
                @Syd

                I checked out javadoc but could not find the given version of transformResponse described.
                Also, to submit dynamicForm's data to the server do we need to use the same approach.

                Regards,
                +Abhishek

                Comment


                  #9
                  @ajjain

                  you find the javadoc for transformResponse here:

                  http://www.smartclient.com/smartgwt/...a.lang.Object)

                  As far as I know (remember I am new to SmartGWT as well and someone should correct me if I am wrong on this) the approach is valid for all databinding-capable UI-components.

                  Comment


                    #10
                    I understand what has been
                    posted thus far (I think), but I'm a little confused on how we use transformResponse once we add it to the DS class.

                    Could you post the code you use where the transformResponse method is called? I'm just not quite sure what to pass in as parameters (where do you get DSResponse, DSRequest, and Object from to pass as parameters?)

                    Comment


                      #11
                      I think this method is called automatically, isn't it ?

                      Regards

                      Comment


                        #12
                        Originally posted by olinsha
                        I think this method is called automatically, isn't it ?

                        Regards
                        If it is called automatically, what signals it to be called? Certain DataSource methods?

                        Comment


                          #13
                          when i try to override transformResponse ,
                          I get this error
                          The method transformResponse(DSResponse, DSRequest, Object) is undefined for the type DataSource

                          Comment


                            #14
                            i resolved the issue, I was using the old jar file.

                            Comment


                              #15
                              Originally posted by Syd
                              @mrquejo

                              Here is my code. To keep it short, I have implemented UPDATE only.

                              Code:
                              public class PersonDSRPC extends DataSource
                              {
                              
                              	private PersonServiceAsync personService;
                              
                              	private static PersonDSRPC instance = null;
                              
                              	public static PersonDSRPC getInstance()
                              	{
                              		if (instance == null) {
                              			instance = new PersonDSRPC("personeDS");
                              		}
                              		return instance;
                              	}
                              
                              	public PersonDSRPC(String identifier)
                              	{
                              		setID(identifier);
                              		setClientOnly(true);
                              
                              		DataSourceIntegerField id = new DataSourceIntegerField("id", "Id");
                              		DataSourceTextField firstName = new DataSourceTextField("firstName",
                              			"First Name");
                              		DataSourceTextField lastName = new DataSourceTextField("lastName",
                              			"Last Name");
                              
                              		id.setPrimaryKey(true);
                              		setFields(id, firstName, lastName);
                              		loadData();
                              	}
                              
                              	private void loadData()
                              	{
                              		if (personService == null) {
                              			personService = (PersonServiceAsync) GWT
                              				.create(PersonService.class);
                              		}
                              
                              		personService.getData(new AsyncCallback<List<Person>>() {
                              
                              			public void onFailure(Throwable caught)
                              			{
                              				System.out.println("Problem getting data from server");
                              				caught.printStackTrace();
                              			}
                              
                              			public void onSuccess(List<Person> result)
                              			{
                              				// Map Part-Entities to ListGridRecords
                              				for (Person person : result) {
                              					addData(new PersonLGR(person.getId(),
                              						person.getFirstName(),
                              						person.getLastName()));
                              				}
                              
                              				// Print received data
                              				System.out.println("Persons received from remote.....");
                              				System.out.println("Number of received persons: " + result
                              					.size());
                              				for (Person p : result) {
                              					System.out.println(p.getId() + "/"
                              						+ p.getFirstName()
                              						+ "/"
                              						+ p.getLastName());
                              				}
                              
                              			}
                              
                              		});
                              	}
                              
                              	@Override
                              	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
                              		if (request.getOperationType() != null) {
                              			System.out.println(getID() + " execute operation " + request.getOperationType());
                              			PersonLGR[] records = (PersonLGR[]) response.getData();
                              			switch (request.getOperationType()) {
                              				case ADD:
                              					//executeAdd(lstRec, true);
                              					break;
                              				case FETCH:
                              					//executeFetch();
                              					break;
                              				case REMOVE:
                              					//executeRemove(lstRec);
                              					break;
                              				case UPDATE:
                              					// Map records to entities
                              					Person[] persons = new Person[records.length];
                              					for(int i = 0; i < records.length; i++) {
                              						persons[i] = new Person(
                              							records[i].getId(), 
                              							records[i].getFirstName(), 
                              							records[i].getLastName());
                              					}
                              					
                              					if (personService == null) {
                              						personService = (PersonServiceAsync) GWT
                              							.create(PersonService.class);
                              					}
                              
                              					personService.updateData(persons, new AsyncCallback<String>() {
                              
                              						public void onFailure(Throwable caught)
                              						{
                              							System.out.println("Problem getting data from server");
                              							caught.printStackTrace();
                              						}
                              
                              						public void onSuccess(String result)
                              						{
                              							// result contains string
                              							System.out.println(result); 
                              						}
                              
                              					});
                              
                              					break;
                              
                              				default:
                              					break;
                              			}
                              		}
                              		super.transformResponse(response, request, data);
                              	}
                              
                              }
                              after 2 days spent to think about how I can put a calendar in place with server interaction and after reading a lot of things on this forum your example saved me ;)

                              Thanks for that !

                              Regards

                              Comment

                              Working...
                              X