View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * The SeriesDataFetcher class implements the ContextDataFetcher interface with GqlSeries as the generic type.
43   * This class is used to fetch series data in a GraphQL context.
44   * <p>
45   * If the requested series is not found, a null value is returned.
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  }