FileDocCategorySizeDatePackage
ReportQueryResult.javaAPI DocGlassfish v2 API19583Thu Jun 28 06:23:00 BST 2007oracle.toplink.essentials.queryframework

ReportQueryResult

public class ReportQueryResult extends Object implements Serializable, Map
Purpose: A single row (type) result for a ReportQuery

Description: Represents a single row of attribute values (converted using mapping) for a ReportQuery. The attributes can be from various objects. Responsibilities:

  • Converted field values into object attribute values.
  • Provide acces to values by index or item name
author
Doug Clarke
since
TOPLink/Java 2.0

Fields Summary
protected Vector
names
Item names to lookup result values
protected Vector
results
Actual converted attribute values
protected Vector
primaryKeyValues
PK values if the retrievPKs flag was set on the ReportQuery. These can be used to get the actual object
protected StringBuffer
key
If an objectLevel distinct is used then generate unique key for this result
Constructors Summary
public ReportQueryResult(Vector results, Vector primaryKeyValues)
INTERNAL: Used to create test results

        this.results = results;
        this.primaryKeyValues = primaryKeyValues;
    
public ReportQueryResult(ReportQuery query, AbstractRecord row, Vector toManyResults)

        super();
        this.names = query.getNames();
        buildResult(query, row, toManyResults);
    
Methods Summary
protected voidbuildResult(oracle.toplink.essentials.queryframework.ReportQuery query, oracle.toplink.essentials.internal.sessions.AbstractRecord row, java.util.Vector toManyData)
INTERNAL: Create an array of attribute values (converted from raw field values using the mapping).

        //GF_ISSUE_395
        if (query.shouldDistinctBeUsed()){
            this.key = new StringBuffer();
        }
        //end GF_ISSUE
        int numberOfPrimaryKeyFields = 0;
        Vector results = new Vector(query.getItems().size());

        if (query.shouldRetrievePrimaryKeys()) {
            numberOfPrimaryKeyFields = query.getDescriptor().getPrimaryKeyFields().size();
            setPrimaryKeyValues(query.getDescriptor().getObjectBuilder().extractPrimaryKeyFromRow(row, query.getSession()));
            // For bug 3115576 this is only used for EXISTS subselects so no result is needed.
        } else if (query.shouldRetrieveFirstPrimaryKey()) {
            numberOfPrimaryKeyFields = 1;
        }

        // CR 4240
        // rowIndex is seperate as there may be place holders in the query that are not in the
        // result set. So we can not compare the index to row size as there may be less
        // objects in the row then there will be in the result set.
        
        for (int index = 0; index < query.getItems().size(); index++) {
            ReportItem item = (ReportItem)query.getItems().elementAt(index);
            if (item.isContructorItem()){
                ConstructorReportItem citem = (ConstructorReportItem)item;
                Class[] constructorArgTypes = citem.getConstructorArgTypes();
                List constructorMappings = citem.getConstructorMappings();
                int numberOfItems = citem.getReportItems().size();
                Object[] constructorArgs = new Object[numberOfItems];
                if (constructorArgTypes==null){
                    constructorArgTypes = new Class[numberOfItems];
                }
                
                for (int i=0;i<numberOfItems;i++){
                    ReportItem ritem = (ReportItem)citem.getReportItems().get(i);
                    if (constructorArgTypes[i]==null){
                        if((constructorMappings != null)&&(constructorMappings.get(i)!=null)){
                            constructorArgTypes[i] = ((DatabaseMapping)constructorMappings.get(i)).getAttributeClassification();
                        }else if (ritem.getResultType() != null) {
                            constructorArgTypes[i] = ritem.getResultType();
                        }else if (ritem.getDescriptor() != null) {
                            constructorArgTypes[i] = ritem.getDescriptor().getJavaClass();
                        }
                    }
                    Object result = processItem(query, row, toManyData, (ReportItem)citem.getReportItems().get(i));
                    constructorArgs[i] = ConversionManager.getDefaultManager().convertObject(result, constructorArgTypes[i]);
                    //no type was specified, so use the object class itself.
                    if (constructorArgTypes[i]==null){
                        constructorArgTypes[i] = constructorArgs[i].getClass();
                    }
                }
                try{
                    java.lang.reflect.Constructor constructor = null;
                    Object returnValue = null;
                    if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                        try {
                            constructor = (Constructor)AccessController.doPrivileged(new PrivilegedGetConstructorFor(citem.getResultType(), constructorArgTypes, true));
                            returnValue = AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, constructorArgs));
                        } catch (PrivilegedActionException exception) {
                            throw QueryException.exceptionWhileUsingConstructorExpression(exception.getException(), query);                       }
                    } else {
                        constructor = PrivilegedAccessHelper.getConstructorFor(citem.getResultType(), constructorArgTypes, true);
                        returnValue = PrivilegedAccessHelper.invokeConstructor(constructor, constructorArgs);
                    }
                    results.addElement(returnValue);
                } catch (NoSuchMethodException exc){
                    throw QueryException.exceptionWhileUsingConstructorExpression(exc, query);
                } catch (IllegalAccessException exc){
                    throw QueryException.exceptionWhileUsingConstructorExpression(exc, query);
                } catch (java.lang.reflect.InvocationTargetException exc){
                    throw QueryException.exceptionWhileUsingConstructorExpression(exc, query);
                } catch (InstantiationException exc){
                    throw QueryException.exceptionWhileUsingConstructorExpression(exc, query);
                }
                
            }else{
                Object value = processItem(query, row, toManyData, item);
                results.addElement(value);
            }
        }

        setResults(results);
    
public voidclear()
PUBLIC: Clear the contents of the result.

        this.names = new Vector();
        this.results = new Vector();
    
public booleancontains(java.lang.Object value)
PUBLIC: Check if the value is contained in the result.

        return containsValue(value);
    
public booleancontainsKey(java.lang.Object key)
PUBLIC: Check if the key is contained in the result.

        return getNames().contains(key);
    
public booleancontainsValue(java.lang.Object value)
PUBLIC: Check if the value is contained in the result.

        return getResults().contains(value);
    
public java.util.Enumerationelements()
PUBLIC: Return an enumeration of the result values.

        return getResults().elements();
    
public java.util.SetentrySet()
PUBLIC: Returns a set of the keys.

        // bug 2669127
        // implemented this method exactly the same way as DatabaseRow.entrySet()
        int size = this.size();
        Map tempMap = new HashMap(size);
        for (int i = 0; i < size; i++) {
            tempMap.put(this.getNames().elementAt(i), this.getResults().elementAt(i));
        }
        return tempMap.entrySet();
    
public booleanequals(java.lang.Object anObject)
PUBLIC: Compare if the two results are equal.

        if (anObject instanceof ReportQueryResult) {
            return equals((ReportQueryResult)anObject);
        }

        return false;
    
public booleanequals(oracle.toplink.essentials.queryframework.ReportQueryResult result)
INTERNAL: Used in testing to compare if results are correct.

        if (this == result) {
            return true;
        }
        if (!Helper.compareOrderedVectors(getResults(), result.getResults())) {
            return false;
        }

        // Compare PKs
        if (getPrimaryKeyValues() != null) {
            if (result.getPrimaryKeyValues() == null) {
                return false;
            }
            return Helper.compareOrderedVectors(getPrimaryKeyValues(), result.getPrimaryKeyValues());
        }

        return true;
    
public java.lang.Objectget(java.lang.Object name)
PUBLIC: Return the value for given item name.

        if (name instanceof String) {
            return get((String)name);
        }

        return null;
    
public java.lang.Objectget(java.lang.String name)
PUBLIC: Return the value for given item name.

        int index = getNames().indexOf(name);
        if (index == -1) {
            return null;
        }

        return getResults().elementAt(index);
    
public java.lang.ObjectgetByIndex(int index)
PUBLIC: Return the indexed value from result.

        return getResults().elementAt(index);
    
public java.util.VectorgetNames()
PUBLIC: Return the names of report items, provided to ReportQuery.

        return names;
    
public java.util.VectorgetPrimaryKeyValues()
PUBLIC: Return the PKs for the corresponding object or null if not requested.

        return primaryKeyValues;
    
public java.lang.StringgetResultKey()
INTERNAL: Return the unique key for this result

        if (this.key != null){
            return this.key.toString();
        }
        return null;
    
public java.util.VectorgetResults()
PUBLIC: Return the results.

        return results;
    
public booleanisEmpty()
PUBLIC: Return if the result is empty.

        return getNames().isEmpty();
    
public java.util.SetkeySet()
PUBLIC: Returns a set of the keys.

        return new HashSet(getNames());
    
public java.util.Enumerationkeys()
PUBLIC: Return an enumeration of the result names.

        return getNames().elements();
    
protected java.lang.ObjectprocessItem(oracle.toplink.essentials.queryframework.ReportQuery query, oracle.toplink.essentials.internal.sessions.AbstractRecord row, java.util.Vector toManyData, oracle.toplink.essentials.internal.queryframework.ReportItem item)
INTERNAL: Return a value from an item and database row (converted from raw field values using the mapping).

        JoinedAttributeManager joinManager = item.getJoinedAttributeManager();
        if (joinManager.isToManyJoin()){
                    joinManager.setDataResults(toManyData, query.getSession());
        }
        DatabaseMapping mapping = item.getMapping();
        Object value = null;
        if (!item.isPlaceHolder()) {
            if (mapping != null){
                //if mapping is not null then it must be a direct mapping - see Reportitem.init
                value = row.getValues().get(item.getResultIndex());
                value = ((AbstractDirectMapping)mapping).getAttributeValue(value, query.getSession());
                // GF_ISSUE_395
                if (this.key != null){
                    this.key.append(value);
                    this.key.append("_");
                }
                // end GF_ISSUE
            }else if (item.getDescriptor() != null){
                //item is for an object result.
                if (item.getDescriptor().getAllFields().size() + item.getResultIndex() > row.size()) {
                    throw QueryException.reportQueryResultSizeMismatch(item.getDescriptor().getAllFields().size() + item.getResultIndex(), row.size());
                }
                Vector trimedFields = Helper.copyVector(row.getFields(), item.getResultIndex(), row.size());
                Vector trimedValues = Helper.copyVector(row.getValues(), item.getResultIndex(), row.size());
                AbstractRecord subRow = new DatabaseRecord(trimedFields, trimedValues);
                value = item.getDescriptor().getObjectBuilder().buildObject(query, subRow, joinManager);
                // GF_ISSUE_395
                if (this.key != null){
                    List list = item.getDescriptor().getObjectBuilder().extractPrimaryKeyFromRow(subRow, query.getSession());
                    if(list!=null){//GF bug3233, Distinct Processing fails with NPE when referenced target is null in database. 
	                    for (Iterator iterator = list.iterator(); iterator.hasNext();){
	                        this.key.append(iterator.next());
	                        this.key.append("-");
	                    }
                    }
                    this.key.append("_");
                }
                // end GF_ISSUE
            }else{
                value = row.getValues().get(item.getResultIndex());
                // GF_ISSUE_395
                if (this.key != null){
                    this.key.append(value);
                }
                // end GF_ISSUE
            }
        }
        return value;
    
public java.lang.Objectput(java.lang.Object name, java.lang.Object value)
ADVANCED: Set the value for given item name.

        int index = getNames().indexOf(name);
        if (index == -1) {
            getNames().addElement(name);
            getResults().addElement(value);
            return null;
        }

        Object oldValue = getResults().elementAt(index);
        getResults().setElementAt(value, index);
        return oldValue;
    
public voidputAll(java.util.Map map)
PUBLIC: Add all of the elements.

        Iterator entriesIterator = map.entrySet().iterator();
        while (entriesIterator.hasNext()) {
            Map.Entry entry = (Map.Entry)entriesIterator.next();
            put(entry.getKey(), entry.getValue());
        }
    
public java.lang.ObjectreadObject(java.lang.Class javaClass, oracle.toplink.essentials.sessions.Session session)
PUBLIC: If the PKs were retrieved with the attributes then this method can be used to read the real object from the database.

        if (getPrimaryKeyValues() == null) {
            throw QueryException.reportQueryResultWithoutPKs(this);
        }

        ReadObjectQuery query = new ReadObjectQuery(javaClass);
        query.setSelectionKey(getPrimaryKeyValues());

        return session.executeQuery(query);
    
public java.lang.Objectremove(java.lang.Object name)
INTERNAL: Remove the name key and value from the result.

        int index = getNames().indexOf(name);
        if (index >= 0) {
            getNames().removeElementAt(index);
            Object value = getResults().elementAt(index);
            getResults().removeElementAt(index);
            return value;
        }
        return null;
    
protected voidsetNames(java.util.Vector names)

        this.names = names;
    
protected voidsetPrimaryKeyValues(java.util.Vector primaryKeyValues)
INTERNAL: Set the PK values for the result row's object.

        this.primaryKeyValues = primaryKeyValues;
    
public voidsetResults(java.util.Vector results)
INTERNAL: Set the results.

        this.results = results;
    
public intsize()
PUBLIC: Return the number of name/value pairs in the result.

        return getNames().size();
    
public java.lang.Object[]toArray()
INTERNAL: Converts the ReportQueryResult to a simple array of values.

       List list = getResults();
       return (list == null) ? null : list.toArray();
    
public java.util.ListtoList()
INTERNAL: Converts the ReportQueryResult to a simple list of values.

        return this.getResults();
    
public java.lang.StringtoString()

        java.io.StringWriter writer = new java.io.StringWriter();
        writer.write("ReportQueryResult(");
        for (int index = 0; index < getResults().size(); index++) {
            writer.write(String.valueOf(getResults().elementAt(index)));
            if (index < (getResults().size() - 1)) {
                writer.write(", ");
            }
        }
        writer.write(")");
        return writer.toString();
    
public java.util.Collectionvalues()
PUBLIC: Returns an collection of the values.

        return getResults();