'Returns a list of Items joined with their most recent ItemRevision,
potentially filtered by the criteria above.'

def get_all_ctype_filtered(self, tech=None, account=None, region=None, name=None, include_inactive=False):

    item_map = {}
    query = Item.query
    if tech:
        query = query.join((Technology, (Item.tech_id == Technology.id))).filter((Technology.name == tech))
    if account:
        query = query.join((Account, (Item.account_id == Account.id))).filter((Account.name == account))
    filter_by = {'region': region, 'name': name}
    for (k, v) in filter_by.items():
        if (not v):
            del filter_by[k]
    query = query.filter_by(**filter_by)
    attempt = 1
    while True:
        try:
            items = query.all()
            break
        except Exception as e:
            app.logger.warn('Database    Exception    in    Datastore::get_all_ctype_filtered.    Sleeping    for    a    few    seconds.    Attempt    {}.'.format(attempt))
            app.logger.debug('Exception:    {}'.format(e))
            import time
            time.sleep(5)
            attempt = (attempt + 1)
            if (attempt > 5):
                raise Exception('Too    many    retries    for    database    connections.')
    for item in items:
        if (not item.latest_revision_id):
            app.logger.debug('There    are    no    itemrevisions    for    this    item:    {}'.format(item.id))
            continue
        most_recent = ItemRevision.query.get(item.latest_revision_id)
        if ((not most_recent.active) and (not include_inactive)):
            continue
        item_map[item] = most_recent
    return item_map
