Announcement

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

    Nested Multiple Datasources

    I am all confused wid multiple Datasources. I have read through all the threads mentioned in the forum relative to this topic. The most explanatory ones were:
    http://forums.smartclient.com/showpo...83&postcount=6
    http://forums.smartclient.com/showthread.php?t=4775
    http://forums.smartclient.com/showthread.php?t=4739

    After a lot of trial and error am able to get multiple elements to be displayed on the grid. But not their values. They appear as [object Object] comma seperated values.

    XML structure I am using:

    <Root protocol="tcp" name="DC:2">
    <ExtendedLanguage>
    <Flow direction="target-server" state="established"/>
    <Match match-order="0">
    <ExtendedPattern type="binary" pattern="/e/e/e"/>
    </Match>
    <Match match-order="1">
    <PayloadTest relative-to-previous="yes" num-bytes="4" />
    </Match>
    <Match match-order="2">
    <PCRE present="yes" num-bytes="4" />
    </Match>
    </ExtendedLanguage>
    <EventGroup>Dummy 1</EventGroup>
    <EventGroup>Dummy 2</EventGroup>
    <Description><![CDATA[This is for a test]]></Description>
    <Reference name="URLREF"><![CDATA[http://www.microsoft.com]]> </Reference>
    <Reference name="CVE" value="CVE"/>
    </Root>


    Like you can see I have a nested structure present in the data:
    For starters I wrote define a RootDS which deals wid Root node and its related attributes. All ValueXPaths by default give me 1st Element in case of multiple element structures.
    for eg: //Root/ExtendedLanguage/Match/@match-order gives me value 0. What I needed was some way to show all 3 values in the grid.

    I created the following datasources:
    Code:
    public RootDS(String identifier) {
    		
    		setID(identifier);
    		setDataFormat(DSDataFormat.XML);
    DataSourceTextField nameField = new DataSourceTextField("name", "Name");
    		nameField.setValueXPath("@name");
    		DataSourceTextField descField = new DataSourceTextField("description", "Description",500);
    		descField.setValueXPath("Description");
    		DataSourceTextField protocolField = new DataSourceTextField("protocol", "Protocol");
            protocolField.setValueXPath("@protocol");
            DataSourceTextField eventGroupField = new DataSourceTextField("eventGroup", "Event Group");
            eventGroupField.setValueXPath("EventGroup");
            DataSourceTextField refNameField = new DataSourceTextField("refName", "Reference Type");
            refNameField.setValueXPath("Reference/@name");
          
    //This is for the ExtendedLanguage Element containing multiple match elements  
     DataSourceField matchField = new DataSourceField("match",FieldType.TEXT, "Match");
            matchField.setValueXPath("ExtendedLanguage/Match");
            **matchField.setTypeAsDataSource(new RestDataSource()); 
            matchField.setMultiple(true);
    
    // This part doesnt work and I cannot find any attribute named order in the response.getData();
    
    DataSourceField matchOrder = new DataSourceField("order",FieldType.TEXT,"Order");
            matchOrder.setTypeAsDataSource(new RestDataSource);
            matchOrder.setMultiple(true);
    matchOrder.setValueXPath("ExtendedLanguage/Match/@match-order");
    Like suggested for nested elements I created another datasource ExtendedLanguageDS and used this line instead of the one above **:
    matchField.setTypeasDataSource(ExtendedLanguageDS.getInstance());

    This has no apparent difference since no matter what fields I put in this datasource they have no effect on the first one.

    Code:
    public class ExtendedLanguageDS extends RestDataSource {
    private static ExtendedLanguageDS instance = null;
    	
    	public static ExtendedLanguageDS getInstance() 
    	{
    		if (instance == null) {
    			
    				instance = new ExtendedLanguageDS("extendedLangTypeDS");
    			
    		}
    		return instance;
    	}
    	
    	public ExtendedLanguageDS(String identifier) {
    
    		setID(identifier);
    		setDataFormat(DSDataFormat.XML);
    		// Doesnt matter whether u hv fields or not.. it still shows the objects...
    		/*DataSourceField matchField = new DataSourceField("match",FieldType.TEXT,"Match");
    		matchField.setMultiple(true);
    		matchField.setValueXPath("Root/ExtendedLanguage/Match");
    		
    		DataSourceField matchOrder = new DataSourceField("order",FieldType.INTENUM,"Order");
    		matchOrder.setTypeAsDataSource(MatchDS.getInstance());
    		matchOrder.setMultiple(true);
    		matchOrder.setValueXPath("Root/ExtendedLanguage/Match/@match-order");
    		
    		setFields(matchField, matchOrder);
    		*/
    	}
    	
    }

    So what I concluded from this was when the Xpath //Root/ExtendedLanguage/Match is fired, I get the match elements as JS Objs from the server (which in my case is coming from eixstDB via restlets).

    If on the matchField if I setTypeAsDataSource it allows me to say that it belongs to another datasource ==> its nested.

    I have a few questions on this and let me tell you that I have not understood datasources completely.

    1) Why are the fields in the second datasource absolutely redundant?
    2) How shud I get the attribute values for eg: match-order or pattern coz it seems setChildTagName can only get values of Elements. for eg: I was able to get DUMMY1 and DUMMY2 to be displayed in the grid coz they are in the structure <EventGroup>value</EventGroup>

    3) Incase of ExtendedLanguage where match Elements inturn consists of different elements like PCRE, ExtendedPattern, how should its Datasource be defined so that all the elements are recognized.

    I would highly appreciate any help on this since I spent 4 days and really havent got through anything.

    Thanks,
    Hetal
    Attached Files
    Last edited by hgaglani; 27 Mar 2009, 05:54.

    #2
    Hello Hetal,

    First, basic question: what do you actually want to display?

    If you want to "flatten" this XML so that nested elements are shown like normal fields, use valueXPath, and don't use a nested DataSource. If you want some other visual display, please describe it.

    Note you should not be setting multiple in conjunction with valueXPath - that has a very specific expectation for format as described in the docs, and it doesn't match what you have.

    Comment


      #3
      Nested Multiple Datasources

      Sorry if I was unclear. But what I really want to understand is the whole DataSource model functionality. But guess for now.. here is my question:


      My main concern is display as well as access to values of elements in the XML. The XML structure in focus here is

      <ExtendedLanguage>
      <Flow direction="target-server" state="established"/>
      <Match match-order="0">
      <ExtendedPattern type="binary" pattern="/e/e/e"/>
      </Match>
      <Match match-order="1">
      <PayloadTest relative-to-previous="yes" num-bytes="4" />
      </Match>
      <Match match-order="2">
      <PCRE present="yes" num-bytes="4" />
      </Match>
      <Match match-order="3">
      <ExtendedPattern type="binary" pattern="/f/f/f"/>
      </Match>
      </ExtendedLanguage>

      First I want the match-order elements to be shown in the grid as comma seperated. Since there are more than one, flatten XML by default returns only the first one if I use the following code:

      DataSourceField matchOrder = new DataSourceField("order",FieldType.TEXT,"Order");
      matchOrder.setValueXPath("ExtendedLanguage/Match/@match-order");
      //matchOrder.setMultiple(true); --> This stmt has no effect.

      Second I want displayed is the pattern element in ExtendedPattern if more than one are present again to be shown as comma seperated values in the grid. same issues as match-order incase I use valueXPath.

      Moreover, on click of the record in the grid, I have a dynamic form populated which should show the match-order / pattern elements in a combobox allowing multiple select. There is more to this but not relevant here.

      How should I tackle this?? valueXPath doesnt give me what I want to see. Nested Datasources: creating a new one with same mapping fields didnt really have any effect. When we setTypeAsDataSource, the implication is for the RootDS to realise that it has a nested element. But how does it get values after that???

      One more point I think I need to mention is only my RootDS has fetch/update URL's specified. The other DS's didnt have that.

      I was trying to implement the alternative method suggested in this post:
      http://forums.smartclient.com/showpost.php?p=12783&postcount=6

      Comment


        #4
        You should be able to select those multiple values via XPath. Try variants on your XPath and take a look at XPath reference materials, once you have it figured out, come back and apply it in SmartGWT.

        There isn't a built-in Combobox with multiple select but there you can use a SelectItem with setMultiple(true).

        Comment


          #5
          Dear SmartGWT users, inside my XML structure I have a field with multiple element.

          <dum-list>
          <dumID>2</dumID>
          <dumID>3</dumID>
          </dum-list>

          I use this sintax for the field:

          DataSourceTextField dums = new DataSourceTextField( "dums", "DUM List");
          dums.setValueXPath( "dum-list/dumID" );
          dums.setMultiple( true );

          When i try to get the array

          String[] array = project.getAttributeAsStringArray( "dums" );

          I find out only the first element has been loaded.

          I read carefully the XPath syntax guide: it states that my expression should select every dumID inside the dum list.

          Could you help me?

          Comment


            #6
            I have the same problem that yary.

            Could you help me?

            Comment


              #7
              I solved the problem, but I have to tell you that the solution did not arrive by my effort: right after I linked that DataSourceField to another datasource the problem disappeared.

              See this code:

              Code:
              DataSourceField projects = new DataSourceField( "projects", FieldType.TEXT, "Project List");
              projects.setValueXPath( "projects/projectID" );
              projects.setMultiple( true );
              projects.setTypeAsDataSource( pds );
              projects.setForeignKey( pds.getID() + ".projectID" );
              With this code, I linked my multiple field to another Datasource, pds, whose key corresponds to one or more of my "projects". After that change it worked for me.

              Please tell me if this work for you also.

              Comment

              Working...
              X