Project

General

Profile

Download (14.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.ITermService;
67
import eu.etaxonomy.cdm.api.service.IUserService;
68
import eu.etaxonomy.cdm.api.service.IVocabularyService;
69
import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
70
import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
71
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
72
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
73

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

    
86
	protected ApplicationContext applicationContext;
87

    
88
	@Autowired
89
    //@Qualifier("nameService")
90
    private IAnnotationService annotationService;
91

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

    
190
	@Autowired
191
	private IDescriptiveDataSetService descriptiveDataSetService;
192

    
193

    
194
	//********************** CONSTRUCTOR *********************************************************/
195

    
196
	/**
197
	 * Constructor
198
	 */
199
	protected CdmRepository(){}
200

    
201

    
202
	// ****************************** APPLICATION CONTEXT *************************************************/
203

    
204
	@Override
205
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
206
		this.applicationContext = applicationContext;
207
	}
208

    
209
	// ****************************** GETTER *************************************************/
210

    
211
	@Override
212
	public final Object getBean(String name){
213
	    return this.applicationContext.getBean(name);
214
	}
215

    
216
	@Override
217
	public IAnnotationService getAnnotationService(){
218
	    return this.annotationService;
219
	}
220

    
221
	@Override
222
	public IAgentService getAgentService(){
223
		return this.agentService;
224
	}
225

    
226
	@Override
227
	public IDatabaseService getDatabaseService(){
228
		return this.databaseService;
229
	}
230

    
231

    
232
	@Autowired
233
	public void setDataSource(DataSource dataSource){
234
		this.dataSource = dataSource;
235
	}
236

    
237
	@Override
238
	public INameService getNameService(){
239
		return this.nameService;
240
	}
241

    
242
	@Override
243
	public IReferenceService getReferenceService(){
244
		return this.referenceService;
245
	}
246

    
247
	@Autowired
248
	public void setSessionFactory(SessionFactory sessionFactory){
249
		this.sessionFactory = sessionFactory;
250
	}
251

    
252
	@Override
253
	public ITaxonService getTaxonService(){
254
		return this.taxonService;
255
	}
256

    
257
	@Override
258
	public IClassificationService getClassificationService(){
259
		return this.classificationService;
260
	}
261

    
262
	@Override
263
	public ITaxonNodeService getTaxonNodeService(){
264
		return this.taxonNodeService;
265
	}
266

    
267
	@Override
268
	public IDescriptionService getDescriptionService(){
269
		return this.descriptionService;
270
	}
271

    
272
	@Override
273
	public IOccurrenceService getOccurrenceService(){
274
		return this.occurrenceService;
275
	}
276

    
277
	@Override
278
	public IPrimerService getPrimerService(){
279
		return this.primerService;
280
	}
281

    
282
	@Override
283
	public IAmplificationService getAmplificationService(){
284
		return this.amplificationService;
285
	}
286

    
287
	@Override
288
	public ISequenceService getSequenceService(){
289
		return this.sequenceService;
290
	}
291

    
292
	@Override
293
	public IEventBaseService getEventBaseService() {
294
	    return this.eventBaseService;
295
	}
296

    
297
	@Override
298
	public IMediaService getMediaService(){
299
		return this.mediaService;
300
	}
301

    
302
    /**
303
     * {@inheritDoc}
304
     */
305
    @Override
306
    public IMetadataService getMetadataService() {
307
        return this.metadataService;
308
    }
309

    
310
	@Override
311
	public ITermService getTermService(){
312
		return this.termService;
313
	}
314

    
315
	@Override
316
	public ICommonService getCommonService(){
317
		return this.commonService;
318
	}
319

    
320
	@Override
321
	public ILocationService getLocationService(){
322
		return this.locationService;
323
	}
324

    
325
	@Override
326
	public IUserService getUserService(){
327
		return this.userService;
328
	}
329

    
330
	@Override
331
	public IGrantedAuthorityService getGrantedAuthorityService(){
332
		return this.grantedAuthorityService;
333
	}
334

    
335
	@Override
336
	public PlatformTransactionManager getTransactionManager(){
337
		return this.transactionManager;
338
	}
339

    
340

    
341
	@Autowired
342
	public void setTransactionManager(PlatformTransactionManager transactionManager){
343
		this.transactionManager = (HibernateTransactionManager) transactionManager;
344
	}
345

    
346
	@Override
347
	public ProviderManager getAuthenticationManager(){
348
		return this.authenticationManager;
349
	}
350

    
351
	@Override
352
	public ConversationHolder NewConversation(){
353
		// TODO make this a prototype
354
		return new ConversationHolder(dataSource, sessionFactory, transactionManager);
355
	}
356

    
357
	@Override
358
	public ICollectionService getCollectionService(){
359
		return collectionService;
360
	}
361

    
362
	@Override
363
	public IFeatureTreeService getFeatureTreeService(){
364
		return featureTreeService;
365
	}
366

    
367
	@Override
368
	public IFeatureNodeService getFeatureNodeService(){
369
		return featureNodeService;
370
	}
371

    
372
	@Override
373
    public IPreferenceService getPreferenceService(){
374
        return preferenceService;
375
    }
376

    
377
	@Override
378
	public IVocabularyService getVocabularyService(){
379
		return vocabularyService;
380
	}
381

    
382
	@Override
383
	public IIdentificationKeyService getIdentificationKeyService(){
384
		return identificationKeyService;
385
	}
386

    
387
	@Override
388
	public IPolytomousKeyService getPolytomousKeyService(){
389
		return polytomousKeyService;
390
	}
391

    
392

    
393
	@Override
394
	public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
395
		return polytomousKeyNodeService;
396
	}
397

    
398
    /**
399
     * {@inheritDoc}
400
     */
401
    @Override
402
    public IProgressMonitorService getProgressMonitorService() {
403
        return progressMonitorService;
404
    }
405

    
406
	@Override
407
	public IDescriptiveDataSetService getDescriptiveDataSetService(){
408
		return descriptiveDataSetService;
409
	}
410

    
411
	@Override
412
	public IGroupService getGroupService(){
413
		return groupService;
414
	}
415

    
416

    
417
	@Override
418
	public IEntityValidationService getEntityValidationService(){
419
		return entityValidationService;
420
	}
421

    
422

    
423
	@Override
424
	public IEntityConstraintViolationService getEntityConstraintViolationService(){
425
		return entityConstraintViolationService;
426
	}
427

    
428
	@Override
429
	public ICdmPermissionEvaluator getPermissionEvaluator(){
430
		return permissionEvaluator;
431
	}
432

    
433

    
434
	@Override
435
	public TransactionStatus startTransaction(){
436
		return startTransaction(false);
437
	}
438

    
439
	@Override
440
	public TransactionStatus startTransaction(Boolean readOnly){
441

    
442
		PlatformTransactionManager txManager = getTransactionManager();
443

    
444
		DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
445
		defaultTxDef.setReadOnly(readOnly);
446
		TransactionDefinition txDef = defaultTxDef;
447

    
448
		// Log some transaction-related debug information.
449
		if (logger.isDebugEnabled()) {
450
			logger.debug("Transaction name = " + txDef.getName());
451
			logger.debug("Transaction facets:");
452
			logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
453
			logger.debug("Isolation level = " + txDef.getIsolationLevel());
454
			logger.debug("Timeout = " + txDef.getTimeout());
455
			logger.debug("Read Only = " + txDef.isReadOnly());
456
			// org.springframework.orm.hibernate5.HibernateTransactionManager
457
			// provides more transaction/session-related debug information.
458
		}
459

    
460
		TransactionStatus txStatus = txManager.getTransaction(txDef);
461
		return txStatus;
462
	}
463

    
464

    
465
	@Override
466
	public void commitTransaction(TransactionStatus txStatus){
467
		PlatformTransactionManager txManager = getTransactionManager();
468
		txManager.commit(txStatus);
469
		return;
470
	}
471

    
472
	@Override
473
	public void authenticate(String username, String password){
474
		UsernamePasswordAuthenticationToken tokenForUser = new UsernamePasswordAuthenticationToken(username, password);
475
		Authentication authentication = this.getAuthenticationManager().authenticate(tokenForUser);
476
		SecurityContext context = SecurityContextHolder.getContext();
477
		context.setAuthentication(authentication);
478
	}
479

    
480
    @Override
481
    public IRightsService getRightsService() {
482
        return rightsService;
483
    }
484

    
485
    @Override
486
    public IRegistrationService getRegistrationService() {
487
        return registrationService;
488
    }
489

    
490
    public SessionFactory getSessionFactory() {
491
        return factory;
492
    }
493

    
494
    /**
495
     * Returns the current session.
496
     * A new Session will be opened in case of a HiernateExecption occurs when getting the current Session.
497
     *
498
     * @return
499
     */
500
    public Session getSession(){
501
        Session session ;
502
        try {
503
            session = factory.getCurrentSession();
504
        } catch (HibernateException e) {
505
            logger.debug("Opening new session in turn of a HibernateException: " + e.getMessage());
506
            session = factory.openSession();
507
        }
508

    
509
        return session;
510
    }
511

    
512
}
(4-4/12)