Skip to content

Commit 68a1e95

Browse files
authoredJun 28, 2024··
Merge pull request #4 from qbicsoftware/feature/data-options
allows to list existing and upload new datasets
2 parents 3897229 + 3a99d0d commit 68a1e95

9 files changed

+494
-61
lines changed
 

‎pom.xml

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
<description>A client software written in Java to query openBIS</description>
1212
<packaging>jar</packaging>
1313
<properties>
14-
<maven.compiler.source>11</maven.compiler.source>
15-
<maven.compiler.target>11</maven.compiler.target>
14+
<java.version>11</java.version>
1615
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1716
<spring.version>5.3.31</spring.version>
1817
</properties>
@@ -138,6 +137,14 @@
138137
</execution>
139138
</executions>
140139
</plugin>
140+
<plugin>
141+
<groupId>org.apache.maven.plugins</groupId>
142+
<artifactId>maven-compiler-plugin</artifactId>
143+
<configuration>
144+
<source>${java.version}</source>
145+
<target>${java.version}</target>
146+
</configuration>
147+
</plugin>
141148
</plugins>
142149
</build>
143150
</project>

‎src/main/java/life/qbic/App.java

+37-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package life.qbic;
22

3+
import ch.ethz.sis.openbis.generic.OpenBIS;
34
import life.qbic.io.commandline.CommandLineOptions;
45
import life.qbic.model.Configuration;
5-
import life.qbic.model.download.Authentication;
66
import life.qbic.model.download.AuthenticationException;
77
import life.qbic.model.download.ConnectionException;
88
import org.apache.logging.log4j.LogManager;
@@ -35,30 +35,36 @@ private static Boolean isNotNullOrEmpty(String envVariableCommandLineParameter)
3535
}
3636

3737
/**
38-
* Logs into OpenBIS asks for and verifies password.
38+
* Logs into OpenBIS, asks for and verifies password.
3939
*
4040
* @return An instance of the Authentication class.
4141
*/
42-
public static Authentication loginToOpenBIS(
43-
char[] password, String user, String as_url) {
42+
public static OpenBIS loginToOpenBIS(
43+
char[] password, String user, String url) {
44+
setupLog();
4445

45-
// Ensure 'logs' folder is created
46-
File logFolder = new File(Configuration.LOG_PATH.toAbsolutePath().toString());
47-
if (!logFolder.exists()) {
48-
boolean logFolderCreated = logFolder.mkdirs();
49-
if (!logFolderCreated) {
50-
LOG.error("Could not create log folder '" + logFolder.getAbsolutePath() + "'");
51-
System.exit(1);
52-
}
53-
}
46+
OpenBIS authentication = new OpenBIS(url);
47+
48+
return tryLogin(authentication, user, password);
49+
}
50+
51+
/**
52+
* Logs into OpenBIS, asks for and verifies password, includes Datastore Server connection.
53+
*
54+
* @return An instance of the Authentication class.
55+
*/
56+
public static OpenBIS loginToOpenBIS(
57+
char[] password, String user, String url, String dssUrl) {
58+
setupLog();
59+
60+
OpenBIS authentication = new OpenBIS(url, dssUrl);
61+
62+
return tryLogin(authentication, user, password);
63+
}
5464

55-
Authentication authentication =
56-
new Authentication(
57-
user,
58-
new String(password),
59-
as_url);
65+
private static OpenBIS tryLogin(OpenBIS authentication, String user, char[] password) {
6066
try {
61-
authentication.login();
67+
authentication.login(user, new String(password));
6268
} catch (ConnectionException e) {
6369
LOG.error(e.getMessage(), e);
6470
LOG.error("Could not connect to QBiC's data source. Have you requested access to the "
@@ -70,4 +76,16 @@ public static Authentication loginToOpenBIS(
7076
}
7177
return authentication;
7278
}
79+
80+
private static void setupLog() {
81+
// Ensure 'logs' folder is created
82+
File logFolder = new File(Configuration.LOG_PATH.toAbsolutePath().toString());
83+
if (!logFolder.exists()) {
84+
boolean logFolderCreated = logFolder.mkdirs();
85+
if (!logFolderCreated) {
86+
LOG.error("Could not create log folder '" + logFolder.getAbsolutePath() + "'");
87+
System.exit(1);
88+
}
89+
}
90+
}
7391
}

‎src/main/java/life/qbic/io/commandline/AuthenticationOptions.java

+77-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import static java.util.Objects.nonNull;
44
import static picocli.CommandLine.ArgGroup;
55

6+
import java.io.BufferedReader;
7+
import java.io.File;
8+
import java.io.FileNotFoundException;
9+
import java.io.FileReader;
10+
import java.io.IOException;
611
import java.util.StringJoiner;
12+
import java.util.TreeMap;
713
import org.apache.logging.log4j.LogManager;
814
import org.apache.logging.log4j.Logger;
915
import picocli.CommandLine;
@@ -14,17 +20,49 @@ public class AuthenticationOptions {
1420

1521
@Option(
1622
names = {"-u", "--user"},
17-
required = true,
1823
description = "openBIS user name")
19-
public String user;
24+
private String user;
2025
@ArgGroup(multiplicity = "1") // ensures the password is provided once with at least one of the possible options.
2126
PasswordOptions passwordOptions;
2227

2328
@Option(
2429
names = {"-as", "-as_url"},
2530
description = "ApplicationServer URL",
2631
scope = CommandLine.ScopeType.INHERIT)
27-
public String as_url;
32+
private String as_url;
33+
34+
@Option(
35+
names = {"-dss", "-dss_url"},
36+
description = "DatastoreServer URL",
37+
scope = CommandLine.ScopeType.INHERIT)
38+
private String dss_url;
39+
40+
@Option(
41+
names = {"-config", "-config_file"},
42+
description = "Config file path to provide openbis server information.",
43+
scope = CommandLine.ScopeType.INHERIT)
44+
public String configPath;
45+
46+
public String getUser() {
47+
if(user == null & configPath!=null && !configPath.isBlank()) {
48+
user = ReadProperties.getProperties(configPath).get("user");
49+
}
50+
return user;
51+
}
52+
53+
public String getDSS() {
54+
if(dss_url == null & configPath!=null && !configPath.isBlank()) {
55+
dss_url = ReadProperties.getProperties(configPath).get("dss");
56+
}
57+
return dss_url;
58+
}
59+
60+
public String getAS() {
61+
if(as_url == null & configPath!=null && !configPath.isBlank()) {
62+
as_url = ReadProperties.getProperties(configPath).get("as");
63+
}
64+
return as_url;
65+
}
2866

2967
public char[] getPassword() {
3068
return passwordOptions.getPassword();
@@ -76,4 +114,40 @@ public String toString() {
76114
.toString();
77115
//ATTENTION: do not expose the password here!
78116
}
117+
118+
public static class ReadProperties {
119+
120+
public static TreeMap<String, String> getProperties(String infile) {
121+
122+
TreeMap<String, String> properties = new TreeMap<>();
123+
BufferedReader bfr = null;
124+
try {
125+
bfr = new BufferedReader(new FileReader(new File(infile)));
126+
} catch (FileNotFoundException e) {
127+
throw new RuntimeException(e);
128+
}
129+
130+
String line;
131+
while (true) {
132+
try {
133+
if ((line = bfr.readLine()) == null)
134+
break;
135+
} catch (IOException e) {
136+
throw new RuntimeException(e);
137+
}
138+
if (!line.startsWith("#") && !line.isEmpty()) {
139+
String[] property = line.trim().split("=");
140+
properties.put(property[0].trim(), property[1].trim());
141+
}
142+
}
143+
144+
try {
145+
bfr.close();
146+
} catch (IOException e) {
147+
throw new RuntimeException(e);
148+
}
149+
150+
return(properties);
151+
}
152+
}
79153
}

‎src/main/java/life/qbic/io/commandline/CommandLineOptions.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
// main command with format specifiers for the usage help message
1010
@Command(name = "openbis-scripts",
11-
subcommands = { SampleHierarchyCommand.class },
11+
subcommands = { SampleHierarchyCommand.class, FindDatasetsCommand.class, UploadDatasetCommand.class },
1212
description = "A client software for querying openBIS.",
13-
mixinStandardHelpOptions = true)
13+
mixinStandardHelpOptions = true, versionProvider = ManifestVersionProvider.class)
1414
public class CommandLineOptions {
1515
private static final Logger LOG = LogManager.getLogger(CommandLineOptions.class);
1616

1717
@Option(names = {"-V", "--version"},
1818
versionHelp = true,
1919
description = "print version information",
2020
scope = CommandLine.ScopeType.INHERIT)
21-
boolean versionRequested;
21+
boolean versionRequested = false;
2222

2323
@Option(
2424
names = {"-h", "--help"},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package life.qbic.io.commandline;
2+
3+
import ch.ethz.sis.openbis.generic.OpenBIS;
4+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet;
5+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
6+
import java.text.SimpleDateFormat;
7+
import java.time.Instant;
8+
import java.time.LocalDateTime;
9+
import java.time.ZoneOffset;
10+
import java.time.format.DateTimeFormatter;
11+
import java.util.ArrayList;
12+
import java.util.Comparator;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
import life.qbic.App;
16+
import life.qbic.model.download.OpenbisConnector;
17+
import picocli.CommandLine.Command;
18+
import picocli.CommandLine.Mixin;
19+
import picocli.CommandLine.Option;
20+
import picocli.CommandLine.Parameters;
21+
22+
@Command(name = "list-data",
23+
description = "lists datasets and their details for a given experiment code")
24+
public class DownloadDatasetCommand implements Runnable {
25+
26+
@Parameters(arity = "1", paramLabel = "experiment", description = "The code of the experiment data is attached to")
27+
private String experimentCode;
28+
@Option(arity = "1", paramLabel = "<space>", description = "Optional openBIS spaces to filter results", names = {"-s", "--space"})
29+
private String space;
30+
@Mixin
31+
AuthenticationOptions auth = new AuthenticationOptions();
32+
33+
@Override
34+
public void run() {
35+
List<String> spaces = new ArrayList<>();
36+
if (space != null) {
37+
System.out.println("Querying experiment in space: " + space + "...");
38+
spaces.add(space);
39+
} else {
40+
System.out.println("Querying experiment in all available spaces...");
41+
}
42+
OpenBIS authentication = App.loginToOpenBIS(auth.getPassword(), auth.getUser(), auth.getAS());
43+
OpenbisConnector openbis = new OpenbisConnector(authentication);
44+
List<DataSet> datasets = openbis.listDatasetsOfExperiment(spaces, experimentCode).stream()
45+
.sorted(Comparator.comparing(
46+
(DataSet d) -> d.getExperiment().getProject().getSpace().getCode())).collect(
47+
Collectors.toList());
48+
int datasetIndex = 0;
49+
for (DataSet dataSet : datasets) {
50+
datasetIndex++;
51+
System.out.println("["+datasetIndex+"]");
52+
System.out.println(dataSet.getExperiment().getIdentifier());
53+
System.out.println(dataSet.getCode());
54+
System.out.println(dataSet.getType().getCode());
55+
System.out.println(dataSet.getRegistrationDate());
56+
System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(dataSet.getRegistrationDate()));
57+
Person person = dataSet.getRegistrator();
58+
System.out.println(person.getFirstName() + " " + person.getLastName());
59+
System.out.println();
60+
}
61+
}
62+
63+
private String getTimeStamp() {
64+
final String PATTERN_FORMAT = "YYYY-MM-dd_HHmmss";
65+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);
66+
return LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package life.qbic.io.commandline;
2+
3+
import ch.ethz.sis.openbis.generic.OpenBIS;
4+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet;
5+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
6+
import java.text.SimpleDateFormat;
7+
import java.time.Instant;
8+
import java.time.LocalDateTime;
9+
import java.time.ZoneOffset;
10+
import java.time.format.DateTimeFormatter;
11+
import java.util.ArrayList;
12+
import java.util.Comparator;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
import life.qbic.App;
16+
import life.qbic.model.download.OpenbisConnector;
17+
import picocli.CommandLine.Command;
18+
import picocli.CommandLine.Mixin;
19+
import picocli.CommandLine.Option;
20+
import picocli.CommandLine.Parameters;
21+
22+
@Command(name = "list-data",
23+
description = "lists datasets and their details for a given experiment code")
24+
public class FindDatasetsCommand implements Runnable {
25+
26+
@Parameters(arity = "1", paramLabel = "experiment", description = "The code of the experiment data is attached to")
27+
private String experimentCode;
28+
@Option(arity = "1", paramLabel = "<space>", description = "Optional openBIS spaces to filter samples", names = {"-s", "--space"})
29+
private String space;
30+
@Mixin
31+
AuthenticationOptions auth = new AuthenticationOptions();
32+
33+
@Override
34+
public void run() {
35+
List<String> spaces = new ArrayList<>();
36+
if (space != null) {
37+
System.out.println("Querying experiment in space: " + space + "...");
38+
spaces.add(space);
39+
} else {
40+
System.out.println("Querying experiment in all available spaces...");
41+
}
42+
OpenBIS authentication = App.loginToOpenBIS(auth.getPassword(), auth.getUser(), auth.getAS());
43+
OpenbisConnector openbis = new OpenbisConnector(authentication);
44+
List<DataSet> datasets = openbis.listDatasetsOfExperiment(spaces, experimentCode).stream()
45+
.sorted(Comparator.comparing(
46+
(DataSet d) -> d.getExperiment().getProject().getSpace().getCode())).collect(
47+
Collectors.toList());
48+
int datasetIndex = 0;
49+
System.out.println();
50+
System.out.printf("Found %s datasets for experiment %s:%n", datasets.size(), experimentCode);
51+
for (DataSet dataSet : datasets) {
52+
datasetIndex++;
53+
System.out.println("["+datasetIndex+"]");
54+
System.out.printf("ID: %s (%s)%n", dataSet.getCode(), dataSet.getExperiment().getIdentifier());
55+
System.out.println("Type: "+dataSet.getType().getCode());
56+
Person person = dataSet.getRegistrator();
57+
String simpleTime = new SimpleDateFormat("MM-dd-yy HH:mm:ss").format(dataSet.getRegistrationDate());
58+
String name = person.getFirstName() +" "+ person.getLastName();
59+
String uploadedBy = "Uploaded by "+name+" ("+simpleTime+")";
60+
System.out.println(uploadedBy);
61+
System.out.println();
62+
}
63+
}
64+
65+
private String getTimeStamp() {
66+
final String PATTERN_FORMAT = "YYYY-MM-dd_HHmmss";
67+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);
68+
return LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter);
69+
}
70+
}

‎src/main/java/life/qbic/io/commandline/SampleHierarchyCommand.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package life.qbic.io.commandline;
22

3+
import ch.ethz.sis.openbis.generic.OpenBIS;
34
import java.io.IOException;
45
import java.nio.file.Path;
56
import java.nio.file.Paths;
@@ -14,7 +15,6 @@
1415
import life.qbic.App;
1516
import life.qbic.model.Configuration;
1617
import life.qbic.model.SampleTypeConnection;
17-
import life.qbic.model.download.Authentication;
1818
import life.qbic.model.download.FileSystemWriter;
1919
import life.qbic.model.download.ModelReporter;
2020
import life.qbic.model.download.OpenbisConnector;
@@ -43,8 +43,8 @@ public void run() {
4343
} else {
4444
summary.add("Querying samples in all available spaces...\n");
4545
}
46-
Authentication authentication = App.loginToOpenBIS(auth.getPassword(), auth.user, auth.as_url);
47-
OpenbisConnector openbis = new OpenbisConnector(auth.as_url, authentication.getSessionToken());
46+
OpenBIS authentication = App.loginToOpenBIS(auth.getPassword(), auth.getUser(), auth.getAS());
47+
OpenbisConnector openbis = new OpenbisConnector(authentication);
4848
Map<SampleTypeConnection, Integer> hierarchy = openbis.queryFullSampleHierarchy(spaces);
4949

5050
hierarchy.entrySet().stream()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package life.qbic.io.commandline;
2+
3+
import ch.ethz.sis.openbis.generic.OpenBIS;
4+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.DataSetPermId;
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.time.Instant;
8+
import java.time.LocalDateTime;
9+
import java.time.ZoneOffset;
10+
import java.time.format.DateTimeFormatter;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import life.qbic.App;
14+
import life.qbic.model.download.OpenbisConnector;
15+
import picocli.CommandLine.Command;
16+
import picocli.CommandLine.Mixin;
17+
import picocli.CommandLine.Option;
18+
import picocli.CommandLine.Parameters;
19+
20+
@Command(name = "upload-data",
21+
description = "uploads a dataset and attaches it to an experiment and (optionally) other datasets")
22+
public class UploadDatasetCommand implements Runnable {
23+
24+
@Parameters(arity = "1", paramLabel = "file/folder", description = "The path to the file or folder to upload")
25+
private String dataPath;
26+
@Parameters(arity = "1", paramLabel = "experiment ID", description = "The full identifier of the experiment the data should be attached to. "
27+
+ "The identifier must be of the format: /space/project/experiment")
28+
private String experimentID;
29+
@Option(arity = "1..*", paramLabel = "<parent_datasets>", description = "Optional list of dataset codes to act"
30+
+ " as parents for the upload. E.g. when this dataset has been generated using these datasets as input.", names = {"-pa", "--parents"})
31+
private List<String> parents = new ArrayList<>();
32+
@Mixin
33+
AuthenticationOptions auth = new AuthenticationOptions();
34+
35+
private OpenbisConnector openbis;
36+
37+
@Override
38+
public void run() {
39+
OpenBIS authentication = App.loginToOpenBIS(auth.getPassword(), auth.getUser(), auth.getAS(), auth.getDSS());
40+
openbis = new OpenbisConnector(authentication);
41+
42+
if(!pathValid(dataPath)) {
43+
System.out.printf("Path %s could not be found%n", dataPath);
44+
return;
45+
}
46+
if(!experimentExists(experimentID)) {
47+
System.out.printf("Experiment %s could not be found%n", experimentID);
48+
return;
49+
}
50+
if(!datasetsExist(parents)) {
51+
System.out.printf("One or more datasets %s could not be found%n", parents);
52+
return;
53+
}
54+
System.out.println();
55+
System.out.println("Parameters verified, uploading dataset...");
56+
System.out.println();
57+
DataSetPermId result = openbis.registerDataset(Path.of(dataPath), experimentID, parents);
58+
System.out.printf("Dataset %s was successfully created%n", result.getPermId());
59+
}
60+
61+
private boolean datasetsExist(List<String> datasetCodes) {
62+
return openbis.findDataSets(datasetCodes).size() == datasetCodes.size();
63+
}
64+
65+
private boolean experimentExists(String experimentID) {
66+
return openbis.experimentExists(experimentID);
67+
}
68+
69+
private boolean pathValid(String dataPath) {
70+
return new File(dataPath).exists();
71+
}
72+
73+
private String getTimeStamp() {
74+
final String PATTERN_FORMAT = "YYYY-MM-dd_HHmmss";
75+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);
76+
return LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter);
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
package life.qbic.model.download;
22

3-
import ch.ethz.sis.openbis.generic.asapi.v3.IApplicationServerApi;
3+
import ch.ethz.sis.openbis.generic.OpenBIS;
44
import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.search.SearchResult;
5+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet;
6+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.fetchoptions.DataSetFetchOptions;
7+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.DataSetPermId;
8+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.search.DataSetSearchCriteria;
9+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.EntityKind;
10+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.id.EntityTypePermId;
11+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.fetchoptions.ExperimentFetchOptions;
12+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id.ExperimentIdentifier;
13+
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.search.ExperimentSearchCriteria;
514
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.Sample;
615
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.SampleType;
716
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.fetchoptions.SampleFetchOptions;
817
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.search.SampleSearchCriteria;
918
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space;
1019
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.fetchoptions.SpaceFetchOptions;
1120
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.search.SpaceSearchCriteria;
12-
import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
21+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.dataset.create.UploadedDataSetCreation;
22+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.DataSetFile;
23+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download.DataSetFileDownload;
24+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download.DataSetFileDownloadOptions;
25+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.download.DataSetFileDownloadReader;
26+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.id.DataSetFilePermId;
27+
import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.id.IDataSetFileId;
28+
import java.io.File;
29+
import java.io.FileOutputStream;
30+
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.nio.file.Path;
33+
import java.util.ArrayList;
34+
import java.util.Arrays;
1335
import java.util.HashMap;
1436
import java.util.List;
1537
import java.util.Map;
@@ -21,17 +43,18 @@
2143
public class OpenbisConnector {
2244

2345
private static final Logger LOG = LogManager.getLogger(OpenbisConnector.class);
24-
private final IApplicationServerApi applicationServer;
25-
private final String sessionToken;
46+
OpenBIS openBIS;
47+
2648
/**
2749
* Constructor for a QBiCDataDownloader instance
2850
*
29-
* @param AppServerUri The openBIS application server URL (AS)
51+
* @param AppServerUri The openBIS application server URL (AS)
3052
* @param sessionToken The session token for the datastore & application servers
3153
*/
3254
public OpenbisConnector(
33-
String AppServerUri,
34-
String sessionToken) {
55+
String AppServerUri,
56+
String sessionToken) {
57+
/*
3558
this.sessionToken = sessionToken;
3659
3760
if (!AppServerUri.isEmpty()) {
@@ -41,57 +64,152 @@ public OpenbisConnector(
4164
} else {
4265
applicationServer = null;
4366
}
67+
68+
*/
69+
}
70+
71+
public OpenbisConnector(OpenBIS authentication) {
72+
this.openBIS = authentication;
4473
}
4574

4675
public List<String> getSpaces() {
4776
SpaceSearchCriteria criteria = new SpaceSearchCriteria();
4877
SpaceFetchOptions options = new SpaceFetchOptions();
49-
return applicationServer.searchSpaces(sessionToken, criteria, options).getObjects()
78+
return openBIS.searchSpaces(criteria, options).getObjects()
5079
.stream().map(Space::getCode).collect(Collectors.toList());
5180
}
5281

82+
public DataSetPermId registerDataset(Path uploadPath, String experimentID,
83+
List<String> parentCodes) {
84+
final String uploadId = openBIS.uploadFileWorkspaceDSS(uploadPath);
85+
86+
final UploadedDataSetCreation creation = new UploadedDataSetCreation();
87+
creation.setUploadId(uploadId);
88+
creation.setExperimentId(new ExperimentIdentifier(experimentID));
89+
creation.setParentIds(parentCodes.stream().map(DataSetPermId::new).collect(
90+
Collectors.toList()));
91+
creation.setTypeId(new EntityTypePermId("UNKNOWN", EntityKind.DATA_SET));
92+
93+
try {
94+
return openBIS.createUploadedDataSet(creation);
95+
} catch (final Exception e) {
96+
LOG.error(e.getMessage());
97+
}
98+
return null;
99+
}
100+
101+
private static void copyInputStreamToFile(InputStream inputStream, File file)
102+
throws IOException {
103+
System.err.println(file.getPath());
104+
try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
105+
int read;
106+
byte[] bytes = new byte[8192];
107+
while ((read = inputStream.read(bytes)) != -1) {
108+
outputStream.write(bytes, 0, read);
109+
}
110+
}
111+
}
112+
113+
public List<DataSet> listDatasetsOfExperiment(List<String> spaces, String experiment) {
114+
DataSetSearchCriteria criteria = new DataSetSearchCriteria();
115+
criteria.withExperiment().withCode().thatEquals(experiment);
116+
if (!spaces.isEmpty()) {
117+
criteria.withAndOperator();
118+
criteria.withExperiment().withProject().withSpace().withCodes().thatIn(spaces);
119+
}
120+
DataSetFetchOptions options = new DataSetFetchOptions();
121+
options.withType();
122+
options.withRegistrator();
123+
options.withExperiment().withProject().withSpace();
124+
return openBIS.searchDataSets(criteria, options).getObjects();
125+
}
126+
127+
public void downloadDataset(String targetPath, String datasetID) throws IOException {
128+
DataSetFileDownloadOptions options = new DataSetFileDownloadOptions();
129+
IDataSetFileId fileToDownload = new DataSetFilePermId(new DataSetPermId(datasetID),
130+
"");
131+
132+
System.err.println(fileToDownload);
133+
// Setting recursive flag to true will return both subfolders and files
134+
options.setRecursive(true);
135+
136+
// Read the contents and print them out
137+
InputStream stream = openBIS.downloadFiles(new ArrayList<>(List.of(fileToDownload)),
138+
options);
139+
DataSetFileDownloadReader reader = new DataSetFileDownloadReader(stream);
140+
DataSetFileDownload file = null;
141+
while ((file = reader.read()) != null) {
142+
DataSetFile df = file.getDataSetFile();
143+
String currentPath = df.getPath().replace("original", "");
144+
if (df.isDirectory()) {
145+
File newDir = new File(targetPath, currentPath);
146+
if (!newDir.exists()) {
147+
newDir.mkdirs();
148+
}
149+
} else {
150+
File toWrite = new File(targetPath, currentPath);
151+
copyInputStreamToFile(file.getInputStream(), toWrite);
152+
}
153+
}
154+
}
155+
53156
public Map<SampleTypeConnection, Integer> queryFullSampleHierarchy(List<String> spaces) {
54157
Map<SampleTypeConnection, Integer> hierarchy = new HashMap<>();
55-
if(spaces.isEmpty()) {
158+
if (spaces.isEmpty()) {
56159
spaces = getSpaces();
57160
}
58-
for(String space : spaces) {
161+
for (String space : spaces) {
59162
SampleFetchOptions fetchType = new SampleFetchOptions();
60163
fetchType.withType();
61164
SampleFetchOptions withDescendants = new SampleFetchOptions();
62165
withDescendants.withChildrenUsing(fetchType);
63166
withDescendants.withType();
64167
SampleSearchCriteria criteria = new SampleSearchCriteria();
65168
criteria.withSpace().withCode().thatEquals(space.toUpperCase());
66-
SearchResult<Sample> result = applicationServer.searchSamples(
67-
sessionToken, criteria, withDescendants);
169+
SearchResult<Sample> result = openBIS.searchSamples(criteria, withDescendants);
68170
for (Sample s : result.getObjects()) {
69-
SampleType parentType = s.getType();
70-
List<Sample> children = s.getChildren();
71-
if (children.isEmpty()) {
72-
SampleTypeConnection leaf = new SampleTypeConnection(parentType);
73-
if (hierarchy.containsKey(leaf)) {
74-
int count = hierarchy.get(leaf) + 1;
75-
hierarchy.put(leaf, count);
76-
} else {
77-
hierarchy.put(leaf, 1);
78-
}
171+
SampleType parentType = s.getType();
172+
List<Sample> children = s.getChildren();
173+
if (children.isEmpty()) {
174+
SampleTypeConnection leaf = new SampleTypeConnection(parentType);
175+
if (hierarchy.containsKey(leaf)) {
176+
int count = hierarchy.get(leaf) + 1;
177+
hierarchy.put(leaf, count);
79178
} else {
80-
for (Sample c : children) {
81-
SampleType childType = c.getType();
82-
SampleTypeConnection connection = new SampleTypeConnection(parentType, childType);
83-
if (hierarchy.containsKey(connection)) {
84-
int count = hierarchy.get(connection) + 1;
85-
hierarchy.put(connection, count);
86-
} else {
87-
hierarchy.put(connection, 1);
88-
}
179+
hierarchy.put(leaf, 1);
180+
}
181+
} else {
182+
for (Sample c : children) {
183+
SampleType childType = c.getType();
184+
SampleTypeConnection connection = new SampleTypeConnection(parentType, childType);
185+
if (hierarchy.containsKey(connection)) {
186+
int count = hierarchy.get(connection) + 1;
187+
hierarchy.put(connection, count);
188+
} else {
189+
hierarchy.put(connection, 1);
89190
}
191+
}
90192
}
91193
}
92194
}
93195
return hierarchy;
94196
}
95197

198+
public List<DataSet> findDataSets(List<String> codes) {
199+
if (codes.isEmpty()) {
200+
return new ArrayList<>();
201+
}
202+
DataSetSearchCriteria criteria = new DataSetSearchCriteria();
203+
criteria.withCodes().thatIn(codes);
204+
DataSetFetchOptions options = new DataSetFetchOptions();
205+
return openBIS.searchDataSets(criteria, options).getObjects();
206+
}
207+
208+
public boolean experimentExists(String experimentID) {
209+
ExperimentSearchCriteria criteria = new ExperimentSearchCriteria();
210+
criteria.withIdentifier().thatEquals(experimentID);
96211

212+
return !openBIS.searchExperiments(criteria, new ExperimentFetchOptions()).getObjects()
213+
.isEmpty();
214+
}
97215
}

0 commit comments

Comments
 (0)
Please sign in to comment.