1 28 package net.sf.jasperreports.engine.data; 29 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import net.sf.jasperreports.engine.JRException; 36 import net.sf.jasperreports.engine.JRField; 37 import net.sf.jasperreports.engine.JRRewindableDataSource; 38 import net.sf.jasperreports.engine.JRRuntimeException; 39 import net.sf.jasperreports.engine.query.JRJpaQueryExecuter; 40 41 60 public class JRJpaDataSource extends JRAbstractBeanDataSource implements JRRewindableDataSource { 61 private static final String MAPPING_INDEX_PREFIX = "COLUMN_"; 62 private static final int MAPPING_INDEX_PREFIX_LENGTH = MAPPING_INDEX_PREFIX.length(); 63 private static final String MAPPING_INDEX_PROPERTY_SEP = "."; 64 private static final int MAPPING_INDEX_PROPERTY_SEP_LENGTH = MAPPING_INDEX_PROPERTY_SEP.length(); 65 66 private final JRJpaQueryExecuter queryExecuter; 67 private final int pageSize; 68 private int pageCount; 69 private boolean nextPage; 70 private List returnValues; 71 private Iterator iterator; 72 protected Object currentRow; 73 private Map fieldValueReaders; 74 75 public JRJpaDataSource(JRJpaQueryExecuter queryExecuter, int pageSize) { 76 super(true); 77 78 this.queryExecuter = queryExecuter; 79 this.pageSize = pageSize; 80 81 fieldValueReaders = new HashMap (); 82 83 pageCount = 0; 84 fetchPage(); 85 } 86 87 protected void fetchPage() { 88 if (pageSize <= 0) { 89 returnValues = queryExecuter.getResultList(); 90 nextPage = false; 91 } 92 else { 93 returnValues = queryExecuter.getResultList(pageCount * pageSize, pageSize); 94 nextPage = returnValues.size() == pageSize; 95 } 96 97 ++pageCount; 98 99 initIterator(); 100 } 101 102 public boolean next() { 103 if (iterator == null) { 104 return false; 105 } 106 107 boolean hasNext = iterator.hasNext(); 108 if (!hasNext && nextPage) { 109 fetchPage(); 110 hasNext = iterator != null && iterator.hasNext(); 111 } 112 113 if (hasNext) { 114 currentRow = iterator.next(); 115 } 116 117 return hasNext; 118 } 119 120 public void moveFirst() { 121 if (pageCount == 1) { 122 initIterator(); 123 } 124 else { 125 pageCount = 0; 126 fetchPage(); 127 } 128 } 129 130 private void initIterator() { 131 iterator = returnValues == null ? null : returnValues.iterator(); 132 } 133 134 public Object getFieldValue(JRField field) throws JRException { 135 FieldValueReader reader = getFieldValueReader(field); 136 return reader.getValue(); 137 } 138 139 protected FieldValueReader getFieldValueReader(JRField field) 140 { 141 FieldValueReader reader = (FieldValueReader) fieldValueReaders.get(field.getName()); 142 if (reader == null) 143 { 144 String mapping = getPropertyName(field); 145 reader = createReader(mapping); 146 fieldValueReaders.put(field.getName(), reader); 147 } 148 return reader; 149 } 150 151 private FieldValueReader createReader(String mapping) 152 { 153 FieldValueReader reader = null; 154 if (mapping.startsWith(MAPPING_INDEX_PREFIX)) 155 { 156 int propertySepIdx = mapping.indexOf(MAPPING_INDEX_PROPERTY_SEP, MAPPING_INDEX_PREFIX_LENGTH + 1); 157 if (propertySepIdx < 0) 158 { 159 String indexStr = mapping.substring(MAPPING_INDEX_PREFIX_LENGTH); 160 try 161 { 162 int index = Integer.parseInt(indexStr); 163 reader = new IndexReader(index - 1); 164 } 165 catch (NumberFormatException e) 166 { 167 } 169 } 170 else 171 { 172 String indexStr = mapping.substring(MAPPING_INDEX_PREFIX_LENGTH, propertySepIdx); 173 try 174 { 175 int index = Integer.parseInt(indexStr); 176 String property = mapping.substring(propertySepIdx + MAPPING_INDEX_PROPERTY_SEP_LENGTH); 177 reader = new IndexPropertyReader(index - 1, property); 178 } 179 catch (NumberFormatException e) 180 { 181 } 183 } 184 } 185 186 if (reader == null) 188 { 189 reader = new PropertyReader(mapping); 190 } 191 192 return reader; 193 } 194 195 protected static interface FieldValueReader 196 { 197 Object getValue() throws JRException; 198 } 199 200 protected class PropertyReader implements FieldValueReader 201 { 202 private final String property; 203 204 public PropertyReader(String property) 205 { 206 this.property = property; 207 } 208 209 public Object getValue() throws JRException 210 { 211 return getBeanProperty(currentRow, property); 212 } 213 } 214 215 protected class IndexReader implements FieldValueReader 216 { 217 private final int position; 218 219 public IndexReader(int position) 220 { 221 this.position = position; 222 } 223 224 public Object getValue() throws JRException 225 { 226 Object [] values = (Object []) currentRow; 227 if (position < 0 || position >= values.length) 228 { 229 throw new JRRuntimeException("Index " + position + " out of bounds for query result of lenght " + values.length); 230 } 231 return values[position]; 232 } 233 } 234 235 protected class IndexPropertyReader implements FieldValueReader 236 { 237 private final int position; 238 private final String property; 239 240 public IndexPropertyReader(int position, String property) 241 { 242 this.position = position; 243 this.property = property; 244 } 245 246 public Object getValue() throws JRException 247 { 248 Object [] values = (Object []) currentRow; 249 if (position < 0 || position >= values.length) 250 { 251 throw new JRRuntimeException("Index " + position + " out of bounds for query result of lenght " + values.length); 252 } 253 return getBeanProperty(values[position], property); 254 } 255 } 256 } 257 | Popular Tags |