org.apache.juneau.dto.cognos.DataSet Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.dto.cognos;
import java.util.*;
import org.apache.juneau.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.xml.annotation.*;
/**
* Represents a Cognos dataset.
*
*
* When serialized to XML, creates the following construct (example pulled from AddressBookResource
):
*
* <?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>
*
*
*
* Only 2-dimensional POJOs (arrays or collections of maps or beans) can be serialized to Cognos.
*
*
Example:
*
* The construct shown above is a serialized AddressBook
object which is a subclass of
* LinkedList<Person>
.
* The code for generating the XML is as follows...
*
*
* 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(Person p) {
* return p.addresses .size();
* }
* }
* )
* };
*
* DataSet ds = new DataSet(items, addressBook , BeanContext.DEFAULT );
*
* String xml = XmlSerializer.DEFAULT_SQ .serialize(ds);
*
*/
@SuppressWarnings("unchecked")
@Bean(typeName="dataset", properties="metadata,data")
public class DataSet {
private Column[] metaData;
private List data;
/** Bean constructor. */
public DataSet() {}
/**
* Constructor.
*
* @param columns The meta-data that represents the columns in the dataset.
* @param o
* The POJO being serialized to Cognos.
* Must be an array/collection of beans/maps.
* @param session The bean session used to convert POJOs to strings.
* @throws Exception An error occurred trying to serialize the POJO.
*/
public DataSet(Column[] columns, Object o, BeanSession session) throws Exception {
metaData = columns;
data = new LinkedList();
if (o != null) {
if (o.getClass().isArray())
o = Arrays.asList((Object[])o);
if (o instanceof Collection) {
Collection> c = (Collection>)o;
for (Object o2 : c) {
Row r = new Row();
Map,?> m = null;
if (o2 instanceof Map)
m = (Map,?>)o2;
else
m = session.toBeanMap(o2);
for (Column col : columns) {
Object v;
if (col.pojoSwap != null)
v = col.pojoSwap.swap(session, o2);
else
v = m.get(col.getName());
r.add(v == null ? null : v.toString());
}
data.add(r);
}
}
}
}
/**
* Represents a row of data.
*
*
* When serialized to XML, creates the following construct (example pulled from AddressBookResource
):
*
* <row>
* <value> Barack Obama</value>
* <value> 52</value>
* <value> 2</value>
* </row>
*
*/
@Bean(typeName="row")
public static class Row {
private List values = new LinkedList();
private void add(String value) {
values.add(value);
}
/**
* Returns the values in this row.
*
* @return The values in this row.
*/
@Xml(format=XmlFormat.COLLAPSED, childName="value")
public List getValues() {
return values;
}
}
//--------------------------------------------------------------------------------
// Bean properties
//--------------------------------------------------------------------------------
/**
* Bean property getter: metadata .
*
* @return The value of the metadata property on this bean, or null if it is not set.
*/
@BeanProperty("metadata")
public Column[] getMetaData() {
return metaData;
}
/**
* Bean property setter: metadata .
*
* @param metaData The new value for the metadata property on this bean.
* @return This object (for method chaining).
*/
@BeanProperty("metadata")
public DataSet setMetaData(Column[] metaData) {
this.metaData = metaData;
return this;
}
/**
* Bean property getter: data .
*
* @return The value of the data property on this bean, or null if it is not set.
*/
@BeanProperty("data")
public List getData() {
return data;
}
/**
* Bean property setter: data .
*
* @param data The new value for the data property on this bean.
* @return This object (for method chaining).
*/
@BeanProperty("data")
public DataSet setData(List data) {
this.data = data;
return this;
}
}