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