Skip to content

Commit

Permalink
return the ID of the linked annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rdornier committed Dec 14, 2023
1 parent 75b1024 commit 3dd3732
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/main/java/fr/igred/omero/AnnotatableWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,20 @@ public <A extends GenericAnnotationWrapper<?>> boolean isLinked(Client client, A
* @param client The client handling the connection.
* @param annotation The {@link AnnotationData}.
* @param <A> The type of the annotation.
* @return the ID of the new Annotation.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
protected <A extends AnnotationData> void link(Client client, A annotation)
protected <A extends AnnotationData> long link(Client client, A annotation)
throws ServiceException, AccessException, ExecutionException {
String error = String.format("Cannot add %s to %s", annotation, this);
ExceptionHandler.of(client.getDm(), d -> d.attachAnnotation(client.getCtx(), annotation, data))
.handleOMEROException(error)
.rethrow();
A annotationData = ExceptionHandler.of(client.getDm(),
d -> d.attachAnnotation(client.getCtx(), annotation, data))
.handleOMEROException(error)
.get();
return annotationData.getId();
}


Expand All @@ -127,15 +130,16 @@ protected <A extends AnnotationData> void link(Client client, A annotation)
* @param client The client handling the connection.
* @param annotation Annotation to be added.
* @param <A> The type of the annotation.
* @return the ID of the new Annotation.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public <A extends GenericAnnotationWrapper<?>> void link(Client client, A annotation)
public <A extends GenericAnnotationWrapper<?>> long link(Client client, A annotation)
throws ServiceException, AccessException, ExecutionException {
if (!(annotation instanceof TagAnnotationWrapper) || !((TagAnnotationWrapper) annotation).isTagSet()) {
link(client, annotation.asDataObject());
return link(client, annotation.asDataObject());
} else {
throw new IllegalArgumentException("Tag sets should only be linked to tags");
}
Expand All @@ -147,16 +151,19 @@ public <A extends GenericAnnotationWrapper<?>> void link(Client client, A annota
*
* @param client The client handling the connection.
* @param annotations Annotations to add.
* @return the IDs of the new Annotations.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void link(Client client, GenericAnnotationWrapper<?>... annotations)
public List<Long> link(Client client, GenericAnnotationWrapper<?>... annotations)
throws ServiceException, AccessException, ExecutionException {
List<Long> annotationIds = new ArrayList<>();
for (GenericAnnotationWrapper<?> annotation : annotations) {
link(client, annotation);
annotationIds.add(link(client, annotation));
}
return annotationIds;
}


Expand All @@ -165,17 +172,18 @@ public void link(Client client, GenericAnnotationWrapper<?>... annotations)
*
* @param client The client handling the connection.
* @param annotations Annotations to add.
* @return the IDs of the new Annotations.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void linkIfNotLinked(Client client, GenericAnnotationWrapper<?>... annotations)
public List<Long> linkIfNotLinked(Client client, GenericAnnotationWrapper<?>... annotations)
throws ServiceException, AccessException, ExecutionException {
List<Long> annotationIds = getAnnotationData(client).stream()
.map(DataObject::getId)
.collect(Collectors.toList());
link(client, Arrays.stream(annotations)
return link(client, Arrays.stream(annotations)
.filter(a -> !annotationIds.contains(a.getId()))
.toArray(GenericAnnotationWrapper<?>[]::new));
}
Expand All @@ -187,16 +195,17 @@ public void linkIfNotLinked(Client client, GenericAnnotationWrapper<?>... annota
* @param client The client handling the connection.
* @param name Tag Name.
* @param description Tag description.
* @return the ID of the new TagAnnotation.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void addTag(Client client, String name, String description)
public long addTag(Client client, String name, String description)
throws ServiceException, AccessException, ExecutionException {
TagAnnotationWrapper tag = new TagAnnotationWrapper(new TagAnnotationData(name));
tag.setDescription(description);
link(client, tag);
return link(client, tag);
}


Expand Down Expand Up @@ -356,17 +365,18 @@ public void addPairKeyValue(Client client, String key, String value)
* @param client The client handling the connection.
* @param key Name of the key.
* @param value Value associated to the key.
* @return the ID of the new MapAnnotation.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void addKeyValuePair(Client client, String key, String value)
public long addKeyValuePair(Client client, String key, String value)
throws ServiceException, AccessException, ExecutionException {
List<NamedValue> kv = Collections.singletonList(new NamedValue(key, value));
MapAnnotationWrapper pkv = new MapAnnotationWrapper(kv);
pkv.setNameSpace(NSCLIENTMAPANNOTATION.value);
link(client, pkv);
return link(client, pkv);
}


Expand Down Expand Up @@ -472,21 +482,23 @@ public List<RatingAnnotationWrapper> getRatings(Client client)
*
* @param client The client handling the connection.
* @param rating The rating.
* @return the ID of the new Table.
*
* @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 The thread was interrupted.
*/
public void rate(Client client, int rating)
public long rate(Client client, int rating)
throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException {
List<Long> userIds = Collections.singletonList(client.getCtx().getExperimenter());
List<RatingAnnotationWrapper> ratings = getRatings(client, userIds);

long annotationId;
if (ratings.isEmpty()) {
RatingAnnotationWrapper rate = new RatingAnnotationWrapper(rating);
link(client, rate);
annotationId = link(client, rate);
} else {
int n = ratings.size();
if (n > 1) {
Expand All @@ -495,7 +507,9 @@ public void rate(Client client, int rating)
RatingAnnotationWrapper rate = ratings.get(0);
rate.setRating(rating);
rate.saveAndUpdate(client);
annotationId = rate.getId();
}
return annotationId;
}


Expand Down Expand Up @@ -544,12 +558,13 @@ public void addMapAnnotation(Client client, MapAnnotationWrapper mapAnnotation)
*
* @param client The client handling the connection.
* @param table Table to add to the object.
* @return the ID of the new Table.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
*/
public void addTable(Client client, TableWrapper table)
public long addTable(Client client, TableWrapper table)
throws ServiceException, AccessException, ExecutionException {
TablesFacility tablesFacility = client.getTablesFacility();
TableData tableData = ExceptionHandler.of(tablesFacility,
Expand All @@ -571,6 +586,7 @@ public void addTable(Client client, TableWrapper table)
.mapToLong(DataObject::getId).max().orElse(-1L);
table.setId(id);
table.setFileId(tableData.getOriginalFileId());
return id;
}


Expand All @@ -580,14 +596,15 @@ public void addTable(Client client, TableWrapper table)
* @param client The client handling the connection.
* @param table Table to add to the object.
* @param policy Whether older tables should be unlinked, deleted or deleted only if they become orphaned.
* @return the ID of the new Table.
*
* @throws ServiceException Cannot connect to OMERO.
* @throws AccessException Cannot access data.
* @throws ExecutionException A Facility can't be retrieved or instantiated.
* @throws InterruptedException The thread was interrupted.
* @throws OMEROServerError Server error.
*/
public void addAndReplaceTable(Client client, TableWrapper table, ReplacePolicy policy)
public long addAndReplaceTable(Client client, TableWrapper table, ReplacePolicy policy)
throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException {
Collection<FileAnnotationWrapper> tables = wrap(ExceptionHandler.of(client.getTablesFacility(),
t -> t.getAvailableTables(
Expand All @@ -605,6 +622,7 @@ public void addAndReplaceTable(Client client, TableWrapper table, ReplacePolicy
client.deleteFile(fileAnnotation.getId());
}
}
return table.getId();
}


Expand All @@ -621,9 +639,9 @@ public void addAndReplaceTable(Client client, TableWrapper table, ReplacePolicy
* @throws InterruptedException The thread was interrupted.
* @throws OMEROServerError Server error.
*/
public void addAndReplaceTable(Client client, TableWrapper table)
public long addAndReplaceTable(Client client, TableWrapper table)
throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException {
addAndReplaceTable(client, table, ReplacePolicy.DELETE_ORPHANED);
return addAndReplaceTable(client, table, ReplacePolicy.DELETE_ORPHANED);
}


Expand Down

0 comments on commit 3dd3732

Please sign in to comment.