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.elasticsearch.index.objects.event;
23  
24  import static org.opencastproject.security.api.SecurityConstants.GLOBAL_ADMIN_ROLE;
25  
26  import org.opencastproject.elasticsearch.api.SearchTerms;
27  import org.opencastproject.elasticsearch.api.SearchTerms.Quantifier;
28  import org.opencastproject.elasticsearch.impl.AbstractElasticsearchQueryBuilder;
29  import org.opencastproject.elasticsearch.impl.IndexSchema;
30  import org.opencastproject.security.api.Role;
31  import org.opencastproject.security.api.User;
32  
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import java.util.ArrayList;
37  
38  /**
39   * Opencast {@link EventSearchQuery} implementation of the Elasticsearch query builder.
40   */
41  public class EventQueryBuilder extends AbstractElasticsearchQueryBuilder<EventSearchQuery> {
42  
43    /** The logging facility */
44    private static final Logger logger = LoggerFactory.getLogger(EventQueryBuilder.class);
45  
46    /**
47     * Creates a new elastic search query based on the events query.
48     *
49     * @param query
50     *          the events query
51     */
52    public EventQueryBuilder(EventSearchQuery query) {
53      super(query);
54    }
55  
56    @Override
57    public void buildQuery(EventSearchQuery query) {
58      // Organization
59      if (query.getOrganization() == null) {
60        throw new IllegalStateException("No organization set on the event search query!");
61      }
62  
63      and(EventIndexSchema.ORGANIZATION, query.getOrganization());
64  
65      // Recording identifier
66      if (query.getIdentifier().length > 0) {
67        and(EventIndexSchema.UID, query.getIdentifier());
68      }
69  
70      // Title
71      if (query.getTitle() != null) {
72        and(EventIndexSchema.TITLE, query.getTitle());
73      }
74  
75      // Description
76      if (query.getDescription() != null) {
77        and(EventIndexSchema.DESCRIPTION, query.getDescription());
78      }
79  
80      // Action
81      if (query.getActions() != null && query.getActions().length > 0) {
82        User user = query.getUser();
83        if (!user.hasRole(GLOBAL_ADMIN_ROLE) && !user.hasRole(user.getOrganization().getAdminRole())) {
84          for (Role role : user.getRoles()) {
85            for (String action : query.getActions()) {
86              and(EventIndexSchema.ACL_PERMISSION_PREFIX.concat(action), role.getName());
87            }
88          }
89        }
90      }
91  
92      // Presenter
93      if (query.getPresenters() != null) {
94        for (String presenter : query.getPresenters()) {
95          and(EventIndexSchema.PRESENTER, presenter);
96        }
97      }
98  
99      // Contributors
100     if (query.getContributors().length > 0) {
101       and(EventIndexSchema.CONTRIBUTOR, query.getContributors());
102     }
103 
104     // Subject
105     if (query.getSubject() != null) {
106       and(EventIndexSchema.SUBJECT, query.getSubject());
107     }
108 
109     // Location
110     if (query.getLocation() != null) {
111       and(EventIndexSchema.LOCATION, query.getLocation());
112     }
113 
114     // Series Id
115     if (query.getSeriesId() != null) {
116       and(EventIndexSchema.SERIES_ID, query.getSeriesId());
117     }
118 
119     // Series Name
120     if (query.getSeriesName() != null) {
121       and(EventIndexSchema.SERIES_NAME, query.getSeriesName());
122     }
123 
124     // Language
125     if (query.getLanguage() != null) {
126       and(EventIndexSchema.LANGUAGE, query.getLanguage());
127     }
128 
129     // Source
130     if (query.getSource() != null) {
131       and(EventIndexSchema.SOURCE, query.getSource());
132     }
133 
134     // Created
135     if (query.getCreated() != null) {
136       and(EventIndexSchema.CREATED, query.getCreated());
137     }
138 
139     // Creator
140     if (query.getCreator() != null) {
141       and(EventIndexSchema.CREATOR, query.getCreator());
142     }
143 
144     // Publisher
145     if (query.getPublisher() != null) {
146       and(EventIndexSchema.PUBLISHER, query.getPublisher());
147     }
148 
149     // License
150     if (query.getLicense() != null) {
151       and(EventIndexSchema.LICENSE, query.getLicense());
152     }
153 
154     // Rights
155     if (query.getRights() != null) {
156       and(EventIndexSchema.RIGHTS, query.getRights());
157     }
158 
159     // Access policy
160     if (query.getAccessPolicy() != null) {
161       and(EventIndexSchema.ACCESS_POLICY, query.getAccessPolicy());
162     }
163 
164     // Managed ACL
165     if (query.getManagedAcl() != null) {
166       and(EventIndexSchema.MANAGED_ACL, query.getManagedAcl());
167     }
168 
169     // Workflow state
170     if (query.getWorkflowState() != null) {
171       and(EventIndexSchema.WORKFLOW_STATE, query.getWorkflowState());
172     }
173 
174     // Workflow id
175     if (query.getWorkflowId() != null) {
176       and(EventIndexSchema.WORKFLOW_ID, query.getWorkflowId());
177     }
178 
179     // Workflow definition id
180     if (query.getWorkflowDefinition() != null) {
181       and(EventIndexSchema.WORKFLOW_DEFINITION_ID, query.getWorkflowDefinition());
182     }
183 
184     // Event status
185     if (query.getEventStatus() != null) {
186       and(EventIndexSchema.EVENT_STATUS, query.getEventStatus());
187     }
188 
189     // Recording start date period
190     if (query.getStartFrom() != null && query.getStartTo() != null) {
191       and(EventIndexSchema.START_DATE, query.getStartFrom(), query.getStartTo());
192     }
193 
194     // Recording start date
195     if (query.getStartDate() != null) {
196       and(EventIndexSchema.START_DATE, query.getStartDate());
197     }
198 
199     // Recording duration
200     if (query.getDuration() != null) {
201       and(EventIndexSchema.DURATION, query.getDuration());
202     }
203 
204     // Has comments
205     if (query.getHasComments() != null) {
206       and(EventIndexSchema.HAS_COMMENTS, query.getHasComments());
207     }
208 
209     // Has open comments
210     if (query.getHasOpenComments() != null) {
211       and(EventIndexSchema.HAS_OPEN_COMMENTS, query.getHasOpenComments());
212     }
213 
214     // Publications
215     if (query.getComments() != null) {
216       for (String comment : query.getComments()) {
217         and(EventIndexSchema.COMMENTS, comment);
218       }
219     }
220 
221     // Publications
222     if (query.getPublications() != null) {
223       for (String publication : query.getPublications()) {
224         and(EventIndexSchema.PUBLICATION, publication);
225       }
226     }
227 
228     // Is published
229     if (query.getIsPublished() != null) {
230       and(EventIndexSchema.IS_PUBLISHED, query.getIsPublished());
231     }
232 
233     // Archive version
234     if (query.getArchiveVersion() != null) {
235       and(EventIndexSchema.ARCHIVE_VERSION, query.getArchiveVersion());
236     }
237 
238     // Technical agent identifier
239     if (query.getAgentId() != null) {
240       and(EventIndexSchema.AGENT_ID, query.getAgentId());
241     }
242 
243     // Technical start date period
244     if (query.getTechnicalStartFrom() != null && query.getTechnicalStartTo() != null) {
245       and(EventIndexSchema.TECHNICAL_START, query.getTechnicalStartFrom(), query.getTechnicalStartTo());
246     }
247 
248     // Technical start date
249     if (query.getTechnicalStartTime() != null) {
250       and(EventIndexSchema.TECHNICAL_START, query.getTechnicalStartTime());
251     }
252 
253     // Technical end date
254     if (query.getTechnicalEndTime() != null) {
255       and(EventIndexSchema.TECHNICAL_END, query.getTechnicalEndTime());
256     }
257 
258     // Technical presenters
259     if (query.getTechnicalPresenters().length > 0) {
260       and(EventIndexSchema.TECHNICAL_PRESENTERS, query.getTechnicalPresenters());
261     }
262 
263     // Text
264     if (query.getTerms() != null) {
265       for (SearchTerms<String> terms : query.getTerms()) {
266         StringBuilder queryText = new StringBuilder();
267         for (String term : terms.getTerms()) {
268           if (queryText.length() > 0) {
269             queryText.append(" ");
270           }
271           queryText.append(term);
272         }
273         if (query.isFuzzySearch()) {
274           fuzzyText = queryText.toString();
275         } else {
276           this.text = queryText.toString();
277         }
278         if (Quantifier.All.equals(terms.getQuantifier())) {
279           if (groups == null) {
280             groups = new ArrayList<>();
281           }
282           if (query.isFuzzySearch()) {
283             logger.warn("All quantifier not supported in conjunction with wildcard text");
284           }
285           groups.add(new ValueGroup(IndexSchema.TEXT, (Object[]) terms.getTerms().toArray(new String[terms.size()])));
286         }
287       }
288     }
289 
290     // Filter query
291     if (query.getFilter() != null) {
292       this.filter = query.getFilter();
293     }
294 
295   }
296 
297 }