Project

General

Profile

Download (19.3 KB) Statistics
| Branch: | Tag: | Revision:
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
/**
36
 * @author a.mueller
37
 *
38
 */
39
/**
40
 * @author a.kohlbecker
41
 * @since 23.03.2009
42
 *
43
 * @param <T>
44
 */
45
public interface IService<T extends ICdmBase>{
46

    
47
    // FIXME what does this method do?
48
    public void clear();
49

    
50
    /**
51
     * Obtain the specified lock mode on the given object t
52
     * <BR>
53
     * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
54
     */
55
    public void lock(T t, LockOptions lockOptions);
56

    
57
    /**
58
     * Refreshes a given object t using the specified lockmode
59
     *
60
     * All bean properties given in the <code>propertyPaths</code> parameter are recursively initialized.
61
     * <p>
62
     * For detailed description and examples <b>please refer to:</b>
63
     * {@link IBeanInitializer#initialize(Object, List)}
64
     *
65
     * NOTE: in the case of lockmodes that hit the database (e.g. LockMode.READ), you will need to re-initialize
66
     * child propertiesto avoid a HibernateLazyInitializationException (even if the properties of the child
67
     * were initialized prior to the refresh).
68
     *
69
     * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
70
     *
71
     * @param t
72
     * @param lockOptions
73
     */
74
    public void refresh(T t, LockOptions lockOptions, List<String> propertyPaths);
75

    
76
    /**
77
     * Returns a count of all entities of type <T>  optionally restricted
78
     * to objects belonging to a class that that extends <T>
79
     *
80
     * @param clazz the class of entities to be counted (can be null to count all entities of type <T>)
81
     * @return a count of entities
82
     */
83
    public int count(Class<? extends T> clazz);
84

    
85
    /**
86
     * Delete an existing persistent object
87
     *
88
     * @param persistentObject the object to be deleted
89
     * @return the unique identifier of the deleted entity
90
     * @return deleteResult
91
     */
92
    public DeleteResult delete(UUID persistentObjectUUID) ;
93

    
94

    
95

    
96
    /**
97
     * Returns true if an entity of type <T> with a unique identifier matching the
98
     * identifier supplied exists in the database, or false if no such entity can be
99
     * found.
100
     * @param uuid the unique identifier of the entity required
101
     * @return an entity of type <T> matching the uuid, or null if that entity does not exist
102
     */
103
    public boolean exists(UUID uuid);
104

    
105
    /**
106
     * Return a list of persisted entities that match the unique identifier
107
     * set supplied as an argument
108
     *
109
     * @param uuidSet the set of unique identifiers of the entities required
110
     * @return a list of entities of type <T>
111
     */
112
    public List<T> find(Set<UUID> uuidSet);
113

    
114
    /**
115
     * Return a list of persisted entities that match the unique identifier
116
     * set supplied as an argument and that do match the supplied class.
117
     *
118
     * @param uuidSet the set of unique identifiers of the entities required
119
     * @return a list of entities of type <T>
120
     */
121
    public <S extends T> List<S> find(Class<S> clazz, Set<UUID> uuidSet);
122

    
123
    /**
124
     * Return a persisted entity that matches the unique identifier
125
     * supplied as an argument, or null if the entity does not exist
126
     *
127
     * @param uuid the unique identifier of the entity required
128
     * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
129
     */
130
    public T find(UUID uuid);
131

    
132

    
133

    
134
	/**
135
	 * Return a persisted entity that matches the unique identifier
136
     * supplied as an argument, or null if the entity does not exist.
137
     * <p>
138
     * The difference between this method and {@link #find(UUID) find} is
139
     * that this method makes the hibernate read query with the
140
     * {@link org.hibernate.FlushMode FlushMode} for the session set to 'MANUAL'
141
     * <p>
142
     * <b>WARNING:</b>This method should <em>ONLY</em> be used when it is absolutely
143
     * necessary and safe to ensure that the hibernate session is not flushed before a read
144
     * query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacher CdmCacher},
145
     * (ticket #4276) where a call to {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID) load}
146
     * the CDM Entity using the standard {@link #find(UUID) find} method results in recursion
147
     * due to the fact that the {@link #find(UUID) find} method triggers a hibernate session
148
     * flush which eventually could call {@link eu.etaxonomy.cdm.model.name.NonViralName#getNameCache getNameCache},
149
	 * which in turn (in the event that name cache is null) eventually calls the
150
	 * {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID uuid) load} again.
151
	 * Apart from these kind of exceptional circumstances, the standard {@link #find(UUID) find}
152
	 * method should always be used to ensure that the persistence layer is always in sync with the
153
	 * underlying database.
154
	 *
155
	 * @param uuid
156
	 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
157
	 */
158
	public T findWithoutFlush(UUID uuid);
159

    
160
    /**
161
     * Return a persisted entity that matches the database identifier
162
     * supplied as an argument, or null if the entity does not exist
163
     *
164
     * @param id the database identifier of the entity required
165
     * @return an entity of type <T>, or null if the entity does not exist
166
     */
167
    public T find(int id);
168

    
169
    /**
170
     * Returns a <code>List</code> of persisted entities that match the database identifiers.
171
     * Returns an empty list if no identifier matches.
172
     *
173
     * @param idSet
174
     * @return
175
     * @deprecated use {@link #loadByIds(Set, List)} instead
176
     */
177
    @Deprecated
178
    public List<T> findById(Set<Integer> idSet);  //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
179

    
180

    
181
    // FIXME should we expose this method?
182
    public Session getSession();
183

    
184
    /**
185
     * Returns a sublist of objects matching the grouping projections supplied using the groups parameter
186
     *
187
     * It would be nice to be able to return a pager, but for the moment hibernate doesn't
188
     * seem to support this (HHH-3238 - impossible to get the rowcount for a criteria that has projections)
189
     *
190
     * @param clazz Restrict the query to objects of a certain class, or null for all objects of type T or subclasses
191
     * @param limit the maximum number of entities returned (can be null to return
192
     *            all entities)
193
     * @param start The (0-based) offset from the start of the recordset (can be null, equivalent of starting at the beginning of the recordset)
194
     * @param groups The grouping objects representing a projection, plus an optional ordering on that projected property
195
     * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
196
     * @return a list of arrays of objects, each matching the grouping objects supplied in the parameters.
197
     */
198
    public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths);
199

    
200
    /**
201
     * Returns a list of entities of type <T> optionally restricted
202
     * to objects belonging to a class that that extends <T>
203
     *
204
     * @param type  The type of entities to return (can be null to count all entities of type <T>)
205
     * @param limit The maximum number of objects returned (can be null for all matching objects)
206
     * @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)
207
     * @param orderHints
208
     *            Supports path like <code>orderHints.propertyNames</code> which
209
     *            include *-to-one properties like createdBy.username or
210
     *            authorTeam.persistentTitleCache
211
     * @param propertyPaths properties to be initialized
212
     * @return
213
     */
214
    //TODO refactor to public <S extends T> List<T> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
215
    public <S extends T>  List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
216

    
217
    /**
218
     * Finds the cdm entity specified by the <code>uuid</code> parameter and
219
     * initializes all its *ToOne relations.
220
     *
221
     * @param uuid
222
     * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
223
     */
224
    public T load(UUID uuid);
225

    
226
    /**
227
     * Return a persisted entity that matches the database identifier
228
     * supplied as an argument, or null if the entity does not exist
229
     *
230
     * @param id the database identifier of the entity required
231
     * @param propertyPaths
232
     * @return
233
     */
234
    public T load(int id, List<String> propertyPaths);
235

    
236
    /**
237
     * Finds the cdm entity specified by the <code>uuid</code> parameter and
238
     * recursively initializes all bean properties given in the
239
     * <code>propertyPaths</code> parameter.
240
     * <p>
241
     * For detailed description and examples <b>please refer to:</b>
242
     * {@link IBeanInitializer#initialize(Object, List)}
243
     *
244
     * @param uuid
245
     * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
246
     */
247
    public T load(UUID uuid, List<String> propertyPaths);
248

    
249

    
250
    /**
251
     * Finds the cdm entities specified by the <code>uuids</code>,
252
     * recursively initializes all bean properties given in the
253
     * <code>propertyPaths</code> parameter and returns the initialised
254
     * entity list;
255
     * <p>
256
     * For detailed description and examples <b>please refer to:</b>
257
     * {@link IBeanInitializer#initialize(Object, List)}
258
     * @param uuids
259
     * @param propertyPaths
260
     * @return
261
     */
262
    public List<T> load(List<UUID> uuids, List<String> propertyPaths);
263

    
264
    /**
265
     * Copy the state of the given object onto the persistent object with the same identifier.
266
     *
267
     * @param transientObject the entity to be merged
268
     * @return The unique identifier of the persisted entity
269
     */
270
    public T merge(T transientObject);
271

    
272
    /**
273
     * Returns a paged list of entities of type <T> optionally restricted
274
     * to objects belonging to a class that that extends <T>
275
     *
276
     * @param type  The type of entities to return (can be null to count all entities of type <T>)
277
     * @param pageSize The maximum number of objects returned (can be null for all matching objects)
278
     * @param pageNumber The offset (in pageSize chunks) from the start of the result set (0 - based,
279
     *                   can be null, equivalent of starting at the beginning of the recordset)
280
     * @param orderHints
281
     *            Supports path like <code>orderHints.propertyNames</code> which
282
     *            include *-to-one properties like createdBy.username or
283
     *            authorTeam.persistentTitleCache
284
     * @param propertyPaths properties to be initialized
285
     * @return a pager of objects of type <T>
286
     */
287
    public <S extends T> Pager<S> page(Class<S> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths);
288

    
289
    /**
290
     * Re-read the state of the given instance from the underlying database.
291
     *
292
     * Hibernate claims that it is inadvisable to use refresh in long-running-sessions.
293
     * I don't really see where we would get into a situation where problems as discussed
294
     * this forum thread would apply for our scenario
295
     *
296
     * http://forum.hibernate.org/viewtopic.php?t=974544
297
     *
298
     * @param persistentObject the object to be refreshed
299
     * @return the unique identifier
300
     */
301
    public UUID refresh(T persistentObject);
302

    
303
    /**
304
     * Save a collection containing new entities (persists the entities)
305
     * @param newInstances the new entities to be persisted
306
     * @return A Map containing the new entities, keyed using the generated UUID's
307
     *         of those entities
308
     */
309
    public Map<UUID,T> save(Collection<T> newInstances);
310

    
311
    /**
312
     * Save a new entity (persists the entity)
313
     * @param newInstance the new entity to be persisted
314
     * @return The new persistent entity
315
     */
316
    public T save(T newInstance);
317

    
318
    /**
319
     * Save a new entity or update the persistent state of an existing
320
     * transient entity that has been persisted previously
321
     *
322
     * @param transientObject the entity to be persisted
323
     * @return The unique identifier of the persisted entity
324
     */
325
    public UUID saveOrUpdate(T transientObject);
326

    
327
    /**
328
     * Save new entities or update the persistent state of existing
329
     * transient entities that have been persisted previously
330
     *
331
     * @param transientObjects the entities to be persisted
332
     * @return The unique identifier of the persisted entity
333
     */
334
    public Map<UUID,T> saveOrUpdate(Collection<T> transientObjects);
335

    
336
    /**
337
     * Update the persistent state of an existing transient entity
338
     * that has been persisted previously
339
     *
340
     * @param transientObject the entity to be persisted
341
     * @return The unique identifier of the persisted entity
342
     */
343
    public UUID update(T transientObject);
344

    
345
    /**
346
     * Simply calls the load method.
347
     * Required specifically for the editor to allow load calls which
348
     * can also update the session cache.
349
     *
350
     * @param uuid
351
     * @return
352
     */
353
    public T loadWithUpdate(UUID uuid);
354

    
355
    /**
356
     * Method that lists the objects matching the example provided.
357
     * The includeProperties property is used to specify which properties of the example are used.
358
     *
359
     * If includeProperties is null or empty, then all literal properties are used (restrictions are
360
     * applied as in the Hibernate Query-By-Example API call Example.create(object)).
361
     *
362
     * If includeProperties is not empty then only literal properties that are named in the set are used to
363
     * create restrictions, *PLUS* any *ToOne related entities. Related entities are matched on ID, not by
364
     * their internal literal values (e.g. the call is criteria.add(Restrictions.eq(property,relatedObject)), not
365
     * criteria.createCriteria(property).add(Example.create(relatedObject)))
366
     *
367
     * @param example
368
     * @param includeProperties
369
     * @param limit the maximum number of entities returned (can be null to return
370
     *            all entities)
371
     * @param start The (0-based) offset from the start of the recordset
372
     * @param orderHints
373
     *            Supports path like <code>orderHints.propertyNames</code> which
374
     *            include *-to-one properties like createdBy.username or
375
     * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
376
     * @return a list of matching objects
377
     */
378
    public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
379

    
380
	public DeleteResult delete(T persistentObject);
381

    
382
    /**
383
     * Deletes a collection of persistent objects correponding to the
384
     * given list of uuids. The result will have status as ok if even one
385
     * of the deletes is successful, else error.
386
     *
387
     * @param persistentObjectUUIDs uuids of persistent objects to delete
388
     * @return DeleteResult object
389
     */
390
    public DeleteResult delete(Collection<UUID> persistentObjectUUIDs);
391

    
392
    /**
393
     * Merges a list of detached objects and returns the new
394
     * list of merged objects
395
     *
396
     * @param detachedObjects
397
     * @return a list of merged objects
398
     */
399
    public List<T> merge(List<T> detachedObjects);
400

    
401
    /**
402
     * Loads a batch of entities referenced by their ids.
403
     *
404
     * @param idSet
405
     * @param propertyPaths
406
     * @return
407
     */
408
    List<T> loadByIds(List<Integer> idSet, List<String> propertyPaths);
409

    
410
    /**
411
     * This method allows for the possibility of returning the input transient
412
     * entities instead of the merged persistent entity
413
     *
414
     * WARNING : This method should never be used when the objective of the merge
415
     * is to attach to an existing session which is the standard use case.
416
     * This method should only be used in the
417
     * case of an external call which does not use hibernate sessions and is
418
     * only interested in the entity as a POJO. Apart from the session information
419
     * the only other difference between the transient and persisted object is in the case
420
     * of new objects (id=0) where hibernate sets the id after commit. This id is copied
421
     * over to the transient entity in {@link PostMergeEntityListener#onMerge(MergeEvent,Map)}
422
     * making the two objects identical and allowing the transient object to be used further
423
     * as a POJO
424
     *
425
     * @param detachedObjects
426
     * @param returnTransientEntity
427
     * @return
428
     */
429
    public List<MergeResult<T>> merge(List<T> detachedObjects, boolean returnTransientEntity);
430

    
431
    /**
432
     * This method allows for the possibility of returning the input transient
433
     * entity instead of the merged persistent entity
434
     *
435
     * WARNING : This method should never be used when the objective of the merge
436
     * is to attach to an existing session which is the standard use case.
437
     * This method should only be used in the case of an external call which does
438
     * not use hibernate sessions and is only interested in the entity as a POJO.
439
     * This method returns the root merged transient entity as well as all newly merged
440
     * persistent entities within the return object.
441
     *
442
     * @param newInstance
443
     * @param returnTransientEntity
444
     * @return
445
     */
446
    public MergeResult<T> merge(T newInstance, boolean returnTransientEntity);
447

    
448
    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);
449

    
450
    public <S extends T> Pager<S> pageByRestrictions(Class<S> clazz, String param, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageIndex, List<OrderHint> orderHints,
451
            List<String> propertyPaths);
452

    
453
}
(62-62/103)