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.application;
|
11
|
|
12
|
import javax.sql.DataSource;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.hibernate.HibernateException;
|
16
|
import org.hibernate.Session;
|
17
|
import org.hibernate.SessionFactory;
|
18
|
import org.springframework.beans.BeansException;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.context.ApplicationContext;
|
21
|
import org.springframework.context.ApplicationContextAware;
|
22
|
import org.springframework.context.annotation.Lazy;
|
23
|
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
24
|
import org.springframework.security.authentication.ProviderManager;
|
25
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
26
|
import org.springframework.security.core.Authentication;
|
27
|
import org.springframework.security.core.context.SecurityContext;
|
28
|
import org.springframework.security.core.context.SecurityContextHolder;
|
29
|
import org.springframework.stereotype.Component;
|
30
|
import org.springframework.transaction.PlatformTransactionManager;
|
31
|
import org.springframework.transaction.TransactionDefinition;
|
32
|
import org.springframework.transaction.TransactionStatus;
|
33
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
34
|
|
35
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
36
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
37
|
import eu.etaxonomy.cdm.api.service.IAnnotationService;
|
38
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
39
|
import eu.etaxonomy.cdm.api.service.ICollectionService;
|
40
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
41
|
import eu.etaxonomy.cdm.api.service.IDatabaseService;
|
42
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
43
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
44
|
import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
|
45
|
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
|
46
|
import eu.etaxonomy.cdm.api.service.IEventBaseService;
|
47
|
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
|
48
|
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
|
49
|
import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
|
50
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
51
|
import eu.etaxonomy.cdm.api.service.IIdentificationKeyService;
|
52
|
import eu.etaxonomy.cdm.api.service.ILocationService;
|
53
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
54
|
import eu.etaxonomy.cdm.api.service.IMetadataService;
|
55
|
import eu.etaxonomy.cdm.api.service.INameService;
|
56
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
57
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
|
58
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
|
59
|
import eu.etaxonomy.cdm.api.service.IPreferenceService;
|
60
|
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
|
61
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
62
|
import eu.etaxonomy.cdm.api.service.IRegistrationService;
|
63
|
import eu.etaxonomy.cdm.api.service.IRightsService;
|
64
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
65
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
66
|
import eu.etaxonomy.cdm.api.service.ITermNodeService;
|
67
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
68
|
import eu.etaxonomy.cdm.api.service.ITermTreeService;
|
69
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
70
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
71
|
import eu.etaxonomy.cdm.api.service.longrunningService.ILongRunningTasksService;
|
72
|
import eu.etaxonomy.cdm.api.service.media.MediaInfoFactory;
|
73
|
import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
|
74
|
import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
|
75
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
76
|
import eu.etaxonomy.cdm.persistence.permission.ICdmPermissionEvaluator;
|
77
|
|
78
|
/**
|
79
|
* This class actually is the central access point to all cdm api services and thus to all the
|
80
|
* entities stored in the cdm. From this point of view this class provides a "repository" view
|
81
|
* on the cdm by which you can access everything important for client development.
|
82
|
*
|
83
|
* @author a.mueller
|
84
|
* @since 21.05.2008
|
85
|
*/
|
86
|
@Component
|
87
|
public class CdmRepository implements ICdmRepository, ApplicationContextAware {
|
88
|
|
89
|
private static final Logger logger = Logger.getLogger(CdmRepository.class);
|
90
|
|
91
|
protected ApplicationContext applicationContext;
|
92
|
|
93
|
@Autowired
|
94
|
//@Qualifier("nameService")
|
95
|
private IAnnotationService annotationService;
|
96
|
|
97
|
@Autowired
|
98
|
//@Qualifier("nameService")
|
99
|
private INameService nameService;
|
100
|
@Autowired
|
101
|
//@Qualifier("taxonService")
|
102
|
private ITaxonService taxonService;
|
103
|
@Autowired
|
104
|
//@Qualifier("classificationService")
|
105
|
private IClassificationService classificationService;
|
106
|
@Autowired
|
107
|
//@Qualifier("referenceService")
|
108
|
private IReferenceService referenceService;
|
109
|
@Autowired
|
110
|
//@Qualifier("agentService")
|
111
|
private IAgentService agentService;
|
112
|
@Autowired
|
113
|
//@Qualifier("databaseService")
|
114
|
private IDatabaseService databaseService;
|
115
|
@Autowired
|
116
|
//@Qualifier("termService")
|
117
|
private ITermService termService;
|
118
|
//@Autowired
|
119
|
private HibernateTransactionManager transactionManager;
|
120
|
@Autowired
|
121
|
//@Qualifier("descriptionService")
|
122
|
private IDescriptionService descriptionService;
|
123
|
@Autowired
|
124
|
//@Qualifier("occurrenceService")
|
125
|
private IOccurrenceService occurrenceService;
|
126
|
@Autowired
|
127
|
//@Qualifier("primerService")
|
128
|
private IPrimerService primerService;
|
129
|
@Autowired
|
130
|
//@Qualifier("amplificationService")
|
131
|
private IAmplificationService amplificationService;
|
132
|
@Autowired
|
133
|
//@Qualifier("sequenceService")
|
134
|
private ISequenceService sequenceService;
|
135
|
@Autowired
|
136
|
//@Qualifier("eventBaseService")
|
137
|
private IEventBaseService eventBaseService;
|
138
|
@Autowired
|
139
|
//@Qualifier("mediaService")
|
140
|
private IMediaService mediaService;
|
141
|
@Autowired
|
142
|
//@Qualifier("mediaService")
|
143
|
private IMetadataService metadataService;
|
144
|
@Autowired
|
145
|
//@Qualifier("commonService")
|
146
|
private ICommonService commonService;
|
147
|
@Autowired
|
148
|
private ILocationService locationService;
|
149
|
//@Autowired
|
150
|
private SessionFactory sessionFactory;
|
151
|
//@Autowired
|
152
|
private DataSource dataSource;
|
153
|
@Autowired
|
154
|
@Lazy
|
155
|
private ProviderManager authenticationManager;
|
156
|
@Autowired
|
157
|
private IUserService userService;
|
158
|
@Autowired
|
159
|
private IGrantedAuthorityService grantedAuthorityService;
|
160
|
@Autowired
|
161
|
private IGroupService groupService;
|
162
|
@Autowired
|
163
|
private ICollectionService collectionService;
|
164
|
@Autowired
|
165
|
private IFeatureTreeService featureTreeService;
|
166
|
@Autowired
|
167
|
private IFeatureNodeService featureNodeService;
|
168
|
@Autowired
|
169
|
private ITermTreeService termTreeService;
|
170
|
@Autowired
|
171
|
private ITermNodeService termNodeService;
|
172
|
@Autowired
|
173
|
private IVocabularyService vocabularyService;
|
174
|
@Autowired
|
175
|
private ITaxonNodeService taxonNodeService;
|
176
|
@Autowired
|
177
|
private IIdentificationKeyService identificationKeyService;
|
178
|
@Autowired
|
179
|
private IPolytomousKeyService polytomousKeyService;
|
180
|
@Autowired
|
181
|
private IPolytomousKeyNodeService polytomousKeyNodeService;
|
182
|
@Autowired
|
183
|
private IProgressMonitorService progressMonitorService;
|
184
|
@Autowired
|
185
|
private IEntityValidationService entityValidationService;
|
186
|
@Autowired
|
187
|
private IPreferenceService preferenceService;
|
188
|
@Autowired
|
189
|
private IRightsService rightsService;
|
190
|
@Autowired
|
191
|
private IRegistrationService registrationService;
|
192
|
@Autowired
|
193
|
private ILongRunningTasksService longRunningTasksService;
|
194
|
@Autowired
|
195
|
private IEntityConstraintViolationService entityConstraintViolationService;
|
196
|
@Autowired
|
197
|
private ICdmPermissionEvaluator permissionEvaluator;
|
198
|
@Autowired
|
199
|
private MediaInfoFactory mediaInfoFactory; // FIXME define and use interface
|
200
|
@Autowired
|
201
|
private SessionFactory factory;
|
202
|
|
203
|
@Autowired
|
204
|
private IDescriptiveDataSetService descriptiveDataSetService;
|
205
|
|
206
|
//********************** CONSTRUCTOR *********************************************************/
|
207
|
|
208
|
protected CdmRepository(){}
|
209
|
|
210
|
// ****************************** APPLICATION CONTEXT *************************************************/
|
211
|
|
212
|
@Override
|
213
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
|
214
|
this.applicationContext = applicationContext;
|
215
|
}
|
216
|
|
217
|
// ****************************** GETTER *************************************************/
|
218
|
|
219
|
@Override
|
220
|
public Object getBean(String name){
|
221
|
return this.applicationContext.getBean(name);
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public IAnnotationService getAnnotationService(){
|
226
|
return this.annotationService;
|
227
|
}
|
228
|
|
229
|
@Override
|
230
|
public IAgentService getAgentService(){
|
231
|
return this.agentService;
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
public IDatabaseService getDatabaseService(){
|
236
|
return this.databaseService;
|
237
|
}
|
238
|
|
239
|
|
240
|
@Autowired
|
241
|
public void setDataSource(DataSource dataSource){
|
242
|
this.dataSource = dataSource;
|
243
|
}
|
244
|
|
245
|
@Override
|
246
|
public INameService getNameService(){
|
247
|
return this.nameService;
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
public IReferenceService getReferenceService(){
|
252
|
return this.referenceService;
|
253
|
}
|
254
|
|
255
|
@Autowired
|
256
|
public void setSessionFactory(SessionFactory sessionFactory){
|
257
|
this.sessionFactory = sessionFactory;
|
258
|
}
|
259
|
|
260
|
@Override
|
261
|
public ITaxonService getTaxonService(){
|
262
|
return this.taxonService;
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
public IClassificationService getClassificationService(){
|
267
|
return this.classificationService;
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
public ITaxonNodeService getTaxonNodeService(){
|
272
|
return this.taxonNodeService;
|
273
|
}
|
274
|
|
275
|
@Override
|
276
|
public IDescriptionService getDescriptionService(){
|
277
|
return this.descriptionService;
|
278
|
}
|
279
|
|
280
|
@Override
|
281
|
public IOccurrenceService getOccurrenceService(){
|
282
|
return this.occurrenceService;
|
283
|
}
|
284
|
|
285
|
@Override
|
286
|
public IPrimerService getPrimerService(){
|
287
|
return this.primerService;
|
288
|
}
|
289
|
|
290
|
@Override
|
291
|
public IAmplificationService getAmplificationService(){
|
292
|
return this.amplificationService;
|
293
|
}
|
294
|
|
295
|
@Override
|
296
|
public ISequenceService getSequenceService(){
|
297
|
return this.sequenceService;
|
298
|
}
|
299
|
|
300
|
@Override
|
301
|
public IEventBaseService getEventBaseService() {
|
302
|
return this.eventBaseService;
|
303
|
}
|
304
|
|
305
|
@Override
|
306
|
public IMediaService getMediaService(){
|
307
|
return this.mediaService;
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* {@inheritDoc}
|
312
|
*/
|
313
|
@Override
|
314
|
public IMetadataService getMetadataService() {
|
315
|
return this.metadataService;
|
316
|
}
|
317
|
|
318
|
@Override
|
319
|
public ITermService getTermService(){
|
320
|
return this.termService;
|
321
|
}
|
322
|
|
323
|
@Override
|
324
|
public ICommonService getCommonService(){
|
325
|
return this.commonService;
|
326
|
}
|
327
|
|
328
|
@Override
|
329
|
public ILocationService getLocationService(){
|
330
|
return this.locationService;
|
331
|
}
|
332
|
|
333
|
@Override
|
334
|
public IUserService getUserService(){
|
335
|
return this.userService;
|
336
|
}
|
337
|
|
338
|
@Override
|
339
|
public IGrantedAuthorityService getGrantedAuthorityService(){
|
340
|
return this.grantedAuthorityService;
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
public PlatformTransactionManager getTransactionManager(){
|
345
|
return this.transactionManager;
|
346
|
}
|
347
|
|
348
|
@Autowired
|
349
|
public void setTransactionManager(PlatformTransactionManager transactionManager){
|
350
|
this.transactionManager = (HibernateTransactionManager) transactionManager;
|
351
|
}
|
352
|
|
353
|
@Override
|
354
|
public ProviderManager getAuthenticationManager(){
|
355
|
return this.authenticationManager;
|
356
|
}
|
357
|
|
358
|
@Override
|
359
|
public ConversationHolder NewConversation(){
|
360
|
// TODO make this a prototype
|
361
|
return new ConversationHolder(dataSource, sessionFactory, transactionManager);
|
362
|
}
|
363
|
|
364
|
@Override
|
365
|
public ICollectionService getCollectionService(){
|
366
|
return collectionService;
|
367
|
}
|
368
|
|
369
|
@Override
|
370
|
public IFeatureTreeService getFeatureTreeService(){
|
371
|
return featureTreeService;
|
372
|
}
|
373
|
|
374
|
@Override
|
375
|
public ITermTreeService getTermTreeService(){
|
376
|
return termTreeService;
|
377
|
}
|
378
|
|
379
|
@Override
|
380
|
public IFeatureNodeService getFeatureNodeService(){
|
381
|
return featureNodeService;
|
382
|
}
|
383
|
|
384
|
@Override
|
385
|
public ITermNodeService getTermNodeService(){
|
386
|
return termNodeService;
|
387
|
}
|
388
|
|
389
|
@Override
|
390
|
public IPreferenceService getPreferenceService(){
|
391
|
return preferenceService;
|
392
|
}
|
393
|
|
394
|
@Override
|
395
|
public IVocabularyService getVocabularyService(){
|
396
|
return vocabularyService;
|
397
|
}
|
398
|
|
399
|
@Override
|
400
|
public IIdentificationKeyService getIdentificationKeyService(){
|
401
|
return identificationKeyService;
|
402
|
}
|
403
|
|
404
|
@Override
|
405
|
public IPolytomousKeyService getPolytomousKeyService(){
|
406
|
return polytomousKeyService;
|
407
|
}
|
408
|
|
409
|
@Override
|
410
|
public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
|
411
|
return polytomousKeyNodeService;
|
412
|
}
|
413
|
|
414
|
@Override
|
415
|
public IProgressMonitorService getProgressMonitorService() {
|
416
|
return progressMonitorService;
|
417
|
}
|
418
|
|
419
|
@Override
|
420
|
public IDescriptiveDataSetService getDescriptiveDataSetService(){
|
421
|
return descriptiveDataSetService;
|
422
|
}
|
423
|
|
424
|
@Override
|
425
|
public IGroupService getGroupService(){
|
426
|
return groupService;
|
427
|
}
|
428
|
|
429
|
@Override
|
430
|
public IEntityValidationService getEntityValidationService(){
|
431
|
return entityValidationService;
|
432
|
}
|
433
|
|
434
|
@Override
|
435
|
public IEntityConstraintViolationService getEntityConstraintViolationService(){
|
436
|
return entityConstraintViolationService;
|
437
|
}
|
438
|
|
439
|
@Override
|
440
|
public ICdmPermissionEvaluator getPermissionEvaluator(){
|
441
|
return permissionEvaluator;
|
442
|
}
|
443
|
|
444
|
public MediaInfoFactory getMediaInfoFactory() { // FIXME define and use interface
|
445
|
return mediaInfoFactory;
|
446
|
}
|
447
|
|
448
|
@Override
|
449
|
public TransactionStatus startTransaction(){
|
450
|
return startTransaction(false);
|
451
|
}
|
452
|
|
453
|
@Override
|
454
|
public TransactionStatus startTransaction(Boolean readOnly){
|
455
|
|
456
|
PlatformTransactionManager txManager = getTransactionManager();
|
457
|
|
458
|
DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
|
459
|
defaultTxDef.setReadOnly(readOnly);
|
460
|
TransactionDefinition txDef = defaultTxDef;
|
461
|
|
462
|
// Log some transaction-related debug information.
|
463
|
if (logger.isDebugEnabled()) {
|
464
|
logger.debug("Transaction name = " + txDef.getName());
|
465
|
logger.debug("Transaction facets:");
|
466
|
logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
|
467
|
logger.debug("Isolation level = " + txDef.getIsolationLevel());
|
468
|
logger.debug("Timeout = " + txDef.getTimeout());
|
469
|
logger.debug("Read Only = " + txDef.isReadOnly());
|
470
|
// org.springframework.orm.hibernate5.HibernateTransactionManager
|
471
|
// provides more transaction/session-related debug information.
|
472
|
}
|
473
|
|
474
|
TransactionStatus txStatus = txManager.getTransaction(txDef);
|
475
|
return txStatus;
|
476
|
}
|
477
|
|
478
|
@Override
|
479
|
public void commitTransaction(TransactionStatus txStatus){
|
480
|
PlatformTransactionManager txManager = getTransactionManager();
|
481
|
txManager.commit(txStatus);
|
482
|
return;
|
483
|
}
|
484
|
|
485
|
@Override
|
486
|
public void rollbackTransaction(TransactionStatus txStatus){
|
487
|
PlatformTransactionManager txManager = getTransactionManager();
|
488
|
txManager.rollback(txStatus);
|
489
|
return;
|
490
|
}
|
491
|
|
492
|
@Override
|
493
|
public void authenticate(String username, String password){
|
494
|
UsernamePasswordAuthenticationToken tokenForUser = new UsernamePasswordAuthenticationToken(username, password);
|
495
|
Authentication authentication = this.getAuthenticationManager().authenticate(tokenForUser);
|
496
|
SecurityContext context = SecurityContextHolder.getContext();
|
497
|
context.setAuthentication(authentication);
|
498
|
}
|
499
|
|
500
|
@Override
|
501
|
public IRightsService getRightsService() {
|
502
|
return rightsService;
|
503
|
}
|
504
|
|
505
|
@Override
|
506
|
public IRegistrationService getRegistrationService() {
|
507
|
return registrationService;
|
508
|
}
|
509
|
|
510
|
@Override
|
511
|
public ILongRunningTasksService getLongRunningTasksService() {
|
512
|
return longRunningTasksService;
|
513
|
}
|
514
|
|
515
|
public SessionFactory getSessionFactory() {
|
516
|
return factory;
|
517
|
}
|
518
|
|
519
|
/**
|
520
|
* Returns the current session as obtained from the session factory.
|
521
|
*
|
522
|
* @throws HibernateException
|
523
|
* In case a session bound to the current thread cannot be found
|
524
|
* @return the session bound to the current thread
|
525
|
*/
|
526
|
public Session getSession() throws HibernateException {
|
527
|
return factory.getCurrentSession();
|
528
|
}
|
529
|
|
530
|
/**
|
531
|
* clears the current Session
|
532
|
*/
|
533
|
public void clearSession() {
|
534
|
try {
|
535
|
getSession().clear();
|
536
|
} catch (HibernateException e) {
|
537
|
// no current session: nothing to clear!
|
538
|
}
|
539
|
}
|
540
|
|
541
|
}
|