def test_filter_by_owner(self, gateway, dtype):
     """Query should filter by owner."""
     p = ParametersI()
     p.theFilter = Filter()
     p.theFilter.ownerId = wrap(2)
     # Test using 'params' argument
     with_params = gateway.buildQuery(dtype, params=p)
     # Test using 'opts' dictionary
     with_opts = gateway.buildQuery(dtype, opts={'owner': 1})
     for result in [with_params, with_opts]:
         query, params, wrapper = result
         assert isinstance(query, str)
         assert isinstance(params, Parameters)
         assert isinstance(wrapper(), BlitzObjectWrapper)
         if dtype not in ('experimenter', 'experimentergroup'):
             assert "where owner" in query
         else:
             assert "where owner" not in query
def move_well_annotations(conn, well, ann_type, remove_anns, ns):
    """Move annotations from Images in this Well onto the Well itself."""
    log("Processing Well: %s %s" % (well.id, well.getWellPos()))
    iids = [wellSample.getImage().id for wellSample in well.listChildren()]
    log("  Image IDs: %s" % iids)
    if len(iids) == 0:
        return 0

    # Params to query links. If not Admin, only work with our own links
    params = ParametersI()
    if not conn.isAdmin():
        params.theFilter = Filter()
        params.theFilter.ownerId = rlong(conn.getUserId())

    old_links = list(conn.getAnnotationLinks('Image', iids,
                                             ns=ns, params=params))

    # Filter by type
    old_links = [l for l in old_links
                 if (ann_type is None
                     or (l.child.__class__.__name__ == ann_type))]

    link_ids = [l.id for l in old_links]

    def get_key(ann_link, with_owner=False):
        # We use ann's 'key' to avoid adding duplicate annotations
        # Key includes link owner (allows multiple links with different owners)
        ann = ann_link.child
        return "%s_%s" % (ann_link.details.owner.id.val, ann.id.val)

    links_dict = {}
    # Remove duplicate annotations according to get_key(l)
    for l in old_links:
        links_dict[get_key(l, conn.isAdmin())] = l

    old_links = links_dict.values()

    # Find existing links on Well so we don't try to duplicate them
    existing_well_links = list(conn.getAnnotationLinks('Well', [well.id],
                                                       ns=ns, params=params))
    existing_well_keys = [get_key(l) for l in existing_well_links]

    new_links = []
    for l in old_links:
        if get_key(l) in existing_well_keys:
            continue
        log("    Annotation: %s %s" % (l.child.id.val,
                                       l.child.__class__.__name__))
        link = WellAnnotationLinkI()
        link.parent = WellI(well.id, False)
        link.child = l.child
        # If Admin, the new link Owner is same as old link Owner
        if conn.isAdmin():
            owner_id = l.details.owner.id.val
            link.details.owner = ExperimenterI(owner_id, False)
        new_links.append(link)
    try:
        conn.getUpdateService().saveArray(new_links)
    except Exception, ex:
        log("Failed to create links: %s" % ex.message)
        return 0