1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import oracle.toplink.essentials.exceptions.QueryException; 31 import oracle.toplink.essentials.internal.helper.DatabaseField; 32 import oracle.toplink.essentials.internal.localization.ExceptionLocalization; 33 import oracle.toplink.essentials.internal.queryframework.JoinedAttributeManager; 34 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 35 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 36 import oracle.toplink.essentials.mappings.DatabaseMapping; 37 import oracle.toplink.essentials.descriptors.ClassDescriptor; 38 import oracle.toplink.essentials.exceptions.ValidationException; 39 import oracle.toplink.essentials.expressions.ExpressionBuilder; 40 import oracle.toplink.essentials.mappings.OneToOneMapping; 41 import oracle.toplink.essentials.sessions.DatabaseRecord; 42 43 53 54 public class EntityResult extends SQLResult { 55 56 protected String entityClassName; 57 protected Class entityClass; 58 59 60 protected Map fieldResults; 61 62 65 protected String discriminatorColumn; 66 67 public EntityResult(Class entityClass){ 68 this.entityClass = entityClass; 69 if (this.entityClass == null){ 70 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("null_value_for_entity_result")); 71 } 72 } 73 74 public EntityResult(String entityClassName){ 75 this.entityClassName = entityClassName; 76 if (this.entityClassName == null){ 77 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("null_value_for_entity_result")); 78 } 79 } 80 81 public void addFieldResult(FieldResult fieldResult){ 82 if (fieldResult == null || fieldResult.getAttributeName() == null){ 83 return; 84 } 85 FieldResult existingFieldResult = (FieldResult)getFieldResults().get(fieldResult.getAttributeName()); 86 if (existingFieldResult==null){ 87 getFieldResults().put(fieldResult.getAttributeName(), fieldResult); 88 }else{ 89 existingFieldResult.add(fieldResult); 90 } 91 } 92 93 100 public void convertClassNamesToClasses(ClassLoader classLoader){ 101 super.convertClassNamesToClasses(classLoader); 102 Class entityClass = null; 103 try{ 104 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 105 try { 106 entityClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(entityClassName, true, classLoader)); 107 } catch (PrivilegedActionException exception) { 108 throw ValidationException.classNotFoundWhileConvertingClassNames(entityClassName, exception.getException()); 109 } 110 } else { 111 entityClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(entityClassName, true, classLoader); 112 } 113 } catch (ClassNotFoundException exc){ 114 throw ValidationException.classNotFoundWhileConvertingClassNames(entityClassName, exc); 115 } 116 this.entityClass = entityClass; 117 }; 118 119 123 public Map getFieldResults(){ 124 if (this.fieldResults == null){ 125 this.fieldResults = new HashMap (); 126 } 127 return this.fieldResults; 128 } 129 130 134 public String getDiscriminatorColumn(){ 135 return this.discriminatorColumn; 136 } 137 138 142 public void setDiscriminatorColumn(String column){ 143 if (column == null){ 144 return; 145 } 146 this.discriminatorColumn = column; 147 } 148 149 153 public Object getValueFromRecord(DatabaseRecord record, ResultSetMappingQuery query){ 154 ClassDescriptor descriptor = query.getSession().getDescriptor(this.entityClass); 158 DatabaseRecord entityRecord = new DatabaseRecord(descriptor.getFields().size()); 159 if (descriptor.hasInheritance()){ 160 if (this.discriminatorColumn != null){ 161 Object value = record.get(this.discriminatorColumn); 162 if (value == null){ 163 throw QueryException.discriminatorColumnNotSelected(this.discriminatorColumn, query.getSQLResultSetMapping().getName()); 164 } 165 entityRecord.put(descriptor.getInheritancePolicy().getClassIndicatorField(), record.get(this.discriminatorColumn)); 166 }else{ 167 entityRecord.put(descriptor.getInheritancePolicy().getClassIndicatorField(), record.get(descriptor.getInheritancePolicy().getClassIndicatorField())); 168 } 169 if (descriptor.hasInheritance() && descriptor.getInheritancePolicy().shouldReadSubclasses()) { 172 Class classValue = descriptor.getInheritancePolicy().classFromRow(entityRecord, query.getSession()); 173 descriptor = query.getSession().getDescriptor(classValue); 174 } 175 } 176 for (Iterator mappings = descriptor.getMappings().iterator(); mappings.hasNext();){ 177 DatabaseMapping mapping = (DatabaseMapping)mappings.next(); 178 FieldResult fieldResult = (FieldResult)this.getFieldResults().get(mapping.getAttributeName()); 179 if (fieldResult != null){ 180 if (mapping.getFields().size() == 1 ){ 181 entityRecord.put(mapping.getFields().firstElement(), record.get(fieldResult.getColumnName())); 182 }else if (mapping.getFields().size() >1){ 183 getValueFromRecordForMapping(entityRecord,mapping,fieldResult,record); 184 } 185 }else{ 186 for (Iterator fields = mapping.getFields().iterator(); fields.hasNext();){ 187 DatabaseField field = (DatabaseField)fields.next(); 188 entityRecord.put(field, record.get(field)); 189 } 190 } 191 } 192 query.setReferenceClass(this.entityClass); 193 query.setDescriptor(descriptor); 194 return descriptor.getObjectBuilder().buildObject(query, entityRecord, new JoinedAttributeManager(descriptor, (ExpressionBuilder)null, query)); 195 } 196 197 public boolean isEntityResult(){ 198 return true; 199 } 200 201 205 public void getValueFromRecordForMapping(DatabaseRecord entityRecord,DatabaseMapping mapping, FieldResult fieldResult, DatabaseRecord databaseRecord){ 206 ClassDescriptor currentDescriptor = mapping.getReferenceDescriptor(); 207 208 if (fieldResult.getFieldResults()==null){ 209 DatabaseField dbfield = processValueFromRecordForMapping(currentDescriptor,fieldResult.getMultipleFieldIdentifiers(),1); 210 211 if (mapping.isOneToOneMapping()){ 212 dbfield = (DatabaseField)(((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(dbfield)); 213 } 214 entityRecord.put(dbfield, databaseRecord.get(fieldResult.getColumnName())); 215 return; 216 } 217 218 Iterator fieldResults = fieldResult.getFieldResults().iterator(); 219 while (fieldResults.hasNext()){ 220 FieldResult tempFieldResult = ((FieldResult)fieldResults.next()); 221 DatabaseField dbfield = processValueFromRecordForMapping(currentDescriptor,tempFieldResult.getMultipleFieldIdentifiers(),1); 222 if (mapping.isOneToOneMapping()){ 223 dbfield = (DatabaseField)(((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(dbfield)); 224 } 225 entityRecord.put(dbfield, databaseRecord.get(tempFieldResult.getColumnName())); 226 } 227 } 228 229 233 public DatabaseField processValueFromRecordForMapping(ClassDescriptor descriptor, String [] attributeNames, int currentLoc){ 234 DatabaseMapping mapping = descriptor.getMappingForAttributeName(attributeNames[currentLoc]); 235 if (mapping==null){throw QueryException.mappingForFieldResultNotFound(attributeNames,currentLoc);} 236 currentLoc++; 237 if (attributeNames.length!=currentLoc){ 238 ClassDescriptor currentDescriptor = mapping.getReferenceDescriptor(); 239 DatabaseField df= processValueFromRecordForMapping(currentDescriptor, attributeNames, currentLoc); 240 if (mapping.isOneToOneMapping()){ 241 return (DatabaseField)(((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(df)); 242 } 243 return df; 244 }else{ 245 return (DatabaseField) mapping.getFields().firstElement(); 247 } 248 } 249 250 } 251 | Popular Tags |