merge validation editor branch to trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / validation / ValidationDaemon.java
1 package eu.etaxonomy.taxeditor.editor.validation;
2
3 import java.util.List;
4
5 import org.apache.log4j.Logger;
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.core.runtime.IStatus;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.core.runtime.jobs.Job;
10
11 import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
12 import eu.etaxonomy.cdm.api.service.IEntityValidationResultService;
13 import eu.etaxonomy.cdm.model.validation.EntityValidationResult;
14 import eu.etaxonomy.taxeditor.model.MessagingUtils;
15 import eu.etaxonomy.taxeditor.store.CdmStore;
16
17 /**
18 * A job that repeatedly checks the error tables and refreshes the problem
19 * markers created from them.
20 *
21 * @author ayco_holleman
22 *
23 */
24 public class ValidationDaemon extends Job {
25
26 @SuppressWarnings("unused")
27 private static final Logger logger = Logger.getLogger(ValidationDaemon.class);
28
29 private final IEntityValidationResultService validationResultService;
30
31 @SuppressWarnings("unused")
32 /* Not currently needed but present for future use if/when required */
33 private final IEntityConstraintViolationService constraintViolationService;
34
35 private boolean cancelRequested = false;
36
37 public ValidationDaemon(){
38 super("Initializing validation module");
39 // StoreUtil.info("Initializing validation module");
40 MessagingUtils.info("Initializing validation module");
41 constraintViolationService = CdmStore.getService(IEntityConstraintViolationService.class);
42 validationResultService = CdmStore.getService(IEntityValidationResultService.class);
43 }
44
45
46 @Override
47 protected void canceling(){
48 cancelRequested = true;
49 }
50
51
52 /**
53 * This method is called by {@link ValidationContextListener} rather than
54 * {@link Job#cancel()}, because that method does not have the desired
55 * effect.
56 */
57 public void setCancelRequested(){
58 cancelRequested = true;
59 }
60
61
62 @Override
63 protected IStatus run(IProgressMonitor monitor){
64 MarkerManager markerManager;
65 List<EntityValidationResult> results;
66 try {
67 while (!cancelRequested) {
68 results = validationResultService.getValidationResults();
69 markerManager = new MarkerManager(results);
70 markerManager.deleteObsoleteMarkers();
71 markerManager.createMarkers();
72 // Might want to make this configurable:
73 Thread.sleep(5000);
74 }
75 // StoreUtil.info("Validation module stopped");
76 MessagingUtils.info("Validation module stopped");
77 return Status.OK_STATUS;
78 }
79 catch (Throwable t) {
80 // StoreUtil.info("Validation module terminated unexpectedly: " + t.getMessage());
81 MessagingUtils.info("Validation module terminated unexpectedly: " + t.getMessage());
82 return Status.CANCEL_STATUS;
83 }
84 }
85
86 }