diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java index e3797d8e5fe654e7ec34f002a2bc25f88e76d223..cfe4134a05ec7fa4255583605f471ac3314a5d4e 100644 --- a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java +++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java @@ -32,13 +32,13 @@ public class CinaCommandFactory extends AbstractCommandFactory private static enum Command { - LISTSAMPS, GENCODE, HELP + LISTSAMPS, GENCODE, LISTEXPS, HELP } public List<String> getKnownCommands() { String[] commands = - { "listsamps", "gencode" }; + { "listsamps", "gencode", "listexps" }; return Arrays.asList(commands); } @@ -73,6 +73,9 @@ public class CinaCommandFactory extends AbstractCommandFactory case GENCODE: result = new CommandGenerateSampleCode(); break; + case LISTEXPS: + result = new CommandExperimentLister(); + break; case HELP: result = new CommandHelp(this, PROGRAM_CALL_STRING); break; diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandExperimentLister.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandExperimentLister.java new file mode 100644 index 0000000000000000000000000000000000000000..cfbf0577a3323a2db3367bb6dbc7cc97c819686f --- /dev/null +++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandExperimentLister.java @@ -0,0 +1,103 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.cina.client.util.cli; + +import java.util.List; + +import ch.systemsx.cisd.cina.client.util.v1.ICinaUtilities; +import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.dss.client.api.cli.GlobalArguments; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Experiment; + +/** + * @author Chandrasekhar Ramakrishnan + */ +public class CommandExperimentLister extends + AbstractCinaCommand<CommandExperimentLister.CommandExperimentListerArguments> +{ + static class CommandExperimentListerArguments extends GlobalArguments + { + public String getExperimentTypeCode() + { + return getArguments().get(0).toString().toUpperCase(); + } + + @Override + public boolean isComplete() + { + if (getArguments().size() < 1) + { + return false; + } + if (getExperimentTypeCode().length() < 1) + { + return false; + } + + if (false == super.isComplete()) + return false; + + return true; + } + } + + private static class ExperimentListerExecutor extends + AbstractExecutor<CommandExperimentListerArguments> + { + /** + * @param command The parent command + */ + ExperimentListerExecutor(CommandExperimentLister command) + { + super(command); + } + + @Override + protected int doExecute(ICinaUtilities component) + { + List<Experiment> results = + component.listVisibleExperiments(arguments.getExperimentTypeCode()); + for (Experiment experiment : results) + { + System.out.println(experiment.toString()); + } + return 0; + } + } + + public CommandExperimentLister() + { + super(new CommandExperimentListerArguments()); + } + + public int execute(String[] args) throws UserFailureException, EnvironmentFailureException + { + return new ExperimentListerExecutor(this).execute(args); + } + + public String getName() + { + return "listexps"; + } + + @Override + protected String getRequiredArgumentsString() + { + return "<experiment type>"; + } +} diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/ICinaUtilities.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/ICinaUtilities.java index 40d819a9850a2fa2745ad2d71b253cd6c5240e50..12bf80e0066dea6cd05a080478e5fce39eaf71a1 100644 --- a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/ICinaUtilities.java +++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/ICinaUtilities.java @@ -19,6 +19,7 @@ package ch.systemsx.cisd.cina.client.util.v1; import java.util.List; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Experiment; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; @@ -61,6 +62,18 @@ public interface ICinaUtilities public String generateSampleCode(String sampleTypeCode) throws IllegalStateException, EnvironmentFailureException; + /** + * Return a list of experiments of the given type for which the user has write priveledges. + * + * @param experimentType The type of experiment we want listed + * @return A list of experiments for the given experiment type + * @throws IllegalStateException Thrown if the user has not yet been authenticated. + * @throws EnvironmentFailureException Thrown in cases where it is not possible to connect to + * the server. + */ + public List<Experiment> listVisibleExperiments(String experimentType) + throws IllegalStateException, EnvironmentFailureException; + /** * Logs the current user out. */ diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacade.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacade.java index a1d20e1541001b0092eff5b3403a007160b1ee3d..4cb50873a530a4045a6bb42619c949b64232975f 100644 --- a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacade.java +++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacade.java @@ -16,6 +16,7 @@ package ch.systemsx.cisd.cina.client.util.v1.impl; +import java.util.ArrayList; import java.util.List; import org.springframework.remoting.RemoteConnectFailureException; @@ -28,8 +29,11 @@ import ch.systemsx.cisd.openbis.generic.shared.IETLLIMSService; import ch.systemsx.cisd.openbis.generic.shared.OpenBisServiceFactory; import ch.systemsx.cisd.openbis.generic.shared.ResourceNames; import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Experiment; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Project; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SpaceWithProjectsAndRoleAssignments; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType; /** @@ -177,6 +181,12 @@ public class CinaUtilitiesFacade implements ICinaUtilities { return state.generateSampleCode(sampleTypeCode); } + + public List<Experiment> listVisibleExperiments(String experimentType) + throws IllegalStateException, EnvironmentFailureException + { + return state.listVisibleExperiments(experimentType); + } } /** @@ -213,6 +223,12 @@ abstract class AbstractCinaFacadeState implements ICinaUtilities throw new IllegalStateException("Please log in"); } + public List<Experiment> listVisibleExperiments(String experimentType) + throws IllegalStateException, EnvironmentFailureException + { + throw new IllegalStateException("Please log in"); + } + /** * Authenticates the <code>user</code> with given <code>password</code>. * @@ -337,4 +353,29 @@ class AuthenticatedState extends AbstractCinaFacadeState String.format("%s%d", replicaSampleType.getGeneratedCodePrefix(), sampleCodeSuffix); return sampleCode; } + + @Override + public List<Experiment> listVisibleExperiments(String experimentType) + throws IllegalStateException, EnvironmentFailureException + { + // This functionality has only been supported since version 1.2 + int minorVersion = service.getMinorVersion(); + if (minorVersion < 2) + { + throw new EnvironmentFailureException("Server does not support this feature."); + } + + // First get a list of spaces the user has access to + List<SpaceWithProjectsAndRoleAssignments> spaces = + service.listSpacesWithProjectsAndRoleAssignments(sessionToken, null); + ArrayList<Project> projects = new ArrayList<Project>(); + for (SpaceWithProjectsAndRoleAssignments space : spaces) + { + projects.addAll(space.getProjects()); + } + + // Then get the experiments for these spaces + return service.listExperiments(sessionToken, projects, experimentType); + } + } \ No newline at end of file diff --git a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandFactoryTest.java b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandFactoryTest.java index 4ecd3f56238b246d9e7caaf2953dabc04e9a99d9..15f0ba8e4cb655c43cf5475b665259f6496a602e 100644 --- a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandFactoryTest.java +++ b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandFactoryTest.java @@ -72,8 +72,8 @@ public class CommandFactoryTest extends AssertJUnit cmd.printUsage(out); String helpText = "usage: cina_client.sh COMMAND [options...] <command arguments>\n" + "\n" - + "Commands:\n" + " listsamps\n" + " gencode\n" + " ls\n" + " get\n" - + " put\n" + "\n" + "Options:\n" + + "Commands:\n" + " listsamps\n" + " gencode\n" + " listexps\n" + " ls\n" + + " get\n" + " put\n" + "\n" + "Options:\n" + " [-p,--password] VAL : User login password\n" + " [-s,--server-base-url] VAL : URL for openBIS Server (required)\n" + " [-u,--username] VAL : User login name\n"; diff --git a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacadeTest.java b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacadeTest.java index 85548c9d7da07d99b5cdefd7e9a6bf30c506da5a..328d4c15a1a861ac303d5c42ddd95c4c41ac5a0a 100644 --- a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacadeTest.java +++ b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/v1/impl/CinaUtilitiesFacadeTest.java @@ -28,8 +28,12 @@ import org.testng.annotations.Test; import ch.systemsx.cisd.openbis.generic.shared.IETLLIMSService; import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Experiment; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Project; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Role; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.Sample; import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SearchCriteria; +import ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.SpaceWithProjectsAndRoleAssignments; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SampleType; /** @@ -126,6 +130,49 @@ public class CinaUtilitiesFacadeTest extends AssertJUnit context.assertIsSatisfied(); } + @Test + public void testListVisibleExperiments() + { + final ArrayList<Project> projects = new ArrayList<Project>(); + Project project = new Project("PROJECT-1", "SPACE-1"); + projects.add(project); + + final ArrayList<Experiment> experiments = new ArrayList<Experiment>(); + + final ArrayList<SpaceWithProjectsAndRoleAssignments> spaces = + new ArrayList<SpaceWithProjectsAndRoleAssignments>(); + SpaceWithProjectsAndRoleAssignments space = + new SpaceWithProjectsAndRoleAssignments("SPACE-1"); + space.add(project); + space.add("user", new Role("ADMIN", true)); + spaces.add(space); + context.checking(new Expectations() + { + { + one(service).tryToAuthenticateForAllServices(USER_ID, PASSWORD); + will(returnValue(SESSION_TOKEN)); + + one(service).getMinorVersion(); + will(returnValue(2)); + + one(service).listSpacesWithProjectsAndRoleAssignments(SESSION_TOKEN, null); + will(returnValue(spaces)); + + one(service).listExperiments(SESSION_TOKEN, projects, "EXP-TYPE"); + will(returnValue(experiments)); + + one(service).logout(SESSION_TOKEN); + } + }); + CinaUtilitiesFacade facade = createFacade(service, openbisService); + facade.login(USER_ID, PASSWORD); + assertEquals(facade.getSessionToken(), SESSION_TOKEN); + List<Experiment> result = facade.listVisibleExperiments("EXP-TYPE"); + assertEquals(0, result.size()); + facade.logout(); + context.assertIsSatisfied(); + } + /** * Utility method to create a CinaUtilitiesFacade object for testing. */