The {@link org.apache.juneau.dto.cognos.DataSet} class is a DTO used to convert POJO models directly to Cognos-XML.
Because of the nature of the Cognos XML syntax, only 2-dimensional POJO data structures can be serialized to Cognos-XML.
For example...
Collection<Bean>
Collection<Map>
- {@code MyBean[]}
- {@code HashMap[]}
Example:
The following example shows how to generate Cognos-XML from a POJO.
The example uses the AddressBook sample POJO.
It should be noted that since the {@code AddressBook} class is a subclass of {@code LinkedList}, it fulfills
the requirement of being a tabular data structure.
// Create our POJO with some entries.
AddressBook addressBook = new AddressBook();
addressBook.add(
new Person("Barack Obama", "Aug 4, 1961",
new Address("1600 Pennsylvania Ave", "Washington", "DC", 20500, true),
new Address("5046 S Greenwood Ave", "Chicago", "IL", 60615, false)
)
);
addressBook.add(
new Person("George Walker Bush", "Jul 6, 1946",
new Address("43 Prairie Chapel Rd", "Crawford", "TX", 76638, true),
new Address("1600 Pennsylvania Ave", "Washington", "DC", 20500, false)
)
);
// Define the Cognos metadata
Column[] items = {
new Column("name", "xs:String", 255),
new Column("age", "xs:int"),
new Column("numAddresses", "xs:int")
.addPojoSwap(
new PojoSwap<Person,Integer>() {
@Override
public Integer swap(BeanSession session, Person p) {
return p.addresses.size();
}
}
)
};
// Create the Cognos DataSet object
DataSet ds = new DataSet(items, addressBook, BeanContext.DEFAULT);
// Serialize it to XML
String xml = XmlSerializer.DEFAULT_SQ.serialize(ds);
When run, this code produces the following XML...
<?xml version='1.0' encoding='UTF-8'?>
<c:dataset xmlns:c='http://developer.cognos.com/schemas/xmldata/1/'>
<c:metadata>
<c:item name='name' type='xs:String' length='255'/>
<c:item name='age' type='xs:int'/>
<c:item name='numAddresses' type='xs:int'/>
</c:metadata>
<c:data>
<c:row>
<c:value>Barack Obama</c:value>
<c:value>52</c:value>
<c:value>2</c:value>
</c:row>
<c:row>
<c:value>George Walker Bush</c:value>
<c:value>67</c:value>
<c:value>2</c:value>
</c:row>
</c:data>
</c:dataset>
Other data formats
The following shows examples of what this data structure looks like when serialized to other formats: