KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > report > JREntityListIteratorDataSource


1 /*
2  * $Id: JREntityListIteratorDataSource.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 package org.ofbiz.content.report;
26
27 import net.sf.jasperreports.engine.JRDataSource;
28 import net.sf.jasperreports.engine.JRException;
29 import net.sf.jasperreports.engine.JRField;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.entity.GenericEntity;
33 import org.ofbiz.entity.GenericEntityException;
34 import org.ofbiz.entity.util.EntityListIterator;
35
36 /**
37  * <code>JREntityListIteratorDataSource</code>
38  *
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @author <a HREF="mailto:gielen@aixcept.de">Rene Gielen</a>
41  * @version $Rev: 5462 $
42  */

43 public class JREntityListIteratorDataSource implements JRDataSource {
44     
45     public static final String JavaDoc module = JREntityListIteratorDataSource.class.getName();
46
47     private EntityListIterator entityListIterator = null;
48     private GenericEntity currentEntity = null;
49
50     public JREntityListIteratorDataSource(EntityListIterator entityListIterator) {
51         this.entityListIterator = entityListIterator;
52     }
53
54     public boolean next() throws JRException {
55         if (this.entityListIterator == null) {
56             return false;
57         }
58         Object JavaDoc nextObj = this.entityListIterator.next();
59         if (nextObj != null) {
60             if (nextObj instanceof GenericEntity) {
61                 this.currentEntity = (GenericEntity) nextObj;
62             } else {
63                 throw new JRException("Current collection object does not seem to be a GenericEntity (or GenericValue or GenericPK).");
64             }
65             return true;
66         } else {
67             // nothing left, close here...
68
try {
69                 this.entityListIterator.close();
70             } catch (GenericEntityException e) {
71                 Debug.logError(e, "Error closing EntityListIterator in Jasper Reports DataSource", module);
72                 throw new JRException(e);
73             }
74             this.entityListIterator = null;
75             return false;
76         }
77     }
78
79     public Object JavaDoc getFieldValue(JRField jrField) throws JRException {
80         Object JavaDoc value = null;
81         if (this.currentEntity != null) {
82             try {
83                 value = this.currentEntity.get(jrField.getName());
84             } catch (IllegalArgumentException JavaDoc e) {
85                 try {
86                     value = this.currentEntity.get(org.ofbiz.entity.model.ModelUtil.dbNameToVarName(jrField.getName()));
87                 } catch (IllegalArgumentException JavaDoc ex) {
88                     throw new JRException("The specified field name [" + jrField.getName() + "] is not a valid field-name for the entity: " + this.currentEntity.getEntityName(), e);
89                 }
90             }
91         }
92         return value;
93     }
94 }
95
Popular Tags