Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
6 replies Latest Post - ‏2013-01-05T20:14:18Z by prashanth.obulsetty
prashanth.obulsetty
prashanth.obulsetty
7 Posts
ACCEPTED ANSWER

Pinned topic Create BO from xml file

‏2012-12-27T17:08:31Z |
Hello,

We are working on test harness project and was trying to develop a service that would read an xml from a file and create a request BO, which will then be input to the BPD using UCA.

To read an xml from a file, we leveraged the "Read Text File" integration service from the out-of-the-box SYSTEM DATA toolkit. The service seems to be straightforward in the sense that it takes the filePath (String) as input and returns the fileContents (String) as output. This is working as expected. When we try to consume the fileContents String and populate a BO using the following code, it throws the below exception.


tw.local.AddressBO = tw.system.serializer.fromXml(tw.local.fileContents);


Exception details:

com.lombardisoftware.core.TeamWorksRuntimeException: com.lombardisoftware.core.TeamWorksException: Type mismatch. Value "" must be instance of structured IBM BPM type. Java class found: java.lang.String

The type of the variable tw.local.AddressBO (AddressBO) has been defined as Address. Here is the definition for the Address type:
Note: This type is manually created in the process designer. The schema is provided just for understanding purpose.


<?xml version=
"1.0" encoding=
"utf-8"?> <xs:schema elementFormDefault=
"qualified" xmlns:xs=
"http://www.w3.org/2001/XMLSchema"> <xs:element name=
"Address"> <xs:complexType> <xs:sequence> <xs:element name=
"Recipient" type=
"xs:string" /> <xs:element name=
"House" type=
"xs:string" /> <xs:element name=
"Street" type=
"xs:string" /> <xs:element name=
"Town" type=
"xs:string" /> <xs:element name=
"PostCode" type=
"xs:string" /> <xs:element name=
"Country" type=
"xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>


The xml file that we are try to read is,


<?xml version=
"1.0" encoding=
"utf-8"?> <Address xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"> <Recipient>Mr. Walter C. Brown</Recipient> <House>49</House> <Street>Featherstone Street</Street> <Town>LONDON</Town> <PostCode>EC1Y 8SY</PostCode> <Country>UK</Country> </Address>


Unfortunately, we have technical issues with exporting the process application as a .twx file, so we cannot provide the .twx.

Any help on the above error will be appreciated.

Thanks!
Prashanth
Updated on 2013-01-05T20:14:18Z at 2013-01-05T20:14:18Z by prashanth.obulsetty
  • kolban
    kolban
    3315 Posts
    ACCEPTED ANSWER

    Re: Create BO from xml file

    ‏2012-12-28T14:53:42Z  in response to prashanth.obulsetty
    Thank you for the very well written post. My understanding on the the primary use of the XML Serialization is to allow a Business Object to be serialized for transmission or for writing to secondary storage for later reconstitution. If it were me, the first thing I would do is to write a test which serializes a sample populated Business Object to XML and see what the structure of the resulting XML looks like. I am going to assume it might be different from the sample XML data that you showed in your post. If it is, try reading an instance of the generated XML from the file. If that works, then we know that we weren't quite right in the physical structure of the XML to be serialized.

    It also sounds like you are using BPM to generate a test harness for BPM. This should work ... but if you decide to go a different route, you can submit work requests from an external application using REST API calls. If that is on interest to you, then you may be able to use an existing test case framework (if that is the goal).

    Please post back and let me know how you get on.

    Neil
  • prashanth.obulsetty
    prashanth.obulsetty
    7 Posts
    ACCEPTED ANSWER

    Re: Create BO from xml file

    ‏2012-12-31T16:49:58Z  in response to prashanth.obulsetty
    Thanks for your response Neil!

    As you mentioned, I tried to test with the structure that resulted from serializing the sample populated business object but it resulted in a similar exception.

    Is there a way to convert the String returned from the "Read Text File" integration service to an IBM BPM type (in this case the Address BO)?
    or Is there a way to write our own java integration service that returns a generic XML types (DOcument, Element types...) that the IBM BPM can understand, consume and populate the BO?

    We tried to write our own java code to return different XML types but the BPM doesn't understand any of those types. We don't want to parse through each element in the XML and populate the BO as our BO is huge (not the address one but our own). The last option we would think of is to have the knowledge of the BPM types available at the java end by generating the Java classes from the BPM type that way we can return the actual BPM types from the Java integration service. Not sure if it going to work though.

    We haven't yet decided on a specific test case framework, but trying out options. Thanks for your input on the REST API calls.
    Thanks!
    Prashanth
    • jmac_EmeriCon
      jmac_EmeriCon
      279 Posts
      ACCEPTED ANSWER

      Re: Create BO from xml file

      ‏2012-12-31T17:37:42Z  in response to prashanth.obulsetty
      The following shows a simple test.

      The first log statement is the result of displaying an "initialized" variable of your type Address.

      The second log statement is the result of writting that variable out to a file. So I now have a file which contains the data shown in the first log statement.

      The third log statement shows the result of reading that file into a variable named "reconstituted" (of type ANY) and then running the following javascript statements:

      
      tw.local.myAddress=tw.system.serializer.fromXml(tw.local.reconstituted); log.error(
      "myAddress\n" + tw.local.myAddress)
      


      This is performing a "similar" action to that which you want to perform. Perhaps you can use something like this.

      Result

      
      1. [12/31/12 10:31:19:585 MST] 000004f9 wle_javascrip E   address <variable type=
      "Address"> <recipient type=
      "String"><![CDATA[Walter C. Brown]]></recipient> <house type=
      "String"><![CDATA[49]]></house> <street type=
      "String"><![CDATA[Featherstone Street]]></street> <town type=
      "String"><![CDATA[London]]></town> <postCode type=
      "String"><![CDATA[EC1Y 8SY]]></postCode> <country type=
      "String"><![CDATA[UK]]></country> </variable>   2. [12/31/12 10:31:19:585 MST] 000004f9 SystemOut     O File C:/AddressTest.txt does not exist. Creating 
      
      new one:   3. [12/31/12 10:31:19:595 MST] 000004f9 wle_javascrip E   myAddress <variable type=
      "Address"> <recipient type=
      "String"><![CDATA[Walter C. Brown]]></recipient> <house type=
      "String"><![CDATA[49]]></house> <street type=
      "String"><![CDATA[Featherstone Street]]></street> <town type=
      "String"><![CDATA[London]]></town> <postCode type=
      "String"><![CDATA[EC1Y 8SY]]></postCode> <country type=
      "String"><![CDATA[UK]]></country> </variable>
      


      GOOD LUCK
      John


      _______________________________________________________________________

      John McDonald

      EmeriCon, LLC
  • SystemAdmin
    SystemAdmin
    7615 Posts
    ACCEPTED ANSWER

    Re: Create BO from xml file

    ‏2013-01-02T20:26:48Z  in response to prashanth.obulsetty
    One possiblility is just that you need your variable to be an XMLElement. While BPM generally allows us to cast from XML to String without any hoops, sometimes some manipulation is needed. Try creating a 2nd variable of type "XMLElement" and then set its value to your string. Then use that variable to create the complex type.

    I see that you have an XML declaration in your string, if you can't create the XMLElement from the string by simple assignment, then it is likely this is the cause of the problem.

    Andrew Paier | Director of Special Operations | BP3 Global, Inc. www.bp-3.com
  • SystemAdmin
    SystemAdmin
    7615 Posts
    ACCEPTED ANSWER

    Re: Create BO from xml file

    ‏2013-01-05T00:33:02Z  in response to prashanth.obulsetty
    The error you are encountering is because you have failed to specify the 'type' attribute of your root element.

    tw.system.serializer expects the root element to contain a "type" attribute set to the object type which will be output. It completely ignores namespace and root element tag name. If no type is set, it will output a java.lang.String by default, hence your type mismatch error.
    To fix it, you must specify the "type" attribute on your root xml element to be serialized.

    
    <?xml version=
    "1.0" encoding=
    "utf-8"?> <Address type=
    'Address' xmlns:xsi=
    "http://www.w3.org/2001/XMLSchema-instance"> <Recipient>Mr. Walter C. Brown</Recipient> <House>49</House> <Street>Featherstone Street</Street> <Town>LONDON</Town> <PostCode>EC1Y 8SY</PostCode> <Country>UK</Country> </Address>
    


    Sam
  • prashanth.obulsetty
    prashanth.obulsetty
    7 Posts
    ACCEPTED ANSWER

    Re: Create BO from xml file

    ‏2013-01-05T20:14:18Z  in response to prashanth.obulsetty
    Thanks Sam! It works.

    Thanks everyone for your responses and for your time!

    Prashanth