Project

General

Profile

Download (18 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* 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
package eu.etaxonomy.cdm.api.service;
12

    
13

    
14
import java.util.Collection;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.hibernate.LockOptions;
21
import org.hibernate.Session;
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.initializer.IBeanInitializer;
27
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
28
import eu.etaxonomy.cdm.persistence.hibernate.PostMergeEntityListener;
29
import eu.etaxonomy.cdm.persistence.query.Grouping;
30
import eu.etaxonomy.cdm.persistence.query.OrderHint;
31

    
32

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

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

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

    
55
    /**
56
     * Refreshes a given object t using the specified lockmode
57
     *
58
     * All bean properties given in the <code>propertyPaths</code> parameter are recursively initialized.
59
     * <p>
60
     * For detailed description and examples <b>please refer to:</b>
61
     * {@link IBeanInitializer#initialize(Object, List)}
62
     *
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
     *
67
     * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
68
     *
69
     * @param t
70
     * @param lockOptions
71
     */
72
    public void refresh(T t, LockOptions lockOptions, List<String> propertyPaths);
73

    
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
     * @return deleteResult
89
     */
90
    public DeleteResult delete(UUID persistentObjectUUID) ;
91

    
92

    
93

    
94
    /**
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
     * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
118
     */
119
    public T find(UUID uuid);
120

    
121

    
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
     * <p>
127
     * The difference between this method and {@link #find(UUID) find} is
128
     * that this method makes the hibernate read query with the
129
     * {@link org.hibernate.FlushMode FlushMode} for the session set to 'MANUAL'
130
     * <p>
131
     * <b>WARNING:</b>This method should <em>ONLY</em> be used when it is absolutely
132
     * necessary and safe to ensure that the hibernate session is not flushed before a read
133
     * query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacher CdmCacher},
134
     * (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
     * flush which eventually could call {@link eu.etaxonomy.cdm.model.name.NonViralName#getNameCache getNameCache},
138
	 * 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
	 * Apart from these kind of exceptional circumstances, the standard {@link #find(UUID) find}
141
	 * method should always be used to ensure that the persistence layer is always in sync with the
142
	 * underlying database.
143
	 *
144
	 * @param uuid
145
	 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
146
	 */
147
	public T findWithoutFlush(UUID uuid);
148

    
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
     * Returns a <code>List</code> of persisted entities that match the database identifiers.
160
     * Returns an empty list if no identifier matches.
161
     *
162
     * @param idSet
163
     * @return
164
     * @deprecated use {@link #loadByIds(Set, List)} instead
165
     */
166
    @Deprecated
167
    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
     * Returns a sublist of objects matching the grouping projections supplied using the groups parameter
175
     *
176
     * 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
     *
179
     * @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
     *            all entities)
182
     * @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
    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
    //TODO refactor to public <S extends T> List<T> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
204
    public <S extends T>  List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
205

    
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
     * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
212
     */
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
     * {@link IBeanInitializer#initialize(Object, List)}
222
     *
223
     * @param uuid
224
     * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
225
     */
226
    public T load(UUID uuid, List<String> propertyPaths);
227

    
228

    
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
    /**
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
    public <S extends T> Pager<S> page(Class<S> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths);
267

    
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
    public T save(T newInstance);
298

    
299
    /**
300
     * 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
    /**
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
    /**
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
     * @param start The (0-based) offset from the start of the recordset
353
     * @param orderHints
354
     *            Supports path like <code>orderHints.propertyNames</code> which
355
     *            include *-to-one properties like createdBy.username or
356
     * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
357
     * @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
	public DeleteResult delete(T persistentObject);
362

    
363
    /**
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
    /**
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
    /**
383
     * Loads a batch of entities referenced by their ids.
384
     *
385
     * @param idSet
386
     * @param propertyPaths
387
     * @return
388
     */
389
    List<T> loadByIds(List<Integer> idSet, List<String> propertyPaths);
390

    
391
    /**
392
     * 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
    public List<MergeResult<T>> merge(List<T> detachedObjects, boolean returnTransientEntity);
411

    
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
     * 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
     *
423
     * @param newInstance
424
     * @param returnTransientEntity
425
     * @return
426
     */
427
    public MergeResult<T> merge(T newInstance, boolean returnTransientEntity);
428

    
429
}
(57-57/97)