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