Project

General

Profile

Download (14.6 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.IEntityConstraintViolationService;
43
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
44
import eu.etaxonomy.cdm.api.service.IEventBaseService;
45
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
46
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
47
import eu.etaxonomy.cdm.api.service.IGrantedAuthorityService;
48
import eu.etaxonomy.cdm.api.service.IGroupService;
49
import eu.etaxonomy.cdm.api.service.IIdentificationKeyService;
50
import eu.etaxonomy.cdm.api.service.ILocationService;
51
import eu.etaxonomy.cdm.api.service.IMediaService;
52
import eu.etaxonomy.cdm.api.service.IMetadataService;
53
import eu.etaxonomy.cdm.api.service.INameService;
54
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
55
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
56
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
57
import eu.etaxonomy.cdm.api.service.IPreferenceService;
58
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
59
import eu.etaxonomy.cdm.api.service.IReferenceService;
60
import eu.etaxonomy.cdm.api.service.IRegistrationService;
61
import eu.etaxonomy.cdm.api.service.IRightsService;
62
import eu.etaxonomy.cdm.api.service.IService;
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.IWorkingSetService;
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.model.common.CdmBase;
73
import eu.etaxonomy.cdm.persistence.hibernate.permission.ICdmPermissionEvaluator;
74

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

    
87
	protected ApplicationContext applicationContext;
88

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

    
93
	@Autowired
94
	//@Qualifier("nameService")
95
	private INameService nameService;
96
	@Autowired
97
	//@Qualifier("taxonService")
98
	private ITaxonService taxonService;
99
	@Autowired
100
	//@Qualifier("classificationService")
101
	private IClassificationService classificationService;
102
	@Autowired
103
	//@Qualifier("referenceService")
104
	private IReferenceService referenceService;
105
	@Autowired
106
	//@Qualifier("agentService")
107
	private IAgentService agentService;
108
	@Autowired
109
	//@Qualifier("databaseService")
110
	private IDatabaseService databaseService;
111
	@Autowired
112
	//@Qualifier("termService")
113
	private ITermService termService;
114
	//@Autowired
115
	private HibernateTransactionManager transactionManager;
116
	@Autowired
117
	//@Qualifier("descriptionService")
118
	private IDescriptionService descriptionService;
119
	@Autowired
120
	//@Qualifier("occurrenceService")
121
	private IOccurrenceService occurrenceService;
122
	@Autowired
123
	//@Qualifier("primerService")
124
	private IPrimerService primerService;
125
	@Autowired
126
	//@Qualifier("amplificationService")
127
	private IAmplificationService amplificationService;
128
	@Autowired
129
	//@Qualifier("sequenceService")
130
	private ISequenceService sequenceService;
131
	@Autowired
132
	//@Qualifier("eventBaseService")
133
	private IEventBaseService eventBaseService;
134
	@Autowired
135
	//@Qualifier("mediaService")
136
	private IMediaService mediaService;
137
    @Autowired
138
    //@Qualifier("mediaService")
139
    private IMetadataService metadataService;
140
	@Autowired
141
	//@Qualifier("commonService")
142
	private ICommonService commonService;
143
	@Autowired
144
	private ILocationService locationService;
145
	//@Autowired
146
	private SessionFactory sessionFactory;
147
	//@Autowired
148
	private DataSource dataSource;
149
	@Autowired
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
	//@Qualifier("mainService")
192
	private IService<CdmBase> mainService;
193

    
194
	@Autowired
195
	private IWorkingSetService workingSetService;
196

    
197

    
198
	//********************** CONSTRUCTOR *********************************************************/
199

    
200
	/**
201
	 * Constructor
202
	 */
203
	protected CdmRepository(){}
204

    
205

    
206
	// ****************************** APPLICATION CONTEXT *************************************************/
207

    
208
	@Override
209
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
210
		this.applicationContext = applicationContext;
211
	}
212

    
213
	// ****************************** GETTER *************************************************/
214

    
215
	@Override
216
	public final Object getBean(String name){
217
	    return this.applicationContext.getBean(name);
218
	}
219

    
220
	@Override
221
	public IAnnotationService getAnnotationService(){
222
	    return this.annotationService;
223
	}
224

    
225
	@Override
226
	public IAgentService getAgentService(){
227
		return this.agentService;
228
	}
229

    
230
	@Override
231
	public IDatabaseService getDatabaseService(){
232
		return this.databaseService;
233
	}
234

    
235

    
236
	@Autowired
237
	public void setDataSource(DataSource dataSource){
238
		this.dataSource = dataSource;
239
	}
240

    
241
	@Override
242
	public INameService getNameService(){
243
		return this.nameService;
244
	}
245

    
246
	@Override
247
	public IReferenceService getReferenceService(){
248
		return this.referenceService;
249
	}
250

    
251
	@Autowired
252
	public void setSessionFactory(SessionFactory sessionFactory){
253
		this.sessionFactory = sessionFactory;
254
	}
255

    
256
	@Override
257
	public ITaxonService getTaxonService(){
258
		return this.taxonService;
259
	}
260

    
261
	@Override
262
	public IClassificationService getClassificationService(){
263
		return this.classificationService;
264
	}
265

    
266
	@Override
267
	public ITaxonNodeService getTaxonNodeService(){
268
		return this.taxonNodeService;
269
	}
270

    
271
	@Override
272
	public IDescriptionService getDescriptionService(){
273
		return this.descriptionService;
274
	}
275

    
276
	@Override
277
	public IOccurrenceService getOccurrenceService(){
278
		return this.occurrenceService;
279
	}
280

    
281
	@Override
282
	public IPrimerService getPrimerService(){
283
		return this.primerService;
284
	}
285

    
286
	@Override
287
	public IAmplificationService getAmplificationService(){
288
		return this.amplificationService;
289
	}
290

    
291
	@Override
292
	public ISequenceService getSequenceService(){
293
		return this.sequenceService;
294
	}
295

    
296
	@Override
297
	public IEventBaseService getEventBaseService() {
298
	    return this.eventBaseService;
299
	}
300

    
301
	@Override
302
	public IMediaService getMediaService(){
303
		return this.mediaService;
304
	}
305

    
306
    /**
307
     * {@inheritDoc}
308
     */
309
    @Override
310
    public IMetadataService getMetadataService() {
311
        return this.metadataService;
312
    }
313

    
314
	@Override
315
	public ITermService getTermService(){
316
		return this.termService;
317
	}
318

    
319
	@Override
320
	public ICommonService getCommonService(){
321
		return this.commonService;
322
	}
323

    
324
	@Override
325
	public ILocationService getLocationService(){
326
		return this.locationService;
327
	}
328

    
329
	@Override
330
	public IUserService getUserService(){
331
		return this.userService;
332
	}
333

    
334
	@Override
335
	public IGrantedAuthorityService getGrantedAuthorityService(){
336
		return this.grantedAuthorityService;
337
	}
338

    
339
	@Override
340
	public IService<CdmBase> getMainService(){
341
		return this.mainService;
342
	}
343

    
344
	@Override
345
	public PlatformTransactionManager getTransactionManager(){
346
		return this.transactionManager;
347
	}
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 IFeatureNodeService getFeatureNodeService(){
378
		return featureNodeService;
379
	}
380

    
381
	@Override
382
    public IPreferenceService getPreferenceService(){
383
        return preferenceService;
384
    }
385

    
386
	@Override
387
	public IVocabularyService getVocabularyService(){
388
		return vocabularyService;
389
	}
390

    
391
	@Override
392
	public IIdentificationKeyService getIdentificationKeyService(){
393
		return identificationKeyService;
394
	}
395

    
396
	@Override
397
	public IPolytomousKeyService getPolytomousKeyService(){
398
		return polytomousKeyService;
399
	}
400

    
401

    
402
	@Override
403
	public IPolytomousKeyNodeService getPolytomousKeyNodeService(){
404
		return polytomousKeyNodeService;
405
	}
406

    
407
    /**
408
     * {@inheritDoc}
409
     */
410
    @Override
411
    public IProgressMonitorService getProgressMonitorService() {
412
        return progressMonitorService;
413
    }
414

    
415
	@Override
416
	public IWorkingSetService getWorkingSetService(){
417
		return workingSetService;
418
	}
419

    
420
	@Override
421
	public IGroupService getGroupService(){
422
		return groupService;
423
	}
424

    
425

    
426
	@Override
427
	public IEntityValidationService getEntityValidationService(){
428
		return entityValidationService;
429
	}
430

    
431

    
432
	@Override
433
	public IEntityConstraintViolationService getEntityConstraintViolationService(){
434
		return entityConstraintViolationService;
435
	}
436

    
437
	@Override
438
	public ICdmPermissionEvaluator getPermissionEvaluator(){
439
		return permissionEvaluator;
440
	}
441

    
442

    
443
	@Override
444
	public TransactionStatus startTransaction(){
445
		return startTransaction(false);
446
	}
447

    
448
	@Override
449
	public TransactionStatus startTransaction(Boolean readOnly){
450

    
451
		PlatformTransactionManager txManager = getTransactionManager();
452

    
453
		DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
454
		defaultTxDef.setReadOnly(readOnly);
455
		TransactionDefinition txDef = defaultTxDef;
456

    
457
		// Log some transaction-related debug information.
458
		if (logger.isDebugEnabled()) {
459
			logger.debug("Transaction name = " + txDef.getName());
460
			logger.debug("Transaction facets:");
461
			logger.debug("Propagation behavior = " + txDef.getPropagationBehavior());
462
			logger.debug("Isolation level = " + txDef.getIsolationLevel());
463
			logger.debug("Timeout = " + txDef.getTimeout());
464
			logger.debug("Read Only = " + txDef.isReadOnly());
465
			// org.springframework.orm.hibernate5.HibernateTransactionManager
466
			// provides more transaction/session-related debug information.
467
		}
468

    
469
		TransactionStatus txStatus = txManager.getTransaction(txDef);
470
		return txStatus;
471
	}
472

    
473

    
474
	@Override
475
	public void commitTransaction(TransactionStatus txStatus){
476
		PlatformTransactionManager txManager = getTransactionManager();
477
		txManager.commit(txStatus);
478
		return;
479
	}
480

    
481
	@Override
482
	public void authenticate(String username, String password){
483
		UsernamePasswordAuthenticationToken tokenForUser = new UsernamePasswordAuthenticationToken(username, password);
484
		Authentication authentication = this.getAuthenticationManager().authenticate(tokenForUser);
485
		SecurityContext context = SecurityContextHolder.getContext();
486
		context.setAuthentication(authentication);
487
	}
488

    
489
    @Override
490
    public IRightsService getRightsService() {
491
        return rightsService;
492
    }
493

    
494
    @Override
495
    public IRegistrationService getRegistrationService() {
496
        return registrationService;
497
    }
498

    
499
    public SessionFactory getSessionFactory() {
500
        return factory;
501
    }
502

    
503
    /**
504
     * Returns the current session.
505
     * A new Session will be opened in case of a HiernateExecption occurs when getting the current Session.
506
     *
507
     * @return
508
     */
509
    public Session getSession(){
510
        Session session ;
511
        try {
512
            session = factory.getCurrentSession();
513
        } catch (HibernateException e) {
514
            logger.debug("Opening new session in turn of a HibernateException: " + e.getMessage());
515
            session = factory.openSession();
516
        }
517

    
518
        return session;
519
    }
520

    
521
}
(3-3/9)