-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOpenBisClient.java
1500 lines (1321 loc) · 52.7 KB
/
OpenBisClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package life.qbic.openbis.openbisclient;
import static life.qbic.openbis.openbisclient.helper.OpenBisClientHelper.fetchDataSetsCompletely;
import static life.qbic.openbis.openbisclient.helper.OpenBisClientHelper.fetchExperimentTypesCompletely;
import static life.qbic.openbis.openbisclient.helper.OpenBisClientHelper.fetchExperimentsCompletely;
import static life.qbic.openbis.openbisclient.helper.OpenBisClientHelper.fetchProjectsCompletely;
import static life.qbic.openbis.openbisclient.helper.OpenBisClientHelper.fetchSampleTypesCompletely;
import static life.qbic.openbis.openbisclient.helper.OpenBisClientHelper.fetchSamplesCompletely;
import ch.ethz.sis.openbis.generic.asapi.v3.IApplicationServerApi;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.attachment.Attachment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.authorizationgroup.AuthorizationGroup;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.authorizationgroup.fetchoptions.AuthorizationGroupFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.authorizationgroup.search.AuthorizationGroupSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.interfaces.IEntityType;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.search.SearchResult;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSetType;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.fetchoptions.DataSetTypeFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.search.DataSetSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.search.DataSetTypeSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.Experiment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.ExperimentType;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.fetchoptions.ExperimentFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.fetchoptions.ExperimentTypeFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id.ExperimentIdentifier;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id.IExperimentId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.search.ExperimentSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.search.ExperimentTypeSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.Person;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.fetchoptions.PersonFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.person.search.PersonSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.Project;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.fetchoptions.ProjectFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.id.IProjectId;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.id.ProjectIdentifier;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.search.ProjectSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.property.PropertyType;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.property.fetchoptions.PropertyAssignmentFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.Role;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleAssignment;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.roleassignment.RoleLevel;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.Sample;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.SampleType;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.fetchoptions.SampleFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.fetchoptions.SampleTypeFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.id.SampleIdentifier;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.search.SampleSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.search.SampleTypeSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.fetchoptions.SpaceFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.search.SpaceSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.vocabulary.Vocabulary;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.vocabulary.VocabularyTerm;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.vocabulary.fetchoptions.VocabularyTermFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.vocabulary.search.VocabularyTermSearchCriteria;
import ch.ethz.sis.openbis.generic.asapi.v3.exceptions.NotFetchedException;
import ch.ethz.sis.openbis.generic.dssapi.v3.IDataStoreServerApi;
import ch.systemsx.cisd.common.exceptions.NotImplementedException;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
import ch.systemsx.cisd.openbis.common.api.client.ServiceFinder;
import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
import life.qbic.openbis.openbisclient.helper.OpenBisClientHelper;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* The type Open bis client.
*/
public class OpenBisClient implements IOpenBisClient {
private final int TIMEOUT = 100000;
private String userId, password, sessionToken, serviceURL, url;
private IApplicationServerApi v3;
private IDataStoreServerApi dss3;
private static final Logger logger = LogManager.getLogger(OpenBisClient.class);
/**
* Instantiates a new Open bis client.
*
* @param userId the user id
* @param password the password
* @param apiURL the api url
*/
public OpenBisClient(String userId, String password, String apiURL) {
this.userId = userId;
this.password = password;
this.serviceURL = apiURL + IApplicationServerApi.SERVICE_URL;
this.url = apiURL;
// get a reference to AS API
v3 = HttpInvokerUtils.createServiceStub(IApplicationServerApi.class, serviceURL, TIMEOUT);
dss3 = HttpInvokerUtils.createServiceStub(IDataStoreServerApi.class, serviceURL, TIMEOUT);
sessionToken = null;
}
/**
* Function map an integer value to a char
*
* @param i the integer value which should be mapped
* @return the resulting char value
*/
public static char mapToChar(int i) {
i += 48;
if (i > 57) {
i += 7;
}
return (char) i;
}
/**
* Function to generate the checksum for the given barcode string
*
* @param s the barcode string
* @return the checksum for the given barcode
*/
public static char checksum(String s) {
int i = 1;
int sum = 0;
for (int idx = 0; idx <= s.length() - 1; idx++) {
sum += (((int) s.charAt(idx))) * i;
i += 1;
}
return mapToChar(sum % 34);
}
/**
* Gets v 3.
*
* @return the v 3
*/
// TODO Added for testing reasons...
public IApplicationServerApi getV3() {
return v3;
}
/**
* Checks if we are logged in
*/
@Override
public boolean loggedin() {
try {
return v3.isSessionActive(sessionToken);
} catch (Exception e) {
return false;
}
}
/**
* logs out of the OpenBIS server
*/
@Override
public void logout() {
if (loggedin()) {
v3.logout(sessionToken);
// TODO Set sessionToken to null
sessionToken = null;
} else {
}
}
/**
* logs in to the OpenBIS server with the system userid after calling this function, the user has
* to provide the password
*/
@Override
public void login() {
if (loggedin()) {
logout();
}
// login to obtain a session token
sessionToken = v3.login(userId, password);
}
public void loginAsUser(String user) {
if (loggedin()) {
logout();
}
// login to obtain a session token
sessionToken = v3.loginAs(userId, password, user);
}
/**
* Get session token of current openBIS session
*
* @return session token as string
*/
@Override
public String getSessionToken() {
return sessionToken;
}
/**
* Checks if logged in, reconnects if not
*/
@Override
public void ensureLoggedIn() {
if (!this.loggedin()) {
this.login();
}
}
/**
* Function to get a list of all space identifiers which are registered in this openBIS instance
*
* @return list with the identifiers of all available spaces
*/
@Override
public List<String> listSpaces() {
ensureLoggedIn();
SearchResult<Space> spaces =
v3.searchSpaces(sessionToken, new SpaceSearchCriteria(), new SpaceFetchOptions());
List<String> spaceIdentifiers = new ArrayList<>();
for (Space space : spaces.getObjects()) {
spaceIdentifiers.add(space.getCode());
}
return spaceIdentifiers;
}
/**
* Function to get all projects which are registered in this openBIS instance
*
* @return list with all projects which are registered in this openBIS instance
*/
@Override
public List<Project> listProjects() {
ensureLoggedIn();
SearchResult<Project> projects =
v3.searchProjects(sessionToken, new ProjectSearchCriteria(), fetchProjectsCompletely());
return projects.getObjects();
}
/**
* Function to list all Experiments which are registered in the openBIS instance.
*
* @return list with all experiments registered in this openBIS instance
*/
@Override
public List<Experiment> listExperiments() {
ensureLoggedIn();
SearchResult<Experiment> experiments = v3.searchExperiments(sessionToken,
new ExperimentSearchCriteria(), fetchExperimentsCompletely());
return experiments.getObjects();
}
/**
* Function to retrieve all samples of a given experiment Note: seems to throw a
* ch.systemsx.cisd.common.exceptions.UserFailureException if wrong identifier given TODO Should
* we catch it and throw an illegalargumentexception instead? would be a lot clearer in my opinion
*
* @param experimentIdentifier identifier/code (both should work) of the openBIS experiment
* @return list with all samples of the given experiment
*/
@Override
public List<Sample> getSamplesofExperiment(String experimentIdentifier) {
ensureLoggedIn();
SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria();
sampleSearchCriteria.withExperiment().withCode().thatEquals(experimentIdentifier);
SearchResult<Sample> samplesOfExperiment =
v3.searchSamples(sessionToken, sampleSearchCriteria, fetchSamplesCompletely());
return samplesOfExperiment.getObjects();
}
/**
* Function to retrieve all samples of a given space
*
* @param spaceIdentifier identifier of the openBIS space
* @return list with all samples of the given space
*/
@Override
public List<Sample> getSamplesofSpace(String spaceIdentifier) {
ensureLoggedIn();
SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria();
sampleSearchCriteria.withSpace().withCode().thatEquals(spaceIdentifier);
SearchResult<Sample> samplesOfExperiment =
v3.searchSamples(sessionToken, sampleSearchCriteria, fetchSamplesCompletely());
return samplesOfExperiment.getObjects();
}
@Override
public Sample getSampleByIdentifier(String sampleIdentifier) {
ensureLoggedIn();
SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria();
sampleSearchCriteria.withId().thatEquals(new SampleIdentifier(sampleIdentifier));
SearchResult<Sample> samples =
v3.searchSamples(sessionToken, sampleSearchCriteria, fetchSamplesCompletely());
if (samples.getObjects().isEmpty()) {
return null;
} else {
return samples.getObjects().get(0);
}
}
@Override
public List<PropertyType> getPropertiesOfExperimentType(ExperimentType type) {
ensureLoggedIn();
ExperimentTypeSearchCriteria criteria = new ExperimentTypeSearchCriteria();
criteria.withCode().thatEquals(type.getCode());
ExperimentTypeFetchOptions options = new ExperimentTypeFetchOptions();
PropertyAssignmentFetchOptions paFetchOptions = new PropertyAssignmentFetchOptions();
paFetchOptions.withPropertyType();
options.withPropertyAssignmentsUsing(paFetchOptions);
List<IEntityType> res = new ArrayList<>();
res.addAll(v3.searchExperimentTypes(sessionToken, criteria, options).getObjects());
if (res.isEmpty()) {
throw new NotFetchedException("Experiment type could not be found: " + type.getCode());
}
if (res.size() > 1) {
throw new NotFetchedException("More than one entity type found for: " + type.getCode());
}
IEntityType typeWithProperties = res.get(0);
return OpenBisClientHelper.getPropertiesOfEntityType(typeWithProperties);
}
@Override
public List<PropertyType> getPropertiesOfSampleType(SampleType type) {
ensureLoggedIn();
SampleTypeSearchCriteria criteria = new SampleTypeSearchCriteria();
criteria.withCode().thatEquals(type.getCode());
SampleTypeFetchOptions options = new SampleTypeFetchOptions();
PropertyAssignmentFetchOptions paFetchOptions = new PropertyAssignmentFetchOptions();
paFetchOptions.withPropertyType();
options.withPropertyAssignmentsUsing(paFetchOptions);
List<IEntityType> res = new ArrayList<>();
res.addAll(v3.searchSampleTypes(sessionToken, criteria, options).getObjects());
if (res.isEmpty()) {
throw new NotFetchedException("Sample type could not be found: " + type.getCode());
}
if (res.size() > 1) {
throw new NotFetchedException("More than one entity type found for: " + type.getCode());
}
IEntityType typeWithProperties = res.get(0);
return OpenBisClientHelper.getPropertiesOfEntityType(typeWithProperties);
}
@Override
public List<PropertyType> getPropertiesOfDataSetType(DataSetType type) {
ensureLoggedIn();
DataSetTypeSearchCriteria criteria = new DataSetTypeSearchCriteria();
criteria.withCode().thatEquals(type.getCode());
DataSetTypeFetchOptions options = new DataSetTypeFetchOptions();
PropertyAssignmentFetchOptions paFetchOptions = new PropertyAssignmentFetchOptions();
paFetchOptions.withPropertyType();
options.withPropertyAssignmentsUsing(paFetchOptions);
List<IEntityType> res = new ArrayList<>();
res.addAll(v3.searchDataSetTypes(sessionToken, criteria, options).getObjects());
if (res.isEmpty()) {
throw new NotFetchedException("DataSet type could not be found: " + type.getCode());
}
if (res.size() > 1) {
throw new NotFetchedException("More than one entity type found for: " + type.getCode());
}
IEntityType typeWithProperties = res.get(0);
return OpenBisClientHelper.getPropertiesOfEntityType(typeWithProperties);
}
@Override
public List<Sample> getSamplesOfProject(String projIdentifier) {
ensureLoggedIn();
SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria();
sampleSearchCriteria.withOrOperator();
sampleSearchCriteria.withExperiment().withProject().withCode().thatEquals(projIdentifier);
sampleSearchCriteria.withExperiment().withProject().withId()
.thatEquals(new ProjectIdentifier(projIdentifier));
SearchResult<Sample> samples =
v3.searchSamples(sessionToken, sampleSearchCriteria, fetchSamplesCompletely());
return samples.getObjects();
}
/**
* Function to get a sample with its parents and children
*
* @param sampCode code of the openBIS sample
* @return sample
*/
@Override
public List<Sample> getSamplesWithParentsAndChildren(String sampCode) {
// TODO unclear if parents and children should be fetched or directly included into the list
ensureLoggedIn();
SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria();
sampleSearchCriteria.withCode().thatEquals(sampCode);
SearchResult<Sample> samples =
v3.searchSamples(sessionToken, sampleSearchCriteria, fetchSamplesCompletely());
return samples.getObjects();
}
@Override
public List<Experiment> getExperimentsOfProjectByIdentifier(String projectIdentifier) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withOrOperator();
sc.withProject().withId().thatEquals(new ProjectIdentifier(projectIdentifier));
sc.withProject().withCode().thatEquals(projectIdentifier);
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
return experiments.getObjects();
}
/**
* Function to list all Experiments for a specific project which are registered in the openBIS
* instance. av: 19353 ms
*
* @param project the project for which the experiments should be listed
* @return list with all experiments registered in this openBIS instance
*/
@Override
public List<Experiment> getExperimentsForProject(Project project) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withProject().withCode().thatEquals(project.getCode());
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
return experiments.getObjects();
}
/**
* Function to list all Experiments for a specific project which are registered in the openBIS
* instance.
*
* @param projectIdentifier project identifer as defined by openbis, for which the experiments
* should be listed
* @return list with all experiments registered in this openBIS instance
*/
@Override
public List<Experiment> getExperimentsForProject(String projectIdentifier) {
// TODO equal to getExperimentsOfProjectByIdentifier
ensureLoggedIn();
return getExperimentsOfProjectByIdentifier(projectIdentifier);
}
@Override
public List<Experiment> getExperimentsOfProjectByCode(String projectCode) {
// TODO Could be combined with getExperimentsOfProjectByIdentifier
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withProject().withCode().thatEquals(projectCode);
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
return experiments.getObjects();
}
@Override
public Map<String, List<Experiment>> getProjectExperimentMapping(String spaceIdentifier) {
Map<String, List<Experiment>> projectExperimentMapping = new HashMap<>();
List<Project> projects = getProjectsOfSpace(spaceIdentifier);
for (Project project : projects) {
String code = project.getCode();
projectExperimentMapping.put(code, getExperimentsOfProjectByCode(code));
}
return projectExperimentMapping;
}
@Override
public List<Experiment> getExperimentsOfSpace(String spaceIdentifier) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withProject().withSpace().withCode().thatEquals(spaceIdentifier);
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
return experiments.getObjects();
}
@Override
public List<Experiment> getExperimentsOfType(String type) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withType().withCode().thatEquals(type);
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
return experiments.getObjects();
}
@Override
public List<Sample> getSamplesOfType(String type) {
ensureLoggedIn();
SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria();
sampleSearchCriteria.withType().withCode().thatEquals(type);
SearchResult<Sample> samples =
v3.searchSamples(sessionToken, sampleSearchCriteria, fetchSamplesCompletely());
return samples.getObjects();
}
@Override
public List<Project> getProjectsOfSpace(String space) {
ensureLoggedIn();
ProjectSearchCriteria sc = new ProjectSearchCriteria();
sc.withSpace().withCode().thatEquals(space);
SearchResult<Project> projects = v3.searchProjects(sessionToken, sc, fetchProjectsCompletely());
return projects.getObjects();
}
/**
* Returns Space names a given user should be able to see
*
* @param userID Username found in openBIS
* @return List of space names with projects this user has access to
*/
@Override
public List<String> getUserSpaces(String userID) {
// this sets the user sessionToken
loginAsUser(userID);
List<String> spaceIdentifiers = new ArrayList<>();
// we are not using external functions to make sure this user is actually used
try {
SearchResult<Space> spaces =
v3.searchSpaces(sessionToken, new SpaceSearchCriteria(), new SpaceFetchOptions());
if (spaces != null) {
for (Space space : spaces.getObjects()) {
spaceIdentifiers.add(space.getCode());
}
}
} catch (UserFailureException u) {
logger.error("Could not fetch spaces for user " + userID
+ ", because they could not be logged in. Is user " + this.userId + " an admin user?");
logger.warn("No spaces were returned.");
}
logout();
login();
return spaceIdentifiers;
}
/**
* Returns whether a user is instance admin in openBIS. Checks both a user's direct role
* assignments as well as their groups' assignments
*
* @param userID the user's id
* @return true, if user is instance admin, false otherwise
*/
@Override
public boolean isUserAdmin(String userID) {
Role role = Role.ADMIN;
RoleLevel level = RoleLevel.INSTANCE;
return userHasRole(userID, role, level) || usersGroupHasRole(userID, role, level);
}
/**
* Returns whether a user with a given user Id is assigned a given role at a given role level.
* Does not check user groups of that user!
*
* @param userID the user's id
* @param role the openBIS role
* @param level the openBIS role level, denoting if the user has that role for the instance or
* just one or more spaces or projects
* @return true, if user has that role, false otherwise
*/
@Override
public boolean userHasRole(String userID, Role role, RoleLevel level) {
ensureLoggedIn();
PersonSearchCriteria criteria = new PersonSearchCriteria();
criteria.withUserId().thatEquals(userID);
PersonFetchOptions options = new PersonFetchOptions();
options.withRoleAssignments();
SearchResult<Person> res = v3.searchPersons(sessionToken, criteria, options);
for (Person p : res.getObjects()) {
for (RoleAssignment r : p.getRoleAssignments()) {
if (r.getRole().equals(role) && r.getRoleLevel().equals(level)) {
return true;
}
}
}
return false;
}
/**
* Returns whether a user's user group is assigned a given role at a given role level
*
* @param userID the user's id
* @param role the openBIS role
* @param level the openBIS role level, denoting if the user and their group has that role for the
* instance or just one or more spaces or projects
* @return true, if user has that role through their user group, false otherwise
*/
@Override
public boolean usersGroupHasRole(String userID, Role role, RoleLevel level) {
ensureLoggedIn();
AuthorizationGroupSearchCriteria criteria = new AuthorizationGroupSearchCriteria();
AuthorizationGroupFetchOptions options = new AuthorizationGroupFetchOptions();
options.withRoleAssignments().withAuthorizationGroup().withRoleAssignments();
options.withUsers();
SearchResult<AuthorizationGroup> searchResult =
v3.searchAuthorizationGroups(sessionToken, criteria, options);
for (AuthorizationGroup group : searchResult.getObjects()) {
for (Person person : group.getUsers()) {
if (person.getUserId().equals(userID)) {
for (RoleAssignment r : group.getRoleAssignments()) {
if (r.getRole().equals(role) && r.getRoleLevel().equals(level)) {
return true;
}
}
}
}
}
return false;
}
@Override
public Project getProjectByIdentifier(String projectIdentifier) {
ensureLoggedIn();
ProjectSearchCriteria sc = new ProjectSearchCriteria();
sc.withOrOperator();
sc.withId().thatEquals(new ProjectIdentifier(projectIdentifier));
sc.withCode().thatEquals(projectIdentifier);
SearchResult<Project> projects = v3.searchProjects(sessionToken, sc, fetchProjectsCompletely());
if (projects.getObjects().isEmpty()) {
return null;
} else {
return projects.getObjects().get(0);
}
}
@Override
public Project getProjectByCode(String projectCode) {
ensureLoggedIn();
ProjectSearchCriteria sc = new ProjectSearchCriteria();
sc.withOrOperator();
sc.withId().thatEquals(new ProjectIdentifier(projectCode));
sc.withCode().thatEquals(projectCode);
SearchResult<Project> projects = v3.searchProjects(sessionToken, sc, fetchProjectsCompletely());
if (projects.getObjects().isEmpty()) {
return null;
} else {
return projects.getObjects().get(0);
}
}
@Override
public Experiment getExperimentByCode(String experimentCode) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withCode().thatEquals(experimentCode);
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
if (experiments.getObjects().isEmpty()) {
return null;
} else {
return experiments.getObjects().get(0);
}
}
@Override
public Experiment getExperimentById(String experimentId) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withOrOperator();
sc.withId().thatEquals(new ExperimentIdentifier(experimentId));
SearchResult<Experiment> experiment =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
if (experiment.getObjects().isEmpty()) {
return null;
}
return experiment.getObjects().get(0);
}
@Override
public Project getProjectOfExperimentByIdentifier(String experimentIdentifier) {
ensureLoggedIn();
ExperimentSearchCriteria sc = new ExperimentSearchCriteria();
sc.withId().thatEquals(new ExperimentIdentifier(experimentIdentifier));
SearchResult<Experiment> experiments =
v3.searchExperiments(sessionToken, sc, fetchExperimentsCompletely());
if (experiments.getObjects().isEmpty()) {
return null;
} else {
return experiments.getObjects().get(0).getProject();
}
}
/**
* Function to list all datasets of a specific sample (watch out there are different dataset
* classes)
*
* @param sampleIdentifier identifier of the openBIS sample
* @return list with all datasets of the given sample
*/
@Override
public List<DataSet> getDataSetsOfSampleByIdentifier(String sampleIdentifier) {
DataSetSearchCriteria sc = new DataSetSearchCriteria();
sc.withOrOperator();
sc.withSample().withId().thatEquals(new SampleIdentifier(sampleIdentifier));
SearchResult<DataSet> dataSets = v3.searchDataSets(sessionToken, sc, fetchDataSetsCompletely());
return dataSets.getObjects();
}
/**
* Function to list all datasets of a specific sample (watch out there are different dataset
* classes)
*
* @param sampleCode code or identifier of the openBIS sample
* @return list with all datasets of the given sample
*/
@Override
public List<DataSet> getDataSetsOfSample(String sampleCode) {
DataSetSearchCriteria sc = new DataSetSearchCriteria();
sc.withSample().withCode().thatEquals(sampleCode);
SearchResult<DataSet> dataSets = v3.searchDataSets(sessionToken, sc, fetchDataSetsCompletely());
return dataSets.getObjects();
}
/**
* Function to list all datasets of a specific experiment (watch out there are different dataset
* classes)
*
* @param experimentPermID permId of the openBIS experiment
* @return list with all datasets of the given experiment
*/
@Override
public List<DataSet> getDataSetsOfExperiment(String experimentPermID) {
DataSetSearchCriteria sc = new DataSetSearchCriteria();
sc.withExperiment().withPermId().thatEquals(experimentPermID);
SearchResult<DataSet> dataSets = v3.searchDataSets(sessionToken, sc, fetchDataSetsCompletely());
return dataSets.getObjects();
}
/**
* Returns all datasets of a given experiment. The new version should run smoother
*
* @param experimentIdentifier identifier or code of the openbis experiment
* @return list of all datasets of the given experiment
*/
@Override
public List<DataSet> getDataSetsOfExperimentByIdentifier(String experimentIdentifier) {
DataSetSearchCriteria sc = new DataSetSearchCriteria();
sc.withExperiment().withId().thatEquals(new ExperimentIdentifier(experimentIdentifier));
SearchResult<DataSet> dataSets = v3.searchDataSets(sessionToken, sc, fetchDataSetsCompletely());
return dataSets.getObjects();
}
/**
* Function to list all datasets of a specific openBIS space
*
* @param spaceIdentifier identifier of the openBIS space
* @return list with all datasets of the given space
*/
@Override
public List<DataSet> getDataSetsOfSpaceByIdentifier(String spaceIdentifier) {
DataSetSearchCriteria sc = new DataSetSearchCriteria();
sc.withSample().withSpace().withCode().thatEquals(spaceIdentifier);
SearchResult<DataSet> dataSets = v3.searchDataSets(sessionToken, sc, fetchDataSetsCompletely());
return dataSets.getObjects();
}
/**
* Function to list all datasets of a specific openBIS project
*
* @param projectIdentifier identifier of the openBIS project
* @return list with all datasets of the given project
*/
@Override
public List<DataSet> getDataSetsOfProjectByIdentifier(String projectIdentifier) {
// TODO does not work yet
throw new NotImplementedException();
}
/**
* Function to list all datasets of a specific openBIS project
*
* @param projectIdentifier identifier of the openBIS project
* @return list with all datasets of the given project
*/
@Override
public List<DataSet> getDataSetsOfProjects(List<Project> projectIdentifier) {
// TODO does not work yet
throw new NotImplementedException();
}
@Override
public List<DataSet> getDataSetsByType(String type) {
ensureLoggedIn();
DataSetSearchCriteria sc = new DataSetSearchCriteria();
sc.withType().withCode().thatEquals(type);
SearchResult<DataSet> dataSets = v3.searchDataSets(sessionToken, sc, fetchDataSetsCompletely());
return dataSets.getObjects();
}
@Override
public List<Attachment> listAttachmentsForSampleByIdentifier(String sampleIdentifier) {
ensureLoggedIn();
return getSampleByIdentifier(sampleIdentifier).getAttachments();
}
@Override
public List<Attachment> listAttachmentsForProjectByIdentifier(String projectIdentifier) {
ensureLoggedIn();
return getProjectByIdentifier(projectIdentifier).getAttachments();
}
/**
* Returns all users of a Space.
*
* @param spaceCode code of the openBIS space
* @return set of user names as string
*/
@Override
public Set<String> getSpaceMembers(String spaceCode) {
// TODO cannot find an opportunity to do that
return null;
}
/**
* Function to list the vocabulary codes for a given property which has been added to openBIS. The
* property has to be a Controlled Vocabulary Property. Use
*
* @param property the property type
* @return list of the vocabulary terms of the given property
*/
@Override
public List<String> listVocabularyTermsForProperty(PropertyType property) {
throw new NotImplementedException();
}
/**
* Function to get the label of a CV item for some property
*
* @param propertyType the property type
* @param propertyValue the property value
* @return Label of CV item
*/
@Override
public String getCVLabelForProperty(PropertyType propertyType, String propertyValue) {
throw new NotImplementedException();
}
@Override
public SampleType getSampleTypeByString(String sampleType) {
SampleTypeSearchCriteria sc = new SampleTypeSearchCriteria();
sc.withCode().thatEquals(sampleType);
SearchResult<SampleType> sampleTypes =
v3.searchSampleTypes(sessionToken, sc, fetchSampleTypesCompletely());
if (sampleTypes.getObjects().isEmpty()) {
return null;
} else {
return sampleTypes.getObjects().get(0);
}
}
/**
* Function to retrieve a map with sample type code as key and the sample type object as value
*
* @return map with sample types
*/
@Override
public Map<String, SampleType> getSampleTypes() {
SearchResult<SampleType> sampleTypes = v3.searchSampleTypes(sessionToken,
new SampleTypeSearchCriteria(), fetchSampleTypesCompletely());
Map<String, SampleType> types = new HashMap<>();
for (SampleType t : sampleTypes.getObjects()) {
types.put(t.getCode(), t);
}
return types;
}
/**
* Function to get a ExperimentType object of a experiment type
*
* @param experimentType the experiment type as string
* @return the ExperimentType object of the corresponding experiment type
*/
@Override
public ExperimentType getExperimentTypeByString(String experimentType) {
ExperimentTypeSearchCriteria sc = new ExperimentTypeSearchCriteria();
sc.withCode().thatContains(experimentType);
SearchResult<ExperimentType> experimentTypes =
v3.searchExperimentTypes(sessionToken, sc, fetchExperimentTypesCompletely());
if (experimentTypes.getObjects().isEmpty()) {
return null;
} else {
return experimentTypes.getObjects().get(0);
}
}
/**
* Function to trigger ingestion services registered in openBIS
*
* @param serviceName name of the ingestion service which should be triggered
* @param parameters map with needed information for registration process
* @return object name of the QueryTableModel which is returned by the aggregation service
*/
@Override
public String triggerIngestionService(String serviceName, Map<String, Object> parameters) {
return null;
}
@Override
public String generateBarcode(String proj, int number_of_samples_offset) {
Project project = getProjectByIdentifier(proj);
int numberOfSamples = getSamplesOfProject(project.getCode()).size();
String barcode = project.getCode() + String.format("%03d", (numberOfSamples + 1)) + "S";
barcode += checksum(barcode);
return barcode;
}
/**
* Function to transform openBIS entity type to human readable text. Performs String replacement
* and does not query openBIS!
*
* @param entityCode the entity code as string
* @return entity code as string in human readable text
*/
@Override
public String openBIScodeToString(String entityCode) {
entityCode = WordUtils.capitalizeFully(entityCode.replace("_", " ").toLowerCase());
String edit_string = entityCode.replace("Ngs", "NGS").replace("Hla", "HLA")
.replace("Rna", "RNA").replace("Dna", "DNA").replace("Ms", "MS");
if (edit_string.startsWith("Q ")) {
edit_string = edit_string.replace("Q ", "");
}
return edit_string;
}
/**
* Function to get the download url for a file stored in the openBIS datastore server. Note that
* this method does no checks, whether datasetcode or openbisFilename do exist. Deprecated: Use
* getUrlForDataset() instead
*
* @param dataSetCode code of the openBIS dataset
* @param openbisFilename name of the file stored in the given dataset