1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.io.*; 25 import java.lang.reflect.Constructor ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 import java.util.*; 29 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.internal.helper.*; 32 import oracle.toplink.essentials.internal.queryframework.*; 33 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 34 import oracle.toplink.essentials.internal.security.PrivilegedGetConstructorFor; 35 import oracle.toplink.essentials.internal.security.PrivilegedInvokeConstructor; 36 import oracle.toplink.essentials.mappings.*; 37 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping; 38 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 39 import oracle.toplink.essentials.sessions.DatabaseRecord; 40 import oracle.toplink.essentials.sessions.Session; 41 42 56 public class ReportQueryResult implements Serializable, Map { 57 58 59 protected Vector names; 60 61 62 protected Vector results; 63 64 65 protected Vector primaryKeyValues; 66 67 68 protected StringBuffer key; 70 71 75 public ReportQueryResult(Vector results, Vector primaryKeyValues) { 76 this.results = results; 77 this.primaryKeyValues = primaryKeyValues; 78 } 79 80 public ReportQueryResult(ReportQuery query, AbstractRecord row, Vector toManyResults) { 81 super(); 82 this.names = query.getNames(); 83 buildResult(query, row, toManyResults); 84 } 85 86 90 protected void buildResult(ReportQuery query, AbstractRecord row, Vector toManyData) { 91 if (query.shouldDistinctBeUsed()){ 93 this.key = new StringBuffer (); 94 } 95 int numberOfPrimaryKeyFields = 0; 97 Vector results = new Vector(query.getItems().size()); 98 99 if (query.shouldRetrievePrimaryKeys()) { 100 numberOfPrimaryKeyFields = query.getDescriptor().getPrimaryKeyFields().size(); 101 setPrimaryKeyValues(query.getDescriptor().getObjectBuilder().extractPrimaryKeyFromRow(row, query.getSession())); 102 } else if (query.shouldRetrieveFirstPrimaryKey()) { 104 numberOfPrimaryKeyFields = 1; 105 } 106 107 112 for (int index = 0; index < query.getItems().size(); index++) { 113 ReportItem item = (ReportItem)query.getItems().elementAt(index); 114 if (item.isContructorItem()){ 115 ConstructorReportItem citem = (ConstructorReportItem)item; 116 Class [] constructorArgTypes = citem.getConstructorArgTypes(); 117 List constructorMappings = citem.getConstructorMappings(); 118 int numberOfItems = citem.getReportItems().size(); 119 Object [] constructorArgs = new Object [numberOfItems]; 120 if (constructorArgTypes==null){ 121 constructorArgTypes = new Class [numberOfItems]; 122 } 123 124 for (int i=0;i<numberOfItems;i++){ 125 ReportItem ritem = (ReportItem)citem.getReportItems().get(i); 126 if (constructorArgTypes[i]==null){ 127 if((constructorMappings != null)&&(constructorMappings.get(i)!=null)){ 128 constructorArgTypes[i] = ((DatabaseMapping)constructorMappings.get(i)).getAttributeClassification(); 129 }else if (ritem.getResultType() != null) { 130 constructorArgTypes[i] = ritem.getResultType(); 131 }else if (ritem.getDescriptor() != null) { 132 constructorArgTypes[i] = ritem.getDescriptor().getJavaClass(); 133 } 134 } 135 Object result = processItem(query, row, toManyData, (ReportItem)citem.getReportItems().get(i)); 136 constructorArgs[i] = ConversionManager.getDefaultManager().convertObject(result, constructorArgTypes[i]); 137 if (constructorArgTypes[i]==null){ 139 constructorArgTypes[i] = constructorArgs[i].getClass(); 140 } 141 } 142 try{ 143 java.lang.reflect.Constructor constructor = null; 144 Object returnValue = null; 145 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 146 try { 147 constructor = (Constructor )AccessController.doPrivileged(new PrivilegedGetConstructorFor(citem.getResultType(), constructorArgTypes, true)); 148 returnValue = AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, constructorArgs)); 149 } catch (PrivilegedActionException exception) { 150 throw QueryException.exceptionWhileUsingConstructorExpression(exception.getException(), query); } 151 } else { 152 constructor = PrivilegedAccessHelper.getConstructorFor(citem.getResultType(), constructorArgTypes, true); 153 returnValue = PrivilegedAccessHelper.invokeConstructor(constructor, constructorArgs); 154 } 155 results.addElement(returnValue); 156 } catch (NoSuchMethodException exc){ 157 throw QueryException.exceptionWhileUsingConstructorExpression(exc, query); 158 } catch (IllegalAccessException exc){ 159 throw QueryException.exceptionWhileUsingConstructorExpression(exc, query); 160 } catch (java.lang.reflect.InvocationTargetException exc){ 161 throw QueryException.exceptionWhileUsingConstructorExpression(exc, query); 162 } catch (InstantiationException exc){ 163 throw QueryException.exceptionWhileUsingConstructorExpression(exc, query); 164 } 165 166 }else{ 167 Object value = processItem(query, row, toManyData, item); 168 results.addElement(value); 169 } 170 } 171 172 setResults(results); 173 } 174 175 179 protected Object processItem(ReportQuery query, AbstractRecord row, Vector toManyData, ReportItem item) { 180 JoinedAttributeManager joinManager = item.getJoinedAttributeManager(); 181 if (joinManager.isToManyJoin()){ 182 joinManager.setDataResults(toManyData, query.getSession()); 183 } 184 DatabaseMapping mapping = item.getMapping(); 185 Object value = null; 186 if (!item.isPlaceHolder()) { 187 if (mapping != null){ 188 value = row.getValues().get(item.getResultIndex()); 190 value = ((AbstractDirectMapping)mapping).getAttributeValue(value, query.getSession()); 191 if (this.key != null){ 193 this.key.append(value); 194 this.key.append("_"); 195 } 196 }else if (item.getDescriptor() != null){ 198 if (item.getDescriptor().getAllFields().size() + item.getResultIndex() > row.size()) { 200 throw QueryException.reportQueryResultSizeMismatch(item.getDescriptor().getAllFields().size() + item.getResultIndex(), row.size()); 201 } 202 Vector trimedFields = Helper.copyVector(row.getFields(), item.getResultIndex(), row.size()); 203 Vector trimedValues = Helper.copyVector(row.getValues(), item.getResultIndex(), row.size()); 204 AbstractRecord subRow = new DatabaseRecord(trimedFields, trimedValues); 205 value = item.getDescriptor().getObjectBuilder().buildObject(query, subRow, joinManager); 206 if (this.key != null){ 208 List list = item.getDescriptor().getObjectBuilder().extractPrimaryKeyFromRow(subRow, query.getSession()); 209 for (Iterator iterator = list.iterator(); iterator.hasNext();){ 210 this.key.append(iterator.next()); 211 this.key.append("-"); 212 } 213 this.key.append("_"); 214 } 215 }else{ 217 value = row.getValues().get(item.getResultIndex()); 218 if (this.key != null){ 220 this.key.append(value); 221 } 222 } 224 } 225 return value; 226 } 227 228 229 233 public void clear() { 234 this.names = new Vector(); 235 this.results = new Vector(); 236 } 237 238 242 public boolean contains(Object value) { 243 return containsValue(value); 244 } 245 246 250 public boolean containsKey(Object key) { 251 return getNames().contains(key); 252 } 253 254 258 public boolean containsValue(Object value) { 259 return getResults().contains(value); 260 } 261 262 266 public Enumeration elements() { 267 return getResults().elements(); 268 } 269 270 274 public Set entrySet() { 275 int size = this.size(); 278 Map tempMap = new HashMap(size); 279 for (int i = 0; i < size; i++) { 280 tempMap.put(this.getNames().elementAt(i), this.getResults().elementAt(i)); 281 } 282 return tempMap.entrySet(); 283 } 284 285 289 public boolean equals(Object anObject) { 290 if (anObject instanceof ReportQueryResult) { 291 return equals((ReportQueryResult)anObject); 292 } 293 294 return false; 295 } 296 297 301 public boolean equals(ReportQueryResult result) { 302 if (this == result) { 303 return true; 304 } 305 if (!Helper.compareOrderedVectors(getResults(), result.getResults())) { 306 return false; 307 } 308 309 if (getPrimaryKeyValues() != null) { 311 if (result.getPrimaryKeyValues() == null) { 312 return false; 313 } 314 return Helper.compareOrderedVectors(getPrimaryKeyValues(), result.getPrimaryKeyValues()); 315 } 316 317 return true; 318 } 319 320 324 public Object get(Object name) { 325 if (name instanceof String ) { 326 return get((String )name); 327 } 328 329 return null; 330 } 331 332 336 public Object get(String name) { 337 int index = getNames().indexOf(name); 338 if (index == -1) { 339 return null; 340 } 341 342 return getResults().elementAt(index); 343 } 344 345 349 public Object getByIndex(int index) { 350 return getResults().elementAt(index); 351 } 352 353 357 public String getResultKey(){ 358 if (this.key != null){ 359 return this.key.toString(); 360 } 361 return null; 362 } 363 364 365 369 public Vector getNames() { 370 return names; 371 } 372 373 377 public Vector getPrimaryKeyValues() { 378 return primaryKeyValues; 379 } 380 381 385 public Vector getResults() { 386 return results; 387 } 388 389 393 public boolean isEmpty() { 394 return getNames().isEmpty(); 395 } 396 397 401 public Enumeration keys() { 402 return getNames().elements(); 403 } 404 405 409 public Set keySet() { 410 return new HashSet(getNames()); 411 } 412 413 417 public Object put(Object name, Object value) { 418 int index = getNames().indexOf(name); 419 if (index == -1) { 420 getNames().addElement(name); 421 getResults().addElement(value); 422 return null; 423 } 424 425 Object oldValue = getResults().elementAt(index); 426 getResults().setElementAt(value, index); 427 return oldValue; 428 } 429 430 434 public void putAll(Map map) { 435 Iterator entriesIterator = map.entrySet().iterator(); 436 while (entriesIterator.hasNext()) { 437 Map.Entry entry = (Map.Entry)entriesIterator.next(); 438 put(entry.getKey(), entry.getValue()); 439 } 440 } 441 442 446 public Object readObject(Class javaClass, Session session) { 447 if (getPrimaryKeyValues() == null) { 448 throw QueryException.reportQueryResultWithoutPKs(this); 449 } 450 451 ReadObjectQuery query = new ReadObjectQuery(javaClass); 452 query.setSelectionKey(getPrimaryKeyValues()); 453 454 return session.executeQuery(query); 455 } 456 457 461 public Object remove(Object name) { 462 int index = getNames().indexOf(name); 463 if (index >= 0) { 464 getNames().removeElementAt(index); 465 Object value = getResults().elementAt(index); 466 getResults().removeElementAt(index); 467 return value; 468 } 469 return null; 470 } 471 472 protected void setNames(Vector names) { 473 this.names = names; 474 } 475 476 480 protected void setPrimaryKeyValues(Vector primaryKeyValues) { 481 this.primaryKeyValues = primaryKeyValues; 482 } 483 484 488 public void setResults(Vector results) { 489 this.results = results; 490 } 491 492 496 public int size() { 497 return getNames().size(); 498 } 499 500 504 public Object [] toArray(){ 505 List list = getResults(); 506 return (list == null) ? null : list.toArray(); 507 } 508 509 513 public List toList(){ 514 return this.getResults(); 515 } 516 517 public String toString() { 518 java.io.StringWriter writer = new java.io.StringWriter (); 519 writer.write("ReportQueryResult("); 520 for (int index = 0; index < getResults().size(); index++) { 521 writer.write(String.valueOf(getResults().elementAt(index))); 522 if (index < (getResults().size() - 1)) { 523 writer.write(", "); 524 } 525 } 526 writer.write(")"); 527 return writer.toString(); 528 } 529 530 534 public Collection values() { 535 return getResults(); 536 } 537 } 538 | Popular Tags |