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