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  
23  package org.opencastproject.elasticsearch.index.objects.theme;
24  
25  import org.opencastproject.elasticsearch.api.SearchTerms;
26  import org.opencastproject.elasticsearch.impl.AbstractSearchQuery;
27  import org.opencastproject.elasticsearch.impl.IndexSchema;
28  import org.opencastproject.security.api.User;
29  import org.opencastproject.util.requests.SortCriterion.Order;
30  
31  import org.apache.commons.lang3.StringUtils;
32  
33  import java.util.ArrayList;
34  import java.util.Date;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * This interface defines a fluent api for a query object used to lookup themes in the search index.
40   */
41  public class ThemeSearchQuery extends AbstractSearchQuery {
42  
43    protected List<Long> identifiers = new ArrayList<Long>();
44    private User user = null;
45    private String organization = null;
46    private String creator = null;
47    private Date createdFrom = null;
48    private Date createdTo = null;
49    private Boolean isDefault = null;
50    private String description = null;
51    private String name = null;
52    private Boolean bumperActive = null;
53    private String bumperFile = null;
54    private Boolean licenseSlideActive = null;
55    private String licenseSlideBackground = null;
56    private String licenseSlideDescription = null;
57    private Boolean trailerActive = null;
58    private String trailerFile = null;
59    private Boolean titleSlideActive = null;
60    private String titleSlideBackground = null;
61    private String titleSlideMetadata = null;
62    private Boolean watermarkActive = null;
63    private String watermarkFile = null;
64    private String watermarkPosition = null;
65  
66    private static final Map<String, String> SORT_FIELDS = Map.of(
67        ThemeIndexSchema.CREATOR, ThemeIndexSchema.CREATOR.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
68        ThemeIndexSchema.NAME, ThemeIndexSchema.NAME.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
69        ThemeIndexSchema.DESCRIPTION, ThemeIndexSchema.DESCRIPTION.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION)
70    );
71  
72    @SuppressWarnings("unused")
73    private ThemeSearchQuery() {
74    }
75  
76    @Override
77    protected String sortOrderFieldName(String field) {
78      if (SORT_FIELDS.containsKey(field)) {
79        return SORT_FIELDS.get(field);
80      }
81      return field;
82    }
83  
84    /**
85     * Creates a query that will return theme documents. The user's organization must match the query's organization.
86     *
87     * @param organization
88     *          The organization to run this query with. Cannot be null.
89     * @param user
90     *          The user to run this query as. Cannot be null.
91     * @throws IllegalStateException
92     *           Thrown if the current user's organization doesn't match the search organization, the organization is
93     *           null, or user is null.
94     */
95    public ThemeSearchQuery(String organization, User user) {
96      super(IndexTheme.DOCUMENT_TYPE);
97  
98      if (organization == null) {
99        throw new IllegalStateException("The organization for this query was null.");
100     }
101 
102     if (user == null) {
103       throw new IllegalStateException("The user for this query was null.");
104     }
105 
106     this.organization = organization;
107     this.user = user;
108     if (!user.getOrganization().getId().equals(organization)) {
109       throw new IllegalStateException("User's organization must match search organization");
110     }
111   }
112 
113   /**
114    * Selects themes with the given identifier.
115    * <p>
116    * Note that this method may be called multiple times to support selection of multiple themes.
117    *
118    * @param id
119    *          the theme identifier
120    * @return the enhanced search query
121    */
122   public ThemeSearchQuery withIdentifier(long id) {
123     this.identifiers.add(id);
124     return this;
125   }
126 
127   /**
128    * Returns the list of theme identifiers or an empty array if no identifiers have been specified.
129    *
130    * @return the identifiers
131    */
132   public Long[] getIdentifiers() {
133     return identifiers.toArray(new Long[identifiers.size()]);
134   }
135 
136   /**
137    * Selects themes with the given description.
138    *
139    * @param description
140    *          the description
141    * @return the enhanced search query
142    */
143   public ThemeSearchQuery withDescription(String description) {
144     this.description = description;
145     return this;
146   }
147 
148   /**
149    * Returns the description of the theme query.
150    *
151    * @return the description
152    */
153   public String getDescription() {
154     return description;
155   }
156 
157   /**
158    * Returns the organization of the theme query.
159    *
160    * @return the organization
161    */
162   public String getOrganization() {
163     return organization;
164   }
165 
166   /**
167    * Returns the user of this search query
168    *
169    * @return the user of this search query
170    */
171   public User getUser() {
172     return user;
173   }
174 
175   /**
176    * Selects themes with the given creator.
177    *
178    * @param creator
179    *          the creator
180    * @return the enhanced search query
181    */
182   public ThemeSearchQuery withCreator(String creator) {
183     this.creator = creator;
184     return this;
185   }
186 
187   /**
188    * Returns the creator of the themes.
189    *
190    * @return the creator
191    */
192   public String getCreator() {
193     return creator;
194   }
195 
196   /**
197    * The created date to start looking for themes.
198    *
199    * @param createdFrom
200    *          The created date to start looking for themes
201    * @return the enhanced search query
202    */
203   public ThemeSearchQuery withCreatedFrom(Date createdFrom) {
204     this.createdFrom = createdFrom;
205     return this;
206   }
207 
208   /**
209    * @return The Date after which all series returned should have been created
210    */
211   public Date getCreatedFrom() {
212     return createdFrom;
213   }
214 
215   /**
216    * The created date to stop looking for series.
217    *
218    * @param createdTo
219    *          The created date to stop looking for series
220    * @return the enhanced search query
221    */
222   public ThemeSearchQuery withCreatedTo(Date createdTo) {
223     this.createdTo = createdTo;
224     return this;
225   }
226 
227   /**
228    * @return The Date before which all series returned should have been created
229    */
230   public Date getCreatedTo() {
231     return createdTo;
232   }
233 
234   /**
235    * Selects themes that are default.
236    *
237    * @param isDefault
238    *          Whether to search for themes that are default or not.
239    * @return the enhanced search query
240    */
241   public ThemeSearchQuery withIsDefault(Boolean isDefault) {
242     this.isDefault = isDefault;
243     return this;
244   }
245 
246   /**
247    * Returns whether the theme query is searching for default themes.
248    *
249    * @return whether the search is looking for default themes.
250    */
251   public Boolean getIsDefault() {
252     return isDefault;
253   }
254 
255   /**
256    * Selects themes with the given name.
257    *
258    * @param name
259    *          the name
260    * @return the enhanced search query
261    */
262   public ThemeSearchQuery withName(String name) {
263     this.name = name;
264     return this;
265   }
266 
267   /**
268    * Returns the name looked for by the theme query.
269    *
270    * @return the name
271    */
272   public String getName() {
273     return name;
274   }
275 
276   /**
277    * Selects themes with where the bumper is active.
278    *
279    * @param bumperActive
280    *          Whether the bumperActive
281    * @return the enhanced search query
282    */
283   public ThemeSearchQuery withBumperActive(Boolean bumperActive) {
284     this.bumperActive = bumperActive;
285     return this;
286   }
287 
288   /**
289    * Returns whether the theme query is searching for themes where the bumper is active.
290    *
291    * @return the bumperActive
292    */
293   public Boolean getBumperActive() {
294     return bumperActive;
295   }
296 
297   /**
298    * Selects themes with the given bumper file id.
299    *
300    * @param bumperFile
301    *          the bumper file id
302    * @return the enhanced search query
303    */
304   public ThemeSearchQuery withBumperFile(String bumperFile) {
305     this.bumperFile = bumperFile;
306     return this;
307   }
308 
309   /**
310    * Returns the bumperFile of the theme query.
311    *
312    * @return the bumperFile
313    */
314   public String getBumperFile() {
315     return bumperFile;
316   }
317 
318   /**
319    * Selects themes where the license slide is active.
320    *
321    * @param licenseSlideActive
322    *          If the license slide is active or not.
323    * @return the enhanced search query
324    */
325   public ThemeSearchQuery withLicenseSlideActive(Boolean licenseSlideActive) {
326     this.licenseSlideActive = licenseSlideActive;
327     return this;
328   }
329 
330   /**
331    * Returns whether the theme query is looking for themes where the license slide is active.
332    *
333    * @return the license
334    */
335   public Boolean getLicenseSlideActive() {
336     return licenseSlideActive;
337   }
338 
339   /**
340    * Selects themes with the given license slide background.
341    *
342    * @param licenseSlideBackground
343    *          the license slide background file
344    * @return the enhanced search query
345    */
346   public ThemeSearchQuery withLicenseSlideBackground(String licenseSlideBackground) {
347     this.licenseSlideBackground = licenseSlideBackground;
348     return this;
349   }
350 
351   /**
352    * Returns the license slide background this query is looking for.
353    *
354    * @return the licenseSlideBackground
355    */
356   public String getLicenseSlideBackground() {
357     return licenseSlideBackground;
358   }
359 
360   /**
361    * Selects themes with the given license slide description.
362    *
363    * @param licenseSlideDescription
364    *          the license slide description
365    * @return the enhanced search query
366    */
367   public ThemeSearchQuery withLicenseSlideDescription(String licenseSlideDescription) {
368     this.licenseSlideDescription = licenseSlideDescription;
369     return this;
370   }
371 
372   /**
373    * Returns the license slide description this query is looking for.
374    *
375    * @return the license slide description
376    */
377   public String getLicenseSlideDescription() {
378     return licenseSlideDescription;
379   }
380 
381   /**
382    * Selects themes that have the trailer active
383    *
384    * @param trailerActive
385    *          whether the trailer is active or not
386    * @return the enhanced search query
387    */
388   public ThemeSearchQuery withTrailerActive(Boolean trailerActive) {
389     this.trailerActive = trailerActive;
390     return this;
391   }
392 
393   /**
394    * Returns whether the query is looking for themes where the trailer is active
395    *
396    * @return Whether the query is looking for themes where the trailer is active
397    */
398   public Boolean getTrailerActive() {
399     return trailerActive;
400   }
401 
402   /**
403    * Selects themes with the given trailer file
404    *
405    * @param trailerFile
406    *          the trailer file that should be in the themes
407    * @return the enhanced search query
408    */
409   public ThemeSearchQuery withTrailerFile(String trailerFile) {
410     this.trailerFile = trailerFile;
411     return this;
412   }
413 
414   /**
415    * Returns the trailer file id that is being matched in this query
416    *
417    * @return the trailer file id
418    */
419   public String getTrailerFile() {
420     return trailerFile;
421   }
422 
423   /**
424    * Selects themes where the title slide is active.
425    *
426    * @param titleSlideActive
427    *          Whether to search for themes where the title slide is active
428    * @return the enhanced search query
429    */
430   public ThemeSearchQuery withTitleSlideActive(Boolean titleSlideActive) {
431     this.titleSlideActive = titleSlideActive;
432     return this;
433   }
434 
435   /**
436    * Returns whether this query is searching for themes where the title slide is active.
437    *
438    * @return Whether the query is looking for themes where the title slide is active
439    */
440   public Boolean getTitleSlideActive() {
441     return titleSlideActive;
442   }
443 
444   /**
445    * Selects themes with matching title slide metadata
446    *
447    * @param titleSlideMetadata
448    *          Search themes for this title slide metadata
449    * @return the enhanced search query
450    */
451   public ThemeSearchQuery withTitleSlideMetadata(String titleSlideMetadata) {
452     this.titleSlideMetadata = titleSlideMetadata;
453     return this;
454   }
455 
456   /**
457    * Returns the title slide metadata this query is searching for
458    *
459    * @return the title slide metadata being searched for
460    */
461   public String getTitleSlideMetadata() {
462     return titleSlideMetadata;
463   }
464 
465   /**
466    * Selects themes with the given title slide background id.
467    *
468    * @param titleSlideBackground
469    *          the id for the title slide background file.
470    * @return the enhanced search query
471    */
472   public ThemeSearchQuery withTitleSlideBackground(String titleSlideBackground) {
473     this.titleSlideBackground = titleSlideBackground;
474     return this;
475   }
476 
477   /**
478    * @return Returns the title slide background id this query is searching for.
479    */
480   public String getTitleSlideBackground() {
481     return titleSlideBackground;
482   }
483 
484   /**
485    * Selects themes where a watermark is active
486    *
487    * @param watermarkActive
488    *          Whether to search for themes where the watermark is active
489    * @return the enhanced search query
490    */
491   public ThemeSearchQuery withWatermarkActive(Boolean watermarkActive) {
492     this.watermarkActive = watermarkActive;
493     return this;
494   }
495 
496   /**
497    * @return Returns whether this query is searching for themes where the watermark is active.
498    */
499   public Boolean getWatermarkActive() {
500     return watermarkActive;
501   }
502 
503   /**
504    * Selects themes with the given watermark file
505    *
506    * @param watermarkFile
507    *          the id of the watermark file
508    * @return the enhanced search query
509    */
510   public ThemeSearchQuery withWatermarkFile(String watermarkFile) {
511     this.watermarkFile = watermarkFile;
512     return this;
513   }
514 
515   /**
516    * @return Returns the watermark file id this query is searching for.
517    */
518   public String getWatermarkFile() {
519     return watermarkFile;
520   }
521 
522   /**
523    * Selects themes with the given watermark position.
524    *
525    * @param watermarkPosition
526    *          the watermark position to select
527    * @return the enhanced search query
528    */
529   public ThemeSearchQuery withLicense(String watermarkPosition) {
530     this.watermarkPosition = watermarkPosition;
531     return this;
532   }
533 
534   /**
535    * @return Returns the watermark position this query is searching for.
536    */
537   public String getWatermarkPosition() {
538     return watermarkPosition;
539   }
540 
541   /**
542    * Defines the sort order for the theme by creator username.
543    *
544    * @param order
545    *          the order
546    * @return the enhanced search query
547    */
548   public ThemeSearchQuery sortByCreator(Order order) {
549     withSortOrder(ThemeIndexSchema.CREATOR, order);
550     return this;
551   }
552 
553   /**
554    * Returns the sort order for the theme creator username.
555    *
556    * @return the sort order
557    */
558   public Order getThemeCreatorSortOrder() {
559     return getSortOrder(ThemeIndexSchema.CREATOR);
560   }
561 
562   /**
563    * Defines the sort order for the theme created date &amp; time.
564    *
565    * @param order
566    *          the order
567    * @return the enhanced search query
568    */
569   public ThemeSearchQuery sortByCreatedDateTime(Order order) {
570     withSortOrder(ThemeIndexSchema.CREATION_DATE, order);
571     return this;
572   }
573 
574   /**
575    * Defines the sort order for the theme default property
576    *
577    * @param order
578    *          the order
579    * @return the enhanced search query
580    */
581   public ThemeSearchQuery sortByDefault(Order order) {
582     withSortOrder(ThemeIndexSchema.DEFAULT, order);
583     return this;
584   }
585 
586   /**
587    * Returns the sort order for the theme created date.
588    *
589    * @return the sort order
590    */
591   public Order getSeriesDateSortOrder() {
592     return getSortOrder(ThemeIndexSchema.CREATION_DATE);
593   }
594 
595   /**
596    * Defines the sort order for the theme by names.
597    *
598    * @param order
599    *          the order
600    * @return the enhanced search query
601    */
602   public ThemeSearchQuery sortByName(Order order) {
603     withSortOrder(ThemeIndexSchema.NAME, order);
604     return this;
605   }
606 
607   /**
608    * Returns the sort order for the theme name.
609    *
610    * @return the sort order
611    */
612   public Order getThemeNameSortOrder() {
613     return getSortOrder(ThemeIndexSchema.NAME);
614   }
615 
616   /**
617    * Defines the sort order for the theme by description.
618    *
619    * @param order
620    *          the order
621    * @return the enhanced search query
622    */
623   public ThemeSearchQuery sortByDescription(Order order) {
624     withSortOrder(ThemeIndexSchema.DESCRIPTION, order);
625     return this;
626   }
627 
628   /**
629    * Returns the sort order for the theme name.
630    *
631    * @return the sort order
632    */
633   public Order getThemeDescriptionSortOrder() {
634     return getSortOrder(ThemeIndexSchema.DESCRIPTION);
635   }
636 
637   @Override
638   public String toString() {
639     StringBuilder sb = new StringBuilder();
640     sb.append(ThemeSearchQuery.class.getSimpleName() + " ");
641 
642     if (identifiers.size() > 0) {
643       sb.append("ids:'" + identifiers.toString() + "' ");
644     }
645 
646     if (StringUtils.trimToNull(organization) != null) {
647       sb.append("organization:'" + organization + "' ");
648     }
649 
650     if (StringUtils.trimToNull(creator) != null) {
651       sb.append("creator:'" + creator + "' ");
652     }
653 
654     if (createdFrom != null) {
655       sb.append("Created From:'" + createdFrom + "' ");
656     }
657 
658     if (createdTo != null) {
659       sb.append("Created To:'" + createdTo + "' ");
660     }
661 
662     sb.append("Is Default:'" + isDefault + "' ");
663 
664     if (StringUtils.trimToNull(description) != null) {
665       sb.append("description:'" + description + "' ");
666     }
667 
668     if (StringUtils.trimToNull(name) != null) {
669       sb.append("name:'" + name + "' ");
670     }
671 
672     sb.append("bumper active:'" + bumperActive + "' ");
673 
674     if (StringUtils.trimToNull(bumperFile) != null) {
675       sb.append("bumper file:'" + bumperFile + "' ");
676     }
677 
678     sb.append("license slide active:'" + licenseSlideActive + "' ");
679 
680     if (StringUtils.trimToNull(licenseSlideBackground) != null) {
681       sb.append("license slide background:'" + licenseSlideBackground + "' ");
682     }
683 
684     if (StringUtils.trimToNull(licenseSlideDescription) != null) {
685       sb.append("license slide description:'" + licenseSlideDescription + "' ");
686     }
687 
688     sb.append("trailer active:'" + trailerActive + "' ");
689 
690     if (StringUtils.trimToNull(trailerFile) != null) {
691       sb.append("trailer file:'" + trailerFile + "' ");
692     }
693 
694     sb.append("title slide active:'" + titleSlideActive + "' ");
695 
696     if (StringUtils.trimToNull(titleSlideBackground) != null) {
697       sb.append("title slide background:'" + titleSlideBackground + "' ");
698     }
699 
700     if (StringUtils.trimToNull(titleSlideMetadata) != null) {
701       sb.append("title slide metadata:'" + titleSlideMetadata + "' ");
702     }
703 
704     sb.append("watermark active:'" + watermarkActive + "' ");
705 
706     if (StringUtils.trimToNull(watermarkFile) != null) {
707       sb.append("watermark file:'" + watermarkFile + "' ");
708     }
709 
710     if (StringUtils.trimToNull(watermarkPosition) != null) {
711       sb.append("watermark position:'" + watermarkPosition + "' ");
712     }
713 
714     if (getTerms().size() > 0) {
715       sb.append("Text:");
716       for (SearchTerms<String> searchTerm : getTerms()) {
717         sb.append("'" + searchTerm.getTerms() + "' ");
718       }
719     }
720     return sb.toString();
721   }
722 }