Announcement

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

    RestDataSource.xmlSerialization() format problem

    I'm trying to get the XML serialized form of AdvancedCriteria in the same format as it is sent when it is used with RestDataSource.

    Using
    <artifactId>smartgwt-lgpl</artifactId>
    <version>6.0-p20161211</version>


    Code:
          
            AdvancedCriteria crit1 = new AdvancedCriteria(OperatorId.OR, new AdvancedCriteria[] {
                    new AdvancedCriteria("ec_ea_id", OperatorId.EQUALS, "52100"),
                    new AdvancedCriteria("ec_ea_id", OperatorId.EQUALS, "52101"),
            });
    The above is sent to the server (using DSRequest) like

    Code:
    ...
     <data>
             <_constructor>AdvancedCriteria</_constructor>
             <criteria>
                     <criterion>
                             <fieldName>ec_ea_id</fieldName>
                             <operator>equals</operator>
                             <value>52100</value>
                     </criterion>
                     <criterion>
                             <fieldName>ec_ea_id</fieldName>
                             <operator>equals</operator>
                             <value>52101</value>
                     </criterion>
             </criteria>
             <operator>or</operator>
     </data>
     ...
    It does make sense and works consequently, by the way.

    However, if I serialize it to XML by

    Code:
           RestDataSource q = new RestDataSource();
            GWT.log(q.xmlSerialize(crit1.getJsObj()));
    It results
    Code:
     <isc_RestDataSource_0 _constructor="AdvancedCriteria"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <operator>or</operator>
        <criteria>
            <fieldName>ec_ea_id</fieldName>
            <operator>equals</operator>
            <value>52100</value>
        </criteria>
        <criteria>
            <fieldName>ec_ea_id</fieldName>
            <operator>equals</operator>
            <value>52101</value>
        </criteria>
    </isc_RestDataSource_0>
    Please not that <criteria> is used instead of <criterion> and the containting <criteria> tag is missing.

    Moreover, if I try to serialize something more complex like

    Code:
            AdvancedCriteria crit2 = new AdvancedCriteria(OperatorId.OR, new AdvancedCriteria[] {
                    new AdvancedCriteria("ec_ea_id", OperatorId.EQUALS, "52100"),
                    new AdvancedCriteria("ec_ea_id", OperatorId.EQUALS, "52101"),
                    new AdvancedCriteria(OperatorId.OR, new AdvancedCriteria[] {
                            new AdvancedCriteria("ec_ea_id", OperatorId.EQUALS, "52100"),
                            new AdvancedCriteria("ec_ea_id", OperatorId.EQUALS, "52101"),
                    })
            });
    Then I get

    Code:
    <isc_RestDataSource_0 _constructor="AdvancedCriteria"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <operator>or</operator>
        <criteria>
            <fieldName>ec_ea_id</fieldName>
            <operator>equals</operator>
            <value>52100</value>
        </criteria>
        <criteria>
            <fieldName>ec_ea_id</fieldName>
            <operator>equals</operator>
            <value>52101</value>
        </criteria>
        <criteria>
            <operator>or</operator>
            <criteria>
                <elem>
                    <fieldName>ec_ea_id</fieldName>
                    <operator>equals</operator>
                    <value>52100</value>
                </elem>
                <elem>
                    <fieldName>ec_ea_id</fieldName>
                    <operator>equals</operator>
                    <value>52101</value>
                </elem>
            </criteria>
        </criteria>
    </isc_RestDataSource_0>
    Which is not consistent with even itself: please note that at top level there are the two criterias of value 52100 and 52101 within <criteria> tags and without a containing element, while the nested "or" has the same expression but there are <elem> tags instead of <criteria> and there is a <criteria> element containing them outside.

    Summarized:
    1. RestDataSource.xmlSerialize() seems to work inconsistently
    2. It would be nice to know how to achive the same serialized format as it is actually sent to the server.
    Thanks,
    ilab
    Last edited by ilab; 26 Apr 2017, 06:27.

    #2
    When you call RestDataSource.xmlSerialize() you are asking to serialize the passed data as *an instance of RestDataSource*, so when you pass an AdvancedCriteria object, that doesn't make sense, hence your results.

    Internally, the way RestDataSource serializes criteria for transmittal is to set up a DataSource which declares a field "criteria" as a multiple:true field with childTagName:"criterion" and type being the same DataSource. This whole approach is covered in the docs for xmlSerialize().

    You could do the same, or, you might consider serializing to JSON, which is much easier to round-trip to store and then get back and AdvancedCriteria object - see the docs for AdvancedCriteria for an explanation of how to do this.

    Comment


      #3
      Originally posted by Isomorphic View Post
      Internally, the way RestDataSource serializes criteria for transmittal is to set up a DataSource which declares a field "criteria" as a multiple:true field with childTagName:"criterion" and type being the same DataSource. This whole approach is covered in the docs for xmlSerialize().
      Thank you for the info - however I just can't find the above referenced info in xmlSerialize() docs in https://www.smartclient.com/smartgwt...ataSource.html
      Could you please refine the pointer?

      Comment


        #4
        You just linked to the docs for RestDataSource. We said to look at DataSource.xmlSerialize().

        Comment


          #5
          I am terribly sorry, the correct link to the info provided by you, where I can't find the whole approach covered, is https://www.smartclient.com/smartgwt...aScriptObject-

          By the way, the very thing I can't find is how it is possible to drive xmlSerialize() in SmartGwt with information like "multiple:true" and "childTagName:criterion" (just to go before the next trivial comment).

          Comment


            #6
            So we mentioned that the properties childTagName and multiple:true can be set on a field to influence serialization, and that indeed is what the javadoc for such properties describes. What is it you are unable to find?

            Just in case, here's a link to the DataSourceField.multiple JavaDoc.

            Comment


              #7
              I see, thank you for the link.

              FYI: last time you suggested to look at not DataSourceField, but DataSource.xmlSerialize() which indeed does not cover influencing serialization.

              Anyway, as I pointed out above, I need to get the correct serialized form of an AdvancedCriteria.
              I think DataSourceFields are related to only the fields of the datasource, not criterias, therefore I cannot influence the serialization of criterias with DataSourceField or do I miss something?
              Using DataSource.xmlSerialize() might not be the right way at all, though it seems to be a general serialization method? If so, what should I use instead?


              Comment


                #8
                We referred you to both xmlSerialize and the properties on DataSourceFields that influence its behaviors:

                Internally, the way RestDataSource serializes criteria for transmittal is to set up a DataSource which declares a field "criteria" as a multiple:true field with childTagName:"criterion" and type being the same DataSource. This whole approach is covered in the docs for xmlSerialize().
                So yes, again, properties on DataSourceField influence how xmlSerialize() works - and this is covered quite prominently in the docs for those properties (in the DataSourceField JavaDoc).

                Comment


                  #9
                  Thank you for the info.

                  A working example for reference:

                  Code:
                          DataSource ds = new DataSource();
                  
                          DataSourceField dsf = new DataSourceField();
                          ds.addField(dsf);
                          dsf.setName("criteria");
                          dsf.setTypeAsDataSource(ds);
                          dsf.setMultiple(true);
                          dsf.setChildTagName("criterion");
                  
                          GWT.log("XML: " + ds.xmlSerialize(new AdvancedCriteria(OperatorId.OR, new AdvancedCriteria[] {
                                  new AdvancedCriteria("field1", OperatorId.EQUALS, "1"),
                                  new AdvancedCriteria("field2", OperatorId.EQUALS, "2"),
                                  new AdvancedCriteria(OperatorId.OR, new AdvancedCriteria[] {
                                          new AdvancedCriteria("field3", OperatorId.EQUALS, "3"),
                                          new AdvancedCriteria("field4", OperatorId.EQUALS, "4"),
                                  })
                          }).getJsObj()));

                  Comment

                  Working...
                  X