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.theme;
23  
24  import org.opencastproject.elasticsearch.api.SearchTerms;
25  import org.opencastproject.elasticsearch.api.SearchTerms.Quantifier;
26  import org.opencastproject.elasticsearch.impl.AbstractElasticsearchQueryBuilder;
27  import org.opencastproject.elasticsearch.impl.IndexSchema;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.util.ArrayList;
33  
34  /**
35   * Opencast {@link ThemeSearchQuery} implementation of the Elasticsearch query builder.
36   */
37  public class ThemeQueryBuilder extends AbstractElasticsearchQueryBuilder<ThemeSearchQuery> {
38  
39    /** The logging facility */
40    private static final Logger logger = LoggerFactory.getLogger(ThemeQueryBuilder.class);
41  
42    /**
43     * Creates a new search query based on the theme query.
44     *
45     * @param query
46     *          the theme query
47     */
48    public ThemeQueryBuilder(ThemeSearchQuery query) {
49      super(query);
50    }
51  
52    @Override
53    public void buildQuery(ThemeSearchQuery query) {
54      // Organization
55      if (query.getOrganization() == null) {
56        throw new IllegalStateException("No organization set on the theme search query!");
57      }
58  
59      and(ThemeIndexSchema.ORGANIZATION, query.getOrganization());
60  
61      // theme identifier
62      if (query.getIdentifiers().length > 0) {
63        and(ThemeIndexSchema.ID, query.getIdentifiers());
64      }
65  
66      if (query.getCreator() != null) {
67        and(ThemeIndexSchema.CREATOR, query.getCreator());
68      }
69  
70      if (query.getCreatedFrom() != null && query.getCreatedTo() != null) {
71        and(ThemeIndexSchema.CREATION_DATE, query.getCreatedFrom(), query.getCreatedTo());
72      }
73  
74      if (query.getIsDefault() != null) {
75        and(ThemeIndexSchema.DEFAULT, query.getIsDefault());
76      }
77  
78      if (query.getDescription() != null) {
79        and(ThemeIndexSchema.DESCRIPTION, query.getDescription());
80      }
81  
82      if (query.getName() != null) {
83        and(ThemeIndexSchema.NAME, query.getName());
84      }
85  
86      if (query.getBumperActive() != null) {
87        and(ThemeIndexSchema.BUMPER_ACTIVE, query.getBumperActive());
88      }
89  
90      if (query.getBumperFile() != null) {
91        and(ThemeIndexSchema.BUMPER_FILE, query.getBumperFile());
92      }
93  
94      if (query.getLicenseSlideActive() != null) {
95        and(ThemeIndexSchema.LICENSE_SLIDE_ACTIVE, query.getLicenseSlideActive());
96      }
97  
98      if (query.getLicenseSlideBackground() != null) {
99        and(ThemeIndexSchema.LICENSE_SLIDE_BACKGROUND, query.getLicenseSlideBackground());
100     }
101 
102     if (query.getLicenseSlideDescription() != null) {
103       and(ThemeIndexSchema.LICENSE_SLIDE_DESCRIPTION, query.getLicenseSlideDescription());
104     }
105 
106     if (query.getTrailerActive() != null) {
107       and(ThemeIndexSchema.TRAILER_ACTIVE, query.getTrailerActive());
108     }
109 
110     if (query.getTrailerFile() != null) {
111       and(ThemeIndexSchema.TRAILER_FILE, query.getTrailerFile());
112     }
113 
114     if (query.getTitleSlideActive() != null) {
115       and(ThemeIndexSchema.TITLE_SLIDE_ACTIVE, query.getTitleSlideActive());
116     }
117 
118     if (query.getTitleSlideBackground() != null) {
119       and(ThemeIndexSchema.TITLE_SLIDE_BACKGROUND, query.getTitleSlideBackground());
120     }
121 
122     if (query.getTitleSlideMetadata() != null) {
123       and(ThemeIndexSchema.TITLE_SLIDE_METADATA, query.getTitleSlideMetadata());
124     }
125 
126     if (query.getWatermarkActive() != null) {
127       and(ThemeIndexSchema.WATERMARK_ACTIVE, query.getWatermarkActive());
128     }
129 
130     if (query.getWatermarkFile() != null) {
131       and(ThemeIndexSchema.WATERMARK_FILE, query.getWatermarkFile());
132     }
133 
134     if (query.getWatermarkPosition() != null) {
135       and(ThemeIndexSchema.WATERMARK_POSITION, query.getWatermarkPosition());
136     }
137 
138     // Text
139     if (query.getTerms() != null) {
140       for (SearchTerms<String> terms : query.getTerms()) {
141         StringBuilder queryText = new StringBuilder();
142         for (String term : terms.getTerms()) {
143           if (queryText.length() > 0) {
144             queryText.append(" ");
145           }
146           queryText.append(term);
147         }
148         if (query.isFuzzySearch()) {
149           fuzzyText = queryText.toString();
150         } else {
151           this.text = queryText.toString();
152         }
153         if (Quantifier.All.equals(terms.getQuantifier())) {
154           if (groups == null) {
155             groups = new ArrayList<ValueGroup>();
156           }
157           if (query.isFuzzySearch()) {
158             logger.warn("All quantifier not supported in conjunction with wildcard text");
159           }
160           groups.add(new ValueGroup(IndexSchema.TEXT, (Object[]) terms.getTerms().toArray(new String[terms.size()])));
161         }
162       }
163     }
164 
165     // Filter query
166     if (query.getFilter() != null) {
167       this.filter = query.getFilter();
168     }
169 
170   }
171 
172 }