KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > reports > JacDataSource


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.reports;
20
21 import dori.jasper.engine.JRDataSource;
22 import dori.jasper.engine.JRException;
23 import dori.jasper.engine.JRField;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import org.objectweb.jac.aspects.gui.GuiAC;
27 import org.objectweb.jac.core.ObjectRepository;
28 import org.objectweb.jac.core.rtti.ClassItem;
29 import org.objectweb.jac.core.rtti.ClassRepository;
30 import org.objectweb.jac.core.rtti.CollectionItem;
31
32 /**
33  * A data source for JasperReports.
34  */

35 public class JacDataSource implements JRDataSource {
36     ClassItem componentType;
37     Collection JavaDoc collection;
38     
39     Iterator JavaDoc it;
40     Object JavaDoc current;
41
42     /**
43      * Create a data source of all instances of a class.
44      */

45     public JacDataSource(ClassItem cl) {
46         this.componentType = cl;
47         this.collection = ObjectRepository.getObjects(cl);
48     }
49
50     /**
51      * Creates a data source for a collection.
52      *
53      * @param collection a collection to fetch data from
54      * @param componentType the type of the elements in the collection. It can be null.
55      */

56     public JacDataSource(Collection JavaDoc collection, ClassItem componentType) {
57         this.collection = collection;
58         this.componentType = componentType;
59     }
60
61     /**
62      * Creates a data source for a collection of an object
63      *
64      * @param collection a collection to fetch data from
65      * @param substance
66      */

67     public JacDataSource(Object JavaDoc substance, CollectionItem collection) {
68         this.collection = collection.getActualCollectionThroughAccessor(substance);
69         this.componentType = collection.getComponentType();
70     }
71
72
73     /**
74      * Creates a data source for a collection of an object
75      *
76      * @param substance object holding the collection
77      * @param collectionName name of the collection to fetch data from
78      */

79     public JacDataSource(Object JavaDoc substance, String JavaDoc collectionName) {
80         this(substance,ClassRepository.get().getClass(substance).getCollection(collectionName));
81     }
82     // implementation of dori.jasper.engine.JRDataSource interface
83

84     public boolean next() throws JRException
85     {
86         if (it==null) {
87             it = collection.iterator();
88         }
89         boolean result = it.hasNext();
90         if (result)
91             current = it.next();
92         return result;
93     }
94
95     /**
96      * Uses the documentation of the field as the full name of the
97      * field, since the field's name can not contain dots.
98      */

99     public Object JavaDoc getFieldValue(JRField field) throws JRException
100     {
101         if (current==null) {
102             throw new JRException(
103                 "JacDataSource: No current object to get field "+
104                 field.getName());
105         }
106         String JavaDoc name = field.getDescription();
107         Object JavaDoc value = null;
108         if (componentType!=null) {
109             value = componentType.getField(name).getThroughAccessor(current);
110         } else {
111             ClassItem cl = ClassRepository.get().getClass(current);
112             value = cl.getField(name).getThroughAccessor(current);
113         }
114
115         if (field.getValueClass()==String JavaDoc.class &&
116             value!=null &&
117             value.getClass()!=String JavaDoc.class) {
118             value = GuiAC.toString(value);
119         }
120         return value;
121     }
122
123 }
124
Popular Tags