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.playlist.datafetcher;
23  
24  import org.opencastproject.graphql.datafetcher.ContextDataFetcher;
25  import org.opencastproject.graphql.exception.GraphQLRuntimeException;
26  import org.opencastproject.graphql.exception.OpencastErrorType;
27  import org.opencastproject.graphql.execution.context.OpencastContext;
28  import org.opencastproject.graphql.playlist.type.output.GqlPlaylist;
29  import org.opencastproject.playlists.Playlist;
30  import org.opencastproject.playlists.PlaylistService;
31  import org.opencastproject.security.api.UnauthorizedException;
32  import org.opencastproject.util.NotFoundException;
33  
34  import java.util.Objects;
35  
36  import graphql.schema.DataFetchingEnvironment;
37  
38  public class PlaylistDataFetcher implements ContextDataFetcher<GqlPlaylist> {
39  
40    private final String playlistId;
41  
42    public PlaylistDataFetcher(String playlistId) {
43      Objects.requireNonNull(playlistId, "Playlist identifier must not be null.");
44      this.playlistId = playlistId;
45    }
46  
47    @Override
48    public GqlPlaylist get(OpencastContext opencastContext, DataFetchingEnvironment dataFetchingEnvironment) {
49      PlaylistService playlistService = opencastContext.getService(PlaylistService.class);
50  
51      try {
52        Playlist playlist = playlistService.getPlaylistById(playlistId);
53        if (playlist == null) {
54          return null;
55        }
56        try {
57          //playlist = playlistService.enrich(playlist).toPlaylist();
58        } catch (Throwable ignored) {
59        }
60        return new GqlPlaylist(playlist);
61      } catch (NotFoundException e) {
62        return null;
63      } catch (UnauthorizedException e) {
64        throw new GraphQLRuntimeException("Unauthorized", OpencastErrorType.Unauthorized, e);
65      } catch (Exception e) {
66        throw new GraphQLRuntimeException("Error fetching playlist", OpencastErrorType.InternalError, e);
67      }
68    }
69  }
70