1 32 33 package it.businesslogic.ireport.data.hibernate; 34 35 import bsh.Interpreter; 36 import it.businesslogic.ireport.IReportConnection; 37 import it.businesslogic.ireport.JRField; 38 import it.businesslogic.ireport.JRParameter; 39 import it.businesslogic.ireport.connection.JRHibernateConnection; 40 import it.businesslogic.ireport.data.FieldClassWrapper; 41 import it.businesslogic.ireport.gui.MainFrame; 42 import it.businesslogic.ireport.gui.ReportQueryDialog; 43 import it.businesslogic.ireport.util.Misc; 44 45 import java.util.Collection ; 46 import java.util.Enumeration ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.Map ; 50 import java.util.Vector ; 51 52 import org.hibernate.Hibernate; 53 import org.hibernate.Query; 54 import org.hibernate.SessionFactory; 55 import org.hibernate.Transaction; 56 import org.hibernate.cfg.Configuration; 57 import org.hibernate.classic.Session; 58 import org.hibernate.type.Type; 59 60 64 public class HQLFieldsReader { 65 66 private static final Map hibernateTypeMap; 67 68 69 static 70 { 71 hibernateTypeMap = new HashMap (); 72 hibernateTypeMap.put(Boolean .class, Hibernate.BOOLEAN); 73 hibernateTypeMap.put(Byte .class, Hibernate.BYTE); 74 hibernateTypeMap.put(Double .class, Hibernate.DOUBLE); 75 hibernateTypeMap.put(Float .class, Hibernate.FLOAT); 76 hibernateTypeMap.put(Integer .class, Hibernate.INTEGER); 77 hibernateTypeMap.put(Long .class, Hibernate.LONG); 78 hibernateTypeMap.put(Short .class, Hibernate.SHORT); 79 hibernateTypeMap.put(java.math.BigDecimal .class, Hibernate.BIG_DECIMAL); 80 hibernateTypeMap.put(java.math.BigInteger .class, Hibernate.BIG_INTEGER); 81 hibernateTypeMap.put(Character .class, Hibernate.CHARACTER); 82 hibernateTypeMap.put(String .class, Hibernate.STRING); 83 hibernateTypeMap.put(java.util.Date .class, Hibernate.DATE); 84 hibernateTypeMap.put(java.sql.Timestamp .class, Hibernate.TIMESTAMP); 85 hibernateTypeMap.put(java.sql.Time .class, Hibernate.TIME); 86 } 87 88 private Interpreter interpreter = null; 89 private Vector reportParameters = null; 90 private String queryString = ""; 91 private HashMap queryParameters = new HashMap (); 92 93 private Vector notScalars = new Vector (); 94 95 96 public HQLFieldsReader(String query, Vector reportParameters) { 97 98 99 this.setQueryString(query); 100 this.setReportParameters(reportParameters); 101 } 102 103 104 public String prepareQuery() throws Exception  105 { 106 System.out.println(getReportParameters()); 107 Enumeration enumParams = getReportParameters().elements(); 108 109 while( enumParams.hasMoreElements() ) { 110 111 JRParameter param = (JRParameter)enumParams.nextElement(); 112 String parameterName = param.getName(); 113 114 if (queryString.indexOf("$P!{" + parameterName + "}") > 0) 115 { 116 Object paramVal = ReportQueryDialog.recursiveInterpreter( getInterpreter(), param.getDefaultValueExpression(),getReportParameters()); 117 118 if (paramVal == null) 119 { 120 paramVal = ""; 121 } 122 123 queryString = Misc.string_replace(""+paramVal, "$P!{" + parameterName + "}", queryString); 124 } 125 else if (getQueryString().indexOf("$P{" + parameterName + "}") > 0) 126 { 127 Object paramVal = ReportQueryDialog.recursiveInterpreter( getInterpreter(), param.getDefaultValueExpression(),getReportParameters()); 128 String parameterReplacement = "_" + getLiteral(parameterName); 129 130 queryParameters.put(parameterReplacement, paramVal); 131 132 queryString = Misc.string_replace(":"+parameterReplacement, "$P{" + parameterName + "}", queryString); 133 134 System.out.println( queryString ); 135 } 136 } 137 return queryString; 138 } 139 140 141 public Vector readFields() throws Exception  142 { 143 prepareQuery(); 144 145 146 SessionFactory hb_sessionFactory = null; 147 Session hb_session = null; 148 Transaction transaction = null; 149 150 notScalars.clear(); 151 152 try { 153 IReportConnection conn = (IReportConnection)MainFrame.getMainInstance().getProperties().get("DefaultConnection"); 154 if (!(conn instanceof JRHibernateConnection)) 155 { 156 throw new Exception ("No Hibernate connection selected."); 157 } 158 159 hb_session = ((JRHibernateConnection)conn).createSession(); 160 161 if (hb_session == null) 162 { 163 throw new Exception ("Problem creating the Session object for Hibernate"); 164 } 165 166 transaction = hb_session.beginTransaction(); 167 Query q = hb_session.createQuery(getQueryString()); 168 169 Iterator paramIterator = queryParameters.keySet().iterator(); 170 171 while (paramIterator.hasNext()) 172 { 173 String hqlParamName = ""+paramIterator.next(); 174 setParameter(hb_session,q, hqlParamName, queryParameters.get(hqlParamName)); 175 } 176 177 q.setFetchSize(1); 178 java.util.Iterator iterator = q.iterate(); 179 181 String [] aliases = q.getReturnAliases(); 182 Type[] types = q.getReturnTypes(); 183 184 185 Vector fields = new Vector (); 186 187 for (int i =0; i<types.length; ++i) 188 { 189 if (types[i].isComponentType() || 190 types[i].isEntityType()) 191 { 192 193 String aliasName = null; 195 if (aliases != null && aliases.length > i && !aliases[i].equals(i+"")) 196 { 197 aliasName = aliases[i]; 198 JRField field = new JRField(aliases[i], types[i].getReturnedClass().getName()); 199 field.setDescription(aliases[i]); 200 fields.add(field); 201 202 203 } 204 205 java.beans.PropertyDescriptor [] pd = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(types[i].getReturnedClass()); 207 208 if (aliasName != null) 209 { 210 notScalars.add( new FieldClassWrapper(aliasName, types[i].getReturnedClass().getName())); 211 } 212 else 213 { 214 notScalars.add( types[i].getReturnedClass().getName()); 215 } 216 217 for (int nd =0; nd < pd.length; ++nd) 218 { 219 String fieldName = pd[nd].getName(); 220 if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) 221 { 222 if (fieldName.equals("class")) continue; 223 224 String returnType = pd[nd].getPropertyType().getName(); 225 226 it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(fieldName, returnType); 227 if (types.length > 1 && aliasName != null) 228 { 229 fieldName = aliasName+"."+fieldName; 230 field.setDescription(fieldName); field.setName(fieldName); 232 } 233 fields.add(field); 234 } 235 } 236 237 238 } 239 else 240 { 241 String fieldName = types[i].getName(); 242 if (aliases != null && 243 aliases.length > i && 244 !aliases[i].equals(""+i)) fieldName = aliases[i]; 245 JRField field = new JRField(fieldName, types[i].getReturnedClass().getName()); 246 field.setDescription(""); 247 fields.add(field); 248 } 249 } 250 269 270 return fields; 271 272 } catch (Exception ex) 273 { 274 ex.printStackTrace(); 275 throw ex; 276 } finally { 277 278 if (transaction != null) try { transaction.rollback(); } catch (Exception ex) { } 279 if (hb_session != null) try { hb_session.close(); } catch (Exception ex) { } 280 } 281 } 282 283 288 protected void setParameter(Session session, Query query, String hqlParamName, Object parameterValue) throws Exception  289 { 290 292 if (parameterValue == null) 293 { 294 System.out.println("Parameter: " + hqlParamName); 295 query.setParameter(hqlParamName, parameterValue); 296 return; 297 } 298 299 Class clazz = parameterValue.getClass(); 300 Type type = (Type) hibernateTypeMap.get(clazz); 301 302 if (type != null) 303 { 304 query.setParameter(hqlParamName, parameterValue, type); 305 } 306 else if (Collection .class.isAssignableFrom(clazz)) 307 { 308 query.setParameterList(hqlParamName, (Collection ) parameterValue); 309 } 310 else 311 { 312 if (session.getSessionFactory().getClassMetadata(clazz) != null) { 314 query.setEntity(hqlParamName, parameterValue); 315 } 316 else { 318 query.setParameter(hqlParamName, parameterValue); 319 } 320 } 321 } 322 323 private Interpreter prepareExpressionEvaluator() throws bsh.EvalError { 324 325 Interpreter interpreter = new Interpreter(); 326 interpreter.setClassLoader(interpreter.getClass().getClassLoader()); 327 return interpreter; 328 } 329 330 331 337 public static String getLiteral(String name) 338 { 339 if (isValidLiteral(name)) 340 { 341 return name; 342 } 343 344 StringBuffer buffer = new StringBuffer (name.length() + 5); 345 346 char[] literalChars = new char[name.length()]; 347 name.getChars(0, literalChars.length, literalChars, 0); 348 349 for (int i = 0; i < literalChars.length; i++) 350 { 351 if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) 352 { 353 buffer.append((int)literalChars[i]); 354 } 355 else if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) 356 { 357 buffer.append((int)literalChars[i]); 358 } 359 else 360 { 361 buffer.append(literalChars[i]); 362 } 363 } 364 365 return buffer.toString(); 366 } 367 368 369 374 private static boolean isValidLiteral(String literal) 375 { 376 boolean result = true; 377 378 char[] literalChars = new char[literal.length()]; 379 literal.getChars(0, literalChars.length, literalChars, 0); 380 381 for (int i = 0; i < literalChars.length; i++) 382 { 383 if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) 384 { 385 result = false; 386 break; 387 } 388 389 if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) 390 { 391 result = false; 392 break; 393 } 394 } 395 396 return result; 397 } 398 399 public Interpreter getInterpreter() { 400 401 if (interpreter == null) 402 { 403 try { 404 interpreter = prepareExpressionEvaluator(); 405 } catch (Exception ex) 406 { 407 408 } 409 } 410 return interpreter; 411 } 412 413 public void setInterpreter(Interpreter interpreter) { 414 this.interpreter = interpreter; 415 } 416 417 public Vector getReportParameters() { 418 return reportParameters; 419 } 420 421 public void setReportParameters(Vector reportParameters) { 422 this.reportParameters = reportParameters; 423 } 424 425 public String getQueryString() { 426 return queryString; 427 } 428 429 public void setQueryString(String queryString) { 430 this.queryString = queryString; 431 } 432 433 public HashMap getQueryParameters() { 434 return queryParameters; 435 } 436 437 public void setQueryParameters(HashMap queryParameters) { 438 this.queryParameters = queryParameters; 439 } 440 441 public Vector getNotScalars() { 442 return notScalars; 443 } 444 445 public void setNotScalars(Vector notScalars) { 446 this.notScalars = notScalars; 447 } 448 449 } 450 | Popular Tags |