1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
40
41 public class EventQueryBuilder extends AbstractElasticsearchQueryBuilder<EventSearchQuery> {
42
43
44 private static final Logger logger = LoggerFactory.getLogger(EventQueryBuilder.class);
45
46
47
48
49
50
51
52 public EventQueryBuilder(EventSearchQuery query) {
53 super(query);
54 }
55
56 @Override
57 public void buildQuery(EventSearchQuery query) {
58
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
66 if (query.getIdentifier().length > 0) {
67 and(EventIndexSchema.UID, query.getIdentifier());
68 }
69
70
71 if (query.getTitle() != null) {
72 and(EventIndexSchema.TITLE, query.getTitle());
73 }
74
75
76 if (query.getDescription() != null) {
77 and(EventIndexSchema.DESCRIPTION, query.getDescription());
78 }
79
80
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
93 if (query.getPresenters() != null) {
94 for (String presenter : query.getPresenters()) {
95 and(EventIndexSchema.PRESENTER, presenter);
96 }
97 }
98
99
100 if (query.getContributors().length > 0) {
101 and(EventIndexSchema.CONTRIBUTOR, query.getContributors());
102 }
103
104
105 if (query.getSubject() != null) {
106 and(EventIndexSchema.SUBJECT, query.getSubject());
107 }
108
109
110 if (query.getLocation() != null) {
111 and(EventIndexSchema.LOCATION, query.getLocation());
112 }
113
114
115 if (query.getSeriesId() != null) {
116 and(EventIndexSchema.SERIES_ID, query.getSeriesId());
117 }
118
119
120 if (query.getSeriesName() != null) {
121 and(EventIndexSchema.SERIES_NAME, query.getSeriesName());
122 }
123
124
125 if (query.getLanguage() != null) {
126 and(EventIndexSchema.LANGUAGE, query.getLanguage());
127 }
128
129
130 if (query.getSource() != null) {
131 and(EventIndexSchema.SOURCE, query.getSource());
132 }
133
134
135 if (query.getCreated() != null) {
136 and(EventIndexSchema.CREATED, query.getCreated());
137 }
138
139
140 if (query.getCreator() != null) {
141 and(EventIndexSchema.CREATOR, query.getCreator());
142 }
143
144
145 if (query.getPublisher() != null) {
146 and(EventIndexSchema.PUBLISHER, query.getPublisher());
147 }
148
149
150 if (query.getLicense() != null) {
151 and(EventIndexSchema.LICENSE, query.getLicense());
152 }
153
154
155 if (query.getRights() != null) {
156 and(EventIndexSchema.RIGHTS, query.getRights());
157 }
158
159
160 if (query.getAccessPolicy() != null) {
161 and(EventIndexSchema.ACCESS_POLICY, query.getAccessPolicy());
162 }
163
164
165 if (query.getManagedAcl() != null) {
166 and(EventIndexSchema.MANAGED_ACL, query.getManagedAcl());
167 }
168
169
170 if (query.getWorkflowState() != null) {
171 and(EventIndexSchema.WORKFLOW_STATE, query.getWorkflowState());
172 }
173
174
175 if (query.getWorkflowId() != null) {
176 and(EventIndexSchema.WORKFLOW_ID, query.getWorkflowId());
177 }
178
179
180 if (query.getWorkflowDefinition() != null) {
181 and(EventIndexSchema.WORKFLOW_DEFINITION_ID, query.getWorkflowDefinition());
182 }
183
184
185 if (query.getEventStatus() != null) {
186 and(EventIndexSchema.EVENT_STATUS, query.getEventStatus());
187 }
188
189
190 if (query.getStartFrom() != null && query.getStartTo() != null) {
191 and(EventIndexSchema.START_DATE, query.getStartFrom(), query.getStartTo());
192 }
193
194
195 if (query.getStartDate() != null) {
196 and(EventIndexSchema.START_DATE, query.getStartDate());
197 }
198
199
200 if (query.getDuration() != null) {
201 and(EventIndexSchema.DURATION, query.getDuration());
202 }
203
204
205 if (query.getHasComments() != null) {
206 and(EventIndexSchema.HAS_COMMENTS, query.getHasComments());
207 }
208
209
210 if (query.getHasOpenComments() != null) {
211 and(EventIndexSchema.HAS_OPEN_COMMENTS, query.getHasOpenComments());
212 }
213
214
215 if (query.getComments() != null) {
216 for (String comment : query.getComments()) {
217 and(EventIndexSchema.COMMENTS, comment);
218 }
219 }
220
221
222 if (query.getPublications() != null) {
223 for (String publication : query.getPublications()) {
224 and(EventIndexSchema.PUBLICATION, publication);
225 }
226 }
227
228
229 if (query.getIsPublished() != null) {
230 and(EventIndexSchema.IS_PUBLISHED, query.getIsPublished());
231 }
232
233
234 if (query.getArchiveVersion() != null) {
235 and(EventIndexSchema.ARCHIVE_VERSION, query.getArchiveVersion());
236 }
237
238
239 if (query.getAgentId() != null) {
240 and(EventIndexSchema.AGENT_ID, query.getAgentId());
241 }
242
243
244 if (query.getTechnicalStartFrom() != null && query.getTechnicalStartTo() != null) {
245 and(EventIndexSchema.TECHNICAL_START, query.getTechnicalStartFrom(), query.getTechnicalStartTo());
246 }
247
248
249 if (query.getTechnicalStartTime() != null) {
250 and(EventIndexSchema.TECHNICAL_START, query.getTechnicalStartTime());
251 }
252
253
254 if (query.getTechnicalEndTime() != null) {
255 and(EventIndexSchema.TECHNICAL_END, query.getTechnicalEndTime());
256 }
257
258
259 if (query.getTechnicalPresenters().length > 0) {
260 and(EventIndexSchema.TECHNICAL_PRESENTERS, query.getTechnicalPresenters());
261 }
262
263
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
291 if (query.getFilter() != null) {
292 this.filter = query.getFilter();
293 }
294
295 }
296
297 }