Project

General

Profile

Download (18.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.event.spi.MergeEvent;
22

    
23
import eu.etaxonomy.cdm.api.service.pager.Pager;
24
import eu.etaxonomy.cdm.model.common.ICdmBase;
25
import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;
26
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
27
import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;
28
import eu.etaxonomy.cdm.persistence.query.Grouping;
29
import eu.etaxonomy.cdm.persistence.query.OrderHint;
30

    
31

    
32
/**
33
 * @author a.mueller
34
 *
35
 */
36
/**
37
 * @author a.kohlbecker
38
 * @date 23.03.2009
39
 *
40
 * @param <T>
41
 */
42
public interface IService<T extends ICdmBase>{
43

    
44
    // FIXME what does this method do?
45
    public void clear();
46

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

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

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

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

    
91

    
92

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

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

    
111
    /**
112
     * Return a persisted entity that matches the unique identifier
113
     * supplied as an argument, or null if the entity does not exist
114
     *
115
     * @param uuid the unique identifier of the entity required
116
     * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
117
     */
118
    public T find(UUID uuid);
119

    
120

    
121

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

    
148
    /**
149
     * Return a persisted entity that matches the database identifier
150
     * supplied as an argument, or null if the entity does not exist
151
     *
152
     * @param id the database identifier of the entity required
153
     * @return an entity of type <T>, or null if the entity does not exist
154
     */
155
    public T find(int id);
156

    
157
    /**
158
     * Returns a <code>List</code> of persisted entities that match the database identifiers.
159
     * Returns an empty list if no identifier matches.
160
     *
161
     * @param idSet
162
     * @return
163
     * @deprecated use {@link #loadByIds(Set, List)} instead
164
     */
165
    @Deprecated
166
    public List<T> findById(Set<Integer> idSet);  //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
167

    
168

    
169
    // FIXME should we expose this method?
170
    public Session getSession();
171

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

    
188
    /**
189
     * Returns a list of entities of type <T> optionally restricted
190
     * to objects belonging to a class that that extends <T>
191
     *
192
     * @param type  The type of entities to return (can be null to count all entities of type <T>)
193
     * @param limit The maximum number of objects returned (can be null for all matching objects)
194
     * @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)
195
     * @param orderHints
196
     *            Supports path like <code>orderHints.propertyNames</code> which
197
     *            include *-to-one properties like createdBy.username or
198
     *            authorTeam.persistentTitleCache
199
     * @param propertyPaths properties to be initialized
200
     * @return
201
     */
202
    //TODO refactor to public <S extends T> List<T> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
203
    public <S extends T>  List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
204

    
205
    /**
206
     * Finds the cdm entity specified by the <code>uuid</code> parameter and
207
     * initializes all its *ToOne relations.
208
     *
209
     * @param uuid
210
     * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
211
     */
212
    public T load(UUID uuid);
213

    
214
    /**
215
     * Return a persisted entity that matches the database identifier
216
     * supplied as an argument, or null if the entity does not exist
217
     *
218
     * @param id the database identifier of the entity required
219
     * @param propertyPaths
220
     * @return
221
     */
222
    public T load(int id, List<String> propertyPaths);
223

    
224
    /**
225
     * Finds the cdm entity specified by the <code>uuid</code> parameter and
226
     * recursively initializes all bean properties given in the
227
     * <code>propertyPaths</code> parameter.
228
     * <p>
229
     * For detailed description and examples <b>please refer to:</b>
230
     * {@link IBeanInitializer#initialize(Object, List)}
231
     *
232
     * @param uuid
233
     * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
234
     */
235
    public T load(UUID uuid, List<String> propertyPaths);
236

    
237

    
238
    /**
239
     * Finds the cdm entities specified by the <code>uuids</code>,
240
     * recursively initializes all bean properties given in the
241
     * <code>propertyPaths</code> parameter and returns the initialised
242
     * entity list;
243
     * <p>
244
     * For detailed description and examples <b>please refer to:</b>
245
     * {@link IBeanInitializer#initialize(Object, List)}
246
     * @param uuids
247
     * @param propertyPaths
248
     * @return
249
     */
250
    public List<T> load(List<UUID> uuids, List<String> propertyPaths);
251

    
252
    /**
253
     * Copy the state of the given object onto the persistent object with the same identifier.
254
     *
255
     * @param transientObject the entity to be merged
256
     * @return The unique identifier of the persisted entity
257
     */
258
    public T merge(T transientObject);
259

    
260
    /**
261
     * Returns a paged list of entities of type <T> optionally restricted
262
     * to objects belonging to a class that that extends <T>
263
     *
264
     * @param type  The type of entities to return (can be null to count all entities of type <T>)
265
     * @param pageSize The maximum number of objects returned (can be null for all matching objects)
266
     * @param pageNumber The offset (in pageSize chunks) from the start of the result set (0 - based,
267
     *                   can be null, equivalent of starting at the beginning of the recordset)
268
     * @param orderHints
269
     *            Supports path like <code>orderHints.propertyNames</code> which
270
     *            include *-to-one properties like createdBy.username or
271
     *            authorTeam.persistentTitleCache
272
     * @param propertyPaths properties to be initialized
273
     * @return a pager of objects of type <T>
274
     */
275
    public <S extends T> Pager<S> page(Class<S> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths);
276

    
277
    /**
278
     * Re-read the state of the given instance from the underlying database.
279
     *
280
     * Hibernate claims that it is inadvisable to use refresh in long-running-sessions.
281
     * I don't really see where we would get into a situation where problems as discussed
282
     * this forum thread would apply for our scenario
283
     *
284
     * http://forum.hibernate.org/viewtopic.php?t=974544
285
     *
286
     * @param persistentObject the object to be refreshed
287
     * @return the unique identifier
288
     */
289
    public UUID refresh(T persistentObject);
290

    
291
    public List<T> rows(String tableName, int limit, int start);
292

    
293
    /**
294
     * Save a collection containing new entities (persists the entities)
295
     * @param newInstances the new entities to be persisted
296
     * @return A Map containing the new entities, keyed using the generated UUID's
297
     *         of those entities
298
     */
299
    public Map<UUID,T> save(Collection<T> newInstances);
300

    
301
    /**
302
     * Save a new entity (persists the entity)
303
     * @param newInstance the new entity to be persisted
304
     * @return A generated UUID for the new persistent entity
305
     */
306
    public T save(T newInstance);
307

    
308
    /**
309
     * Save a new entity or update the persistent state of an existing
310
     * transient entity that has been persisted previously
311
     *
312
     * @param transientObject the entity to be persisted
313
     * @return The unique identifier of the persisted entity
314
     */
315
    public UUID saveOrUpdate(T transientObject);
316

    
317
    /**
318
     * Save new entities or update the persistent state of existing
319
     * transient entities that have been persisted previously
320
     *
321
     * @param transientObjects the entities to be persisted
322
     * @return The unique identifier of the persisted entity
323
     */
324
    public Map<UUID,T> saveOrUpdate(Collection<T> transientObjects);
325

    
326
    /**
327
     * Update the persistent state of an existing transient entity
328
     * that has been persisted previously
329
     *
330
     * @param transientObject the entity to be persisted
331
     * @return The unique identifier of the persisted entity
332
     */
333
    public UUID update(T transientObject);
334

    
335
    /**
336
     * Simply calls the load method.
337
     * Required specifically for the editor to allow load calls which
338
     * can also update the session cache.
339
     *
340
     * @param uuid
341
     * @return
342
     */
343
    public T loadWithUpdate(UUID uuid);
344

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

    
370
	public DeleteResult delete(T persistentObject);
371

    
372
    /**
373
     * Deletes a collection of persistent objects correponding to the
374
     * given list of uuids. The result will have status as ok if even one
375
     * of the deletes is successful, else error.
376
     *
377
     * @param persistentObjectUUIDs uuids of persistent objects to delete
378
     * @return DeleteResult object
379
     */
380
    public DeleteResult delete(Collection<UUID> persistentObjectUUIDs);
381

    
382
    /**
383
     * Merges a list of detached objects and returns the new
384
     * list of merged objects
385
     *
386
     * @param detachedObjects
387
     * @return a list of merged objects
388
     */
389
    public List<T> merge(List<T> detachedObjects);
390

    
391
    /**
392
     * Loads a batch of entities referenced by their ids.
393
     *
394
     * @param idSet
395
     * @param propertyPaths
396
     * @return
397
     */
398
    List<T> loadByIds(List<Integer> idSet, List<String> propertyPaths);
399

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

    
421
    /**
422
     * This method allows for the possibility of returning the input transient
423
     * entity instead of the merged persistent entity
424
     *
425
     * WARNING : This method should never be used when the objective of the merge
426
     * is to attach to an existing session which is the standard use case.
427
     * This method should only be used in the case of an external call which does
428
     * not use hibernate sessions and is only interested in the entity as a POJO.
429
     * This method returns the root merged transient entity as well as all newly merged
430
     * persistent entities within the return object.
431
     *
432
     * @param newInstance
433
     * @param returnTransientEntity
434
     * @return
435
     */
436
    public MergeResult<T> merge(T newInstance, boolean returnTransientEntity);
437

    
438
}
(59-59/101)