1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.api.validation;
|
11
|
|
12
|
import javax.annotation.PostConstruct;
|
13
|
|
14
|
import org.hibernate.SessionFactory;
|
15
|
import org.hibernate.event.service.spi.EventListenerRegistry;
|
16
|
import org.hibernate.event.spi.EventType;
|
17
|
import org.hibernate.internal.SessionFactoryImpl;
|
18
|
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
import org.springframework.beans.factory.annotation.Qualifier;
|
21
|
import org.springframework.context.annotation.Lazy;
|
22
|
import org.springframework.scheduling.TaskScheduler;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
|
26
|
import eu.etaxonomy.cdm.api.validation.batch.BatchValidator;
|
27
|
import eu.etaxonomy.cdm.api.validation.batch.ValidationScheduler;
|
28
|
import eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl;
|
29
|
import eu.etaxonomy.cdm.persistence.hibernate.Level2ValidationEventListener;
|
30
|
import eu.etaxonomy.cdm.persistence.hibernate.Level3ValidationEventListener;
|
31
|
import eu.etaxonomy.cdm.persistence.validation.ValidationExecutor;
|
32
|
|
33
|
/**
|
34
|
* This bean
|
35
|
*
|
36
|
* @author a.mueller
|
37
|
* @date 09.01.2015
|
38
|
*
|
39
|
*/
|
40
|
@Component
|
41
|
@Lazy(false)
|
42
|
public class ValidationManager {
|
43
|
|
44
|
private boolean validationEnabled = true;
|
45
|
private final boolean batchValidationEnabled = true;
|
46
|
|
47
|
private boolean level2Enabled = false;
|
48
|
private boolean level3Enabled = false;
|
49
|
|
50
|
private boolean isInitialized = false;
|
51
|
|
52
|
private Level2ValidationEventListener l2Listener;
|
53
|
private Level3ValidationEventListener l3Listener;
|
54
|
|
55
|
@Autowired
|
56
|
private SessionFactory sessionFactory;
|
57
|
|
58
|
@Autowired
|
59
|
private BatchValidator batchValidator;
|
60
|
|
61
|
@Autowired
|
62
|
@Qualifier("cdmApplicationDefaultConfiguration")
|
63
|
private ICdmApplicationConfiguration cdmApplicationDefaultConfiguration;
|
64
|
|
65
|
@Autowired
|
66
|
// IEntityValidationService validationService;
|
67
|
// IEntityValidationCrud validationService;
|
68
|
private EntityValidationCrudJdbcImpl validationDao;
|
69
|
|
70
|
// private TaskExecutor taskExecutor;
|
71
|
|
72
|
private TaskScheduler scheduler;
|
73
|
|
74
|
@PostConstruct
|
75
|
public void initializeManager(){
|
76
|
registerValidationListeners();
|
77
|
initTaskExecutor();
|
78
|
}
|
79
|
|
80
|
|
81
|
/**
|
82
|
*
|
83
|
*/
|
84
|
private void initTaskExecutor() {
|
85
|
// taskExecutor = new ThreadPoolTaskExecutor();
|
86
|
ValidationScheduler validationScheduler = new ValidationScheduler();
|
87
|
validationScheduler.initialize();
|
88
|
scheduler = validationScheduler;
|
89
|
scheduler.scheduleWithFixedDelay(batchValidator, 5000);
|
90
|
//TODO how to disable scheduling if not wanted for a certain time
|
91
|
|
92
|
}
|
93
|
|
94
|
public void startBatchValidation(){
|
95
|
batchValidator.run();
|
96
|
}
|
97
|
|
98
|
|
99
|
public void registerValidationListeners(){
|
100
|
if (!isInitialized){
|
101
|
if (sessionFactory != null && sessionFactory instanceof SessionFactoryImpl){
|
102
|
ServiceRegistryImplementor serviceRegistry = ((SessionFactoryImpl)sessionFactory).getServiceRegistry();
|
103
|
|
104
|
final EventListenerRegistry eventRegistry = serviceRegistry.getService(EventListenerRegistry.class);
|
105
|
|
106
|
//duplication strategy
|
107
|
// eventRegistry.addDuplicationStrategy(CdmListenerDuplicationStrategy.NewInstance);
|
108
|
eventRegistry.getEventListenerGroup(EventType.POST_INSERT);
|
109
|
|
110
|
ValidationExecutor validationExecutor = new ValidationExecutor();
|
111
|
|
112
|
//level2
|
113
|
l2Listener = new Level2ValidationEventListener(validationDao);
|
114
|
l2Listener.setValidationExecutor(validationExecutor);
|
115
|
|
116
|
//level3
|
117
|
l3Listener = new Level3TransactionalValidationEventListener(cdmApplicationDefaultConfiguration, validationDao);
|
118
|
l3Listener.setValidationExecutor(validationExecutor);
|
119
|
|
120
|
// prepend to register before or append to register after
|
121
|
|
122
|
eventRegistry.appendListeners(EventType.POST_INSERT, l2Listener , l3Listener);
|
123
|
eventRegistry.appendListeners(EventType.POST_UPDATE, l2Listener , l3Listener);
|
124
|
//TODO don't we need l2Listener validation also for deleting the results?
|
125
|
eventRegistry.appendListeners(EventType.POST_DELETE, l3Listener);
|
126
|
|
127
|
isInitialized = true;
|
128
|
|
129
|
}else{
|
130
|
throw new RuntimeException("Session factory not available or not of type SessionFactoryImpl");
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
|
135
|
//for future use
|
136
|
private void enableLevel2Listener(boolean enabled){
|
137
|
level2Enabled = enabled;
|
138
|
l2Listener.setEnabled(level2Enabled && validationEnabled);
|
139
|
}
|
140
|
|
141
|
private void enableLevel3Listener(boolean enabled){
|
142
|
level3Enabled = enabled;
|
143
|
l3Listener.setEnabled(level3Enabled && validationEnabled);
|
144
|
}
|
145
|
|
146
|
private void enableValidation(boolean enabled){
|
147
|
validationEnabled = enabled;
|
148
|
l2Listener.setEnabled(level2Enabled);
|
149
|
l3Listener.setEnabled(level3Enabled);
|
150
|
}
|
151
|
|
152
|
}
|