2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.validation
;
11 import javax
.annotation
.PostConstruct
;
13 import org
.hibernate
.SessionFactory
;
14 import org
.hibernate
.event
.service
.spi
.EventListenerRegistry
;
15 import org
.hibernate
.event
.spi
.EventType
;
16 import org
.hibernate
.internal
.SessionFactoryImpl
;
17 import org
.hibernate
.service
.spi
.ServiceRegistryImplementor
;
18 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
19 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
20 import org
.springframework
.context
.annotation
.Lazy
;
21 import org
.springframework
.scheduling
.TaskScheduler
;
22 import org
.springframework
.stereotype
.Component
;
24 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
25 import eu
.etaxonomy
.cdm
.api
.validation
.batch
.BatchValidator
;
26 import eu
.etaxonomy
.cdm
.api
.validation
.batch
.ValidationScheduler
;
27 import eu
.etaxonomy
.cdm
.persistence
.dao
.jdbc
.validation
.EntityValidationCrudJdbcImpl
;
28 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.Level2ValidationEventListener
;
29 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.Level3ValidationEventListener
;
30 import eu
.etaxonomy
.cdm
.persistence
.validation
.ValidationExecutor
;
41 public class ValidationManager
{
43 private boolean validationEnabled
= true;
44 private final boolean batchValidationEnabled
= true;
46 private boolean level2Enabled
= false;
47 private boolean level3Enabled
= false;
49 private boolean isInitialized
= false;
51 private Level2ValidationEventListener l2Listener
;
52 private Level3ValidationEventListener l3Listener
;
55 private SessionFactory sessionFactory
;
58 private BatchValidator batchValidator
;
61 @Qualifier("cdmApplicationDefaultConfiguration")
62 private ICdmApplicationConfiguration cdmApplicationDefaultConfiguration
;
65 // IEntityValidationService validationService;
66 // IEntityValidationCrud validationService;
67 private EntityValidationCrudJdbcImpl validationDao
;
69 // private TaskExecutor taskExecutor;
71 private TaskScheduler scheduler
;
74 public void initializeManager(){
75 registerValidationListeners();
83 private void initTaskExecutor() {
84 // taskExecutor = new ThreadPoolTaskExecutor();
85 ValidationScheduler validationScheduler
= new ValidationScheduler();
86 validationScheduler
.initialize();
87 scheduler
= validationScheduler
;
88 scheduler
.scheduleWithFixedDelay(batchValidator
, 5000);
89 //TODO how to disable scheduling if not wanted for a certain time
93 public void startBatchValidation(){
98 public void registerValidationListeners(){
100 if (sessionFactory
!= null && sessionFactory
instanceof SessionFactoryImpl
){
101 ServiceRegistryImplementor serviceRegistry
= ((SessionFactoryImpl
)sessionFactory
).getServiceRegistry();
103 final EventListenerRegistry eventRegistry
= serviceRegistry
.getService(EventListenerRegistry
.class);
105 //duplication strategy
106 // eventRegistry.addDuplicationStrategy(CdmListenerDuplicationStrategy.NewInstance);
107 eventRegistry
.getEventListenerGroup(EventType
.POST_INSERT
);
109 ValidationExecutor validationExecutor
= new ValidationExecutor();
112 l2Listener
= new Level2ValidationEventListener(validationDao
);
113 l2Listener
.setValidationExecutor(validationExecutor
);
116 l3Listener
= new Level3TransactionalValidationEventListener(cdmApplicationDefaultConfiguration
, validationDao
);
117 l3Listener
.setValidationExecutor(validationExecutor
);
119 // prepend to register before or append to register after
121 eventRegistry
.appendListeners(EventType
.POST_INSERT
, l2Listener
, l3Listener
);
122 eventRegistry
.appendListeners(EventType
.POST_UPDATE
, l2Listener
, l3Listener
);
123 //TODO don't we need l2Listener validation also for deleting the results?
124 eventRegistry
.appendListeners(EventType
.POST_DELETE
, l3Listener
);
126 isInitialized
= true;
129 throw new RuntimeException("Session factory not available or not of type SessionFactoryImpl");
135 private void enableLevel2Listener(boolean enabled
){
136 level2Enabled
= enabled
;
137 l2Listener
.setEnabled(level2Enabled
&& validationEnabled
);
140 private void enableLevel3Listener(boolean enabled
){
141 level3Enabled
= enabled
;
142 l3Listener
.setEnabled(level3Enabled
&& validationEnabled
);
145 private void enableValidation(boolean enabled
){
146 validationEnabled
= enabled
;
147 l2Listener
.setEnabled(level2Enabled
);
148 l3Listener
.setEnabled(level3Enabled
);