Class JoinedAttributeManager

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class JoinedAttributeManager
    extends java.lang.Object
    implements java.lang.Cloneable, java.io.Serializable

    Purpose: A common class to be used by ObjectLevelReadQueries and ReportItems. This Class will be used to store Joined Attribute Expressions. It will also store the indexes for object construction.

    Since:
    EJB3.0 RI
    Author:
    Gordon Yorke
    See Also:
    Serialized Form
    • Field Detail

      • joinedAggregateMappings

        protected transient java.util.List<DatabaseMapping> joinedAggregateMappings
        Stores AggregateObjectMapping expressions used within local join expressions
      • joinedAttributeMappings

        protected transient java.util.List<DatabaseMapping> joinedAttributeMappings
        indexed list of mappings corresponding to
      • joinedAttributeExpressions

        protected java.util.List<Expression> joinedAttributeExpressions
        Stores the joined attributes added through the query
      • joinedMappingExpressions

        protected java.util.List<Expression> joinedMappingExpressions
        Stores the joined attributes as specified in the descriptor
      • joinedAttributes

        protected java.util.List<Expression> joinedAttributes
        PERF: Cache the local joined attribute expressions.
      • isToManyJoin

        protected boolean isToManyJoin
        Used to determine if -m joining has been used.
      • hasOuterJoinedAttribute

        protected boolean hasOuterJoinedAttribute
        PERF: Used to avoid null checks for inner attribute joining.
      • joinedMappingIndexes

        protected transient java.util.Map<DatabaseMapping,​java.lang.Object> joinedMappingIndexes
        Used internally for joining.
      • dataResults

        protected transient java.util.List<AbstractRecord> dataResults
        Stored all row results to -m joining.
      • dataResultsByPrimaryKey

        protected transient java.util.Map<java.lang.Object,​java.util.List<AbstractRecord>> dataResultsByPrimaryKey
        Stored all row results to -m joining by cache key.
      • descriptor

        protected transient ClassDescriptor descriptor
        Stores the descriptor that these joins apply on
      • baseExpressionBuilder

        protected ExpressionBuilder baseExpressionBuilder
        Stores the base builder for resolving joined attributes by name.
      • lastJoinedAttributeBaseExpression

        protected Expression lastJoinedAttributeBaseExpression
        Stores the last used base expression while adding joined attribute expression.
      • parentResultIndex

        protected int parentResultIndex
        Stores the result index of the parent, used for oneToMany joins.
      • shouldFilterDuplicates

        protected boolean shouldFilterDuplicates
        Determine if duplicate rows should be filter when using 1-m joining.
      • orderByExpressions

        protected transient java.util.List<Expression> orderByExpressions
      • additionalFieldExpressions

        protected transient java.util.List<Expression> additionalFieldExpressions
    • Method Detail

      • shouldFilterDuplicates

        public boolean shouldFilterDuplicates()
        Return if duplicate rows should be filter when using 1-m joining.
      • setShouldFilterDuplicates

        public void setShouldFilterDuplicates​(boolean shouldFilterDuplicates)
        Set if duplicate rows should be filter when using 1-m joining.
      • addJoinedAttribute

        public void addJoinedAttribute​(Expression attributeExpression)
      • addJoinedAttributeExpression

        public void addJoinedAttributeExpression​(Expression attributeExpression)
      • addJoinedMappingExpression

        public void addJoinedMappingExpression​(Expression mappingExpression)
        Add an attribute represented by the given attribute name to the list of joins for this query. Note: Mapping level joins are represented separately from query level joins.
      • addJoinedMapping

        public void addJoinedMapping​(java.lang.String attributeName)
        Add an attribute represented by the given attribute name to the list of joins for this query. Note: Mapping level joins are represented separately from query level joins.
      • clone

        public JoinedAttributeManager clone()
        Clones the Joined Attribute Manager. Generally called from Query.clone().
        Overrides:
        clone in class java.lang.Object
      • copyFrom

        public void copyFrom​(JoinedAttributeManager otherJoinManager)
        Copies settings from another manager. Should copy all the attributes that clone method clones.
      • clear

        public void clear()
        Clear the joining state. This is used to redefine a queries joins for nested joins.
      • computeJoiningMappingIndexes

        public int computeJoiningMappingIndexes​(boolean includeAllSubclassFields,
                                                AbstractSession session,
                                                int offset)
        For joining the resulting rows include the field/values for many objects. As some of the objects may have the same field names, these row partitions need to be calculated. The indexes are stored in the query and used later when building the objects.
      • computeNestedQueriesForJoinedExpressions

        protected void computeNestedQueriesForJoinedExpressions​(java.util.List joinedExpressions,
                                                                AbstractSession session,
                                                                ObjectLevelReadQuery readQuery)
        This method is used when computing the nested queries for joined mappings. It recurses computing the nested mapping queries and their join indexes.
      • computeJoiningMappingQueries

        public void computeJoiningMappingQueries​(AbstractSession session)
        Used to optimize joining by pre-computing the nested join queries for the mappings.
      • computeIndexesForJoinedExpressions

        protected int computeIndexesForJoinedExpressions​(java.util.List joinedExpressions,
                                                         int currentIndex,
                                                         AbstractSession session)
        This method is used when computing the indexes for joined mappings. It iterates through a list of join expressions and adds an index that represents where the fields represented by that expression will appear in the row returned by a read query. Method computeNestedQueriesForJoinedExpressions(List, AbstractSession, ObjectLevelReadQuery) must be already called.
        Parameters:
        joinedExpressions - Join expressions List.
        currentIndex - Current joined mapping index.
        session - Current session.
        Returns:
        Current joined mapping index updated.
      • getAdditionalFieldExpressions

        public java.util.List<Expression> getAdditionalFieldExpressions()
        Get the list of additional field expressions.
      • getAdditionalFieldExpressions_

        public java.util.List<Expression> getAdditionalFieldExpressions_()
        Get the list of additional field expressions.
      • getBaseExpressionBuilder

        public ExpressionBuilder getBaseExpressionBuilder()
        Returns the base expression builder for this query.
      • getDataResults_

        public java.util.List<AbstractRecord> getDataResults_()
        Return all of the rows fetched by the query, used for 1-m joining.
      • hasAdditionalFieldExpressions

        public boolean hasAdditionalFieldExpressions()
        Return if there are additional field expressions.
      • setAdditionalFieldExpressions_

        public void setAdditionalFieldExpressions_​(java.util.List<Expression> expressions)
        Set the list of additional field expressions.
      • getJoinedAggregateMappings

        public java.util.List<DatabaseMapping> getJoinedAggregateMappings()
        Return the attributes that must be joined.
      • getJoinedAttributeExpressions

        public java.util.List<Expression> getJoinedAttributeExpressions()
        Return the attributes that must be joined.
      • getJoinedAttributeMappings

        public java.util.List<DatabaseMapping> getJoinedAttributeMappings()
        Return the attributes that must be joined.
      • getJoinedAttributes

        public java.util.List<Expression> getJoinedAttributes()
        Return the attributes that must be joined.
      • getJoinedMappingExpressions

        public java.util.List<Expression> getJoinedMappingExpressions()
        Get the list of expressions that represent elements that are joined because of their mapping for this query.
      • hasJoinedAttributeExpressions

        public boolean hasJoinedAttributeExpressions()
        Return the attributes that must be joined.
      • hasJoinedExpressions

        public boolean hasJoinedExpressions()
        This method checks both attribute expressions and mapping expressions and determines if there are any joins to be made.
      • hasJoinedMappingExpressions

        public boolean hasJoinedMappingExpressions()
        Return the attributes that must be joined.
      • hasJoinedAttributes

        public boolean hasJoinedAttributes()
        Return if any attributes are joined. This is a convience method that is only valid after prepare.
      • hasOuterJoinedAttributeQuery

        public boolean hasOuterJoinedAttributeQuery()
        PERF: Return if the query uses any outer attribute joins, used to avoid null checks in building objects.
      • getOrderByExpressions

        public java.util.List<Expression> getOrderByExpressions()
        Get the list of orderBy expressions.
      • getOrderByExpressions_

        public java.util.List<Expression> getOrderByExpressions_()
        Get the list of orderBy expressions.
      • getValueFromObjectForExpression

        public java.lang.Object getValueFromObjectForExpression​(AbstractSession session,
                                                                java.lang.Object clone,
                                                                ObjectExpression expression)
        INTERNAL: Helper method to get the value from the clone for the expression passed in, triggering joins on all intermediate steps. Example expression "emp.project.pk" with a clone Employee will trigger indirection and return the project pk value.
        Parameters:
        session -
        clone -
        expression -
        Returns:
      • hasOrderByExpressions

        public boolean hasOrderByExpressions()
        Return if there are orderBy expressions.
      • setOrderByExpressions_

        public void setOrderByExpressions_​(java.util.List<Expression> expressions)
        Set the list of orderBy expressions.
      • isToManyJoin

        public boolean isToManyJoin()
        Return if the query uses any -m joins, and thus return duplicate/multiple rows.
      • isAttributeJoined

        public boolean isAttributeJoined​(ClassDescriptor mappingDescriptor,
                                         DatabaseMapping attributeMapping)
        Return if the attribute is specified for joining.
      • isMappingInJoinedExpressionList

        protected boolean isMappingInJoinedExpressionList​(DatabaseMapping attributeMapping,
                                                          java.util.List joinedExpressionList)
        Iterate through a list of expressions searching for the given attribute name. Return true if it is found, false otherwise. Only use if the query was preprepared so that join expressions were processed.
      • isAttributeNameInJoinedExpressionList

        protected boolean isAttributeNameInJoinedExpressionList​(java.lang.String attributeName,
                                                                java.util.List joinedExpressionList)
        Iterate through a list of expressions searching for the given attribute name. Return true if it is found, false otherwise.
      • isAttributeExpressionJoined

        protected boolean isAttributeExpressionJoined​(DatabaseMapping attributeMapping)
        Return if the attribute is specified for joining.
      • isAttributeMappingJoined

        protected boolean isAttributeMappingJoined​(DatabaseMapping attributeMapping)
        Return whether the given attribute is joined as a result of a join on a mapping
      • setJoinedAttributeExpressions_

        public void setJoinedAttributeExpressions_​(java.util.List joinedExpressions)
        Set the list of expressions that represent elements that are joined because of their mapping for this query.
      • setJoinedMappingExpressions_

        public void setJoinedMappingExpressions_​(java.util.List joinedMappingExpressions)
        Set the list of expressions that represent elements that are joined because of their mapping for this query.
      • getJoinedMappingIndexes_

        public java.util.Map<DatabaseMapping,​java.lang.Object> getJoinedMappingIndexes_()
        Return the joined mapping indexes, used to compute mapping row partitions.
      • getJoinedMappingQueries_

        public java.util.Map<DatabaseMapping,​ObjectLevelReadQuery> getJoinedMappingQueries_()
        Return the joined mapping queries, used optimize joining, only compute the nested queries once.
      • getNestedJoinedMappingQuery

        public ObjectLevelReadQuery getNestedJoinedMappingQuery​(Expression expression)
        INTERNAL: Returns the nested query corresponding to the expression. The passed expression should be either join mapping or joined attribute expression.
      • setJoinedMappingQueries_

        public void setJoinedMappingQueries_​(java.util.Map joinedMappingQueries)
        Set the joined mapping queries, used optimize joining, only compute the nested queries once.
      • setJoinedMappingIndexes_

        public void setJoinedMappingIndexes_​(java.util.Map joinedMappingIndexes)
        Set the joined mapping indexes, used to compute mapping row partitions.
      • setIsOuterJoinedAttributeQuery

        protected void setIsOuterJoinedAttributeQuery​(boolean isOuterJoinedAttribute)
        PERF: Set if the query uses any outer attribute joins, used to avoid null checks in building objects.
      • setIsToManyJoinQuery

        public void setIsToManyJoinQuery​(boolean isToManyJoin)
        Set if the query uses any -m joins, and thus return duplicate/multiple rows.
      • prepareJoinExpressions

        public void prepareJoinExpressions​(AbstractSession session)
        Validate and prepare join expressions.
      • addExpressionAndBaseToGroupedList

        protected Expression addExpressionAndBaseToGroupedList​(Expression expression,
                                                               java.util.List expressionlist,
                                                               Expression lastJoinedAttributeBaseExpression)
        adds expression and its base expressions recursively to the expressionList in groups, so that an expression is never listed before its base expression
        Parameters:
        expression -
        expressionlist -
        lastJoinedAttributeBaseExpression -
        Returns:
      • processJoinedMappings

        public void processJoinedMappings​(AbstractSession session)
        This method collects the Joined Mappings from the descriptor and initializes them. Excludes the mapping that are not in the passed mappingsAllowedToJoin set (if it's not null).
      • reset

        public void reset()
        Reset the JoinedAttributeManager. This will be called when the Query is re-prepared
      • setBaseQuery

        public void setBaseQuery​(ObjectLevelReadQuery query)
        This method is called from within this package it is used when initializing a report Item
      • setBaseExpressionBuilder

        protected void setBaseExpressionBuilder​(ExpressionBuilder builder)
        This method is called from within this package, it is used when initializing a ReportItem
      • getDataResultsByPrimaryKey

        public java.util.Map<java.lang.Object,​java.util.List<AbstractRecord>> getDataResultsByPrimaryKey()
        Return all of the rows fetched by the query by cache-key, used for 1-m joining.
      • setDataResultsByPrimaryKey

        protected void setDataResultsByPrimaryKey​(java.util.Map<java.lang.Object,​java.util.List<AbstractRecord>> dataResultsByPrimaryKey)
        Set all of the rows fetched by the query by cache-key, used for 1-m joining.
      • setDataResults

        public void setDataResults​(java.util.List dataResults,
                                   AbstractSession session)
        Set all of the rows fetched by the query, used for 1-m joining.
      • processDataResults

        protected void processDataResults​(AbstractSession session)
        Process the data-results for joined data for a 1-m join. This allows all the data to be processed once, instead of n times for each object.
      • clearDataResults

        public void clearDataResults()
        Clear the data-results for joined data for a 1-m join.
      • processDataResults

        public AbstractRecord processDataResults​(AbstractRecord row,
                                                 Cursor cursor,
                                                 boolean forward)
        Process the data-results for joined data for a 1-m join. This allows incremental processing for a cursor.
      • setDescriptor

        public void setDescriptor​(ClassDescriptor descriptor)
        Called to set the descriptor on a Join Managerwith in a ReportItem, durring initialization, and durring DatabaseQuery.checkDescriptor.
      • setupLockingClauseForJoinedExpressions

        public ForUpdateOfClause setupLockingClauseForJoinedExpressions​(ForUpdateOfClause lockingClause,
                                                                        AbstractSession session)
        Used for joining in conjunction with pessimistic locking. Iterate through a list of joined expressions and ensure expression is set on the locking clause for each expression that represents a pessimisically locked descriptor.
      • setParentResultIndex

        public void setParentResultIndex​(int parentsResultIndex)
      • getParentResultIndex

        public int getParentResultIndex()
      • setJoinedMappingQueryClones

        public void setJoinedMappingQueryClones​(java.util.Map joinedMappingQueryClones)