1
|
/**
|
2
|
* Copyright (C) 2015 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
|
package eu.etaxonomy.cdm.api.validation.batch;
|
10
|
|
11
|
import java.util.List;
|
12
|
import java.util.Set;
|
13
|
|
14
|
import javax.validation.ConstraintViolation;
|
15
|
import javax.validation.Validation;
|
16
|
import javax.validation.Validator;
|
17
|
import javax.validation.ValidatorFactory;
|
18
|
|
19
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
20
|
import org.hibernate.validator.HibernateValidator;
|
21
|
import org.hibernate.validator.HibernateValidatorConfiguration;
|
22
|
import org.springframework.context.ApplicationContext;
|
23
|
import org.springframework.context.ApplicationContextAware;
|
24
|
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
25
|
import org.springframework.stereotype.Component;
|
26
|
import org.springframework.transaction.PlatformTransactionManager;
|
27
|
import org.springframework.transaction.TransactionDefinition;
|
28
|
import org.springframework.transaction.TransactionStatus;
|
29
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
30
|
|
31
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
32
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
33
|
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
|
34
|
import eu.etaxonomy.cdm.api.service.IService;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
37
|
import eu.etaxonomy.cdm.model.validation.CRUDEventType;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl;
|
39
|
import eu.etaxonomy.cdm.validation.Level2;
|
40
|
import eu.etaxonomy.cdm.validation.Level3;
|
41
|
|
42
|
/**
|
43
|
* @author ayco_holleman
|
44
|
* @author a.mueller
|
45
|
* @since 27 jan. 2015
|
46
|
*/
|
47
|
@Component("batchValidator")
|
48
|
public class BatchValidator implements Runnable, ApplicationContextAware {
|
49
|
|
50
|
static final Class<?>[] DEFAULT_VALIDATION_GROUPS = new Class<?>[] { Level2.class, Level3.class };
|
51
|
|
52
|
private static final Logger logger = LogManager.getLogger(BatchValidator.class);
|
53
|
|
54
|
|
55
|
private ICdmRepository repository;
|
56
|
|
57
|
private ApplicationContext appContext;
|
58
|
|
59
|
private Validator validator;
|
60
|
private Class<?>[] validationGroups;
|
61
|
|
62
|
|
63
|
@Override
|
64
|
public void setApplicationContext(ApplicationContext appContext) {
|
65
|
this.appContext = appContext;
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public void run() {
|
70
|
Thread.currentThread().setPriority(1);
|
71
|
initValidator();
|
72
|
validate();
|
73
|
}
|
74
|
|
75
|
private void initValidator() {
|
76
|
if (getValidator() == null){
|
77
|
HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
|
78
|
ValidatorFactory factory = config.buildValidatorFactory();
|
79
|
setValidator(factory.getValidator());
|
80
|
}
|
81
|
if (validationGroups == null) {
|
82
|
validationGroups = DEFAULT_VALIDATION_GROUPS;
|
83
|
}
|
84
|
}
|
85
|
|
86
|
private <T extends ICdmBase, S extends T> void validate() {
|
87
|
logger.info("Starting batch validation");
|
88
|
|
89
|
// Get service for saving errors to database
|
90
|
// IEntityValidationService validationResultService = context.getEntityValidationService();
|
91
|
IEntityValidationService entityValidationService = appContext.getBean(IEntityValidationService.class);
|
92
|
|
93
|
EntityValidationCrudJdbcImpl jdbcPersister = appContext.getBean(EntityValidationCrudJdbcImpl.class);
|
94
|
|
95
|
// Get all services dealing with "real" entities
|
96
|
List<Class<CdmBase>> classesToValidate = BatchValidationUtil.getClassesToValidate();
|
97
|
|
98
|
for (Class<CdmBase> entityClass : classesToValidate) {
|
99
|
//TODO currently this seems to work only on the exact class, we may move it down
|
100
|
//to single entity validation again but cache the information for each class
|
101
|
if (true || BatchValidationUtil.isConstrainedEntityClass(validator, entityClass)){
|
102
|
|
103
|
// ICommonService commonService = repository.getCommonService();
|
104
|
ICommonService commonService = appContext.getBean(ICommonService.class);
|
105
|
logger.info("Loading entities of type " + entityClass.getName());
|
106
|
//false for saving validation results
|
107
|
//TODO can we handle results in a different transaction?
|
108
|
boolean readOnly = false;
|
109
|
TransactionStatus txStatus = startTransaction(readOnly);
|
110
|
handleSingleClass(commonService, entityClass, entityValidationService, jdbcPersister);
|
111
|
commitTransaction(txStatus);
|
112
|
}
|
113
|
}
|
114
|
|
115
|
logger.info("Batch validation complete");
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* @param txStatus
|
120
|
*/
|
121
|
private void commitTransaction(TransactionStatus txStatus) {
|
122
|
PlatformTransactionManager txManager = getTransactionManager();
|
123
|
txManager.commit(txStatus);
|
124
|
|
125
|
}
|
126
|
|
127
|
private TransactionStatus startTransaction(boolean readOnly) {
|
128
|
PlatformTransactionManager txManager = getTransactionManager();
|
129
|
|
130
|
DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
|
131
|
defaultTxDef.setReadOnly(readOnly);
|
132
|
TransactionDefinition txDef = defaultTxDef;
|
133
|
TransactionStatus txStatus = txManager.getTransaction(txDef);
|
134
|
return txStatus;
|
135
|
}
|
136
|
|
137
|
private PlatformTransactionManager getTransactionManager() {
|
138
|
PlatformTransactionManager txManager = appContext.getBean(HibernateTransactionManager.class);
|
139
|
return txManager;
|
140
|
}
|
141
|
|
142
|
private void handleSingleClass(ICommonService commonService, Class<CdmBase> entityClass, IEntityValidationService entityValidationService, EntityValidationCrudJdbcImpl jdbcPersister) {
|
143
|
int n = Long.valueOf(commonService.count(entityClass)).intValue();
|
144
|
int pageSize = 1000;
|
145
|
for (int page = 0; page < n ; page = page + pageSize ){
|
146
|
handlePage(commonService, entityClass, entityValidationService, jdbcPersister,
|
147
|
page/pageSize, pageSize);
|
148
|
}
|
149
|
}
|
150
|
|
151
|
private void handlePage(ICommonService commonService, Class<CdmBase> entityClass, IEntityValidationService entityValidationService, EntityValidationCrudJdbcImpl jdbcPersister, int start, int pageSize) {
|
152
|
|
153
|
List<CdmBase> entities;
|
154
|
|
155
|
try {
|
156
|
// commonService.count()
|
157
|
entities = commonService.list(entityClass, pageSize, 0, null, null);
|
158
|
} catch (Throwable t) {
|
159
|
//TODO handle exception
|
160
|
logger.error("Failed to load entities", t);
|
161
|
return;
|
162
|
}
|
163
|
for (CdmBase entity : entities) {
|
164
|
try {
|
165
|
Set<ConstraintViolation<CdmBase>> errors = getValidator().validate(entity, getValidationGroups());
|
166
|
if (errors.size() != 0) {
|
167
|
if (logger.isInfoEnabled()){logger.info(errors.size() + " constraint violation(s) detected in entity " + entity.toString());}
|
168
|
// entityValidationService.saveEntityValidation(entity, errors, CRUDEventType.NONE,
|
169
|
// getValidationGroups());
|
170
|
|
171
|
jdbcPersister.saveEntityValidation(entity, errors, CRUDEventType.NONE, getValidationGroups());
|
172
|
}
|
173
|
} catch (Exception e) {
|
174
|
// TODO Exception handling
|
175
|
e.printStackTrace();
|
176
|
}
|
177
|
}
|
178
|
|
179
|
}
|
180
|
|
181
|
private <T extends ICdmBase, S extends T> void validate_old() {
|
182
|
logger.info("Starting batch validation");
|
183
|
|
184
|
if (validationGroups == null) {
|
185
|
validationGroups = DEFAULT_VALIDATION_GROUPS;
|
186
|
}
|
187
|
|
188
|
// Get service for saving errors to database
|
189
|
IEntityValidationService validationResultService = repository.getEntityValidationService();
|
190
|
|
191
|
// Get all services dealing with "real" entities
|
192
|
List<EntityValidationUnit<T, S>> validationUnits = BatchValidationUtil.getAvailableServices(repository);
|
193
|
|
194
|
for (EntityValidationUnit<T, S> unit : validationUnits) {
|
195
|
Class<S> entityClass = unit.getEntityClass();
|
196
|
IService<T> entityLoader = unit.getEntityLoader();
|
197
|
logger.info("Loading entities of type " + entityClass.getName());
|
198
|
List<S> entities;
|
199
|
try {
|
200
|
entities = entityLoader.list(entityClass, 0, 0, null, null);
|
201
|
} catch (Throwable t) {
|
202
|
logger.error("Failed to load entities", t);
|
203
|
continue;
|
204
|
}
|
205
|
for (S entity : entities) {
|
206
|
if (BatchValidationUtil.isConstrainedEntityClass(getValidator(), entity.getClass())) {
|
207
|
Set<ConstraintViolation<S>> errors = getValidator().validate(entity, validationGroups);
|
208
|
if (errors.size() != 0) {
|
209
|
logger.warn(errors.size() + " error(s) detected in entity " + entity.toString());
|
210
|
validationResultService.saveEntityValidation(entity, errors, CRUDEventType.NONE,
|
211
|
validationGroups);
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
|
217
|
logger.info("Batch validation complete");
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Get the application context that will provide the services that will, on
|
222
|
* their turn, provide the entities to be validated.
|
223
|
*
|
224
|
* @return The application context
|
225
|
*/
|
226
|
public ICdmRepository getAppController() {
|
227
|
return repository;
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Set the application context.
|
232
|
*
|
233
|
* @param context
|
234
|
* The application context
|
235
|
*/
|
236
|
public void setAppController(ICdmRepository context) {
|
237
|
this.repository = context;
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Get the {@code Validator} instance that will carry out the validations.
|
242
|
*
|
243
|
* @return The {@code Validator}
|
244
|
*/
|
245
|
public Validator getValidator() {
|
246
|
return validator;
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Set the {@code Validator} instance that will carry out the validations.
|
251
|
*
|
252
|
* @param validator
|
253
|
* The {@code Validator}
|
254
|
*/
|
255
|
public void setValidator(Validator validator) {
|
256
|
this.validator = validator;
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Get the validation groups to be applied by the {@code Validator}.
|
261
|
*
|
262
|
* @return The validation groups
|
263
|
*/
|
264
|
public Class<?>[] getValidationGroups() {
|
265
|
return validationGroups;
|
266
|
}
|
267
|
|
268
|
/**
|
269
|
* Set the validation groups to be applied by the {@code Validator}. By
|
270
|
* default all Level2 and Level3 will be checked. So if that is what you
|
271
|
* want, you do not need to call this method before calling {@link #run()}.
|
272
|
*
|
273
|
* @param validationGroups
|
274
|
* The validation groups
|
275
|
*/
|
276
|
public void setValidationGroups(Class<?>... validationGroups) {
|
277
|
this.validationGroups = validationGroups;
|
278
|
}
|
279
|
}
|