Merge branch 'release/4.5.0'
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / validation / ValidationManager.java
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;
10
11 import javax.annotation.PostConstruct;
12
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;
23
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;
31
32 /**
33 * This bean
34 *
35 * @author a.mueller
36 * @date 09.01.2015
37 *
38 */
39 @Component
40 @Lazy(false)
41 public class ValidationManager {
42
43 private boolean validationEnabled = true;
44 private final boolean batchValidationEnabled = true;
45
46 private boolean level2Enabled = false;
47 private boolean level3Enabled = false;
48
49 private boolean isInitialized = false;
50
51 private Level2ValidationEventListener l2Listener;
52 private Level3ValidationEventListener l3Listener;
53
54 @Autowired
55 private SessionFactory sessionFactory;
56
57 @Autowired
58 private BatchValidator batchValidator;
59
60 @Autowired
61 @Qualifier("cdmApplicationDefaultConfiguration")
62 private ICdmApplicationConfiguration cdmApplicationDefaultConfiguration;
63
64 @Autowired
65 // IEntityValidationService validationService;
66 // IEntityValidationCrud validationService;
67 private EntityValidationCrudJdbcImpl validationDao;
68
69 // private TaskExecutor taskExecutor;
70
71 private TaskScheduler scheduler;
72
73 @PostConstruct
74 public void initializeManager(){
75 registerValidationListeners();
76 initTaskExecutor();
77 }
78
79
80 /**
81 *
82 */
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
90
91 }
92
93 public void startBatchValidation(){
94 batchValidator.run();
95 }
96
97
98 public void registerValidationListeners(){
99 if (!isInitialized){
100 if (sessionFactory != null && sessionFactory instanceof SessionFactoryImpl){
101 ServiceRegistryImplementor serviceRegistry = ((SessionFactoryImpl)sessionFactory).getServiceRegistry();
102
103 final EventListenerRegistry eventRegistry = serviceRegistry.getService(EventListenerRegistry.class);
104
105 //duplication strategy
106 // eventRegistry.addDuplicationStrategy(CdmListenerDuplicationStrategy.NewInstance);
107 eventRegistry.getEventListenerGroup(EventType.POST_INSERT);
108
109 ValidationExecutor validationExecutor = new ValidationExecutor();
110
111 //level2
112 l2Listener = new Level2ValidationEventListener(validationDao);
113 l2Listener.setValidationExecutor(validationExecutor);
114
115 //level3
116 l3Listener = new Level3TransactionalValidationEventListener(cdmApplicationDefaultConfiguration, validationDao);
117 l3Listener.setValidationExecutor(validationExecutor);
118
119 // prepend to register before or append to register after
120
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);
125
126 isInitialized = true;
127
128 }else{
129 throw new RuntimeException("Session factory not available or not of type SessionFactoryImpl");
130 }
131 }
132 }
133
134 //for future use
135 private void enableLevel2Listener(boolean enabled){
136 level2Enabled = enabled;
137 l2Listener.setEnabled(level2Enabled && validationEnabled);
138 }
139
140 private void enableLevel3Listener(boolean enabled){
141 level3Enabled = enabled;
142 l3Listener.setEnabled(level3Enabled && validationEnabled);
143 }
144
145 private void enableValidation(boolean enabled){
146 validationEnabled = enabled;
147 l2Listener.setEnabled(level2Enabled);
148 l3Listener.setEnabled(level3Enabled);
149 }
150
151 }