1 28 package net.sf.jasperreports.engine.query; 29 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 import javax.persistence.EntityManager; 37 import javax.persistence.Query; 38 39 import net.sf.jasperreports.engine.JRDataSource; 40 import net.sf.jasperreports.engine.JRDataset; 41 import net.sf.jasperreports.engine.JRException; 42 import net.sf.jasperreports.engine.JRParameter; 43 import net.sf.jasperreports.engine.JRPropertiesMap; 44 import net.sf.jasperreports.engine.JRRuntimeException; 45 import net.sf.jasperreports.engine.JRValueParameter; 46 import net.sf.jasperreports.engine.data.JRJpaDataSource; 47 import net.sf.jasperreports.engine.util.JRProperties; 48 import net.sf.jasperreports.engine.util.JRStringUtil; 49 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 111 public class JRJpaQueryExecuter extends JRAbstractQueryExecuter { 112 113 private static final Log log = LogFactory.getLog(JRJpaQueryExecuter.class); 114 115 private final Integer reportMaxCount; 116 117 private EntityManager em; 118 private Query query; 119 120 public JRJpaQueryExecuter(JRDataset dataset, Map parameters) { 121 super(dataset, parameters); 122 123 em = (EntityManager)getParameterValue(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER); 124 reportMaxCount = (Integer )getParameterValue(JRParameter.REPORT_MAX_COUNT); 125 126 if (em == null) { 127 log.warn("The supplied javax.persistence.EntityManager object is null."); 128 } 129 130 parseQuery(); 131 } 132 133 public JRDataSource createDatasource() throws JRException { 134 JRDataSource datasource = null; 135 String queryString = getQueryString(); 136 137 if (em != null && queryString != null && queryString.trim().length() > 0) { 138 createQuery(queryString); 139 140 datasource = createResultDatasource(); 141 } 142 143 return datasource; 144 } 145 146 151 protected synchronized void createQuery(String queryString) { 152 153 if (log.isDebugEnabled()) 154 { 155 log.debug("EJBQL query: " + queryString); 156 } 157 158 query = em.createQuery(queryString); 159 160 List parameterNames = getCollectedParameterNames(); 162 if (!parameterNames.isEmpty()) { 163 Set namesSet = new HashSet (); 165 for (Iterator iter = parameterNames.iterator(); iter.hasNext();) { 166 String parameterName = (String )iter.next(); 167 if (namesSet.add(parameterName)) { 168 JRValueParameter parameter = getValueParameter(parameterName); 169 String ejbParamName = getEjbqlParameterName(parameterName); 170 Object paramValue = parameter.getValue(); 171 172 if (log.isDebugEnabled()) 173 { 174 log.debug("Parameter " + ejbParamName + ": " + paramValue); 175 } 176 177 query.setParameter(ejbParamName, paramValue); 178 } 179 } 180 } 181 182 Map queryHintsMap = (Map )getParameterValue(JRJpaQueryExecuterFactory.PARAMETER_JPA_QUERY_HINTS_MAP); 185 if (queryHintsMap != null) { 186 for (Iterator i = queryHintsMap.entrySet().iterator(); i.hasNext(); ) { 187 Map.Entry pairs = (Map.Entry )i.next(); 188 log.debug("EJBQL query hint [" + pairs.getKey() + "] set."); 189 query.setHint((String )pairs.getKey(), pairs.getValue()); 190 } 191 } 192 JRPropertiesMap datasetProperties = dataset.getPropertiesMap(); 196 String [] propertyNames = datasetProperties.getPropertyNames(); 197 for (int i = 0; i < propertyNames.length; i++) { 198 String propertyName = propertyNames[i]; 199 if (propertyName.startsWith(JRJpaQueryExecuterFactory.PROPERTY_JPA_QUERY_HINT_PREFIX)) { 200 String queryHint = propertyName.replaceFirst( 201 JRJpaQueryExecuterFactory.PROPERTY_JPA_QUERY_HINT_PREFIX, 202 ""); 203 if (queryHint != null && queryHint.length() > 0) { 204 String property = datasetProperties.getProperty(propertyName); 205 log.debug("EJBQL query hint [" + queryHint + "] set to: " + property); 206 query.setHint(queryHint, property); 207 } 208 } 209 } 210 } 211 212 217 protected JRDataSource createResultDatasource() { 218 JRDataSource resDatasource; 219 220 JRPropertiesMap datasetProperties = dataset.getPropertiesMap(); 221 222 try { 223 int pageSize = JRProperties.getIntegerProperty(datasetProperties, 224 JRJpaQueryExecuterFactory.PROPERTY_JPA_QUERY_PAGE_SIZE, 225 0); 226 227 resDatasource = new JRJpaDataSource(this, pageSize); 228 } 229 catch (NumberFormatException e) { 230 throw new JRRuntimeException("The " + JRJpaQueryExecuterFactory.PROPERTY_JPA_QUERY_PAGE_SIZE + 231 " property must be numerical."); 232 } 233 234 return resDatasource; 235 } 236 237 public synchronized void close() { 238 query = null; 239 } 240 241 public synchronized boolean cancelQuery() throws JRException { 242 return false; 243 } 244 245 protected String getParameterReplacement(String parameterName) { 246 return ':' + getEjbqlParameterName(parameterName); 247 } 248 249 protected String getEjbqlParameterName(String parameterName) { 250 return JRStringUtil.getLiteral(parameterName); 251 } 252 253 260 public List getResultList() { 261 if (reportMaxCount != null) { 262 query.setMaxResults(reportMaxCount.intValue()); 263 } 264 265 return query.getResultList(); 266 } 267 268 275 public List getResultList(int firstIndex, int resultCount) { 276 if (reportMaxCount != null && firstIndex + resultCount > reportMaxCount.intValue()) { 277 resultCount = reportMaxCount.intValue() - firstIndex; 278 } 279 280 query.setFirstResult(firstIndex); 281 query.setMaxResults(resultCount); 282 283 return query.getResultList(); 284 } 285 } 286 | Popular Tags |