From 65c5bc98a2b7fffcfc13c42ac940d64f4ac8a6aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Dornier?= Date: Mon, 19 Jun 2023 11:34:12 +0200 Subject: [PATCH 01/14] Add methods to delete multiple tables with one API call --- src/main/java/fr/igred/omero/Client.java | 36 +++++++++++++++++++ .../java/fr/igred/omero/GatewayWrapper.java | 20 +++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index ca8aac72..2eca5e1b 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -230,11 +230,47 @@ public void delete(GenericObjectWrapper object) * @throws OMEROServerError Server error. * @throws InterruptedException If block(long) does not return. */ + @Deprecated public void delete(TableWrapper table) throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { deleteFile(table.getId()); } + /** + * Deletes a table from OMERO. + * + * @param table Table to delete. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. + * @throws IllegalArgumentException ID not defined. + * @throws OMEROServerError Server error. + * @throws InterruptedException If block(long) does not return. + */ + public void deleteTable(TableWrapper table) + throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { + deleteFile(table.getId()); + } + + + /** + * Deletes a table from OMERO. + * + * @param tables List of tables to delete. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. + * @throws IllegalArgumentException ID not defined. + * @throws OMEROServerError Server error. + * @throws InterruptedException If block(long) does not return. + */ + public void deleteTables(List tables) + throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { + deleteFiles(tables.stream().map(TableWrapper::getId).collect(Collectors.toList())); + } + /** * Returns the user which matches the username. diff --git a/src/main/java/fr/igred/omero/GatewayWrapper.java b/src/main/java/fr/igred/omero/GatewayWrapper.java index e30c7238..63a71e8a 100644 --- a/src/main/java/fr/igred/omero/GatewayWrapper.java +++ b/src/main/java/fr/igred/omero/GatewayWrapper.java @@ -40,11 +40,13 @@ import omero.model.FileAnnotationI; import omero.model.IObject; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collectors; /** @@ -505,6 +507,24 @@ public void deleteFile(Long id) delete(file); } + /** + * Deletes a file from OMERO + * + * @param ids List of files IDs to delete. + * + * @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 void deleteFiles(List ids) + throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { + List files = new ArrayList<>(); + ids.forEach(e->files.add(new FileAnnotationI(e, false))); + delete(files); + } + /** * Overridden to return the host name, the group ID, the username and the connection status. From c4f67d9a6e36a222a8c3758b22c5774a0126fd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Dornier?= Date: Tue, 5 Sep 2023 11:39:07 +0200 Subject: [PATCH 02/14] Add admin / leader experimenter check methods --- .../igred/omero/meta/ExperimenterWrapper.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index a8e58139..ff6c14d3 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -18,8 +18,13 @@ package fr.igred.omero.meta; +import fr.igred.omero.Client; import fr.igred.omero.GenericObjectWrapper; +import omero.ServerError; +import omero.gateway.exception.DSOutOfServiceException; import omero.gateway.model.ExperimenterData; +import omero.model.ExperimenterGroup; +import omero.model.GroupExperimenterMap; import java.util.List; @@ -212,4 +217,51 @@ public boolean isLDAP() { return data.isLDAP(); } + + /** + * Returns {@code true} if the user is a group owner + * + * @param client {@link Client} that handles the connection + * @param groupId The ID of the group + * + * @return See above. + * + * @throws DSOutOfServiceException + * @throws ServerError + */ + public boolean isLeader(Client client, long groupId) throws DSOutOfServiceException, ServerError { + + ExperimenterGroup group = client.getGateway().getAdminService(client.getCtx()).getGroup(groupId); + long id = getId(); + boolean isLeader = false; + if(group.sizeOfGroupExperimenterMap() > 0){ + for(GroupExperimenterMap experimenterLink : group.copyGroupExperimenterMap()){ + if(experimenterLink.getOwner().getValue()){ + if(experimenterLink.getChild().getId().getValue() == id){ + isLeader = true; + break; + } + } + } + } + + return isLeader; + } + + + /** + * Returns {@code true} if the user is a full admin + * + * @param client {@link Client} that handles the connection + * + * @return See above. + * + * @throws DSOutOfServiceException + * @throws ServerError + */ + public boolean isAdmin(Client client) throws DSOutOfServiceException, ServerError { + return !client.getGateway().getAdminService(client.getCtx()).getCurrentAdminPrivileges().isEmpty(); + } + + } From 7e8dde61394bfdf1c2b0cebe4d99f5f268b30ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Dornier?= Date: Tue, 5 Sep 2023 11:41:22 +0200 Subject: [PATCH 03/14] Add getUser and getGroup with userID and grouID argument --- src/main/java/fr/igred/omero/Client.java | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 2eca5e1b..6f9af655 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -31,8 +31,11 @@ import fr.igred.omero.repository.ProjectWrapper; import omero.gateway.Gateway; import omero.gateway.SecurityContext; +import omero.gateway.exception.DSOutOfServiceException; import omero.gateway.model.ExperimenterData; import omero.gateway.model.GroupData; +import omero.model.Experimenter; +import omero.model.ExperimenterGroup; import java.util.ArrayList; import java.util.Collection; @@ -297,6 +300,29 @@ public ExperimenterWrapper getUser(String username) } } + /** + * Returns the user which matches the user ID. + * + * @param userId The ID of the user. + * + * @return The user matching the user ID, or null if it does not exist. + * + * @throws DSOutOfServiceException + * @throws AccessException + * @throws ExecutionException + */ + public ExperimenterWrapper getUser(long userId) + throws DSOutOfServiceException, AccessException, ExecutionException { + Experimenter user = ExceptionHandler.of(getGateway().getAdminService(getCtx()), a -> a.getExperimenter(userId)) + .handleServiceOrAccess("Cannot retrieve user: " + userId) + .get(); + + if (user != null) { + return getUser(user.getOmeName().getValue()); + } else { + throw new NoSuchElementException(String.format("User not found: %d", userId)); + } + } /** * Returns the group which matches the name. @@ -323,6 +349,31 @@ public GroupWrapper getGroup(String groupName) } + /** + * Returns the group which matches the group ID. + * + * @param groupId The ID of the group. + * + * @return The group with the appropriate group ID, if it exists. + * + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. + * @throws NoSuchElementException The requested group does not exist. + */ + public GroupWrapper getGroup(long groupId) + throws DSOutOfServiceException, AccessException, ExecutionException { + ExperimenterGroup group = ExceptionHandler.of(getGateway().getAdminService(getCtx()), a->a.getGroup(groupId)) + .handleServiceOrAccess("Cannot retrieve group: " + groupId) + .get(); + + if (group != null) { + return getGroup(group.getName().getValue()); + } else { + throw new NoSuchElementException(String.format("Group not found: %d", groupId)); + } + } + /** * Gets the client associated with the username in the parameters. The user calling this function needs to have * administrator rights. All action realized with the client returned will be considered as his. From 59d6385f31445e1975ea8b025530bcf1e27b1f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Dornier?= Date: Tue, 5 Sep 2023 11:42:49 +0200 Subject: [PATCH 04/14] Add method to retreive all the groups the user belongs to --- src/main/java/fr/igred/omero/Client.java | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 6f9af655..053eedf4 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -374,6 +374,40 @@ public GroupWrapper getGroup(long groupId) } } + + /** + * List of all groups to which the specified user belongs + * + * @param userId The ID of the user + * + * @return the list of groups + * + * @throws DSOutOfServiceException + * @throws AccessException + */ + public List getAllAvailableGroups(long userId) + throws DSOutOfServiceException, AccessException { + + List groups = ExceptionHandler.of(getGateway().getAdminService(getCtx()), a -> a.containedGroups(userId)) + .handleServiceOrAccess("Cannot retrieve available groups from user : " + userId) + .get(); + + if (groups != null && !groups.isEmpty()) { + List groupWrappers = new ArrayList<>(); + groups.forEach(e-> { + try { + groupWrappers.add(getGroup(e.getName().getValue())); + } catch (ExecutionException | ServiceException | AccessException ex) { + throw new RuntimeException(ex); + } + }); + + return groupWrappers; + } else { + throw new NoSuchElementException(String.format("Groups not found for user: %d", userId)); + } + } + /** * Gets the client associated with the username in the parameters. The user calling this function needs to have * administrator rights. All action realized with the client returned will be considered as his. From fee18c64454c1e55494b0f41e2e153e6156a8a51 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Tue, 19 Sep 2023 17:03:24 +0200 Subject: [PATCH 05/14] Remove duplicate deleteTables and deleteFiles methods --- src/main/java/fr/igred/omero/Client.java | 22 +++---------------- .../java/fr/igred/omero/GatewayWrapper.java | 19 ---------------- 2 files changed, 3 insertions(+), 38 deletions(-) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 75edfca3..664420e8 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -251,31 +251,12 @@ public void delete(TableWrapper table) * @throws OMEROServerError Server error. * @throws InterruptedException If block(long) does not return. */ - public void deleteTable(TableWrapper table) throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { deleteFile(table.getId()); } - /** - * Deletes a table from OMERO. - * - * @param tables List of tables to delete. - * - * @throws ServiceException Cannot connect to OMERO. - * @throws AccessException Cannot access data. - * @throws ExecutionException A Facility can't be retrieved or instantiated. - * @throws IllegalArgumentException ID not defined. - * @throws OMEROServerError Server error. - * @throws InterruptedException If block(long) does not return. - */ - public void deleteTables(List tables) - throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { - deleteFiles(tables.stream().map(TableWrapper::getId).collect(Collectors.toList())); - } - - /** * Deletes tables from OMERO. * @@ -319,6 +300,7 @@ public ExperimenterWrapper getUser(String username) } } + /** * Returns the user which matches the user ID. * @@ -343,6 +325,7 @@ public ExperimenterWrapper getUser(long userId) } } + /** * Returns the group which matches the name. * @@ -427,6 +410,7 @@ public List getAllAvailableGroups(long userId) } } + /** * Gets the client associated with the username in the parameters. The user calling this function needs to have * administrator rights. All action realized with the client returned will be considered as his. diff --git a/src/main/java/fr/igred/omero/GatewayWrapper.java b/src/main/java/fr/igred/omero/GatewayWrapper.java index 289fcf1b..0fdbeb6a 100644 --- a/src/main/java/fr/igred/omero/GatewayWrapper.java +++ b/src/main/java/fr/igred/omero/GatewayWrapper.java @@ -40,7 +40,6 @@ import omero.model.FileAnnotationI; import omero.model.IObject; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; @@ -527,24 +526,6 @@ public void deleteFiles(Long... ids) delete(files); } - /** - * Deletes a file from OMERO - * - * @param ids List of files IDs to delete. - * - * @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 void deleteFiles(List ids) - throws ServiceException, AccessException, ExecutionException, OMEROServerError, InterruptedException { - List files = new ArrayList<>(); - ids.forEach(e->files.add(new FileAnnotationI(e, false))); - delete(files); - } - /** * Overridden to return the host name, the group ID, the username and the connection status. From 5700ee546edf1b0d86eecdd4c4b7bee9a6edf639 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Tue, 19 Sep 2023 17:48:45 +0200 Subject: [PATCH 06/14] Only throw simple-omero-client exceptions --- src/main/java/fr/igred/omero/Client.java | 49 ++++++++--------- .../igred/omero/meta/ExperimenterWrapper.java | 53 +++++++++++-------- 2 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 664420e8..95c0cae0 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -31,7 +31,6 @@ import fr.igred.omero.repository.ProjectWrapper; import omero.gateway.Gateway; import omero.gateway.SecurityContext; -import omero.gateway.exception.DSOutOfServiceException; import omero.gateway.model.ExperimenterData; import omero.gateway.model.GroupData; import omero.model.Experimenter; @@ -285,7 +284,7 @@ public void deleteTables(Collection tables) * @throws ServiceException Cannot connect to OMERO. * @throws AccessException Cannot access data. * @throws ExecutionException A Facility can't be retrieved or instantiated. - * @throws NoSuchElementException The requested user does not exist. + * @throws NoSuchElementException The requested user cannot be found. */ public ExperimenterWrapper getUser(String username) throws ExecutionException, ServiceException, AccessException { @@ -308,13 +307,14 @@ public ExperimenterWrapper getUser(String username) * * @return The user matching the user ID, or null if it does not exist. * - * @throws DSOutOfServiceException - * @throws AccessException - * @throws ExecutionException + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. + * @throws NoSuchElementException The requested user cannot be found. */ public ExperimenterWrapper getUser(long userId) - throws DSOutOfServiceException, AccessException, ExecutionException { - Experimenter user = ExceptionHandler.of(getGateway().getAdminService(getCtx()), a -> a.getExperimenter(userId)) + throws ServiceException, AccessException, ExecutionException { + Experimenter user = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getExperimenter(userId)) .handleServiceOrAccess("Cannot retrieve user: " + userId) .get(); @@ -336,7 +336,7 @@ public ExperimenterWrapper getUser(long userId) * @throws ServiceException Cannot connect to OMERO. * @throws AccessException Cannot access data. * @throws ExecutionException A Facility can't be retrieved or instantiated. - * @throws NoSuchElementException The requested group does not exist. + * @throws NoSuchElementException The requested group cannot be found. */ public GroupWrapper getGroup(String groupName) throws ExecutionException, ServiceException, AccessException { @@ -361,11 +361,11 @@ public GroupWrapper getGroup(String groupName) * @throws ServiceException Cannot connect to OMERO. * @throws AccessException Cannot access data. * @throws ExecutionException A Facility can't be retrieved or instantiated. - * @throws NoSuchElementException The requested group does not exist. + * @throws NoSuchElementException The requested group cannot be found. */ public GroupWrapper getGroup(long groupId) - throws DSOutOfServiceException, AccessException, ExecutionException { - ExperimenterGroup group = ExceptionHandler.of(getGateway().getAdminService(getCtx()), a->a.getGroup(groupId)) + throws ServiceException, AccessException, ExecutionException { + ExperimenterGroup group = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getGroup(groupId)) .handleServiceOrAccess("Cannot retrieve group: " + groupId) .get(); @@ -384,26 +384,23 @@ public GroupWrapper getGroup(long groupId) * * @return the list of groups * - * @throws DSOutOfServiceException - * @throws AccessException + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. */ public List getAllAvailableGroups(long userId) - throws DSOutOfServiceException, AccessException { - - List groups = ExceptionHandler.of(getGateway().getAdminService(getCtx()), a -> a.containedGroups(userId)) - .handleServiceOrAccess("Cannot retrieve available groups from user : " + userId) + throws ServiceException, AccessException, ExecutionException { + String error = String.format("Cannot retrieve available groups from user %d: ", userId); + List groups = ExceptionHandler.of(getGateway(), + g -> g.getAdminService(getCtx()).containedGroups(userId)) + .handleServiceOrAccess(error) .get(); if (groups != null && !groups.isEmpty()) { - List groupWrappers = new ArrayList<>(); - groups.forEach(e-> { - try { - groupWrappers.add(getGroup(e.getName().getValue())); - } catch (ExecutionException | ServiceException | AccessException ex) { - throw new RuntimeException(ex); - } - }); - + List groupWrappers = new ArrayList<>(groups.size()); + for (ExperimenterGroup group : groups) { + groupWrappers.add(getGroup(group.getName().getValue())); + } return groupWrappers; } else { throw new NoSuchElementException(String.format("Groups not found for user: %d", userId)); diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index ff6c14d3..83f49908 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -20,13 +20,16 @@ import fr.igred.omero.Client; import fr.igred.omero.GenericObjectWrapper; -import omero.ServerError; -import omero.gateway.exception.DSOutOfServiceException; +import fr.igred.omero.exception.AccessException; +import fr.igred.omero.exception.ExceptionHandler; +import fr.igred.omero.exception.OMEROServerError; +import fr.igred.omero.exception.ServiceException; import omero.gateway.model.ExperimenterData; import omero.model.ExperimenterGroup; import omero.model.GroupExperimenterMap; import java.util.List; +import java.util.concurrent.ExecutionException; /** @@ -221,26 +224,29 @@ public boolean isLDAP() { /** * Returns {@code true} if the user is a group owner * - * @param client {@link Client} that handles the connection + * @param client {@link Client} that handles the connection * @param groupId The ID of the group * * @return See above. * - * @throws DSOutOfServiceException - * @throws ServerError + * @throws ServiceException Cannot connect to OMERO. + * @throws OMEROServerError Server error. */ - public boolean isLeader(Client client, long groupId) throws DSOutOfServiceException, ServerError { - - ExperimenterGroup group = client.getGateway().getAdminService(client.getCtx()).getGroup(groupId); - long id = getId(); + public boolean isLeader(Client client, long groupId) throws ServiceException, OMEROServerError { + ExperimenterGroup group = ExceptionHandler.of(client, + c -> c.getGateway() + .getAdminService(client.getCtx()) + .getGroup(groupId)) + .handleServiceOrServer("Cannot retrieve group: " + groupId) + .get(); + long id = getId(); boolean isLeader = false; - if(group.sizeOfGroupExperimenterMap() > 0){ - for(GroupExperimenterMap experimenterLink : group.copyGroupExperimenterMap()){ - if(experimenterLink.getOwner().getValue()){ - if(experimenterLink.getChild().getId().getValue() == id){ - isLeader = true; - break; - } + if (group.sizeOfGroupExperimenterMap() > 0) { + for (GroupExperimenterMap experimenterLink : group.copyGroupExperimenterMap()) { + if (experimenterLink.getOwner().getValue() + && (experimenterLink.getChild().getId().getValue() == id)) { + isLeader = true; + break; } } } @@ -256,12 +262,17 @@ public boolean isLeader(Client client, long groupId) throws DSOutOfServiceExcept * * @return See above. * - * @throws DSOutOfServiceException - * @throws ServerError + * @throws ServiceException Cannot connect to OMERO. + * @throws OMEROServerError Server error. */ - public boolean isAdmin(Client client) throws DSOutOfServiceException, ServerError { - return !client.getGateway().getAdminService(client.getCtx()).getCurrentAdminPrivileges().isEmpty(); + public boolean isAdmin(Client client) throws ServiceException, OMEROServerError { + return !ExceptionHandler.of(client, + c -> c.getGateway() + .getAdminService(client.getCtx()) + .getCurrentAdminPrivileges()) + .handleServiceOrServer("Could not retrieve admin privileges.") + .get() + .isEmpty(); } - } From ad90aa5ff2ecda366be85391bb83bb7194781295 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Thu, 21 Sep 2023 17:34:00 +0200 Subject: [PATCH 07/14] Rework isLeader and isAdmin methods in ExperimenterWrapper --- .../igred/omero/meta/ExperimenterWrapper.java | 49 ++++++------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index 83f49908..6b048f88 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -24,11 +24,14 @@ import fr.igred.omero.exception.ExceptionHandler; import fr.igred.omero.exception.OMEROServerError; import fr.igred.omero.exception.ServiceException; +import omero.gateway.exception.DSAccessException; +import omero.gateway.exception.DSOutOfServiceException; import omero.gateway.model.ExperimenterData; import omero.model.ExperimenterGroup; import omero.model.GroupExperimenterMap; import java.util.List; +import java.util.NoSuchElementException; import java.util.concurrent.ExecutionException; @@ -222,55 +225,33 @@ public boolean isLDAP() { /** - * Returns {@code true} if the user is a group owner + * Returns {@code true} if the user owns the specified group. * - * @param client {@link Client} that handles the connection - * @param groupId The ID of the group + * @param group The group. * * @return See above. * - * @throws ServiceException Cannot connect to OMERO. - * @throws OMEROServerError Server error. + * @throws NoSuchElementException The requested group cannot be found. */ - public boolean isLeader(Client client, long groupId) throws ServiceException, OMEROServerError { - ExperimenterGroup group = ExceptionHandler.of(client, - c -> c.getGateway() - .getAdminService(client.getCtx()) - .getGroup(groupId)) - .handleServiceOrServer("Cannot retrieve group: " + groupId) - .get(); - long id = getId(); - boolean isLeader = false; - if (group.sizeOfGroupExperimenterMap() > 0) { - for (GroupExperimenterMap experimenterLink : group.copyGroupExperimenterMap()) { - if (experimenterLink.getOwner().getValue() - && (experimenterLink.getChild().getId().getValue() == id)) { - isLeader = true; - break; - } - } - } - - return isLeader; + public boolean isLeader(GroupWrapper group) { + return group.getLeaders().stream().anyMatch(e -> e.getId() == data.getId()); } /** - * Returns {@code true} if the user is a full admin + * Returns {@code true} if the user is a full admin. * * @param client {@link Client} that handles the connection * * @return See above. * - * @throws ServiceException Cannot connect to OMERO. - * @throws OMEROServerError Server error. + * @throws ServiceException Cannot connect to OMERO. + * @throws AccessException Cannot access data. + * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - public boolean isAdmin(Client client) throws ServiceException, OMEROServerError { - return !ExceptionHandler.of(client, - c -> c.getGateway() - .getAdminService(client.getCtx()) - .getCurrentAdminPrivileges()) - .handleServiceOrServer("Could not retrieve admin privileges.") + public boolean isAdmin(Client client) throws ServiceException, AccessException, ExecutionException { + return !ExceptionHandler.of(client.getAdminFacility(), a -> a.getAdminPrivileges(client.getCtx(), data)) + .handleServiceOrAccess("Cannot retrieve admin privileges.") .get() .isEmpty(); } From 67d63aaa9ac146ab24c723f7f16278b175a31339 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Thu, 21 Sep 2023 17:34:19 +0200 Subject: [PATCH 08/14] Add tests for ExperimenterWrapper --- .../fr/igred/omero/meta/ExperimenterTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java index 8616703f..27ccd795 100644 --- a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java +++ b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java @@ -121,4 +121,34 @@ void testGetGroups() throws Exception { assertEquals(GROUP1.name, experimenter.getDefaultGroup().getName()); } + + @Test + void testIsNotGroupLeader() throws Exception { + ExperimenterWrapper experimenter = client.getUser(USER1.name); + GroupWrapper group = client.getGroup(GROUP1.name); + assertFalse(experimenter.isLeader(group)); + } + + + @Test + void testIsGroupLeader() throws Exception { + ExperimenterWrapper experimenter = client.getUser("testUser4"); + GroupWrapper group = client.getGroup("testGroup3"); + assertTrue(experimenter.isLeader(group)); + } + + + @Test + void testIsNotAdmin() throws Exception { + ExperimenterWrapper experimenter = client.getUser(USER1.name); + assertFalse(experimenter.isAdmin(client)); + } + + + @Test + void testIsAdmin() throws Exception { + ExperimenterWrapper experimenter = client.getUser(); + assertTrue(experimenter.isAdmin(client)); + } + } From 00704b57a129678b269cb83f84430e9ee0f89b50 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 22 Sep 2023 10:50:48 +0200 Subject: [PATCH 09/14] Fix methods to retrieve groups/users by id --- src/main/java/fr/igred/omero/Client.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 95c0cae0..2990456f 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -310,18 +310,19 @@ public ExperimenterWrapper getUser(String username) * @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 NoSuchElementException The requested user cannot be found. */ public ExperimenterWrapper getUser(long userId) - throws ServiceException, AccessException, ExecutionException { + throws ServiceException, AccessException, ExecutionException, OMEROServerError { Experimenter user = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getExperimenter(userId)) - .handleServiceOrAccess("Cannot retrieve user: " + userId) + .handleServiceOrServer("Cannot retrieve user: " + userId) .get(); - if (user != null) { + if (user != null && user.getOmeName() != null) { return getUser(user.getOmeName().getValue()); } else { - throw new NoSuchElementException(String.format("User not found: %d", userId)); + throw new NoSuchElementException(String.format("User cannot be found: %d", userId)); } } @@ -361,18 +362,19 @@ public GroupWrapper getGroup(String groupName) * @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 NoSuchElementException The requested group cannot be found. */ public GroupWrapper getGroup(long groupId) - throws ServiceException, AccessException, ExecutionException { + throws ServiceException, AccessException, ExecutionException, OMEROServerError { ExperimenterGroup group = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getGroup(groupId)) - .handleServiceOrAccess("Cannot retrieve group: " + groupId) + .handleServiceOrServer("Cannot retrieve group: " + groupId) .get(); - if (group != null) { + if (group != null && group.getName() != null) { return getGroup(group.getName().getValue()); } else { - throw new NoSuchElementException(String.format("Group not found: %d", groupId)); + throw new NoSuchElementException(String.format("Group cannot be found: %d", groupId)); } } From f35364abf1b4ae888f13d81b7f75589f3351410d Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 22 Sep 2023 10:51:09 +0200 Subject: [PATCH 10/14] Add tests --- .../java/fr/igred/omero/meta/ExperimenterTest.java | 13 +++++++++++++ src/test/java/fr/igred/omero/meta/GroupTest.java | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java index 27ccd795..a58b82da 100644 --- a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java +++ b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java @@ -19,6 +19,7 @@ import fr.igred.omero.RootTest; +import fr.igred.omero.exception.OMEROServerError; import org.junit.jupiter.api.Test; import java.util.List; @@ -39,6 +40,18 @@ void testGetWrongUser() { } + @Test + void testGetWrongUserId() { + assertThrows(OMEROServerError.class, () -> client.getUser(859L)); + } + + + @Test + void testGetUserById() throws Exception { + assertEquals(USER1.name, client.getUser(USER1.id).getUserName()); + } + + @Test void testSudoWrongUser() { assertThrows(NoSuchElementException.class, () -> client.sudoGetUser("nonexistent")); diff --git a/src/test/java/fr/igred/omero/meta/GroupTest.java b/src/test/java/fr/igred/omero/meta/GroupTest.java index aa8b5b43..b1bda3de 100644 --- a/src/test/java/fr/igred/omero/meta/GroupTest.java +++ b/src/test/java/fr/igred/omero/meta/GroupTest.java @@ -19,6 +19,7 @@ import fr.igred.omero.RootTest; +import fr.igred.omero.exception.OMEROServerError; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -37,6 +38,18 @@ void testGetWrongGroup() { } + @Test + void testGetWrongGroupId() { + assertThrows(OMEROServerError.class, () -> client.getGroup(859L)); + } + + + @Test + void testGetGroupById() throws Exception { + assertEquals(GROUP1.name, client.getGroup(GROUP1.id).getName()); + } + + @Test void testSetGroupName() throws Exception { GroupWrapper group = client.getGroup("testGroup2"); From 6570453cb12e5e684ebaf13d6420fc18c4d165d7 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 22 Sep 2023 13:08:02 +0200 Subject: [PATCH 11/14] Improve methods that retrieve groups/users by id --- src/main/java/fr/igred/omero/Client.java | 29 +++++++------------ .../igred/omero/meta/ExperimenterWrapper.java | 5 ---- .../fr/igred/omero/meta/ExperimenterTest.java | 3 +- .../java/fr/igred/omero/meta/GroupTest.java | 3 +- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 2990456f..60c678fa 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -29,6 +29,7 @@ import fr.igred.omero.repository.FolderWrapper; import fr.igred.omero.repository.ImageWrapper; import fr.igred.omero.repository.ProjectWrapper; +import omero.ApiUsageException; import omero.gateway.Gateway; import omero.gateway.SecurityContext; import omero.gateway.model.ExperimenterData; @@ -308,22 +309,18 @@ public ExperimenterWrapper getUser(String username) * @return The user matching the user ID, or null if it does not exist. * * @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 NoSuchElementException The requested user cannot be found. */ public ExperimenterWrapper getUser(long userId) - throws ServiceException, AccessException, ExecutionException, OMEROServerError { + throws ServiceException, OMEROServerError { Experimenter user = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getExperimenter(userId)) + .rethrow(ApiUsageException.class, + (m, e) -> new NoSuchElementException(m), + "User not found: " + userId) .handleServiceOrServer("Cannot retrieve user: " + userId) .get(); - - if (user != null && user.getOmeName() != null) { - return getUser(user.getOmeName().getValue()); - } else { - throw new NoSuchElementException(String.format("User cannot be found: %d", userId)); - } + return new ExperimenterWrapper(new ExperimenterData(user)); } @@ -360,22 +357,18 @@ public GroupWrapper getGroup(String groupName) * @return The group with the appropriate group ID, if it exists. * * @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 NoSuchElementException The requested group cannot be found. */ public GroupWrapper getGroup(long groupId) - throws ServiceException, AccessException, ExecutionException, OMEROServerError { + throws ServiceException, OMEROServerError { ExperimenterGroup group = ExceptionHandler.of(getGateway(), g -> g.getAdminService(getCtx()).getGroup(groupId)) + .rethrow(ApiUsageException.class, + (m, e) -> new NoSuchElementException(m), + "User not found: " + groupId) .handleServiceOrServer("Cannot retrieve group: " + groupId) .get(); - - if (group != null && group.getName() != null) { - return getGroup(group.getName().getValue()); - } else { - throw new NoSuchElementException(String.format("Group cannot be found: %d", groupId)); - } + return new GroupWrapper(new GroupData(group)); } diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index 6b048f88..161f1173 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -22,13 +22,8 @@ import fr.igred.omero.GenericObjectWrapper; import fr.igred.omero.exception.AccessException; import fr.igred.omero.exception.ExceptionHandler; -import fr.igred.omero.exception.OMEROServerError; import fr.igred.omero.exception.ServiceException; -import omero.gateway.exception.DSAccessException; -import omero.gateway.exception.DSOutOfServiceException; import omero.gateway.model.ExperimenterData; -import omero.model.ExperimenterGroup; -import omero.model.GroupExperimenterMap; import java.util.List; import java.util.NoSuchElementException; diff --git a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java index a58b82da..99818807 100644 --- a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java +++ b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java @@ -19,7 +19,6 @@ import fr.igred.omero.RootTest; -import fr.igred.omero.exception.OMEROServerError; import org.junit.jupiter.api.Test; import java.util.List; @@ -42,7 +41,7 @@ void testGetWrongUser() { @Test void testGetWrongUserId() { - assertThrows(OMEROServerError.class, () -> client.getUser(859L)); + assertThrows(NoSuchElementException.class, () -> client.getUser(859L)); } diff --git a/src/test/java/fr/igred/omero/meta/GroupTest.java b/src/test/java/fr/igred/omero/meta/GroupTest.java index b1bda3de..a2e35a3e 100644 --- a/src/test/java/fr/igred/omero/meta/GroupTest.java +++ b/src/test/java/fr/igred/omero/meta/GroupTest.java @@ -19,7 +19,6 @@ import fr.igred.omero.RootTest; -import fr.igred.omero.exception.OMEROServerError; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -40,7 +39,7 @@ void testGetWrongGroup() { @Test void testGetWrongGroupId() { - assertThrows(OMEROServerError.class, () -> client.getGroup(859L)); + assertThrows(NoSuchElementException.class, () -> client.getGroup(859L)); } From ac916df7966db984d6ec720d15e40ca526d48753 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 22 Sep 2023 13:10:38 +0200 Subject: [PATCH 12/14] Remove getAllAvailableGroups method as ExperimenterWrapper::getGroups should give the same results --- src/main/java/fr/igred/omero/Client.java | 31 ------------------------ 1 file changed, 31 deletions(-) diff --git a/src/main/java/fr/igred/omero/Client.java b/src/main/java/fr/igred/omero/Client.java index 60c678fa..072c66ba 100644 --- a/src/main/java/fr/igred/omero/Client.java +++ b/src/main/java/fr/igred/omero/Client.java @@ -372,37 +372,6 @@ public GroupWrapper getGroup(long groupId) } - /** - * List of all groups to which the specified user belongs - * - * @param userId The ID of the user - * - * @return the list of groups - * - * @throws ServiceException Cannot connect to OMERO. - * @throws AccessException Cannot access data. - * @throws ExecutionException A Facility can't be retrieved or instantiated. - */ - public List getAllAvailableGroups(long userId) - throws ServiceException, AccessException, ExecutionException { - String error = String.format("Cannot retrieve available groups from user %d: ", userId); - List groups = ExceptionHandler.of(getGateway(), - g -> g.getAdminService(getCtx()).containedGroups(userId)) - .handleServiceOrAccess(error) - .get(); - - if (groups != null && !groups.isEmpty()) { - List groupWrappers = new ArrayList<>(groups.size()); - for (ExperimenterGroup group : groups) { - groupWrappers.add(getGroup(group.getName().getValue())); - } - return groupWrappers; - } else { - throw new NoSuchElementException(String.format("Groups not found for user: %d", userId)); - } - } - - /** * Gets the client associated with the username in the parameters. The user calling this function needs to have * administrator rights. All action realized with the client returned will be considered as his. From c2254da3c9f9b11b4313671f7331175dc5337025 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 22 Sep 2023 13:26:18 +0200 Subject: [PATCH 13/14] Fix Javadoc and improve test --- src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java | 7 +++---- src/test/java/fr/igred/omero/meta/ExperimenterTest.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index 161f1173..f1e04611 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -225,11 +225,9 @@ public boolean isLDAP() { * @param group The group. * * @return See above. - * - * @throws NoSuchElementException The requested group cannot be found. */ public boolean isLeader(GroupWrapper group) { - return group.getLeaders().stream().anyMatch(e -> e.getId() == data.getId()); + return group.getLeaders().stream().anyMatch(l -> l.getId() == data.getId()); } @@ -244,7 +242,8 @@ public boolean isLeader(GroupWrapper group) { * @throws AccessException Cannot access data. * @throws ExecutionException A Facility can't be retrieved or instantiated. */ - public boolean isAdmin(Client client) throws ServiceException, AccessException, ExecutionException { + public boolean isAdmin(Client client) + throws ServiceException, AccessException, ExecutionException { return !ExceptionHandler.of(client.getAdminFacility(), a -> a.getAdminPrivileges(client.getCtx(), data)) .handleServiceOrAccess("Cannot retrieve admin privileges.") .get() diff --git a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java index 99818807..fce13742 100644 --- a/src/test/java/fr/igred/omero/meta/ExperimenterTest.java +++ b/src/test/java/fr/igred/omero/meta/ExperimenterTest.java @@ -137,7 +137,7 @@ void testGetGroups() throws Exception { @Test void testIsNotGroupLeader() throws Exception { ExperimenterWrapper experimenter = client.getUser(USER1.name); - GroupWrapper group = client.getGroup(GROUP1.name); + GroupWrapper group = client.getGroup("testGroup3"); assertFalse(experimenter.isLeader(group)); } From 6283706a01c736bc18de249b967ea6406106d767 Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 22 Sep 2023 23:21:56 +0200 Subject: [PATCH 14/14] Remove useless import --- src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java index f1e04611..6a0dbadd 100644 --- a/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java +++ b/src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java @@ -26,7 +26,6 @@ import omero.gateway.model.ExperimenterData; import java.util.List; -import java.util.NoSuchElementException; import java.util.concurrent.ExecutionException;