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.orm.hibernate5.HibernateTransactionManager;
23
import org.springframework.security.authentication.ProviderManager;
24
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
25
import org.springframework.security.core.Authentication;
26
import org.springframework.security.core.context.SecurityContext;
27
import org.springframework.security.core.context.SecurityContextHolder;
28
import org.springframework.stereotype.Component;
29
import org.springframework.transaction.PlatformTransactionManager;
30
import org.springframework.transaction.TransactionDefinition;
31
import org.springframework.transaction.TransactionStatus;
32
import org.springframework.transaction.support.DefaultTransactionDefinition;
33

    
34
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35
import eu.etaxonomy.cdm.api.service.IAgentService;
36
import eu.etaxonomy.cdm.api.service.IAnnotationService;
37
import eu.etaxonomy.cdm.api.service.IClassificationService;
38
import eu.etaxonomy.cdm.api.service.ICollectionService;
39
import eu.etaxonomy.cdm.api.service.ICommonService;
40
import eu.etaxonomy.cdm.api.service.IDatabaseService;
41
import eu.etaxonomy.cdm.api.service.IDescriptionService;
42
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
43
import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
44
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
45
import eu.etaxonomy.cdm.api.service.IEventBaseService;
46
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
47
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
48
import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
49
import eu.etaxonomy.cdm.api.service.IGroupService;
50
import eu.etaxonomy.cdm.api.service.IIdentificationKeyService;
51
import eu.etaxonomy.cdm.api.service.ILocationService;
52
import eu.etaxonomy.cdm.api.service.IMediaService;
53
import eu.etaxonomy.cdm.api.service.IMetadataService;
54
import eu.etaxonomy.cdm.api.service.INameService;
55
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
56
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
57
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
58
import eu.etaxonomy.cdm.api.service.IPreferenceService;
59
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
60
import eu.etaxonomy.cdm.api.service.IReferenceService;
61
import eu.etaxonomy.cdm.api.service.IRegistrationService;
62
import eu.etaxonomy.cdm.api.service.IRightsService;
63
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
64
import eu.etaxonomy.cdm.api.service.ITaxonService;
65
import eu.etaxonomy.cdm.api.service.ITermService;
66
import eu.etaxonomy.cdm.api.service.IUserService;
67
import eu.etaxonomy.cdm.api.service.IVocabularyService;
68
import eu.etaxonomy.cdm.api.service.molecular.IAmplificationService;
69
import eu.etaxonomy.cdm.api.service.molecular.IPrimerService;
70
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
71
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
72

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

    
85
	protected ApplicationContext applicationContext;
86

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

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

    
188
	@Autowired
189
	private IDescriptiveDataSetService descriptiveDataSetService;
190

    
191

    
192
	//********************** CONSTRUCTOR *********************************************************/
193

    
194
	/**
195
	 * Constructor
196
	 */
197
	protected CdmRepository(){}
198

    
199

    
200
	// ****************************** APPLICATION CONTEXT *************************************************/
201

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

    
207
	// ****************************** GETTER *************************************************/
208

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

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

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

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

    
229

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
338

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

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

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

    
355
	@Override
356
	public ICollectionService getCollectionService(){
357
		return collectionService;
358
	}
359

    
360
	@Override
361
	public IFeatureTreeService getFeatureTreeService(){
362
		return featureTreeService;
363
	}
364

    
365
	@Override
366
	public IFeatureNodeService getFeatureNodeService(){
367
		return featureNodeService;
368
	}
369

    
370
	@Override
371
    public IPreferenceService getPreferenceService(){
372
        return preferenceService;
373
    }
374

    
375
	@Override
376
	public IVocabularyService getVocabularyService(){
377
		return vocabularyService;
378
	}
379

    
380
	@Override
381
	public IIdentificationKeyService getIdentificationKeyService(){
382
		return identificationKeyService;
383
	}
384

    
385
	@Override
386
	public IPolytomousKeyService getPolytomousKeyService(){
387
		return polytomousKeyService;
388
	}
389

    
390

    
391
	@Override
392
	public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
393
		return polytomousKeyNodeService;
394
	}
395

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

    
404
	@Override
405
	public IDescriptiveDataSetService getDescriptiveDataSetService(){
406
		return descriptiveDataSetService;
407
	}
408

    
409
	@Override
410
	public IGroupService getGroupService(){
411
		return groupService;
412
	}
413

    
414

    
415
	@Override
416
	public IEntityValidationService getEntityValidationService(){
417
		return entityValidationService;
418
	}
419

    
420

    
421
	@Override
422
	public IEntityConstraintViolationService getEntityConstraintViolationService(){
423
		return entityConstraintViolationService;
424
	}
425

    
426
	@Override
427
	public ICdmPermissionEvaluator getPermissionEvaluator(){
428
		return permissionEvaluator;
429
	}
430

    
431

    
432
	@Override
433
	public TransactionStatus startTransaction(){
434
		return startTransaction(false);
435
	}
436

    
437
	@Override
438
	public TransactionStatus startTransaction(Boolean readOnly){
439

    
440
		PlatformTransactionManager txManager = getTransactionManager();
441

    
442
		DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
443
		defaultTxDef.setReadOnly(readOnly);
444
		TransactionDefinition txDef = defaultTxDef;
445

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

    
458
		TransactionStatus txStatus = txManager.getTransaction(txDef);
459
		return txStatus;
460
	}
461

    
462

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

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

    
478
    @Override
479
    public IRightsService getRightsService() {
480
        return rightsService;
481
    }
482

    
483
    @Override
484
    public IRegistrationService getRegistrationService() {
485
        return registrationService;
486
    }
487

    
488
    public SessionFactory getSessionFactory() {
489
        return factory;
490
    }
491

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

    
507
        return session;
508
    }
509

    
510
}
(3-3/9)