Skip to content

Commit

Permalink
Improve methods that retrieve groups/users by id
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Sep 22, 2023
1 parent f35364a commit 6570453
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 27 deletions.
29 changes: 11 additions & 18 deletions src/main/java/fr/igred/omero/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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));
}


Expand Down
5 changes: 0 additions & 5 deletions src/main/java/fr/igred/omero/meta/ExperimenterWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/fr/igred/omero/meta/ExperimenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +41,7 @@ void testGetWrongUser() {

@Test
void testGetWrongUserId() {
assertThrows(OMEROServerError.class, () -> client.getUser(859L));
assertThrows(NoSuchElementException.class, () -> client.getUser(859L));
}


Expand Down
3 changes: 1 addition & 2 deletions src/test/java/fr/igred/omero/meta/GroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +39,7 @@ void testGetWrongGroup() {

@Test
void testGetWrongGroupId() {
assertThrows(OMEROServerError.class, () -> client.getGroup(859L));
assertThrows(NoSuchElementException.class, () -> client.getGroup(859L));
}


Expand Down

0 comments on commit 6570453

Please sign in to comment.