01
/**//* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */
02
import java.lang.reflect.InvocationTargetException;
03
import java.lang.reflect.Method;
04
import java.util.Iterator;
05
import java.util.List;
06
07
import net.sf.jasperreports.engine.JRDataSource;
08
import net.sf.jasperreports.engine.JRException;
09
import net.sf.jasperreports.engine.JRField;
10
11
/** *//**
12
* ObjectDataSource class is used to extract object field values for the report.
13
* <br><br>
14
* usage:<br>
15
* List pilots = ...<br>
16
* ObjectDataSource dataSource = new ObjectDataSource(pilots);<br>
17
* In the report (*.jrxml) you will need to define fields. For example: <br>
18
* <field name="Name" class="java.lang.String"/><br>
19
* where field name should correspond to your getter method:<br>
20
* "Name" - for getName()<br>
21
* "Id" - for getId()<br>
22
*
23
*/
24
public class ObjectDataSource implements JRDataSource ...{
25
26
private Iterator iterator;
27
28
private Object currentValue;
29
30
public ObjectDataSource(List list) ...{
31
this.iterator = list.iterator();
32
}
33
// end ObjectDataSource
34
35
public Object getFieldValue(JRField field) throws JRException ...{
36
Object value = null;
37
try ...{
38
// getter method signature is assembled from "get" + field name
39
// as specified in the report
40
Method fieldAccessor = currentValue.getClass().getMethod("get" + field.getName(), null);
41
value = fieldAccessor.invoke(currentValue, null);
42
} catch (IllegalAccessException iae) ...{
43
iae.printStackTrace();
44
} catch (InvocationTargetException ite) ...{
45
ite.printStackTrace();
46
} catch (NoSuchMethodException nsme) ...{
47
nsme.printStackTrace();
48
}
49
return value;
50
}
51
// end getFieldValue
52
53
public boolean next() throws JRException ...{
54
currentValue = iterator.hasNext() ? iterator.next() : null;
55
return (currentValue != null);
56
}
57
// end next
58
59
}