'establish actions and dependencies related to a flush.
These actions will operate on all relevant states
individually.    This occurs only if there are cycles
in the \'aggregated\' version of events.'

def per_state_flush_actions(self, uow, states, isdelete):

    parent_base_mapper = self.parent.primary_base_mapper
    child_base_mapper = self.mapper.primary_base_mapper
    child_saves = unitofwork.SaveUpdateAll(uow, child_base_mapper)
    child_deletes = unitofwork.DeleteAll(uow, child_base_mapper)
    if isdelete:
        before_delete = unitofwork.ProcessAll(uow, self, True, True)
        before_delete.disabled = True
    else:
        after_save = unitofwork.ProcessAll(uow, self, False, True)
        after_save.disabled = True
    if (child_saves not in uow.cycles):
        assert (child_deletes not in uow.cycles)
        child_actions = [(child_saves, False), (child_deletes, True)]
        child_in_cycles = False
    else:
        child_in_cycles = True
    if (not isdelete):
        parent_saves = unitofwork.SaveUpdateAll(uow, self.parent.base_mapper)
        parent_deletes = before_delete = None
        if (parent_saves in uow.cycles):
            parent_in_cycles = True
    else:
        parent_deletes = unitofwork.DeleteAll(uow, self.parent.base_mapper)
        parent_saves = after_save = None
        if (parent_deletes in uow.cycles):
            parent_in_cycles = True
    for state in states:
        sum_ = state.manager[self.key].impl.get_all_pending(state, state.dict)
        if (not sum_):
            continue
        if isdelete:
            before_delete = unitofwork.ProcessState(uow, self, True, state)
            if parent_in_cycles:
                parent_deletes = unitofwork.DeleteState(uow, state, parent_base_mapper)
        else:
            after_save = unitofwork.ProcessState(uow, self, False, state)
            if parent_in_cycles:
                parent_saves = unitofwork.SaveUpdateState(uow, state, parent_base_mapper)
        if child_in_cycles:
            child_actions = []
            for (child_state, child) in sum_:
                if (child_state not in uow.states):
                    child_action = (None, None)
                else:
                    (deleted, listonly) = uow.states[child_state]
                    if deleted:
                        child_action = (unitofwork.DeleteState(uow, child_state, child_base_mapper), True)
                    else:
                        child_action = (unitofwork.SaveUpdateState(uow, child_state, child_base_mapper), False)
                child_actions.append(child_action)
        for (child_action, childisdelete) in child_actions:
            self.per_state_dependencies(uow, parent_saves, parent_deletes, child_action, after_save, before_delete, isdelete, childisdelete)
