Project

General

Profile

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