View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/servlets/reports/JRDataSourceIter.java,v 1.8 2005/11/30 20:31:18 hkollmann Exp $
3    * $Revision: 1.8 $
4    * $Date: 2005/11/30 20:31:18 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  /***
25   * Use an iterator as data source  HOWTO 1) create a Collection of X. Where X
26   * is a complex object with 'fields', X can either be a Set or a Bean. The
27   * container must support Iterator(). Object X must be supported  by apache
28   * bean utils 2) somewhere in pagecontext (like session) put the Collection
29   * with key "jasper.input" 3) invoke  gets field data from multiple sources,
30   * not just DBform fields context : prefix context__xxx  . Uses
31   * pageContext.findAttribute(xxx) session : prefix session__xxxx . Uses
32   * pageContext.getSession().findAttribute(xxx) internal : prefix internal_zzz
33   * attribute of iterator object : no prefix    For internal, only
34   * internal__rownum is currently defined For content and session, xxxx may be
35   * a 'simple' type or 'complex' type a 'complex' type is an object supporting
36   * javabean access, instead of the usual '.' notation, use '__' instead.
37   */
38  package org.dbforms.servlets.reports;
39  
40  import java.util.Iterator;
41  import java.util.Map;
42  
43  import net.sf.jasperreports.engine.JRException;
44  
45  import org.apache.commons.beanutils.PropertyUtils;
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  
50  
51  public final class JRDataSourceIter extends AbstractJRDataSource {
52     private static Log  logCat = LogFactory.getLog(JRDataSourceIter.class
53                                                    .getName());
54     Object              current     = null;
55     private Iterator    iter;
56     private int         rownum      = 0;
57  
58     /***
59      * DOCUMENT ME!
60      *
61      * @param iter
62      * @param pageContext
63      */
64     public JRDataSourceIter(Map attributes, Iterator    iter) {
65        super(attributes);
66     	  this.iter        = iter;
67        rownum           = 0;
68     }
69  
70     /***
71      * @see net.sf.jasperreports.engine.JRDataSource#next()
72      */
73     public boolean next() throws JRException {
74        if (!iter.hasNext()) {
75           return false;
76        }
77        current = iter.next();
78        rownum++;
79        return true;
80     }
81  
82     /***
83      * DOCUMENT ME!
84      *
85      * @param search
86      *
87      * @return
88      */
89     public final Object getFieldValue(String search) {
90        Object obj = null;
91        try {
92           if (current != null) {
93              // complex, current is a container or a bean
94              logCat.debug("calling PropertyUtils.getProperty " + current + " "
95                           + search);
96              obj = PropertyUtils.getProperty(current, search);
97           }
98        } catch (Exception e) {
99           logCat.error("getCurrentValue: " + e);
100       }
101 
102       return obj;
103    }
104 }