improve generics for save() method
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / IService.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.api.service;
11
12
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.UUID;
18
19 import org.hibernate.LockOptions;
20 import org.hibernate.Session;
21 import org.hibernate.criterion.Criterion;
22 import org.hibernate.event.spi.MergeEvent;
23
24 import eu.etaxonomy.cdm.api.service.pager.Pager;
25 import eu.etaxonomy.cdm.model.common.ICdmBase;
26 import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
27 import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
28 import eu.etaxonomy.cdm.persistence.dto.MergeResult;
29 import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;
30 import eu.etaxonomy.cdm.persistence.query.Grouping;
31 import eu.etaxonomy.cdm.persistence.query.MatchMode;
32 import eu.etaxonomy.cdm.persistence.query.OrderHint;
33
34 /**
35 * @author a.kohlbecker
36 * @since 23.03.2009
37 *
38 * @param <T>
39 */
40 public interface IService<T extends ICdmBase>{
41
42 // FIXME what does this method do?
43 public void clear();
44
45 /**
46 * Obtain the specified lock mode on the given object t
47 * <BR>
48 * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
49 */
50 public void lock(T t, LockOptions lockOptions);
51
52 /**
53 * Refreshes a given object t using the specified lockmode
54 *
55 * All bean properties given in the <code>propertyPaths</code> parameter are recursively initialized.
56 * <p>
57 * For detailed description and examples <b>please refer to:</b>
58 * {@link IBeanInitializer#initialize(Object, List)}
59 *
60 * NOTE: in the case of lockmodes that hit the database (e.g. LockMode.READ), you will need to re-initialize
61 * child propertiesto avoid a HibernateLazyInitializationException (even if the properties of the child
62 * were initialized prior to the refresh).
63 *
64 * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
65 *
66 * @param t
67 * @param lockOptions
68 */
69 public void refresh(T t, LockOptions lockOptions, List<String> propertyPaths);
70
71 /**
72 * Returns a count of all entities of type <T> optionally restricted
73 * to objects belonging to a class that that extends <T>
74 *
75 * @param clazz the class of entities to be counted (can be null to count all entities of type <T>)
76 * @return a count of entities
77 */
78 public int count(Class<? extends T> clazz);
79
80 /**
81 * Delete an existing persistent object
82 *
83 * @param persistentObject the object to be deleted
84 * @return the unique identifier of the deleted entity
85 * @return deleteResult
86 */
87 public DeleteResult delete(UUID persistentObjectUUID) ;
88
89 /**
90 * Returns true if an entity of type <T> with a unique identifier matching the
91 * identifier supplied exists in the database, or false if no such entity can be
92 * found.
93 * @param uuid the unique identifier of the entity required
94 * @return an entity of type <T> matching the uuid, or null if that entity does not exist
95 */
96 public boolean exists(UUID uuid);
97
98 /**
99 * Return a list of persisted entities that match the unique identifier
100 * set supplied as an argument
101 *
102 * @param uuidSet the set of unique identifiers of the entities required
103 * @return a list of entities of type <T>
104 */
105 public List<T> find(Set<UUID> uuidSet);
106
107 /**
108 * Return a list of persisted entities that match the unique identifier
109 * set supplied as an argument and that do match the supplied class.
110 *
111 * @param uuidSet the set of unique identifiers of the entities required
112 * @return a list of entities of type <T>
113 */
114 public <S extends T> List<S> find(Class<S> clazz, Set<UUID> uuidSet);
115
116 /**
117 * Return a persisted entity that matches the unique identifier
118 * supplied as an argument, or null if the entity does not exist
119 *
120 * @param uuid the unique identifier of the entity required
121 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
122 */
123 public T find(UUID uuid);
124
125 /**
126 * Return a persisted entity that matches the unique identifier
127 * supplied as an argument, or null if the entity does not exist.
128 * <p>
129 * The difference between this method and {@link #find(UUID) find} is
130 * that this method makes the hibernate read query with the
131 * {@link org.hibernate.FlushMode FlushMode} for the session set to 'MANUAL'
132 * <p>
133 * <b>WARNING:</b>This method should <em>ONLY</em> be used when it is absolutely
134 * necessary and safe to ensure that the hibernate session is not flushed before a read
135 * query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacherBase CdmCacher},
136 * (ticket #4276) where a call to {@link eu.etaxonomy.cdm.api.cache.CdmCacherBase#load(UUID) load}
137 * the CDM Entity using the standard {@link #find(UUID) find} method results in recursion
138 * due to the fact that the {@link #find(UUID) find} method triggers a hibernate session
139 * flush which eventually could call {@link eu.etaxonomy.cdm.model.name.NonViralName#getNameCache getNameCache},
140 * which in turn (in the event that name cache is null) eventually calls the
141 * {@link eu.etaxonomy.cdm.api.cache.CdmCacherBase#load(UUID uuid) load} again.
142 * Apart from these kind of exceptional circumstances, the standard {@link #find(UUID) find}
143 * method should always be used to ensure that the persistence layer is always in sync with the
144 * underlying database.
145 *
146 * @param uuid
147 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
148 */
149 public T findWithoutFlush(UUID uuid);
150
151 /**
152 * Return a persisted entity that matches the database identifier
153 * supplied as an argument, or null if the entity does not exist
154 *
155 * @param id the database identifier of the entity required
156 * @return an entity of type <T>, or null if the entity does not exist
157 */
158 public T find(int id);
159
160 /**
161 * Returns a <code>List</code> of persisted entities that match the database identifiers.
162 * Returns an empty list if no identifier matches.
163 *
164 * @param idSet
165 * @return
166 * @deprecated use {@link #loadByIds(Set, List)} instead
167 */
168 @Deprecated
169 public List<T> findById(Set<Integer> idSet); //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
170
171
172 // FIXME should we expose this method?
173 public Session getSession();
174
175 /**
176 * Returns a sublist of objects matching the grouping projections supplied using the groups parameter
177 *
178 * It would be nice to be able to return a pager, but for the moment hibernate doesn't
179 * seem to support this (HHH-3238 - impossible to get the rowcount for a criteria that has projections)
180 *
181 * @param clazz Restrict the query to objects of a certain class, or null for all objects of type T or subclasses
182 * @param limit the maximum number of entities returned (can be null to return
183 * all entities)
184 * @param start The (0-based) offset from the start of the recordset (can be null, equivalent of starting at the beginning of the recordset)
185 * @param groups The grouping objects representing a projection, plus an optional ordering on that projected property
186 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
187 * @return a list of arrays of objects, each matching the grouping objects supplied in the parameters.
188 */
189 public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths);
190
191 /**
192 * Returns a list of entities of type <T> optionally restricted
193 * to objects belonging to a class that extends <T>
194 *
195 * @param type The type of entities to return (can be null to count all entities of type <T>)
196 * @param limit The maximum number of objects returned (can be null for all matching objects)
197 * @param start The offset from the start of the result set (0 - based, can be null - equivalent of starting at the beginning of the recordset)
198 * @param orderHints
199 * Supports path like <code>orderHints.propertyNames</code> which
200 * include *-to-one properties like createdBy.username or
201 * authorTeam.persistentTitleCache
202 * @param propertyPaths properties to be initialized
203 * @return
204 */
205 //TODO refactor to public <S extends T> List<T> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
206 public <S extends T> List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
207
208 /**
209 * Finds the cdm entity specified by the <code>uuid</code> parameter and
210 * initializes all its *ToOne relations.
211 *
212 * @param uuid
213 * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
214 */
215 public T load(UUID uuid);
216
217 /**
218 * Return a persisted entity that matches the database identifier
219 * supplied as an argument, or null if the entity does not exist
220 *
221 * @param id the database identifier of the entity required
222 * @param propertyPaths
223 * @return
224 */
225 public T load(int id, List<String> propertyPaths);
226
227 /**
228 * Finds the cdm entity specified by the <code>uuid</code> parameter and
229 * recursively initializes all bean properties given in the
230 * <code>propertyPaths</code> parameter.
231 * <p>
232 * For detailed description and examples <b>please refer to:</b>
233 * {@link IBeanInitializer#initialize(Object, List)}
234 *
235 * @param uuid
236 * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
237 */
238 public T load(UUID uuid, List<String> propertyPaths);
239
240
241 /**
242 * Finds the cdm entities specified by the <code>uuids</code>,
243 * recursively initializes all bean properties given in the
244 * <code>propertyPaths</code> parameter and returns the initialised
245 * entity list;
246 * <p>
247 * For detailed description and examples <b>please refer to:</b>
248 * {@link IBeanInitializer#initialize(Object, List)}
249 * @param uuids
250 * @param propertyPaths
251 * @return
252 */
253 public List<T> load(List<UUID> uuids, List<String> propertyPaths);
254
255 /**
256 * Copy the state of the given object onto the persistent object with the same identifier.
257 *
258 * @param transientObject the entity to be merged
259 * @return The unique identifier of the persisted entity
260 */
261 public T merge(T transientObject);
262
263 /**
264 * Returns a paged list of entities of type <T> optionally restricted
265 * to objects belonging to a class that that extends <T>
266 *
267 * @param type The type of entities to return (can be null to count all entities of type <T>)
268 * @param pageSize The maximum number of objects returned (can be null for all matching objects)
269 * @param pageNumber The offset (in pageSize chunks) from the start of the result set (0 - based,
270 * can be null, equivalent of starting at the beginning of the recordset)
271 * @param orderHints
272 * Supports path like <code>orderHints.propertyNames</code> which
273 * include *-to-one properties like createdBy.username or
274 * authorTeam.persistentTitleCache
275 * @param propertyPaths properties to be initialized
276 * @return a pager of objects of type <T>
277 */
278 public <S extends T> Pager<S> page(Class<S> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths);
279
280 /**
281 * Re-read the state of the given instance from the underlying database.
282 *
283 * Hibernate claims that it is inadvisable to use refresh in long-running-sessions.
284 * I don't really see where we would get into a situation where problems as discussed
285 * this forum thread would apply for our scenario
286 *
287 * http://forum.hibernate.org/viewtopic.php?t=974544
288 *
289 * @param persistentObject the object to be refreshed
290 * @return the unique identifier
291 */
292 public UUID refresh(T persistentObject);
293
294 /**
295 * Save a collection containing new entities (persists the entities)
296 * @param newInstances the new entities to be persisted
297 * @return A Map containing the new entities, keyed using the generated UUID's
298 * of those entities
299 */
300 public Map<UUID,T> save(Collection<T> newInstances);
301
302 /**
303 * Save a new entity (persists the entity)
304 * @param newInstance the new entity to be persisted
305 * @return The new persistent entity
306 */
307 public <S extends T> S save(S newInstance);
308
309 /**
310 * Save a new entity or update the persistent state of an existing
311 * transient entity that has been persisted previously
312 *
313 * @param transientObject the entity to be persisted
314 * @return The unique identifier of the persisted entity
315 */
316 public UUID saveOrUpdate(T transientObject);
317
318 /**
319 * Save new entities or update the persistent state of existing
320 * transient entities that have been persisted previously
321 *
322 * @param transientObjects the entities to be persisted
323 * @return The unique identifier of the persisted entity
324 */
325 public Map<UUID,T> saveOrUpdate(Collection<T> transientObjects);
326
327 /**
328 * Update the persistent state of an existing transient entity
329 * that has been persisted previously
330 *
331 * @param transientObject the entity to be persisted
332 * @return The unique identifier of the persisted entity
333 */
334 public UUID update(T transientObject);
335
336 /**
337 * Simply calls the load method.
338 * Required specifically for the editor to allow load calls which
339 * can also update the session cache.
340 *
341 * @param uuid
342 * @return
343 */
344 public T loadWithUpdate(UUID uuid);
345
346 /**
347 * Method that lists the objects matching the example provided.
348 * The includeProperties property is used to specify which properties of the example are used.
349 *
350 * If includeProperties is null or empty, then all literal properties are used (restrictions are
351 * applied as in the Hibernate Query-By-Example API call Example.create(object)).
352 *
353 * If includeProperties is not empty then only literal properties that are named in the set are used to
354 * create restrictions, *PLUS* any *ToOne related entities. Related entities are matched on ID, not by
355 * their internal literal values (e.g. the call is criteria.add(Restrictions.eq(property,relatedObject)), not
356 * criteria.createCriteria(property).add(Example.create(relatedObject)))
357 *
358 * @param example
359 * @param includeProperties
360 * @param limit the maximum number of entities returned (can be null to return
361 * all entities)
362 * @param start The (0-based) offset from the start of the recordset
363 * @param orderHints
364 * Supports path like <code>orderHints.propertyNames</code> which
365 * include *-to-one properties like createdBy.username or
366 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
367 * @return a list of matching objects
368 */
369 public <S extends T> List<S> list(S example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
370
371 public DeleteResult delete(T persistentObject);
372
373 /**
374 * Deletes a collection of persistent objects correponding to the
375 * given list of uuids. The result will have status as ok if even one
376 * of the deletes is successful, else error.
377 *
378 * @param persistentObjectUUIDs uuids of persistent objects to delete
379 * @return DeleteResult object
380 */
381 public DeleteResult delete(Collection<UUID> persistentObjectUUIDs);
382
383 /**
384 * Merges a list of detached objects and returns the new
385 * list of merged objects
386 *
387 * @param detachedObjects
388 * @return a list of merged objects
389 */
390 public List<T> merge(List<T> detachedObjects);
391
392 /**
393 * Loads a batch of entities referenced by their ids.
394 *
395 * @param idSet
396 * @param propertyPaths
397 * @return
398 */
399 List<T> loadByIds(List<Integer> idSet, List<String> propertyPaths);
400
401 /**
402 * Loads a batch of entities referenced by their ids.
403 * @param idSet
404 * @param orderHints
405 * @param propertyPaths
406 * @return
407 */
408 List<T> loadByIds(List<Integer> idSet, List<OrderHint> orderHints, List<String> propertyPaths);
409
410
411 /**
412 * This method allows for the possibility of returning the input transient
413 * entities instead of the merged persistent entity
414 *
415 * WARNING : This method should never be used when the objective of the merge
416 * is to attach to an existing session which is the standard use case.
417 * This method should only be used in the
418 * case of an external call which does not use hibernate sessions and is
419 * only interested in the entity as a POJO. Apart from the session information
420 * the only other difference between the transient and persisted object is in the case
421 * of new objects (id=0) where hibernate sets the id after commit. This id is copied
422 * over to the transient entity in {@link PostMergeEntityListener#onMerge(MergeEvent,Map)}
423 * making the two objects identical and allowing the transient object to be used further
424 * as a POJO
425 *
426 * @param detachedObjects
427 * @param returnTransientEntity
428 * @return
429 */
430 public List<MergeResult<T>> merge(List<T> detachedObjects, boolean returnTransientEntity);
431
432 /**
433 * This method allows for the possibility of returning the input transient
434 * entity instead of the merged persistent entity
435 *
436 * WARNING : This method should never be used when the objective of the merge
437 * is to attach to an existing session which is the standard use case.
438 * This method should only be used in the case of an external call which does
439 * not use hibernate sessions and is only interested in the entity as a POJO.
440 * This method returns the root merged transient entity as well as all newly merged
441 * persistent entities within the return object.
442 *
443 * @param newInstance
444 * @param returnTransientEntity
445 * @return
446 */
447 public MergeResult<T> merge(T newInstance, boolean returnTransientEntity);
448
449 public <S extends T> Pager<S> page(Class<S> clazz, String param, String queryString, MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints, List<String> propertyPaths);
450
451 public <S extends T> Pager<S> pageByParamWithRestrictions(Class<S> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints,
452 List<String> propertyPaths);
453
454 /**
455 * @param clazz
456 * @param restrictions
457 * @param pageSize
458 * @param pageIndex
459 * @param orderHints
460 * @param propertyPaths
461 * @return
462 */
463 public <S extends T> Pager<S> page(Class<S> clazz, List<Restriction<?>> restrictions, Integer pageSize, Integer pageIndex,
464 List<OrderHint> orderHints, List<String> propertyPaths);
465
466 }