View Javadoc

1   /**
2    * 
3    */
4   package ar.com.jiji.kaya.reports;
5   
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import net.sf.jasperreports.engine.JRDataSource;
10  import net.sf.jasperreports.engine.JRException;
11  import net.sf.jasperreports.engine.JRField;
12  
13  /**
14   * copiado de http://www.hibernate.org/79.html * TODO: documentar y hacer tests
15   * 
16   * @author unascribed
17   * 
18   */
19  public class HibernateQueryResultDataSource implements JRDataSource {
20  
21  	private String[] fields;
22  
23  	private Iterator iterator;
24  
25  	private Object currentValue;
26  
27  	public HibernateQueryResultDataSource(List list, String[] fields) {
28  		this.fields = fields;
29  		this.iterator = list.iterator();
30  	}
31  
32  	public Object getFieldValue(JRField field) throws JRException {
33  		Object value = null;
34  		int index = getFieldIndex(field.getName());
35  		if (index > -1) {
36  			Object[] values = (Object[]) currentValue;
37  			value = values[index];
38  		}
39  		return value;
40  	}
41  
42  	public boolean next() throws JRException {
43  		currentValue = iterator.hasNext() ? iterator.next() : null;
44  		return (currentValue != null);
45  	}
46  
47  	private int getFieldIndex(String field) {
48  		int index = -1;
49  		for (int i = 0; i < fields.length; i++) {
50  			if (fields[i].equals(field)) {
51  				index = i;
52  				break;
53  			}
54  		}
55  		return index;
56  	}
57  
58  }