FileDocCategorySizeDatePackage
EntityResult.javaAPI DocGlassfish v2 API12724Tue May 22 16:54:50 BST 2007oracle.toplink.essentials.queryframework

EntityResult

public class EntityResult extends SQLResult

Purpose: Concrete class to represent the EntityResult structure as defined by the EJB 3.0 Persistence specification. This class is a subcompent of the SQLResultSetMapping

see
SQLResultSetMapping
author
Gordon Yorke
since
TopLink Java Essentials

Fields Summary
protected String
entityClassName
Stores the class name of result
protected Class
entityClass
protected Map
fieldResults
Stores the list of FieldResult
protected String
discriminatorColumn
Stores the column that will contain the value to determine the correct subclass to create if applicable.
Constructors Summary
public EntityResult(Class entityClass)

        this.entityClass = entityClass;
        if (this.entityClass == null){
            throw new IllegalArgumentException(ExceptionLocalization.buildMessage("null_value_for_entity_result"));
        }
    
public EntityResult(String entityClassName)

        this.entityClassName = entityClassName;
        if (this.entityClassName == null){
            throw new IllegalArgumentException(ExceptionLocalization.buildMessage("null_value_for_entity_result"));
        }
    
Methods Summary
public voidaddFieldResult(oracle.toplink.essentials.queryframework.FieldResult fieldResult)

        if (fieldResult == null || fieldResult.getAttributeName() == null){
            return;
        }
        FieldResult existingFieldResult = (FieldResult)getFieldResults().get(fieldResult.getAttributeName());
        if (existingFieldResult==null){
            getFieldResults().put(fieldResult.getAttributeName(), fieldResult);
        }else{
            existingFieldResult.add(fieldResult);
        }
    
public voidconvertClassNamesToClasses(java.lang.ClassLoader classLoader)
INTERNAL: Convert all the class-name-based settings in this query to actual class-based settings. This method is used when converting a project that has been built with class names to a project with classes.

param
classLoader

        super.convertClassNamesToClasses(classLoader);
        Class entityClass = null;
        try{
            if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                try {
                    entityClass = (Class)AccessController.doPrivileged(new PrivilegedClassForName(entityClassName, true, classLoader));
                } catch (PrivilegedActionException exception) {
                    throw ValidationException.classNotFoundWhileConvertingClassNames(entityClassName, exception.getException());
                }
            } else {
                entityClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(entityClassName, true, classLoader);
            }
        } catch (ClassNotFoundException exc){
            throw ValidationException.classNotFoundWhileConvertingClassNames(entityClassName, exc);
        }
        this.entityClass = entityClass;
    
public java.lang.StringgetDiscriminatorColumn()
Returns the column name for the column that will store the value used to determine the subclass type if applicable.

        return this.discriminatorColumn;
    
public java.util.MapgetFieldResults()
Accessor for the internally stored list of FieldResult. Calling this method will result in a collection being created to store the FieldResult

        if (this.fieldResults == null){
            this.fieldResults = new HashMap();
        }
        return this.fieldResults;
    
public java.lang.ObjectgetValueFromRecord(oracle.toplink.essentials.sessions.DatabaseRecord record, oracle.toplink.essentials.queryframework.ResultSetMappingQuery query)
INTERNAL: This method is a convience method for extracting values from Results

        //from the row data build result entity.
        // To do this let's collect the column based data for this entity from
        // the results and call build object with this new row.
        ClassDescriptor descriptor = query.getSession().getDescriptor(this.entityClass);
        DatabaseRecord entityRecord = new DatabaseRecord(descriptor.getFields().size());
        if (descriptor.hasInheritance()){
            if (this.discriminatorColumn != null){
                Object value = record.get(this.discriminatorColumn);
                if (value == null){
                    throw QueryException.discriminatorColumnNotSelected(this.discriminatorColumn, query.getSQLResultSetMapping().getName());
                }
                entityRecord.put(descriptor.getInheritancePolicy().getClassIndicatorField(), record.get(this.discriminatorColumn));
            }else{
                entityRecord.put(descriptor.getInheritancePolicy().getClassIndicatorField(), record.get(descriptor.getInheritancePolicy().getClassIndicatorField()));
            }
            // if the descriptor uses inheritance and multiple types may have been read
            //get the correct descriptor.
            if (descriptor.hasInheritance() && descriptor.getInheritancePolicy().shouldReadSubclasses()) {
                Class classValue = descriptor.getInheritancePolicy().classFromRow(entityRecord, query.getSession());
                descriptor = query.getSession().getDescriptor(classValue);
            }
        }
        for (Iterator mappings = descriptor.getMappings().iterator(); mappings.hasNext();){
            DatabaseMapping mapping = (DatabaseMapping)mappings.next();
            FieldResult fieldResult = (FieldResult)this.getFieldResults().get(mapping.getAttributeName());
            if (fieldResult != null){
                if (mapping.getFields().size() == 1 ){
                    entityRecord.put(mapping.getFields().firstElement(), record.get(fieldResult.getColumnName()));
                }else if (mapping.getFields().size() >1){
                    getValueFromRecordForMapping(entityRecord,mapping,fieldResult,record);
                }
            }else{
                for (Iterator fields = mapping.getFields().iterator(); fields.hasNext();){
                    DatabaseField field = (DatabaseField)fields.next();
                    entityRecord.put(field, record.get(field));
                }
            }
        }
        query.setReferenceClass(this.entityClass);
        query.setDescriptor(descriptor);
        return descriptor.getObjectBuilder().buildObject(query, entityRecord, new JoinedAttributeManager(descriptor, (ExpressionBuilder)null, query));
    
public voidgetValueFromRecordForMapping(oracle.toplink.essentials.sessions.DatabaseRecord entityRecord, oracle.toplink.essentials.mappings.DatabaseMapping mapping, oracle.toplink.essentials.queryframework.FieldResult fieldResult, oracle.toplink.essentials.sessions.DatabaseRecord databaseRecord)
INTERNAL: This method is for processing all FieldResults for a mapping. Adds DatabaseFields to the passed in entityRecord

        ClassDescriptor currentDescriptor = mapping.getReferenceDescriptor();
        /** check if this FieldResult contains any other FieldResults, process it if it doesn't */
        if (fieldResult.getFieldResults()==null){
            DatabaseField dbfield = processValueFromRecordForMapping(currentDescriptor,fieldResult.getMultipleFieldIdentifiers(),1);
            /** If it is a 1:1 mapping we need to do the target to source field conversion.  If it is an aggregate, it is fine as it is*/
            if (mapping.isOneToOneMapping()){
                dbfield = (DatabaseField)(((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(dbfield));
            }
            entityRecord.put(dbfield, databaseRecord.get(fieldResult.getColumnName()));
            return;
        }
        /** This processes each FieldResult stored in the collection of FieldResults individually */
        Iterator fieldResults = fieldResult.getFieldResults().iterator();
        while (fieldResults.hasNext()){
            FieldResult tempFieldResult = ((FieldResult)fieldResults.next());
            DatabaseField dbfield = processValueFromRecordForMapping(currentDescriptor,tempFieldResult.getMultipleFieldIdentifiers(),1);
             if (mapping.isOneToOneMapping()){
                dbfield = (DatabaseField)(((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(dbfield));
            }
            entityRecord.put(dbfield, databaseRecord.get(tempFieldResult.getColumnName()));
        }
    
public booleanisEntityResult()

        return true;
    
public oracle.toplink.essentials.internal.helper.DatabaseFieldprocessValueFromRecordForMapping(oracle.toplink.essentials.descriptors.ClassDescriptor descriptor, java.lang.String[] attributeNames, int currentLoc)
INTERNAL: This method is for processing a single FieldResult, returning the DatabaseField it refers to.

        DatabaseMapping mapping = descriptor.getMappingForAttributeName(attributeNames[currentLoc]);
        if (mapping==null){throw QueryException.mappingForFieldResultNotFound(attributeNames,currentLoc);}
        currentLoc++;
        if (attributeNames.length!=currentLoc){
            ClassDescriptor currentDescriptor = mapping.getReferenceDescriptor();
            DatabaseField df= processValueFromRecordForMapping(currentDescriptor, attributeNames, currentLoc);
            if (mapping.isOneToOneMapping()){
                return (DatabaseField)(((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(df));
            }
            return df;
        }else{
            //this is it.. return this mapping's field
            return (DatabaseField) mapping.getFields().firstElement();
        }
    
public voidsetDiscriminatorColumn(java.lang.String column)
Sets the column name for the column that will store the value used to determine the subclass type if applicable.

        if (column == null){
            return;
        }
        this.discriminatorColumn = column;