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.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
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