Announcement

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

    #16
    BTW I was not able to use DSRequest.parentNode. It appeared to be null in most cases.
    It's present when fetching children only.

    Suggestion for a possible enhancement? ... perhaps a feature that allows specifying certain non-primary-key fields that must be provided along with primary-key values in data source requests
    The combination of dsRequest.oldValues and dsRequest.parentNode would seem to cover this already, for all scenarios.

    Comment


      #17
      It's not the use case described here, exactly, but I found that I was able to edit/update a primary key by using the addRelatedUpdates approach outlined earlier. For what it's worth, with the sample 'employees' datasource and a 10/26 4.1d Power build:

      Code:
          public void onModuleLoad() {
      
          	TreeGrid grid = new TreeGrid();
          	DataSource ds = DataSource.get("employees");
          	
          	grid.setWidth(400);
          	grid.setHeight(800);
          	grid.setCanEdit(true);
          	grid.setAutoFetchData(true);
      
          	grid.setDataSource(ds);
          	grid.setUpdateOperation("updatePrimaryKey");
          	grid.setFields(new ListGridField("Name"), new ListGridField("EmployeeId"));
          	
          	VLayout layout = new VLayout();
          	layout.addMember(grid);
          	
          	layout.draw();
          }
      Code:
      public class UpdatePrimaryKey {
      
      	private static Logger log = new Logger(UpdatePrimaryKey.class);
      	
      	public DSResponse update(DSRequest request) throws Exception {
      
      		DataSource ds = request.getRPCManager().getDataSource(request.getDataSourceName());
      
      		Object oldKey = null;
      		Map<String, Object> oldValuesMap = request.getOldValues();
      		if (oldValuesMap != null) {
      			oldKey = oldValuesMap.get("EmployeeId");
      		}
      		
      		Object key = request.getFieldValue("EmployeeId");
      		if (oldKey == null || oldKey.equals(key)) {
      			return request.execute();
      		}
      
      		//first update the record as if the key were not being changed
      		request.setCriteria("EmployeeId", oldKey);
      		request.setFieldValue("EmployeeId", oldKey);
      		
      		DSResponse response = request.execute();
      
      		//then delete the row
      		DSRequest delete = new DSRequest(ds.getName(), DataSource.OP_REMOVE, request.getRPCManager());
      		delete.setCriteria("EmployeeId", oldKey);
      		DSResponse deleted = delete.execute();
      
      		//and add the complete record, as returned by request.execute, back with the new key 
      		DSRequest add = new DSRequest(ds.getName(), DataSource.OP_ADD, request.getRPCManager());
      		add.setValues(response.getDataMap());
      		add.setFieldValue("EmployeeId", key);		
      		DSResponse added = add.execute();
      		
      		//finally, update any related nodes to take the new key
      		DSRequest update = new DSRequest(ds.getName(), DataSource.OP_UPDATE, request.getRPCManager());
      		update.setAllowMultiUpdate(true);
      		update.setCriteria("ReportsTo", oldKey);
      		update.setFieldValue("ReportsTo", key);
      		DSResponse updated = update.execute();
      		
      		//include the value of the record as it was before it was updated, so the client can match one last time for cache sync
      		response.setFieldValue("EmployeeId", oldKey);
      		
      		//and let it know about the fresh record we replaced it with
      		response.addRelatedUpdate(added);
      		response.addRelatedUpdate(deleted);
      		
      		if (updated.getAffectedRows() > 0) {
      			response.addRelatedUpdate(updated);				
      		}
      		
      		return response;
      		
      	}
      }

      Comment

      Working...
      X