1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.graphql.datafetcher.series;
23
24 import org.opencastproject.elasticsearch.api.SearchIndexException;
25 import org.opencastproject.elasticsearch.api.SearchResult;
26 import org.opencastproject.elasticsearch.index.ElasticsearchIndex;
27 import org.opencastproject.elasticsearch.index.objects.series.Series;
28 import org.opencastproject.elasticsearch.index.objects.series.SeriesSearchQuery;
29 import org.opencastproject.graphql.datafetcher.ContextDataFetcher;
30 import org.opencastproject.graphql.exception.GraphQLRuntimeException;
31 import org.opencastproject.graphql.exception.OpencastErrorType;
32 import org.opencastproject.graphql.execution.context.OpencastContext;
33 import org.opencastproject.graphql.series.GqlSeries;
34 import org.opencastproject.security.api.SecurityService;
35
36 import java.util.Objects;
37
38 import graphql.schema.DataFetchingEnvironment;
39
40
41
42
43
44
45
46
47 public class SeriesDataFetcher implements ContextDataFetcher<GqlSeries> {
48
49 private final String seriesId;
50
51 public SeriesDataFetcher(String seriesId) {
52 Objects.requireNonNull(seriesId, "Series identifier must not be null.");
53 this.seriesId = seriesId;
54 }
55
56
57 @Override
58 public GqlSeries get(OpencastContext opencastContext, DataFetchingEnvironment dataFetchingEnvironment) {
59 SecurityService securityService = opencastContext.getService(SecurityService.class);
60 ElasticsearchIndex searchIndex = opencastContext.getService(ElasticsearchIndex.class);
61
62 try {
63 SearchResult<Series> result = searchIndex.getByQuery(
64 new SeriesSearchQuery(securityService.getOrganization().getId(), securityService.getUser())
65 .withIdentifier(seriesId)
66 );
67 if (result.getDocumentCount() == 0) {
68 return null;
69 } else if (result.getDocumentCount() == 1) {
70 return new GqlSeries(result.getItems()[0].getSource());
71 }
72 throw new GraphQLRuntimeException(
73 "Multiple series found with the same identifier",
74 OpencastErrorType.InternalError
75 );
76 } catch (SearchIndexException e) {
77 throw new GraphQLRuntimeException(OpencastErrorType.InternalError, e);
78 }
79 }
80 }