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.event;
23  
24  
25  import org.opencastproject.elasticsearch.api.SearchResult;
26  import org.opencastproject.elasticsearch.api.SearchResultItem;
27  import org.opencastproject.elasticsearch.index.objects.event.Event;
28  import org.opencastproject.graphql.type.output.OffsetPageInfo;
29  
30  import java.util.Arrays;
31  import java.util.List;
32  import java.util.stream.Collectors;
33  
34  import graphql.annotations.annotationTypes.GraphQLDescription;
35  import graphql.annotations.annotationTypes.GraphQLField;
36  import graphql.annotations.annotationTypes.GraphQLName;
37  import graphql.annotations.annotationTypes.GraphQLNonNull;
38  
39  @GraphQLName(GqlEventList.TYPE_NAME)
40  @GraphQLNonNull
41  @GraphQLDescription("A list of events")
42  public class GqlEventList {
43  
44    public static final String TYPE_NAME = "EventList";
45  
46    protected final SearchResult<Event> searchResult;
47  
48    public GqlEventList(SearchResult<Event> searchResult) {
49      this.searchResult = searchResult;
50    }
51  
52    @GraphQLField
53    @GraphQLNonNull
54    public Long totalCount() {
55      return searchResult.getHitCount();
56    }
57  
58    @GraphQLField
59    @GraphQLNonNull
60    public OffsetPageInfo pageInfo() {
61      return OffsetPageInfo.from(searchResult);
62    }
63  
64    @GraphQLField
65    @GraphQLNonNull
66    public List<GqlEvent> nodes() {
67      return Arrays.stream(searchResult.getItems())
68          .map(SearchResultItem::getSource)
69          .map(GqlEvent::new)
70          .collect(Collectors.toList());
71    }
72  
73  }