1
1
package life .qbic .portal .sampletracking .data ;
2
2
3
+ import static java .util .stream .Collectors .groupingBy ;
4
+ import static java .util .stream .Collectors .toList ;
5
+
3
6
import ch .ethz .sis .openbis .generic .asapi .v3 .IApplicationServerApi ;
4
7
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .common .search .SearchResult ;
5
8
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .fetchoptions .ProjectFetchOptions ;
8
11
import ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .search .SampleSearchCriteria ;
9
12
import ch .systemsx .cisd .common .spring .HttpInvokerUtils ;
10
13
import java .util .ArrayList ;
11
- import java .util .Comparator ;
12
14
import java .util .HashMap ;
13
15
import java .util .List ;
14
16
import java .util .Map ;
17
+ import java .util .Map .Entry ;
15
18
import java .util .Optional ;
16
19
import java .util .function .Function ;
17
20
import java .util .function .Predicate ;
18
- import java .util .stream .Collectors ;
19
21
import life .qbic .datamodel .dtos .portal .PortalUser ;
20
22
import life .qbic .portal .sampletracking .view .projects .viewmodel .Project ;
21
23
import life .qbic .portal .sampletracking .view .samples .viewmodel .Sample ;
22
24
23
- public class OpenBisConnector implements ProjectRepository , SampleRepository {
25
+ public class OpenBisConnector implements ProjectRepository , SampleRepository , NgsSampleRepository {
24
26
25
27
private final String sessionToken ;
26
28
27
29
private final IApplicationServerApi api ;
28
30
29
31
private static final int TIMEOUT = 10_000 ;
30
-
31
- private final List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project > cachedProjects = new ArrayList <>();
32
- private final Map <String , List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample >> cachedSamples = new HashMap <>();
32
+ private final Map <String , List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample >> sampleCache = new HashMap <>();
33
+ private final Map <String , ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project > projectCache = new HashMap <>();
33
34
34
35
35
36
@@ -43,64 +44,72 @@ public OpenBisConnector(Credentials credentials, PortalUser portalUser,
43
44
44
45
@ Override
45
46
public List <Project > findAllProjects () {
46
- if (cachedProjects .isEmpty ()) {
47
- ProjectFetchOptions fetchOptions = new ProjectFetchOptions ();
48
- fetchOptions .withSpace ();
49
- SearchResult <ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project > projectSearchResult =
50
- api .searchProjects (sessionToken , new ProjectSearchCriteria (), fetchOptions );
51
-
52
- List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project > projects = projectSearchResult .getObjects ().stream ()
53
- .filter (hasValidProjectCode ())
54
- .sorted (Comparator .comparing (
55
- ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project ::getModificationDate ).reversed ())
56
- .collect (Collectors .toList ());
57
- cachedProjects .addAll (projects );
47
+ if (projectCache .isEmpty ()) {
48
+ updateCache ();
58
49
}
59
- return cachedProjects .stream ()
60
- .map (it -> new Project (it .getCode (),
61
- Optional .ofNullable (it .getDescription ()).orElse ("" )))
62
- .collect (Collectors .toList ());
50
+ return projectCache .entrySet ().stream ()
51
+ .map (it -> new Project (it .getKey (), Optional .ofNullable (it .getValue ().getDescription ()).orElse ("" )))
52
+ .collect (toList ());
53
+ }
54
+
55
+ @ Override
56
+ public List <String > findNGSSamplesForProject (String projectCode ){
57
+
58
+ return findAllSamplesForProject (projectCode ).stream ()
59
+ .map (Sample ::code )
60
+ .collect (toList ());
63
61
}
64
62
65
63
@ Override
66
64
public List <Sample > findAllSamplesForProject (String projectCode ) {
67
65
68
- List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample > samples ;
69
- if (cachedSamples .containsKey (projectCode )) {
70
- samples = cachedSamples .get (projectCode );
71
- } else {
72
- SampleSearchCriteria sampleSearchCriteria = new SampleSearchCriteria ();
73
- sampleSearchCriteria .withCode ().thatStartsWith (projectCode );
74
- sampleSearchCriteria .withType ().withCode ().thatEquals ("Q_TEST_SAMPLE" );
75
- SampleFetchOptions fetchOptions = new SampleFetchOptions ();
76
- fetchOptions .withType ();
77
- fetchOptions .withProperties ();
78
- SearchResult <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample > sampleSearchResult = api .searchSamples (
79
- sessionToken , sampleSearchCriteria , fetchOptions );
80
- samples = sampleSearchResult .getObjects ();
81
- cachedSamples .put (projectCode , samples );
82
- }
83
- return samples .stream ()
66
+ List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample > cachedProjectSamples = sampleCache
67
+ .entrySet ().stream ()
68
+ .filter (it -> it .getKey ().equals (projectCode )).map (Entry ::getValue ).findAny ().orElse (new ArrayList <>());
69
+ return cachedProjectSamples .stream ().map (convertToSample ()).collect (toList ());
70
+ }
71
+
72
+ private void updateCache () {
73
+
74
+ SampleSearchCriteria isNgs = new SampleSearchCriteria ();
75
+ isNgs .withOrOperator ();
76
+ isNgs .withProperty ("Q_SAMPLE_TYPE" ).thatContains ("DNA" );
77
+ isNgs .withProperty ("Q_SAMPLE_TYPE" ).thatContains ("RNA" );
78
+ // this guarantees that no q-entity is contained as only test samples have Q_SAMPLE_TYPE
79
+
80
+ SampleFetchOptions fetchOptions = new SampleFetchOptions ();
81
+ fetchOptions .withType ();
82
+ fetchOptions .withProperties ();
83
+ SearchResult <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample > sampleSearchResult = api .searchSamples (
84
+ sessionToken , isNgs , fetchOptions );
85
+ Map <String , List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample >> projectSampleMap = sampleSearchResult .getObjects ()
86
+ .stream ()
84
87
.filter (hasValidSampleCode ())
85
- .filter (notAnEntity ())
86
- .map (convertToSample ())
87
- .collect (Collectors .toList ());
88
+ .collect (groupingBy (this ::getProject ));
89
+ sampleCache .putAll (projectSampleMap );
90
+
91
+ ProjectFetchOptions projectFetchOptions = new ProjectFetchOptions ();
92
+ projectFetchOptions .withSpace ();
93
+ ProjectSearchCriteria projectSearchCriteria = new ProjectSearchCriteria ();
94
+
95
+ SearchResult <ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project > projectSearchResult = api .searchProjects (
96
+ sessionToken , projectSearchCriteria , projectFetchOptions );
97
+ List <ch .ethz .sis .openbis .generic .asapi .v3 .dto .project .Project > projects = projectSearchResult .getObjects ()
98
+ .stream ().filter (it -> sampleCache .containsKey (it .getCode ())).collect (toList ());
99
+ projects .forEach (it -> projectCache .put (it .getCode (), it ));
88
100
}
89
101
90
- private Predicate <? super ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample > hasValidSampleCode () {
91
- return sample -> sample .getCode ().matches ("Q[A-X0-9]{4}[0-9]{3}[A-X0-9]{2}$" );
102
+ private String getProject (
103
+ ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample sample ) {
104
+ return sample .getCode ().substring (0 , 5 );
92
105
}
93
106
94
- private Predicate <ch .ethz .sis .openbis .generic .asapi .v3 .dto .project . Project > hasValidProjectCode () {
95
- return project -> project .getCode ().matches ("^ Q[A-X0-9]{4}$" );
107
+ private Predicate <? super ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample . Sample > hasValidSampleCode () {
108
+ return sample -> sample .getCode ().matches ("Q[A-X0-9]{4}[0-9]{3}[A-X0-9]{2 }$" );
96
109
}
97
110
98
111
private static Function <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample , Sample > convertToSample () {
99
112
return it -> new Sample (it .getCode (), it .getProperty ("Q_SECONDARY_NAME" ));
100
113
}
101
114
102
- private static Predicate <ch .ethz .sis .openbis .generic .asapi .v3 .dto .sample .Sample > notAnEntity () {
103
- return it -> !it .getCode ().contains ("ENTITY" );
104
- }
105
-
106
115
}
0 commit comments