Announcement

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

    Treegrid with different DataSources selected, editing with Dynamic Form

    Hi,

    by reading the following thread I managed to get my treegrid filled with the objects of two different datasources;
    http://forums.smartclient.com/showthread.php?t=11861&highlight=treegrid+datasources

    I have two ds.xml files for each source which I load within the Store class and convert the objects of both sources two a common interface:
    Code:
    public interface TreeItem
    {
      public enum OriginalType
      {
        Server,
        Job
      }
    	
      public Long getId();
      public void setId(Long id);
    	
      public String getTitle();
      public void setTitle(String title);
    	
      public Long getParentId();
      public void setParentId(Long parentId);
    	
      public OriginalType getOriginalType();
    }
    Code:
    DataSource serverDS = DataSourceManager.get("servers");
    List servers = serverDS.fetch(new HashMap());
    int maxServerRows = servers.size();
    if (maxServerRows > 300) maxServerRows = 300;
    for (int i = 0; i < maxServerRows; i++) {
      Map properties = (Map)servers.get(i);
      TreeItem item = new ServerItem();
      DataTools.setProperties(properties, item);
      item.setParentId(0L);
      storeItem(item);
    }
                
    DataSource jobDS = DataSourceManager.get("jobs");
    List jobs = jobDS.fetch(new HashMap());
    int maxJobRows = jobs.size();
    if (maxJobRows > 300) maxJobRows = 300;
    for (int i = 0; i < maxJobRows; i++) {
      Map properties = (Map)jobs.get(i);
      TreeItem item = new JobItem();
      DataTools.setProperties(properties, item);
      item.setId(item.getId()*10);	// DEBUG
      storeItem(item);
    }
    With this interface the tree gets filled by this xml definition:
    Code:
    <DataSource ID="treeitems" serverType="generic">
      <fields>
        <field name="id" title="ID" type="integer" primaryKey="true" required="true" hidden="true" />
        <field name="title" title="Title" type="text" />
        <field name="parentId" title="Parent ID" type="integer" foreignKey="treeitems.id" rootvalue="0" />
        <field name="originalType" title="Entity type" type="com.hp.dp7.serverdialog.smartgwt.server.TreeItem.OriginalType" hidden="true" />
      </fields>
    	
      <serverObject lookupStyle="new" className="com.hp.dp7.serverdialog.smartgwt.server.TreeItemsDMI"/>
    </DataSource>


    Now I am facing 2 major problems:

    1) How to handle the IDs? Right now I am converting the job IDs by multiplying by 10 so the IDs stay unique. But this is not an option, because when I have more than 100 servers, the IDs would not be unique again.
    What do you recommend?
    (I need the IDs to indicate dependencies from jobs that run on certain servers.)

    2) How do I implement the following scenario: A click on an object within the tree opens either a server specific dynamic editor or a job specific one - depending on the former type of the object. I can read the type with the following code:
    Code:
    tree.addSelectionChangedHandler(new SelectionChangedHandler()
      {
        @Override
        public void onSelectionChanged(SelectionEvent event)
        {
          //Window.alert("Selected a " + event.getRecord().getAttributeAsString("originalType"));
        }
      });
    But having a dynamic form with the "servers" datasource attached I cannot go with
    Code:
    editRecord(event.getRecord())
    as these are two records from different datasources.

    So how do I manage to open a selected item from the tree inside a dynamic form that is attached to either "servers" DS or "jobs" DS.
    (Just the opening or searching for the openRecord(XXX) is required as I can read the type as shown in my code.)

    Big thank you for your help!

    #2
    1) Use compound ids like "job:2141"

    2) Use setDataSource() to switch the form's DataSource based on the type of the record, immediately before editRecord(). Or, use two forms and alternately show and hide them.

    Comment


      #3
      Originally posted by Isomorphic
      1) Use compound ids like "job:2141"
      This worked fine for me, thank you for the solution!


      Originally posted by Isomorphic
      2) Use setDataSource() to switch the form's DataSource based on the type of the record, immediately before editRecord(). Or, use two forms and alternately show and hide them.
      Yeah sure about that. But how do I tell the form which is bound to e.g. "servers" DataSource to load a certain entity when in the tree grid I only have a record from the "treeitems" DS?
      Last edited by chrisw; 15 Jul 2010, 01:14.

      Comment


        #4
        You can either return all the data for each record type in the "treeitems" DataSource response (which is allowed - the Tree will only look at the fields declared in the "treeitems" DataSource, but other data can be present) or if you decide the data returned by the "treeitems" DataSource will only be the tree-related fields, you can use the id of the "servers" in Criteria passed to DataSource.fetchData() to fetch the single related "servers" record before calling editRecord() on the DynamicForm.

        Comment

        Working...
        X