1 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 43 public class JREntityListIteratorDataSource implements JRDataSource { 44 45 public static final String 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 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 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 getFieldValue(JRField jrField) throws JRException { 80 Object value = null; 81 if (this.currentEntity != null) { 82 try { 83 value = this.currentEntity.get(jrField.getName()); 84 } catch (IllegalArgumentException e) { 85 try { 86 value = this.currentEntity.get(org.ofbiz.entity.model.ModelUtil.dbNameToVarName(jrField.getName())); 87 } catch (IllegalArgumentException 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 |