RESTful Grails: how do I include related entities in my XML? - rest

Let's say I have a class called Store that has many Employees. My RESTful listXML method looks like this:
def listXML = {
render Store.list() as XML
}
And the result looks like this:
<stores>
<store id="1">
<name>My Store</name>
<employees>
<employee id="1" />
</employees>
</store>
</store>
My question is, how do I include all the data of each Employee class, so that my XML looks something like this?
<stores>
<store id="1">
<name>My Store</name>
<employees>
<employee id="1">
<name>John Smith</name>
<hireDate>2008-01-01</hireDate>
</employee>
</employees>
</store>
</store>

In your controller, you'll want to import the deep converter:
import grails.converters.deep.XML
You can read about it in the first couple of paragraphs of the Converters Reference.

As of Grails 1.1 you will be able to configure Grails to default to deeply serializing by including this in your grails-app/conf/Config.groovy:
grails.converters.xml.default.deep = true
1.1 also introduces named configurations for Converters. The deep converters will be deprecated and the named configuration "deep" should be used instead.
XML.use("deep") {
render model as XML
}

Related

Creating a comment that gets displayed in generated request? WSDL SOAP xml

Im in the process of writing a wsdl file for an existing system. I'd like to add comments to generated requests.
For instance this:
<xsd:simpleType name="coffeetype">
<xsd:restriction base="xsd:integer">
<!--0=likescoffee,1=doesnotlikecoffe-->
<xsd:enumeration value="0" />
<xsd:enumeration value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="CoffeeRequestInput" nillable="false" type="tns:coffeetype" />
Should look like this in the generated request: (eg. when loading the WSDL in SoapUI)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="https://example.com/some.wsdl">
<soapenv:Header/>
<soapenv:Body>
<!--0=likescoffee,1=doesnotlikecoffe-->
<wsdl:CoffeeRequestInput>0</wsdl:CoffeeRequestInput>
</soapenv:Body>
</soapenv:Envelope>
I was able to see these comments when opening the WSDL but not when generating a request from that WSDL.
Already looked into annotations but I wasn't able to use them to create the result I wanted. (Probably an error on my side)
In short you cannot create documentation in requests as you would like to. However you can generate documentation from your WSDL that can be be very useful. By using the "xsd:documentation" tag you can add documentation directly to the elements.
For example
<xsd:simpleType name="coffeetype">
<xsd:restriction base="xsd:integer">
<xsd:enumeration value="0" />
<xsd:enumeration value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="CoffeeRequestInput" nillable="false" type="tns:coffeetype">
<xsd:annotation>
<xsd:documentation>
This object is the CoffeeRequestInput object is an Enumeration which can be used to determine is the user sending the request likes coffee or not.
Valid values for the enumeration is as follows:
0 = like coffee
1 = does not like coffee (probably a user not a programmer making a request).
Some other things that you need to document goes here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
You can then use software such as Altova StyleForce, LiquidXML and OxyGen to generate PDF, Word documents or HTML pages which shows the SOAP services and operations with all your comments included.
If you feel up to it you can write your own XLST to transform your WSDL and XSD's into a neat HTML page which documents you interfaces as well. The best part about this is that when you update the WSDL with new operations and so on that the documentation is updated as well.

scala - add sequence number to xml element

I have an XML as a NodeSeq. I am looking for an option to traverse the tags inside the xml and assign a sequence number to each tag.
For example, consider the below XML is available as a Node Seq. I need to add an attribute SeqNum and assign a sequence number value for each of the tags in a List of tags {Employee, name, age}.
<Employees>
<Employee>
<name>Name1</name>
<age>30</age>
<Remarks>xxxx</Remarks>
</Employee>
<Employee>
<name>Name1</name>
<age>30</age>
<Remarks>xxxx</Remarks>
</Employee>
</Employees>
The output xml should be as below:
<Employees>
<Employee SeqNum="001001">
<name SeqNum="001002">Name1</name>
<age SeqNum="001003">30</age>
<Remarks>xxxx</Remarks>
</Employee>
<Employee SeqNum="002001">
<name SeqNum="002002">Name1</name>
<age SeqNum="002003">30</age>
<Remarks>xxxx</Remarks>
</Employee>
</Employees>
Please share your suggestion to achieve this in scala.

Qlikview REST connector pagination namespaced XML

We have a XML file that is on somewebsite and looks in a way like this (confidential parts stripped)
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://somewebsite.com/crm/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Accounts</title>
<id></id>
<updated>2016-02-04T08:36:56Z</updated>
<link rel="self" title="Accounts" href="Accounts" />
<entry>
<title type="text"></title>
<updated>2016-02-04T08:36:56Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:Type>A</d:Type>
<d:UniqueTaxpayerReference m:null="true" />
<d:VATLiability m:null="true" />
<d:VATNumber m:null="true" />
<d:Website m:null="true" />
</m:properties>
</content>
</entry>
<link rel="next" href="https://somewebsite.com/Accounts?$skiptoken=guid'ee6bc390-a8ac-4bbd-8a4d-0a1f04ab9bd3'" />
</feed>
We use the new Rest connector to get the data out of this XML file.
The XML has pagination and every 60 entries you can load the next 60 with the link at the bottom of this xml file.
The problem i have is when, in the REST connector, we want to enable pagination with these setting:
Pagination Type: Next URL
Next URL field path:
/*[name()="feed"]/*[name()="link"][contains(#rel,"next")]/#href
It doesn't seem to work...
side note: the XML file has namespaces so i need to target the elements this way instead of /feed/link/...
I'm using Xpath syntax to target the link href, but maybe this is not the way to go? Or maybe the REST connector isn't using Xpath syntax?
Really hope someone can help me!
Actually it turns out that this seems to be due to a "bug" in the "Qlik REST Connector 1.0" so the pagination doesn't work.
But there is a fix for it:
1) Make sure that the Qlik REST Connector 1.0 connect dialog box has the NextUrl set to:
feed/link/attr:href
2) When the SQL has been generated after using the SELECT-button and going through the wizard you have to modify the sub-SELECT that reads like this:
.....
(SELECT
"attr:rel" AS "rel",
"attr:title" AS "title",
"attr:href" AS href,
"__FK_link"
FROM "link" FK "__FK_link"),
.....
On line 05 you will have to remove the text AS href.
So it should look like this:
.....
(SELECT
"attr:rel" AS "rel",
"attr:title" AS "title",
"attr:href",
"__FK_link"
FROM "link" FK "__FK_link"),
....
3) Find the LOAD statement that loads from this sub-select with a RESIDENT [MasterREST further down in the load script and make sure that the reference to href is changed to [attr:href] - or else you will get an error message while loading.
Should look like this after the change:
[link]:
LOAD [rel],
[title],
[attr:href],
[__FK_link] AS [__KEY_feed]
RESIDENT RestConnectorMasterTable
WHERE NOT IsNull([__FK_link]);
This worked for me:
/*[name()='feed']/*[name()='link'][#rel='next']/#href
Yours should also work actually, maybe whatever you are using does not agree with double quotes instead of single quotes.

Objects are missing in WADL

I have a web application using Jersey, and I would like to describe its' REST API with WADL.
Jersey generates .wadl file by default, but it does not contain objects (some of my REST calls send/return objects). Actually, I get something like this:
<method id="save" name="POST">
<request>
<representation mediaType="application/json"/>
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
How can I create .wadl that describes also objects?
Acceptable and returned types are described by XML Schema only when you are accepting/returning application/xml type. There is no generally accepted description of JSON types or structures, so WADL generated from Jersey doesn't contain related description (simply because there is no standard way how to achieve that).

How to generate Objective C class files from XML schema?

I have an XML schema that defines my data model. I would now like to have Objective C source files generated from the XML schema. Does anyone know how to accomplish this?
Take a look at this Stack Overflow question on XML serialization, which mentions a project along these lines.
Without knowing the details my immediate thought would be to probably use xslt for this. e.g. if you had something like (I appreciate
<element name="SomeEntity">
<attribute name="someAttr" type="integer" />
<complexType>
<sequence>
<element name="someOtherAttr" type="string" />
</sequence>
</complexType>
</entity>
Create a bunch of templates to translate this,e.g.
<xsl:template match="element">
<xsl:apply-template select="." mode="header"/>
<xsl:apply-template select="." mode="impl"/>
</xsl:template>
<xsl:template match="element" mode="header">
class <xsl:value-of select="#name"/> {
public:
<xsl:apply-template select="attribute" mode="header"/>
<xsl:apply-template select="complexType/element" mode="header"/>
</xsl:template>
...
Though if the logic on the generation is more complex I would probably go down the road of importing the xml into an object model and programmatically process that, possibly using a template engine such as Velocity, as while it is possible complex logic in xslt is a pain.