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.event;
23
24 import org.opencastproject.elasticsearch.api.SearchIndexException;
25 import org.opencastproject.elasticsearch.index.ElasticsearchIndex;
26 import org.opencastproject.elasticsearch.index.objects.event.Event;
27 import org.opencastproject.graphql.datafetcher.ContextDataFetcher;
28 import org.opencastproject.graphql.event.GqlEvent;
29 import org.opencastproject.graphql.exception.GraphQLNotFoundException;
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.index.service.api.IndexService;
34 import org.opencastproject.index.service.exception.IndexServiceException;
35 import org.opencastproject.index.service.impl.util.EventUtils;
36 import org.opencastproject.metadata.dublincore.DublinCoreMetadataCollection;
37 import org.opencastproject.metadata.dublincore.EventCatalogUIAdapter;
38 import org.opencastproject.metadata.dublincore.MetadataField;
39 import org.opencastproject.security.api.SecurityService;
40
41 import java.text.DateFormat;
42 import java.text.ParseException;
43 import java.text.SimpleDateFormat;
44 import java.util.Date;
45 import java.util.HashMap;
46 import java.util.Map;
47 import java.util.Optional;
48 import java.util.TimeZone;
49
50 import graphql.schema.DataFetchingEnvironment;
51
52 public class CommonEventMetadataDataFetcher implements ContextDataFetcher<Map<String, Object>> {
53
54 @Override
55 public Map<String, Object> get(OpencastContext opencastContext, DataFetchingEnvironment dataFetchingEnvironment) {
56 String eventId = ((GqlEvent)dataFetchingEnvironment.getSource()).id();
57 SecurityService securityService = opencastContext.getService(SecurityService.class);
58 ElasticsearchIndex searchIndex = opencastContext.getService(ElasticsearchIndex.class);
59 IndexService indexService = opencastContext.getService(IndexService.class);
60
61 try {
62 GqlEvent e = getEvent(searchIndex, eventId, securityService);
63 Event event = getEventFromIndexService(indexService, eventId, searchIndex);
64 EventCatalogUIAdapter eventCatalogUiAdapter = indexService.getCommonEventCatalogUIAdapter();
65 DublinCoreMetadataCollection collection = EventUtils.getEventMetadata(event, eventCatalogUiAdapter);
66
67 return getOutputFields(collection);
68 } catch (SearchIndexException | ParseException | IndexServiceException e) {
69 throw new GraphQLRuntimeException(OpencastErrorType.InternalError, e);
70 }
71 }
72
73 private GqlEvent getEvent(ElasticsearchIndex searchIndex, String eventId, SecurityService securityService)
74 throws SearchIndexException {
75 return searchIndex.getEvent(eventId, securityService.getOrganization().toString(), securityService.getUser())
76 .map(GqlEvent::new).orElseThrow(() -> new GraphQLNotFoundException(
77 String.format("Could not resolve to a %s with the id of %s", GqlEvent.TYPE_NAME, eventId)));
78 }
79
80 private Event getEventFromIndexService(IndexService indexService, String eventId, ElasticsearchIndex searchIndex)
81 throws IndexServiceException, SearchIndexException {
82 Optional<Event> opt = indexService.getEvent(eventId, searchIndex);
83 if (opt.isEmpty()) {
84 throw new GraphQLNotFoundException(
85 String.format("Could not resolve to a %s with the id of %s", GqlEvent.TYPE_NAME, eventId));
86 }
87 return opt.get();
88 }
89
90 private Map<String, Object> getOutputFields(DublinCoreMetadataCollection collection) {
91 Map<String, Object> result = new HashMap<>();
92 collection.getOutputFields().values().forEach(f -> {
93 Object value = f.getValue();
94 String outputID = f.getOutputID();
95 MetadataField.Type type = f.getType();
96
97 if (type.equals(MetadataField.Type.DATE)) {
98 Date date = (Date) value;
99 if (date != null) {
100 DateFormat df = new SimpleDateFormat(f.getPattern());
101 df.setTimeZone(TimeZone.getTimeZone("UTC"));
102 result.put(outputID, df.format(date));
103 }
104 } else if (type.equals(MetadataField.Type.DURATION)) {
105 if (value instanceof String) {
106 result.put(outputID, Long.parseLong(((String) value).isEmpty() ? "0" : (String) value));
107 } else if (value instanceof Long || value instanceof Integer) {
108 result.put(outputID, value);
109 }
110 } else {
111 result.put(outputID, value);
112 }
113 });
114 return result;
115 }
116 }