Project

General

Profile

Download (17.7 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
     */
165
    public List<T> findById(Set<Integer> idSet);  //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
166

    
167

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

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

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

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

    
213
    /**
214
     * Finds the cdm entity specified by the <code>uuid</code> parameter and
215
     * recursively initializes all bean properties given in the
216
     * <code>propertyPaths</code> parameter.
217
     * <p>
218
     * For detailed description and examples <b>please refer to:</b>
219
     * {@link IBeanInitializer#initialize(Object, List)}
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, List<String> propertyPaths);
225

    
226

    
227
    /**
228
     * Finds the cdm entities specified by the <code>uuids</code>,
229
     * recursively initializes all bean properties given in the
230
     * <code>propertyPaths</code> parameter and returns the initialised
231
     * entity list;
232
     * <p>
233
     * For detailed description and examples <b>please refer to:</b>
234
     * {@link IBeanInitializer#initialize(Object, List)}
235
     * @param uuids
236
     * @param propertyPaths
237
     * @return
238
     */
239
    public List<T> load(List<UUID> uuids, List<String> propertyPaths);
240

    
241
    /**
242
     * Copy the state of the given object onto the persistent object with the same identifier.
243
     *
244
     * @param transientObject the entity to be merged
245
     * @return The unique identifier of the persisted entity
246
     */
247
    public T merge(T transientObject);
248

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

    
266
    /**
267
     * Re-read the state of the given instance from the underlying database.
268
     *
269
     * Hibernate claims that it is inadvisable to use refresh in long-running-sessions.
270
     * I don't really see where we would get into a situation where problems as discussed
271
     * this forum thread would apply for our scenario
272
     *
273
     * http://forum.hibernate.org/viewtopic.php?t=974544
274
     *
275
     * @param persistentObject the object to be refreshed
276
     * @return the unique identifier
277
     */
278
    public UUID refresh(T persistentObject);
279

    
280
    public List<T> rows(String tableName, int limit, int start);
281

    
282
    /**
283
     * Save a collection containing new entities (persists the entities)
284
     * @param newInstances the new entities to be persisted
285
     * @return A Map containing the new entities, keyed using the generated UUID's
286
     *         of those entities
287
     */
288
    public Map<UUID,T> save(Collection<T> newInstances);
289

    
290
    /**
291
     * Save a new entity (persists the entity)
292
     * @param newInstance the new entity to be persisted
293
     * @return A generated UUID for the new persistent entity
294
     */
295
    public T save(T newInstance);
296

    
297
    /**
298
     * Save a new entity or update the persistent state of an existing
299
     * transient entity that has been persisted previously
300
     *
301
     * @param transientObject the entity to be persisted
302
     * @return The unique identifier of the persisted entity
303
     */
304
    public UUID saveOrUpdate(T transientObject);
305

    
306
    /**
307
     * Save new entities or update the persistent state of existing
308
     * transient entities that have been persisted previously
309
     *
310
     * @param transientObjects the entities to be persisted
311
     * @return The unique identifier of the persisted entity
312
     */
313
    public Map<UUID,T> saveOrUpdate(Collection<T> transientObjects);
314

    
315
    /**
316
     * Update the persistent state of an existing transient entity
317
     * that has been persisted previously
318
     *
319
     * @param transientObject the entity to be persisted
320
     * @return The unique identifier of the persisted entity
321
     */
322
    public UUID update(T transientObject);
323

    
324
    /**
325
     * Simply calls the load method.
326
     * Required specifically for the editor to allow load calls which
327
     * can also update the session cache.
328
     *
329
     * @param uuid
330
     * @return
331
     */
332
    public T loadWithUpdate(UUID uuid);
333

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

    
359
	public DeleteResult delete(T persistentObject);
360

    
361
    /**
362
     * Deletes a collection of persistent objects correponding to the
363
     * given list of uuids. The result will have status as ok if even one
364
     * of the deletes is successful, else error.
365
     *
366
     * @param persistentObjectUUIDs uuids of persistent objects to delete
367
     * @return DeleteResult object
368
     */
369
    public DeleteResult delete(Collection<UUID> persistentObjectUUIDs);
370

    
371
    /**
372
     * Merges a list of detached objects and returns the new
373
     * list of merged objects
374
     *
375
     * @param detachedObjects
376
     * @return a list of merged objects
377
     */
378
    public List<T> merge(List<T> detachedObjects);
379

    
380
    /**
381
     * This method allows for the possibility of returning the input transient
382
     * entities instead of the merged persistent entity
383
     *
384
     * WARNING : This method should never be used when the objective of the merge
385
     * is to attach to an existing session which is the standard use case.
386
     * This method should only be used in the
387
     * case of an external call which does not use hibernate sessions and is
388
     * only interested in the entity as a POJO. Apart from the session information
389
     * the only other difference between the transient and persisted object is in the case
390
     * of new objects (id=0) where hibernate sets the id after commit. This id is copied
391
     * over to the transient entity in {@link PostMergeEntityListener#onMerge(MergeEvent,Map)}
392
     * making the two objects identical and allowing the transient object to be used further
393
     * as a POJO
394
     *
395
     * @param detachedObjects
396
     * @param returnTransientEntity
397
     * @return
398
     */
399
    public List<MergeResult<T>> merge(List<T> detachedObjects, boolean returnTransientEntity);
400

    
401
    /**
402
     * This method allows for the possibility of returning the input transient
403
     * entity instead of the merged persistent entity
404
     *
405
     * WARNING : This method should never be used when the objective of the merge
406
     * is to attach to an existing session which is the standard use case.
407
     * This method should only be used in the case of an external call which does
408
     * not use hibernate sessions and is only interested in the entity as a POJO.
409
     * This method returns the root merged transient entity as well as all newly merged
410
     * persistent entities within the return object.
411
     *
412
     * @param newInstance
413
     * @param returnTransientEntity
414
     * @return
415
     */
416
    public MergeResult<T> merge(T newInstance, boolean returnTransientEntity);
417

    
418
}
(57-57/97)