1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.opencastproject.elasticsearch.index.objects.series;
22
23 import static org.opencastproject.security.api.SecurityConstants.GLOBAL_ADMIN_ROLE;
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.Permissions;
29 import org.opencastproject.security.api.Permissions.Action;
30 import org.opencastproject.security.api.User;
31 import org.opencastproject.util.requests.SortCriterion.Order;
32
33 import org.apache.commons.lang3.StringUtils;
34
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42
43
44
45 public class SeriesSearchQuery extends AbstractSearchQuery {
46
47 protected List<String> identifiers = new ArrayList<String>();
48 private String title = null;
49 private User user = null;
50 private String description = null;
51 private final Set<String> actions = new HashSet<String>();
52 private final List<String> subjects = new ArrayList<String>();
53 private String organization = null;
54 private String language = null;
55 private String creator = null;
56 private String license = null;
57 private String accessPolicy = null;
58 private String managedAcl = null;
59 private final List<String> organizers = new ArrayList<String>();
60 private final List<String> contributors = new ArrayList<String>();
61 private final List<String> publishers = new ArrayList<String>();
62 private Date createdFrom = null;
63 private Date createdTo = null;
64 private boolean editOnly = false;
65 private String rightsHolder = null;
66 private Long theme = null;
67
68 private static final Map<String, String> SORT_FIELDS = Map.of(
69 SeriesIndexSchema.TITLE, SeriesIndexSchema.TITLE.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
70 SeriesIndexSchema.DESCRIPTION, SeriesIndexSchema.DESCRIPTION.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
71 SeriesIndexSchema.SUBJECT, SeriesIndexSchema.SUBJECT.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
72 SeriesIndexSchema.CREATOR, SeriesIndexSchema.CREATOR.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
73 SeriesIndexSchema.ORGANIZERS, SeriesIndexSchema.ORGANIZERS.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
74 SeriesIndexSchema.CONTRIBUTORS, SeriesIndexSchema.CONTRIBUTORS.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
75 SeriesIndexSchema.PUBLISHERS, SeriesIndexSchema.PUBLISHERS.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION),
76 SeriesIndexSchema.RIGHTS_HOLDER, SeriesIndexSchema.RIGHTS_HOLDER.concat(IndexSchema.SORT_FIELD_NAME_EXTENSION)
77 );
78
79 @SuppressWarnings("unused")
80 private SeriesSearchQuery() {
81 }
82
83 @Override
84 protected String sortOrderFieldName(String field) {
85 if (SORT_FIELDS.containsKey(field)) {
86 return SORT_FIELDS.get(field);
87 }
88 return field;
89 }
90
91
92
93
94 public SeriesSearchQuery(String organization, User user) {
95 super(Series.DOCUMENT_TYPE);
96 this.organization = organization;
97 this.user = user;
98 this.actions.add(Permissions.Action.READ.toString());
99 if (!user.hasRole(GLOBAL_ADMIN_ROLE)) {
100 if (!user.getOrganization().getId().equals(organization)) {
101 throw new IllegalStateException("User's organization must match search organization");
102 }
103 }
104 }
105
106
107
108
109
110
111
112
113
114
115 public SeriesSearchQuery withIdentifier(String id) {
116 if (StringUtils.isBlank(id)) {
117 throw new IllegalArgumentException("Identifier cannot be null");
118 }
119 this.identifiers.add(id);
120 return this;
121 }
122
123
124
125
126
127
128 public String[] getIdentifier() {
129 return identifiers.toArray(new String[identifiers.size()]);
130 }
131
132
133
134
135
136
137
138
139 public SeriesSearchQuery withTitle(String title) {
140 this.title = title;
141 return this;
142 }
143
144
145
146
147
148
149 public String getTitle() {
150 return title;
151 }
152
153
154
155
156
157
158 public SeriesSearchQuery withoutActions() {
159 this.actions.clear();
160 return this;
161 }
162
163
164
165
166
167
168
169
170
171
172 public SeriesSearchQuery withAction(Action action) {
173 if (action == null) {
174 throw new IllegalArgumentException("Action cannot be null");
175 }
176 this.actions.add(action.toString());
177 return this;
178 }
179
180
181
182
183
184
185 public String[] getActions() {
186 return actions.toArray(new String[actions.size()]);
187 }
188
189
190
191
192
193
194
195
196 public SeriesSearchQuery withDescription(String description) {
197 this.description = description;
198 return this;
199 }
200
201
202
203
204
205
206 public String getDescription() {
207 return description;
208 }
209
210
211
212
213
214
215
216
217
218
219 public SeriesSearchQuery withSubject(String subject) {
220 if (StringUtils.isBlank(subject)) {
221 throw new IllegalArgumentException("Subject cannot be null");
222 }
223 this.subjects.add(subject);
224 return this;
225 }
226
227
228
229
230
231
232 public String[] getSubjects() {
233 return subjects.toArray(new String[subjects.size()]);
234 }
235
236
237
238
239
240
241 public String getOrganization() {
242 return organization;
243 }
244
245
246
247
248
249
250 public User getUser() {
251 return user;
252 }
253
254
255
256
257
258
259
260
261 public SeriesSearchQuery withLanguage(String language) {
262 this.language = language;
263 return this;
264 }
265
266
267
268
269
270
271 public String getLanguage() {
272 return language;
273 }
274
275
276
277
278
279
280
281
282 public SeriesSearchQuery withCreator(String creator) {
283 this.creator = creator;
284 return this;
285 }
286
287
288
289
290
291
292 public String getCreator() {
293 return creator;
294 }
295
296
297
298
299
300
301
302
303 public SeriesSearchQuery withLicense(String license) {
304 this.license = license;
305 return this;
306 }
307
308
309
310
311
312
313 public String getLicense() {
314 return license;
315 }
316
317
318
319
320
321
322
323
324 public SeriesSearchQuery withAccessPolicy(String accessPolicy) {
325 this.accessPolicy = accessPolicy;
326 return this;
327 }
328
329
330
331
332
333
334 public String getAccessPolicy() {
335 return accessPolicy;
336 }
337
338
339
340
341
342
343
344
345 public SeriesSearchQuery withTheme(long theme) {
346 this.theme = theme;
347 return this;
348 }
349
350
351
352
353
354
355 public Long getTheme() {
356 return theme;
357 }
358
359
360
361
362
363
364
365
366 public SeriesSearchQuery withManagedAcl(String managedAcl) {
367 this.managedAcl = managedAcl;
368 return this;
369 }
370
371
372
373
374
375
376 public String getManagedAcl() {
377 return managedAcl;
378 }
379
380
381
382
383
384
385
386
387
388
389 public SeriesSearchQuery withOrganizer(String organizer) {
390 if (StringUtils.isBlank(organizer)) {
391 throw new IllegalArgumentException("Organizer cannot be null");
392 }
393 this.organizers.add(organizer);
394 return this;
395 }
396
397
398
399
400
401
402 public String[] getOrganizers() {
403 return organizers.toArray(new String[organizers.size()]);
404 }
405
406
407
408
409
410
411
412
413
414
415 public SeriesSearchQuery withContributor(String contributor) {
416 if (StringUtils.isBlank(contributor)) {
417 throw new IllegalArgumentException("Contributor can't be null");
418 }
419 this.contributors.add(contributor);
420 return this;
421 }
422
423
424
425
426
427
428 public String[] getContributors() {
429 return contributors.toArray(new String[contributors.size()]);
430 }
431
432
433
434
435
436
437
438
439 public SeriesSearchQuery withPublisher(String publisher) {
440 if (StringUtils.isBlank(publisher)) {
441 throw new IllegalArgumentException("Publisher can't be null");
442 }
443 this.publishers.add(publisher);
444 return this;
445 }
446
447
448
449
450
451
452 public String[] getPublishers() {
453 return publishers.toArray(new String[publishers.size()]);
454 }
455
456
457
458
459
460
461
462
463 public SeriesSearchQuery withCreatedFrom(Date createdFrom) {
464 this.createdFrom = createdFrom;
465 return this;
466 }
467
468
469
470
471 public Date getCreatedFrom() {
472 return createdFrom;
473 }
474
475
476
477
478
479
480
481
482 public SeriesSearchQuery withCreatedTo(Date createdTo) {
483 this.createdTo = createdTo;
484 return this;
485 }
486
487
488
489
490 public Date getCreatedTo() {
491 return createdTo;
492 }
493
494
495
496
497
498
499 public SeriesSearchQuery withEdit(Boolean edit) {
500 this.editOnly = edit;
501 return this;
502 }
503
504
505
506
507 public boolean isEditOnly() {
508 return editOnly;
509 }
510
511
512
513
514
515
516 public SeriesSearchQuery withRightsHolder(String rightsHolder) {
517 this.rightsHolder = rightsHolder;
518 return this;
519 }
520
521
522
523
524 public String getRightsHolder() {
525 return rightsHolder;
526 }
527
528
529
530
531
532
533
534
535 public SeriesSearchQuery sortByIdentifer(Order order) {
536 withSortOrder(SeriesIndexSchema.UID, order);
537 return this;
538 }
539
540
541
542
543
544
545 public Order getSeriesIdentifierSortOrder() {
546 return getSortOrder(SeriesIndexSchema.UID);
547 }
548
549
550
551
552
553
554
555
556 public SeriesSearchQuery sortBySubject(Order order) {
557 withSortOrder(SeriesIndexSchema.SUBJECT, order);
558 return this;
559 }
560
561
562
563
564
565
566 public Order getSeriesSubjectSortOrder() {
567 return getSortOrder(SeriesIndexSchema.SUBJECT);
568 }
569
570
571
572
573
574
575
576
577 public SeriesSearchQuery sortByCreator(Order order) {
578 withSortOrder(SeriesIndexSchema.CREATOR, order);
579 return this;
580 }
581
582
583
584
585
586
587 public Order getSeriesCreatorSortOrder() {
588 return getSortOrder(SeriesIndexSchema.CREATOR);
589 }
590
591
592
593
594
595
596
597
598 public SeriesSearchQuery sortByPublishers(Order order) {
599 withSortOrder(SeriesIndexSchema.PUBLISHERS, order);
600 return this;
601 }
602
603
604
605
606
607
608 public Order getSeriesPublishersSortOrder() {
609 return getSortOrder(SeriesIndexSchema.PUBLISHERS);
610 }
611
612
613
614
615
616
617
618
619 public SeriesSearchQuery sortByDescription(Order order) {
620 withSortOrder(SeriesIndexSchema.DESCRIPTION, order);
621 return this;
622 }
623
624
625
626
627
628
629 public Order getSeriesDescriptionSortOrder() {
630 return getSortOrder(SeriesIndexSchema.DESCRIPTION);
631 }
632
633
634
635
636
637
638
639
640 public SeriesSearchQuery sortByLanguage(Order order) {
641 withSortOrder(SeriesIndexSchema.LANGUAGE, order);
642 return this;
643 }
644
645
646
647
648
649
650 public Order getSeriesLanguageSortOrder() {
651 return getSortOrder(SeriesIndexSchema.LANGUAGE);
652 }
653
654
655
656
657
658
659
660
661 public SeriesSearchQuery sortByRightsHolder(Order order) {
662 withSortOrder(SeriesIndexSchema.RIGHTS_HOLDER, order);
663 return this;
664 }
665
666
667
668
669
670
671 public Order getSeriesRightsHolderSortOrder() {
672 return getSortOrder(SeriesIndexSchema.RIGHTS_HOLDER);
673 }
674
675
676
677
678
679
680
681
682 public SeriesSearchQuery sortByLicense(Order order) {
683 withSortOrder(SeriesIndexSchema.LICENSE, order);
684 return this;
685 }
686
687
688
689
690
691
692 public Order getSeriesLicenseSortOrder() {
693 return getSortOrder(SeriesIndexSchema.LICENSE);
694 }
695
696
697
698
699
700
701
702
703 public SeriesSearchQuery sortByContributors(Order order) {
704 withSortOrder(SeriesIndexSchema.CONTRIBUTORS, order);
705 return this;
706 }
707
708
709
710
711
712
713 public Order getSeriesContributorsSortOrder() {
714 return getSortOrder(SeriesIndexSchema.CONTRIBUTORS);
715 }
716
717
718
719
720
721
722
723
724 public SeriesSearchQuery sortByManagedAcl(Order order) {
725 withSortOrder(SeriesIndexSchema.MANAGED_ACL, order);
726 return this;
727 }
728
729
730
731
732
733
734 public Order getSeriesManagedAclSortOrder() {
735 return getSortOrder(SeriesIndexSchema.MANAGED_ACL);
736 }
737
738
739
740
741
742
743
744
745 public SeriesSearchQuery sortByCreatedDateTime(Order order) {
746 withSortOrder(SeriesIndexSchema.CREATED_DATE_TIME, order);
747 return this;
748 }
749
750
751
752
753
754
755 public Order getSeriesDateSortOrder() {
756 return getSortOrder(SeriesIndexSchema.CREATED_DATE_TIME);
757 }
758
759
760
761
762
763
764
765
766 public SeriesSearchQuery sortByOrganizers(Order order) {
767 withSortOrder(SeriesIndexSchema.ORGANIZERS, order);
768 return this;
769 }
770
771
772
773
774
775
776 public Order getSeriesOrganizersSortOrder() {
777 return getSortOrder(SeriesIndexSchema.ORGANIZERS);
778 }
779
780
781
782
783
784
785
786
787 public SeriesSearchQuery sortByTitle(Order order) {
788 withSortOrder(SeriesIndexSchema.TITLE, order);
789 return this;
790 }
791
792
793
794
795
796
797 public Order getSeriesTitleSortOrder() {
798 return getSortOrder(SeriesIndexSchema.TITLE);
799 }
800
801 @Override
802 public String toString() {
803 StringBuilder sb = new StringBuilder();
804 sb.append(SeriesSearchQuery.class.getSimpleName() + " ");
805 if (identifiers.size() > 0) {
806 sb.append("ids:'" + identifiers.toString() + "' ");
807 }
808 if (StringUtils.trimToNull(title) != null) {
809 sb.append("title:'" + title + "' ");
810 }
811 if (StringUtils.trimToNull(description) != null) {
812 sb.append("description:'" + description + "' ");
813 }
814 if (subjects.size() > 0) {
815 sb.append("subjects:'" + subjects.toString() + "' ");
816 }
817 if (StringUtils.trimToNull(organization) != null) {
818 sb.append("organization:'" + organization + "' ");
819 }
820 if (StringUtils.trimToNull(language) != null) {
821 sb.append("language:'" + language + "' ");
822 }
823 if (StringUtils.trimToNull(creator) != null) {
824 sb.append("creator:'" + creator + "' ");
825 }
826 if (StringUtils.trimToNull(license) != null) {
827 sb.append("license:'" + license + "' ");
828 }
829 if (StringUtils.trimToNull(accessPolicy) != null) {
830 sb.append("ACL:'" + accessPolicy + "' ");
831 }
832
833 if (createdFrom != null) {
834 sb.append("Created From:'" + createdFrom + "' ");
835 }
836
837 if (createdTo != null) {
838 sb.append("Created To:'" + createdTo + "' ");
839 }
840
841 if (organizers.size() > 0) {
842 sb.append("organizers:'" + organizers.toString() + "' ");
843 }
844
845 if (contributors.size() > 0) {
846 sb.append("contributors:'" + contributors.toString() + "' ");
847 }
848
849 if (publishers.size() > 0) {
850 sb.append("publishers:'" + publishers.toString() + "' ");
851 }
852
853 if (theme != null) {
854 sb.append("Theme:'" + theme + "' ");
855 }
856
857 sb.append("Edit:'" + editOnly + "' ");
858
859 if (getTerms().size() > 0) {
860 sb.append("Text:");
861 for (SearchTerms<String> searchTerm : getTerms()) {
862 sb.append("'" + searchTerm.getTerms() + "' ");
863 }
864 }
865
866 if (StringUtils.trimToNull(rightsHolder) != null) {
867 sb.append("Rights Holder:'" + rightsHolder + "' ");
868 }
869
870 return sb.toString();
871
872 }
873
874 }