/
IQuery.java
323 lines (299 loc) · 12 KB
/
IQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* ome.api.IQuery
*
* Copyright 2006 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.api;
import java.util.List;
import ome.annotations.NotNull;
import ome.conditions.ApiUsageException;
import ome.conditions.ValidationException;
import ome.model.IObject;
import ome.parameters.Filter;
import ome.parameters.Parameters;
import ome.parameters.QueryParameter;
/**
* Provides methods for directly querying object graphs. As far as is possible,
* IQuery should be considered the lowest level DB-access (SELECT) interface.
* Unlike the {@link ome.api.IUpdate} interface, using other methods will most
* likely not leave the database in an inconsitent state, but may provide stale
* data in some situations.
*
* By convention, all methods that begin with <code>get</code> will never
* return a null or empty {@link java.util.Collection}, but instead will throw
* a {@link ome.conditions.ValidationException}.
*
* @author Josh Moore <a
* href="mailto:josh.moore@gmx.de">josh.moore@gmx.de</a>
* @version 3.0
* @since 3.0
* @see Filter
* @see Parameters
* @see QueryParameter
*/
public interface IQuery extends ServiceInterface {
// ~ Simple Lookups
// =========================================================================
/**
* lookup an entity by class and id. If no such object exists, an exception
* will be thrown.
*
* @param klass
* the type of the entity. Not null.
* @param id
* the entity's id
* @return an initialized entity
* @throws ValidationException
* if the id doesn't exist.
*/
<T extends IObject> T get(@NotNull
Class<T> klass, long id) throws ValidationException;
/**
* lookup an entity by class and id. If no such objects exists, return a
* null.
*
* @param klass
* klass the type of the entity. Not null.
* @param id
* the entity's id
* @return an initialized entity or null if id doesn't exist.
*/
<T extends IObject> T find(@NotNull
Class<T> klass, long id);
/**
* lookup all entities that belong to this class and match filter.
*
* @param klass
* entity type to be searched. Not null.
* @param filter
* filters the result set. Can be null.
* @return a collection if initialized entities or an empty List if none
* exist.
*/
<T extends IObject> List<T> findAll(@NotNull
Class<T> klass, Filter filter);
// ~ Example-based Queries
// =========================================================================
/**
* search based on provided example entity. The example entity should
* <em>uniquely</em> specify the entity or an exception will be thrown.
*
* Note: findByExample does not operate on the <code>id</code> field. For
* that, use {@link #find(Class, long)}, {@link #get(Class, long)},
* {@link #findByQuery(String, Parameters)}, or
* {@link #findAllByQuery(String, Parameters)}
*
* @param example
* Non-null example object.
* @return Possibly null IObject result.
* @throws ApiUsageException
* if more than one result is return.
*/
<T extends IObject> T findByExample(@NotNull
T example) throws ApiUsageException;
/**
* search based on provided example entity. The returned entities will be
* limited by the {@link Filter} object.
*
* Note: findAllbyExample does not operate on the <code>id</code> field.
* For that, use {@link #find(Class, long)}, {@link #get(Class, long)},
* {@link #findByQuery(String, Parameters)}, or
* {@link #findAllByQuery(String, Parameters)}
*
*
* @param example
* Non-null example object.
* @param filter
* filters the result set. Can be null.
* @return Possibly empty List of IObject results.
*/
<T extends IObject> List<T> findAllByExample(@NotNull
T example, Filter filter);
// ~ String-field-Queries
// =========================================================================
/**
* search a given field matching against a String. Method does <em>not</em>
* allow for case sensitive or insensitive searching since this is
* essentially a lookup. The existence of more than one result will result
* in an exception.
*
* @param klass
* type of entity to be searched
* @param field
* the name of the field, either as simple string or as public
* final static from the entity class, e.g.
* {@link ome.model.containers.Project#NAME}
* @param value
* String used for search.
* @return found entity or possibly null.
* @throws ome.conditions.ApiUsageException
* if more than one result.
*/
<T extends IObject> T findByString(@NotNull
Class<T> klass, @NotNull
String field, String value) throws ApiUsageException;
/**
* search a given field matching against a String. Method allows for case
* sensitive or insensitive searching using the (I)LIKE comparators. Result
* set will be reduced by the {@link Filter} instance.
*
* @param klass
* type of entity to be searched. Not null.
* @param field
* the name of the field, either as simple string or as public
* final static from the entity class, e.g.
* {@link ome.model.containers.Project#NAME}. Not null.
* @param stringValue
* String used for search. Not null.
* @param caseSensitive
* whether to use LIKE or ILIKE
* @param filter
* filters the result set. Can be null.
* @return A list (possibly empty) with the results.
*/
<T extends IObject> List<T> findAllByString(@NotNull
Class<T> klass, @NotNull
String field, String stringValue, boolean caseSensitive, Filter filter);
// ~ Parameter-based Queries
// =========================================================================
// These methods use the ome.parameters package for representing
// arbitrary (Integer, Long, String, IObject, etc.) parameters. We have
// removed java.lang.Object based parameters from the API for cross-language
// support.
// We don't provide methods with Page argument. Include in QueryParameters.
// Available queries:
// is class in hibernate? use parameters as field name.
// see ClassnameQuery for documentation
// is class of querysource?
// lookup in hibernate named query
// lookup in database
// else: see StringQuerySource documentation.
/**
* executes the stored query with the given name. If a query with the name
* cannot be found, an exception will be thrown.
*
* The queryName parameter can be an actual query String if the
* StringQuerySource is configured on the server and the user running the
* query has proper permissions.
*
* @param queryName
* String identifier of the query to execute
* @param parameters
* array of {@link QueryParameter}. Not null. The
* {@link QueryParameter#name} field maps to a field-name which
* is then matched against the {@link QueryParameter#value}
* @return Possibly null IObject result.
* @throws ValidationException
*/
<T extends IObject> T findByQuery(@NotNull
String queryName, Parameters parameters) throws ValidationException;
/**
* executes the stored query with the given name. If a query with the name
* cannot be found, an exception will be thrown.
*
* The queryName parameter can be an actual query String if the
* StringQuerySource is configured on the server and the user running the
* query has proper permissions.
*
* Queries can only return lists of {@link IObject} instances. This means
* all must be of the form:
*
* <pre>
* select this from SomeModelClass this ...
* </pre>
*
* though the alias "this" is unimportant. Do not try to return multiple
* classes in one call like:
*
* <pre>
* select this, that from SomeClass this, SomeOtherClass that ...
* </pre>
*
* nor to project values out of an object:
*
* <pre>
* select this.name from SomeClass this ...
* </pre>
*
* If a page is desired, add it to the query parameters.
*
* @param queryName
* String identifier of the query to execute. Not null.
* @param parameters
* array of {@link QueryParameter}. The
* {@link QueryParameter#name} field maps to a field-name which
* is then matched against the {@link QueryParameter#value}
* @return Possibly empty List of IObject results.
*/
<T extends IObject> List<T> findAllByQuery(@NotNull
String queryName, Parameters parameters);
/**
* executes a full text search based on Lucene. Each term in the query can
* also be prefixed by the name of the field to which is should be
* restricted.
*
* Examples:
* <ul>
* <li>owner:root AND annotation:someTag</li>
* <li>file:xml AND name:*hoechst*</li>
* </ul>
*
* For more information, see
* <a href="http://lucene.apache.org/java/docs/queryparsersyntax.html">Query Parser Synax</a>
*
* The return values are first filtered by the security system.
*
* @param <T>
* @param type
* A non-null class specification of which type should be
* searched.
* @param query
* A non-null query string. An empty string will return no
* results.
* @param parameters
* Currently the parameters themselves are unusued. But the
* {@link Parameters#filter} can be used to limit the number
* of results returned ({@link Filter#limit}) or the
* user for who the results will be found ({@link Filter#owner()}).
* @return A list of loaded {@link IObject} instances. Never null.
*/
<T extends IObject> List<T> findAllByFullText(@NotNull
Class<T> type, @NotNull
String query, Parameters parameters);
// ~ Projections
// =========================================================================
/**
* Return a list of Java {@link Object} instances (not {@link IObject}
* instances). These are the column names as specified in the HQL
* select statement (more than one is required).
*
* If an aggregation statement is used, a group by clause must be added.
*
* Examples:
* <ul>
* <li>select i.name, i.description from Image i where i.name like '%.dv'</li>
* <li>select tag.textValue, tagset.textValue from TagAnnotation tag join tag.annotationLinks l join l.child tagset</li>
* <li>select p.pixelsType.value, count(p.id) from Pixel p group by p.pixelsType.value</li>
* </ul>
*/
List<Object[]> projection(@NotNull String query, Parameters parameters);
// ~ Other
// =========================================================================
/**
* refreshes an entire {@link IObject} graph, recursive loading all data for
* the managed instances in the graph from the database. If any non-managed
* entities are detected (e.g. without ids), an {@link ApiUsageException}
* will be thrown.
*
* @param iObject
* Non-null managed {@link IObject} graph which should have all
* values re-assigned from the database
* @return a similar {@link IObject} graph (with possible additions and
* deletions) which is in-sync with the database.
* @throws ApiUsageException
* if any non-managed entities are found.
*/
<T extends IObject> T refresh(@NotNull
T iObject) throws ApiUsageException;
}