001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui.reports;
020    
021    import dori.jasper.engine.JRDataSource;
022    import dori.jasper.engine.JRException;
023    import dori.jasper.engine.JRField;
024    import java.util.Collection;
025    import java.util.Iterator;
026    import org.objectweb.jac.aspects.gui.GuiAC;
027    import org.objectweb.jac.core.ObjectRepository;
028    import org.objectweb.jac.core.rtti.ClassItem;
029    import org.objectweb.jac.core.rtti.ClassRepository;
030    import org.objectweb.jac.core.rtti.CollectionItem;
031    
032    /**
033     * A data source for JasperReports.
034     */
035    public class JacDataSource implements JRDataSource {
036        ClassItem componentType;
037        Collection collection;
038        
039        Iterator it;
040        Object current;
041    
042        /**
043         * Create a data source of all instances of a class.
044         */
045        public JacDataSource(ClassItem cl) {
046            this.componentType = cl;
047            this.collection = ObjectRepository.getObjects(cl);
048        }
049    
050        /**
051         * Creates a data source for a collection. 
052         *
053         * @param collection a collection to fetch data from
054         * @param componentType the type of the elements in the collection. It can be null.
055         */
056        public JacDataSource(Collection collection, ClassItem componentType) {
057            this.collection = collection;
058            this.componentType = componentType;
059        }
060    
061        /**
062         * Creates a data source for a collection of an object
063         *
064         * @param collection a collection to fetch data from
065         * @param substance 
066         */
067        public JacDataSource(Object substance, CollectionItem collection) {
068            this.collection = collection.getActualCollectionThroughAccessor(substance);
069            this.componentType = collection.getComponentType();
070        }
071    
072    
073        /**
074         * Creates a data source for a collection of an object
075         *
076         * @param collection a collection to fetch data from
077         * @param substance 
078         */
079        public JacDataSource(Object substance, String collectionName) {
080            this(substance,ClassRepository.get().getClass(substance).getCollection(collectionName));
081        }
082        // implementation of dori.jasper.engine.JRDataSource interface
083    
084        public boolean next() throws JRException
085        {
086            if (it==null) {
087                it = collection.iterator();
088            }
089            boolean result = it.hasNext();
090            if (result)
091                current = it.next();
092            return result;
093        }
094    
095        /**
096         * Uses the documentation of the field as the full name of the
097         * field, since the field's name can not contain dots.
098         */
099        public Object 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 name = field.getDescription();
107            Object 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.class && 
116                value!=null &&
117                value.getClass()!=String.class) {
118                value = GuiAC.toString(value);
119            }
120            return value;
121        }
122    
123    }