1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.sql.Connection ; 31 import java.util.Map ; 32 33 import net.sf.jasperreports.engine.JRDataSource; 34 import net.sf.jasperreports.engine.JRDatasetParameter; 35 import net.sf.jasperreports.engine.JRDatasetRun; 36 import net.sf.jasperreports.engine.JRException; 37 import net.sf.jasperreports.engine.JRExpression; 38 import net.sf.jasperreports.engine.JRParameter; 39 import net.sf.jasperreports.engine.JRQuery; 40 import net.sf.jasperreports.engine.JRScriptletException; 41 import net.sf.jasperreports.engine.JRVariable; 42 43 49 public class JRFillDatasetRun implements JRDatasetRun 50 { 51 private final JRBaseFiller filler; 52 53 private final JRFillDataset dataset; 54 55 private JRExpression parametersMapExpression; 56 57 private JRDatasetParameter[] parameters; 58 59 private JRExpression connectionExpression; 60 61 private JRExpression dataSourceExpression; 62 63 64 71 public JRFillDatasetRun(JRBaseFiller filler, JRDatasetRun datasetRun, JRFillObjectFactory factory) 72 { 73 factory.put(datasetRun, this); 74 75 this.filler = filler; 76 this.dataset = (JRFillDataset) filler.datasetMap.get(datasetRun.getDatasetName()); 77 78 parametersMapExpression = datasetRun.getParametersMapExpression(); 79 parameters = datasetRun.getParameters(); 80 connectionExpression = datasetRun.getConnectionExpression(); 81 dataSourceExpression = datasetRun.getDataSourceExpression(); 82 } 83 84 85 92 public void evaluate(JRFillElementDataset elementDataset, byte evaluation) throws JRException 93 { 94 Map parameterValues = 95 JRFillSubreport.getParameterValues( 96 filler, 97 parametersMapExpression, 98 parameters, 99 evaluation, 100 false, 101 dataset.getResourceBundle() != null, false ); 104 105 try 106 { 107 if (dataSourceExpression != null) 108 { 109 JRDataSource dataSource = (JRDataSource) filler.evaluateExpression(dataSourceExpression, evaluation); 110 dataset.setDatasourceParameterValue(parameterValues, dataSource); 111 } 112 else if (connectionExpression != null) 113 { 114 Connection connection = (Connection ) filler.evaluateExpression(connectionExpression, evaluation); 115 dataset.setConnectionParameterValue(parameterValues, connection); 116 } 117 118 copyConnectionParameter(parameterValues); 119 120 dataset.setParameterValues(parameterValues); 121 122 dataset.filterElementDatasets(elementDataset); 123 124 dataset.initCalculator(); 125 126 iterate(); 127 } 128 finally 129 { 130 dataset.closeDatasource(); 131 dataset.restoreElementDatasets(); 132 } 133 } 134 135 protected void copyConnectionParameter(Map parameterValues) 136 { 137 JRQuery query = dataset.getQuery(); 138 if (query != null) 139 { 140 String language = query.getLanguage(); 141 if (connectionExpression == null && 142 (language.equals("sql") || language.equals("SQL")) && 143 !parameterValues.containsKey(JRParameter.REPORT_CONNECTION)) 144 { 145 JRFillParameter connParam = (JRFillParameter) filler.getParametersMap().get(JRParameter.REPORT_CONNECTION); 146 Connection connection = (Connection ) connParam.getValue(); 147 parameterValues.put(JRParameter.REPORT_CONNECTION, connection); 148 } 149 } 150 } 151 152 protected void iterate() throws JRException 153 { 154 dataset.start(); 155 156 init(); 157 158 if (dataset.next()) 159 { 160 detail(); 161 162 while (dataset.next()) 163 { 164 checkInterrupted(); 165 166 group(); 167 168 detail(); 169 } 170 } 171 172 } 173 174 175 protected void checkInterrupted() 176 { 177 if (Thread.currentThread().isInterrupted() || filler.isInterrupted()) 178 { 179 filler.setInterrupted(true); 180 181 throw new JRFillInterruptedException(); 182 } 183 } 184 185 186 protected void group() throws JRException, JRScriptletException 187 { 188 dataset.calculator.estimateGroupRuptures(); 189 190 dataset.scriptlet.callBeforeGroupInit(); 191 dataset.calculator.initializeVariables(JRVariable.RESET_TYPE_GROUP); 192 dataset.scriptlet.callAfterGroupInit(); 193 } 194 195 protected void init() throws JRScriptletException, JRException 196 { 197 dataset.scriptlet.callBeforeReportInit(); 198 dataset.calculator.initializeVariables(JRVariable.RESET_TYPE_REPORT); 199 dataset.scriptlet.callAfterReportInit(); 200 } 201 202 protected void detail() throws JRScriptletException, JRException 203 { 204 dataset.scriptlet.callBeforeDetailEval(); 205 dataset.calculator.calculateVariables(); 206 dataset.scriptlet.callAfterDetailEval(); 207 } 208 209 public String getDatasetName() 210 { 211 return dataset.getName(); 212 } 213 214 public JRExpression getParametersMapExpression() 215 { 216 return parametersMapExpression; 217 } 218 219 public JRDatasetParameter[] getParameters() 220 { 221 return parameters; 222 } 223 224 public JRExpression getConnectionExpression() 225 { 226 return connectionExpression; 227 } 228 229 public JRExpression getDataSourceExpression() 230 { 231 return dataSourceExpression; 232 } 233 234 protected JRFillDataset getDataset() 235 { 236 return dataset; 237 } 238 } 239 | Popular Tags |