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.series;
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 SeriesSearchQuery} implementation of the Elasticsearch query builder.
40   */
41  public class SeriesQueryBuilder extends AbstractElasticsearchQueryBuilder<SeriesSearchQuery> {
42  
43    /** The logging facility */
44    private static final Logger logger = LoggerFactory.getLogger(SeriesQueryBuilder.class);
45  
46    /**
47     * Creates a new elastic search query based on the series query.
48     *
49     * @param query
50     *          the series query
51     */
52    public SeriesQueryBuilder(SeriesSearchQuery query) {
53      super(query);
54    }
55  
56    @Override
57    public void buildQuery(SeriesSearchQuery query) {
58      // Organization
59      if (query.getOrganization() == null) {
60        throw new IllegalStateException("No organization set on the series search query!");
61      }
62  
63      and(SeriesIndexSchema.ORGANIZATION, query.getOrganization());
64  
65      // Series identifier
66      if (query.getIdentifier().length > 0) {
67        and(SeriesIndexSchema.UID, query.getIdentifier());
68      }
69  
70      // Title
71      if (query.getTitle() != null) {
72        and(SeriesIndexSchema.TITLE, query.getTitle());
73      }
74  
75      // Action
76      if (query.getActions() != null && query.getActions().length > 0) {
77        User user = query.getUser();
78        if (!user.hasRole(GLOBAL_ADMIN_ROLE) && !user.hasRole(user.getOrganization().getAdminRole())) {
79          for (Role role : user.getRoles()) {
80            for (String action : query.getActions()) {
81              and(SeriesIndexSchema.ACL_PERMISSION_PREFIX.concat(action), role.getName());
82            }
83          }
84        }
85      }
86  
87      if (query.getDescription() != null) {
88        and(SeriesIndexSchema.DESCRIPTION, query.getDescription());
89      }
90  
91      if (query.getSubjects().length > 0) {
92        and(SeriesIndexSchema.SUBJECT, query.getSubjects());
93      }
94  
95      if (query.getLanguage() != null) {
96        and(SeriesIndexSchema.LANGUAGE, query.getLanguage());
97      }
98  
99      if (query.getCreator() != null) {
100       and(SeriesIndexSchema.CREATOR, query.getCreator());
101     }
102 
103     if (query.getLicense() != null) {
104       and(SeriesIndexSchema.LICENSE, query.getLicense());
105     }
106 
107     if (query.getAccessPolicy() != null) {
108       and(SeriesIndexSchema.ACCESS_POLICY, query.getAccessPolicy());
109     }
110 
111     if (query.getCreatedFrom() != null && query.getCreatedTo() != null) {
112       and(SeriesIndexSchema.CREATED_DATE_TIME, query.getCreatedFrom(), query.getCreatedTo());
113     }
114 
115     if (query.getOrganizers().length > 0) {
116       and(SeriesIndexSchema.ORGANIZERS, query.getOrganizers());
117     }
118 
119     if (query.getContributors().length > 0) {
120       and(SeriesIndexSchema.CONTRIBUTORS, query.getContributors());
121     }
122 
123     if (query.getPublishers().length > 0) {
124       and(SeriesIndexSchema.PUBLISHERS, query.getPublishers());
125     }
126 
127     if (query.getManagedAcl() != null) {
128       and(SeriesIndexSchema.MANAGED_ACL, query.getManagedAcl());
129     }
130 
131     if (query.getRightsHolder() != null) {
132       and(SeriesIndexSchema.RIGHTS_HOLDER, query.getRightsHolder());
133     }
134 
135     if (query.getTheme() != null) {
136       and(SeriesIndexSchema.THEME, query.getTheme());
137     }
138 
139     if (query.getCreatedFrom() != null && query.getCreatedTo() != null) {
140       and(SeriesIndexSchema.CREATED_DATE_TIME, query.getCreatedFrom(), query.getCreatedTo());
141     }
142 
143     // Text
144     if (query.getTerms() != null) {
145       for (SearchTerms<String> terms : query.getTerms()) {
146         StringBuffer queryText = new StringBuffer();
147         for (String term : terms.getTerms()) {
148           if (queryText.length() > 0) {
149             queryText.append(" ");
150           }
151           queryText.append(term);
152         }
153 
154         additionalMultiQueryFields.add(SeriesIndexSchema.UID);
155 
156         fuzzy = query.isFuzzySearch();
157         this.text = queryText.toString();
158 
159         if (Quantifier.All.equals(terms.getQuantifier())) {
160           if (groups == null) {
161             groups = new ArrayList<ValueGroup>();
162           }
163           if (query.isFuzzySearch()) {
164             logger.warn("All quantifier not supported in conjunction with wildcard text");
165           }
166           groups.add(new ValueGroup(IndexSchema.TEXT, (Object[]) terms.getTerms().toArray(new String[terms.size()])));
167         }
168       }
169     }
170 
171     // Filter query
172     if (query.getFilter() != null) {
173       this.filter = query.getFilter();
174     }
175 
176   }
177 
178 }