Project

General

Profile

Download (15.4 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.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.molecular.IAmplificationService;
73
import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
74
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
75
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
76

    
77
/**
78
 * This class actually is the central access point to all cdm api services and thus to all the
79
 * entities stored in the cdm. From this point of view this class provides a "repository" view
80
 * on the cdm by which you can access everything important for client development.
81
 *
82
 * @author a.mueller
83
 * @since 21.05.2008
84
 */
85
@Component
86
public class CdmRepository implements ICdmRepository, ApplicationContextAware {
87

    
88
    private static final Logger logger = Logger.getLogger(CdmRepository.class);
89

    
90
	protected ApplicationContext applicationContext;
91

    
92
	@Autowired
93
    //@Qualifier("nameService")
94
    private IAnnotationService annotationService;
95

    
96
	@Autowired
97
	//@Qualifier("nameService")
98
	private INameService nameService;
99
	@Autowired
100
	//@Qualifier("taxonService")
101
	private ITaxonService taxonService;
102
	@Autowired
103
	//@Qualifier("classificationService")
104
	private IClassificationService classificationService;
105
	@Autowired
106
	//@Qualifier("referenceService")
107
	private IReferenceService referenceService;
108
	@Autowired
109
	//@Qualifier("agentService")
110
	private IAgentService agentService;
111
	@Autowired
112
	//@Qualifier("databaseService")
113
	private IDatabaseService databaseService;
114
	@Autowired
115
	//@Qualifier("termService")
116
	private ITermService termService;
117
	//@Autowired
118
	private HibernateTransactionManager transactionManager;
119
	@Autowired
120
	//@Qualifier("descriptionService")
121
	private IDescriptionService descriptionService;
122
	@Autowired
123
	//@Qualifier("occurrenceService")
124
	private IOccurrenceService occurrenceService;
125
	@Autowired
126
	//@Qualifier("primerService")
127
	private IPrimerService primerService;
128
	@Autowired
129
	//@Qualifier("amplificationService")
130
	private IAmplificationService amplificationService;
131
	@Autowired
132
	//@Qualifier("sequenceService")
133
	private ISequenceService sequenceService;
134
	@Autowired
135
	//@Qualifier("eventBaseService")
136
	private IEventBaseService eventBaseService;
137
	@Autowired
138
	//@Qualifier("mediaService")
139
	private IMediaService mediaService;
140
    @Autowired
141
    //@Qualifier("mediaService")
142
    private IMetadataService metadataService;
143
	@Autowired
144
	//@Qualifier("commonService")
145
	private ICommonService commonService;
146
	@Autowired
147
	private ILocationService locationService;
148
	//@Autowired
149
	private SessionFactory sessionFactory;
150
	//@Autowired
151
	private DataSource dataSource;
152
	@Autowired
153
	@Lazy
154
	private ProviderManager authenticationManager;
155
	@Autowired
156
	private IUserService userService;
157
	@Autowired
158
	private IGrantedAuthorityService grantedAuthorityService;
159
	@Autowired
160
	private IGroupService groupService;
161
	@Autowired
162
	private ICollectionService collectionService;
163
	@Autowired
164
	private IFeatureTreeService featureTreeService;
165
	@Autowired
166
	private IFeatureNodeService featureNodeService;
167
	@Autowired
168
	private ITermTreeService termTreeService;
169
	@Autowired
170
	private ITermNodeService termNodeService;
171
	@Autowired
172
	private IVocabularyService vocabularyService;
173
	@Autowired
174
	private ITaxonNodeService taxonNodeService;
175
	@Autowired
176
	private IIdentificationKeyService identificationKeyService;
177
	@Autowired
178
	private IPolytomousKeyService polytomousKeyService;
179
	@Autowired
180
	private IPolytomousKeyNodeService polytomousKeyNodeService;
181
	@Autowired
182
	private IProgressMonitorService progressMonitorService;
183
	@Autowired
184
	private IEntityValidationService entityValidationService;
185
	@Autowired
186
    private IPreferenceService preferenceService;
187
	@Autowired
188
    private IRightsService rightsService;
189
    @Autowired
190
    private IRegistrationService registrationService;
191
    @Autowired
192
    private ILongRunningTasksService longRunningTasksService;
193
	@Autowired
194
	private IEntityConstraintViolationService entityConstraintViolationService;
195
	@Autowired
196
	private ICdmPermissionEvaluator permissionEvaluator;
197
	@Autowired
198
    private SessionFactory factory;
199

    
200
	@Autowired
201
	private IDescriptiveDataSetService descriptiveDataSetService;
202

    
203

    
204
	//********************** CONSTRUCTOR *********************************************************/
205

    
206
	/**
207
	 * Constructor
208
	 */
209
	protected CdmRepository(){}
210

    
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 final 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

    
351
	@Autowired
352
	public void setTransactionManager(PlatformTransactionManager transactionManager){
353
		this.transactionManager = (HibernateTransactionManager) transactionManager;
354
	}
355

    
356
	@Override
357
	public ProviderManager getAuthenticationManager(){
358
		return this.authenticationManager;
359
	}
360

    
361
	@Override
362
	public ConversationHolder NewConversation(){
363
		// TODO make this a prototype
364
		return new ConversationHolder(dataSource, sessionFactory, transactionManager);
365
	}
366

    
367
	@Override
368
	public ICollectionService getCollectionService(){
369
		return collectionService;
370
	}
371

    
372
	@Override
373
	public IFeatureTreeService getFeatureTreeService(){
374
		return featureTreeService;
375
	}
376

    
377
	@Override
378
	public ITermTreeService getTermTreeService(){
379
	    return termTreeService;
380
	}
381

    
382
	@Override
383
	public IFeatureNodeService getFeatureNodeService(){
384
		return featureNodeService;
385
	}
386

    
387
    @Override
388
    public ITermNodeService getTermNodeService(){
389
        return termNodeService;
390
    }
391

    
392
	@Override
393
    public IPreferenceService getPreferenceService(){
394
        return preferenceService;
395
    }
396

    
397
	@Override
398
	public IVocabularyService getVocabularyService(){
399
		return vocabularyService;
400
	}
401

    
402
	@Override
403
	public IIdentificationKeyService getIdentificationKeyService(){
404
		return identificationKeyService;
405
	}
406

    
407
	@Override
408
	public IPolytomousKeyService getPolytomousKeyService(){
409
		return polytomousKeyService;
410
	}
411

    
412

    
413
	@Override
414
	public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
415
		return polytomousKeyNodeService;
416
	}
417

    
418
    /**
419
     * {@inheritDoc}
420
     */
421
    @Override
422
    public IProgressMonitorService getProgressMonitorService() {
423
        return progressMonitorService;
424
    }
425

    
426
	@Override
427
	public IDescriptiveDataSetService getDescriptiveDataSetService(){
428
		return descriptiveDataSetService;
429
	}
430

    
431
	@Override
432
	public IGroupService getGroupService(){
433
		return groupService;
434
	}
435

    
436

    
437
	@Override
438
	public IEntityValidationService getEntityValidationService(){
439
		return entityValidationService;
440
	}
441

    
442

    
443
	@Override
444
	public IEntityConstraintViolationService getEntityConstraintViolationService(){
445
		return entityConstraintViolationService;
446
	}
447

    
448
	@Override
449
	public ICdmPermissionEvaluator getPermissionEvaluator(){
450
		return permissionEvaluator;
451
	}
452

    
453

    
454
	@Override
455
	public TransactionStatus startTransaction(){
456
		return startTransaction(false);
457
	}
458

    
459
	@Override
460
	public TransactionStatus startTransaction(Boolean readOnly){
461

    
462
		PlatformTransactionManager txManager = getTransactionManager();
463

    
464
		DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
465
		defaultTxDef.setReadOnly(readOnly);
466
		TransactionDefinition txDef = defaultTxDef;
467

    
468
		// Log some transaction-related debug information.
469
		if (logger.isDebugEnabled()) {
470
			logger.debug("Transaction name = " + txDef.getName());
471
			logger.debug("Transaction facets:");
472
			logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
473
			logger.debug("Isolation level = " + txDef.getIsolationLevel());
474
			logger.debug("Timeout = " + txDef.getTimeout());
475
			logger.debug("Read Only = " + txDef.isReadOnly());
476
			// org.springframework.orm.hibernate5.HibernateTransactionManager
477
			// provides more transaction/session-related debug information.
478
		}
479

    
480
		TransactionStatus txStatus = txManager.getTransaction(txDef);
481
		return txStatus;
482
	}
483

    
484

    
485
	@Override
486
	public void commitTransaction(TransactionStatus txStatus){
487
		PlatformTransactionManager txManager = getTransactionManager();
488
		txManager.commit(txStatus);
489
		return;
490
	}
491

    
492
    @Override
493
    public void rollbackTransaction(TransactionStatus txStatus){
494
        PlatformTransactionManager txManager = getTransactionManager();
495
        txManager.rollback(txStatus);
496
        return;
497
    }
498

    
499
	@Override
500
	public void authenticate(String username, String password){
501
		UsernamePasswordAuthenticationToken tokenForUser = new UsernamePasswordAuthenticationToken(username, password);
502
		Authentication authentication = this.getAuthenticationManager().authenticate(tokenForUser);
503
		SecurityContext context = SecurityContextHolder.getContext();
504
		context.setAuthentication(authentication);
505
	}
506

    
507
    @Override
508
    public IRightsService getRightsService() {
509
        return rightsService;
510
    }
511

    
512
    @Override
513
    public IRegistrationService getRegistrationService() {
514
        return registrationService;
515
    }
516

    
517
    @Override
518
	public ILongRunningTasksService getLongRunningTasksService() {
519
		return longRunningTasksService;
520
	}
521

    
522
    public SessionFactory getSessionFactory() {
523
        return factory;
524
    }
525

    
526
    /**
527
     * Returns the current session as obtained from the session factory.
528
     *
529
     * @throws HibernateException
530
     *             In case a session bound to the current thread cannot be found
531
     * @return the session bound to the current thread
532
     */
533
    public Session getSession() throws HibernateException {
534
        return factory.getCurrentSession();
535
    }
536

    
537
    /**
538
     * clears the current Session
539
     */
540
    public void clearSession() {
541
        try {
542
            getSession().clear();
543
        } catch (HibernateException e) {
544
            // no current session: nothing to clear!
545
        }
546

    
547
    }
548

    
549

    
550

    
551

    
552
}
(4-4/12)