Skip to content

Commit

Permalink
Batch unlink (#81)
Browse files Browse the repository at this point in the history
* Add methods to unlink multiple links/annotations at once
* Modify tests
  • Loading branch information
Rdornier authored Feb 23, 2024
1 parent 1548d8d commit 0303c4b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
53 changes: 49 additions & 4 deletions src/main/java/fr/igred/omero/AnnotatableWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import omero.model.IObject;
import omero.model.NamedValue;
import omero.model.TagAnnotationI;
import omero.sys.ParametersI;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -841,6 +842,53 @@ public <A extends GenericAnnotationWrapper<?>> void unlink(Client client, A anno
}


/**
* Unlinks the given annotations from the current object.
*
* @param client The client handling the connection.
* @param annotations List of annotations
* @param <A> The type of the annotation.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
* @throws OMEROServerError Server error.
* @throws InterruptedException If block(long) does not return.
*/
public <A extends GenericAnnotationWrapper<?>> void unlink(Client client, Collection<A> annotations)
throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException {
removeLinks(client, annotationLinkType(), annotations.stream().map(A::getId).collect(Collectors.toList()));
}


/**
* Removes the link of the given type with the given child IDs.
*
* @param client The client handling the connection.
* @param linkType The link type.
* @param childIds List of link child IDs.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
* @throws OMEROServerError Server error.
* @throws InterruptedException If block(long) does not return.
*/
protected void removeLinks(Client client, String linkType, Collection<Long> childIds)
throws ServiceException, OMEROServerError, AccessException, ExecutionException, InterruptedException {
String template = "select link from %s link where link.parent = %d and link.child.id in (:ids)";
String query = String.format(template, linkType, getId());
ParametersI param = new ParametersI();
param.addIds(childIds);
List<IObject> os = ExceptionHandler.of(client.getGateway(),
g -> g.getQueryService(client.getCtx())
.findAllByQuery(query, param))
.handleOMEROException("Cannot get links from " + this)
.get();
client.delete(os);
}


/**
* Removes the link of the given type with the given child ID.
*
Expand All @@ -856,10 +904,7 @@ public <A extends GenericAnnotationWrapper<?>> void unlink(Client client, A anno
*/
protected void removeLink(Client client, String linkType, long childId)
throws ServiceException, OMEROServerError, AccessException, ExecutionException, InterruptedException {
List<IObject> os = client.findByQuery("select link from " + linkType +
" link where link.parent = " + getId() +
" and link.child = " + childId);
delete(client, os.iterator().next());
removeLinks(client, linkType, Collections.singletonList(childId));
}


Expand Down
2 changes: 1 addition & 1 deletion src/test/java/fr/igred/omero/repository/FolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void testAddAndRemoveTagFromFolder() throws Exception {

List<TagAnnotationWrapper> tags = folder.getTags(client);
assertEquals(1, tags.size());
folder.unlink(client, tags.get(0));
folder.unlink(client, tags);

List<TagAnnotationWrapper> removed = folder.getTags(client);
assertEquals(0, removed.size());
Expand Down

0 comments on commit 0303c4b

Please sign in to comment.