Announcement

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

    TreeNode and Gwt RPC - Help?

    (I've moved my question out of GWT-RPC DataSource implementation, because I can't tell if anyone has viewed it...)

    I need some help understanding how to set the primary key and foreign key for TreeNodes, using the GwtDataSource example. I'm building a TreeGrid, but the nodes are displayed at the same root level and reference the original nodes, so they unfold recursively.

    Here's an example with a static list of two nodes. Record 1 is the root and record 6 refers (or connectsTo) record 1:

    Code:
    public class ServiceNodesServiceImpl 
    		extends RemoteServiceServlet 
    		implements ServiceNodesService, Serializable, IsSerializable {
    	static List<ServiceNodeRecord> staticList = new ArrayList<ServiceNodeRecord>();
    	static ServiceNodeRecord record = null;		
    	// load static data for testing
    	static {	
    		record = new ServiceNodeRecord();
    		record.setConnectsTo("0");
    		record.setNodeId("1");
    		record.setInstitutionId("220");
    		record.setServiceName("hub-dev");
    		record.setNodeType("HUB");
    
    		staticList.add(record);
    
    		record = new ServiceNodeRecord();		
    		
    		record.setConnectsTo("1");		
    		record.setNodeId("6");
    		record.setInstitutionId("220");
    		record.setServiceName("mmc");
    		record.setNodeType("MMC");	
    				
    	}
    	
    	public List<ServiceNodeRecord> fetch() {
    		return staticList;		
    	}
    }
    
    public class ServiceNodeDataSource
        extends GwtRpcDataSource {
    
        public ServiceNodeDataSource () {
            DataSourceField field;
    
            field = new DataSourceIntegerField ("nodeId", "nodeId");
    //        field.setPrimaryKey (true);  	
            field.setRequired (true);		
            addField (field);
    
            field = new DataSourceTextField ("institutionId", "Id");        
            field.setRequired (true);		
            addField (field);
    
            field = new DataSourceTextField ("connectsTo", "Connects");
    //        field.setForeignKey("connectsTo"); 
            field.setRequired (true);		
            addField (field);
            
            field = new DataSourceTextField ("serviceName", "Service");
            field.setRequired (true);
            addField (field);
                    
            field = new DataSourceTextField ("nodeType", "Type");
            field.setRequired (true);
            addField (field);
            
        @Override
        protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        	SC.logWarn ("in executeFetch");
        	
            ServiceNodesServiceAsync service = GWT.create (ServiceNodesService.class);
            service.fetch (new AsyncCallback<List<ServiceNodeRecord>> () {
                public void onFailure (Throwable caught) {
                    response.setStatus (RPCResponse.STATUS_FAILURE);
                    processResponse (requestId, response);
                }
                public void onSuccess (List<ServiceNodeRecord> result) {            	
                    TreeNode[] list = new TreeNode[result.size ()];
                    for (int i = 0; i < list.length; i++) {
                    	TreeNode record = new TreeNode();
                        copyValues (result.get (i), record);
                        list[i] = record;
                    }
                    
                    response.setData (list);
                    processResponse (requestId, response);
                }
            });
        }        
        
            // fetch from, to
        private static void copyValues (ServiceNodeRecord from, TreeNode to) {
        	to.setParentID(from.getNodeId());
        	System.out.println("setParentID = " + from.getNodeId());
        	
            to.setAttribute("nodeId", from.getNodeId());        
            to.setAttribute("connectsTo", from.getConnectsTo());
            to.setAttribute("serviceName", from.getServiceName ());
            to.setAttribute("institutionId", from.getInstitutionId ());
            to.setAttribute("nodeType", from.getNodeType());
        }
     }
     
     public class ServiceNodesTreeGrid extends TreeGrid {
    	public ServiceNodesTreeGrid() {
    		TreeGridField institutionId = new TreeGridField("institutionId", "Node");	
            TreeGridField serviceNameField = new TreeGridField("serviceName", "Service");
            TreeGridField nodeTypeField = new TreeGridField("nodeType", "Type");
                    
            setFields(institutionId, serviceNameField, nodeTypeField);
    
            ServiceNodeDataSource dataSource = new ServiceNodeDataSource();
            setDataSource(dataSource);
            this.invalidateCache();
            this.fetchData();        
    	}
    }
    
    public class ServiceNodeRecord 
    	implements Serializable { 
    
    	private static final long serialVersionUID = 1L;
    	private String nodeId;	
    	private String serviceName;
    	private String institutionCode;
    	private String nodeType;
    	private String connectsTo;
    
    	public String getNodeId() {
    		return nodeId;
    	}
    	public void setNodeId(String nodeId) {
    		this.nodeId = nodeId;
    	}
    	public String getConnectsTo() {
    		return connectsTo;
    	}
    	public void setConnectsTo(String connectsTo) {
    		this.connectsTo = connectsTo;
    	}	
    	public String getNodeType() {
    		return nodeType;
    	}
    	public void setNodeType(String nodeType) {
    		this.nodeType = nodeType;
    	}
    	public String getServiceName() {
    		return serviceName;
    	}
    	public void setServiceName(String serviceName) {
    		this.serviceName = serviceName;
    	}	
    	public String getInstitutionId() {
    		return institutionId;
    	}
    	public void setInstitutionId(String institutionId) {
    		this.institutionId = institutionId;
    	}
    }
    
    public class MMCClient implements EntryPoint {
    	private ServiceNodesTreeGrid serviceNodesCanvas;
    	
    	public void onModuleLoad() {
            VLayout containerLayout = new VLayout();
            HLayout topPane = new HLayout();        
            serviceNodesCanvas = new ServiceNodesTreeGrid();
            topPane.addMember(serviceNodesCanvas);
    		containerLayout.addMember(topPane);
    		RootPanel.get("mmcclient").add(containerLayout);
    }

    #2
    Solved: DataSource is missing setPrimaryKey and setForeignKey

    Ok - this should fix the problem: I had commented out setPrimaryKey and setForeignKey in my earlier example, because they were causing FireFox to complain about the amount of time Javascript was taking to complete. I know see I had a recursive reference with setForeignKey pointing to itself ('connectsTo') when it should have pointed to the primaryKey ('nodeId').

    The correction to the datasource (ServiceNodeDataSource) below now works!

    Code:
    public class ServiceNodeDataSource
        extends GwtRpcDataSource {
    
        public ServiceNodeDataSource () {
            DataSourceField field;
    
            field = new DataSourceIntegerField ("nodeId", "nodeId");
            field.setPrimaryKey (true);  	
            field.setRequired (true);		
            addField (field);
    
            field = new DataSourceTextField ("institutionId", "Id");        
            field.setRequired (true);		
            addField (field);
    
            field = new DataSourceTextField ("connectsTo", "Connects");
            field.setForeignKey("nodeId"); 
            field.setRequired (true);		
            addField (field);
            
            field = new DataSourceTextField ("serviceName", "Service");
            field.setRequired (true);
            addField (field);
                    
            field = new DataSourceTextField ("nodeType", "Type");
            field.setRequired (true);
            addField (field);

    Comment

    Working...
    X