Project

General

Profile

« Previous | Next » 

Revision 7c8422d0

Added by Ayco Hollemann about 9 years ago

EntitValidationResult -> EntityValidation

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/validation/MarkerManager.java
1 1
package eu.etaxonomy.taxeditor.editor.validation;
2 2

  
3
import java.util.HashMap;
3
import java.util.ArrayList;
4 4
import java.util.List;
5 5
import java.util.Set;
6 6

  
......
23 23
 * @author ayco_holleman
24 24
 * 
25 25
 */
26

  
26 27
/*
27 28
 * See following for problems with icons in Problems view
28 29
 * http://stackoverflow.com
......
44 45
	public static final String MARKER_TYPE_ID = "eu.etaxonomy.taxeditor.markers.validationerror";
45 46

  
46 47
	/**
47
	 * The primary key (id) of the EntityValidation record
48
	 * The primary key (id) of the EntityConstraintViolation record
48 49
	 */
49 50
	public static final String ATTRIB_DATABASE_ID = "databaseId";
50 51

  
......
85 86
	 */
86 87
	public static final String ATTRIB_ENTITY_ID = "entityId";
87 88

  
88
	private final IWorkspaceRoot root;
89
	private final IMarker[] markers;
90
	private final List<EntityValidation> results;
91
	private final HashMap<Integer, EntityValidation> resultMap;
92
	private final HashMap<Integer, IMarker> markerMap;
89
	private final List<EntityConstraintViolation> problems;
93 90

  
94 91

  
95 92
	MarkerManager(List<EntityValidation> results) throws CoreException
96 93
	{
97
		this.root = ResourcesPlugin.getWorkspace().getRoot();
98
		this.markers = root.findMarkers(MARKER_TYPE_ID, true, IResource.DEPTH_INFINITE);
99
		this.markerMap = new HashMap<Integer, IMarker>();
100
		for (IMarker marker : markers) {
101
			markerMap.put(getDatabaseId(marker), marker);
102
		}
103
		this.results = results;
104
		this.resultMap = new HashMap<Integer, EntityValidation>();
94
		this.problems = new ArrayList<EntityConstraintViolation>(64);
105 95
		for (EntityValidation result : results) {
106
			resultMap.put(result.getId(), result);
96
			Set<EntityConstraintViolation> problems = result.getEntityConstraintViolations();
97
			for (EntityConstraintViolation problem : problems) {
98
				problem.setEntityValidation(result);
99
				problems.add(problem);
100
			}
107 101
		}
108 102
	}
109 103

  
......
119 113
	int deleteObsoleteMarkers() throws CoreException
120 114
	{
121 115
		int i = 0;
116
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
122 117
		IMarker[] markers = root.findMarkers(MARKER_TYPE_ID, true, IResource.DEPTH_INFINITE);
123 118
		for (IMarker marker : markers) {
124 119
			if (isObsoleteMarker(marker)) {
......
141 136
	int createMarkers() throws CoreException
142 137
	{
143 138
		int i = 0;
139
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
144 140
		IMarker[] markers = root.findMarkers(MARKER_TYPE_ID, true, IResource.DEPTH_INFINITE);
145
		for (EntityValidation result : results) {
146
			if (!isNewResult(result)) {
147
				continue;
148
			}
149
			Set<EntityConstraintViolation> problems = result.getEntityConstraintViolations();
150
			for (EntityConstraintViolation problem : problems) {
151
				if (markerExistsForProblem(problem, markers)) {
152
					continue;
153
				}
141
		for (EntityConstraintViolation problem : problems) {
142
			if (isNewProblem(problem, markers)) {
154 143
				IMarker marker = root.createMarker(MARKER_TYPE_ID);
155 144
				++i;
156 145
				if (problem.getSeverity() == Severity.ERROR) {
......
162 151
				else {
163 152
					marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
164 153
				}
154
				EntityValidation result = problem.getEntityValidation();
165 155
				marker.setAttribute(IMarker.MESSAGE, problem.getMessage());
166
				marker.setAttribute(ATTRIB_DATABASE_ID, result.getId());
156
				marker.setAttribute(ATTRIB_DATABASE_ID, problem.getId());
167 157
				marker.setAttribute(ATTRIB_USER_FRIENDLY_TYPE_NAME, result.getUserFriendlyTypeName());
168 158
				marker.setAttribute(ATTRIB_USER_FRIENDLY_DESCRIPTION, result.getUserFriendlyDescription());
169 159
				marker.setAttribute(ATTRIB_USER_FRIENDLY_FIELD_NAME, problem.getUserFriendlyFieldName());
......
178 168
	}
179 169

  
180 170

  
181
	/**
182
	 * Is there a problem marker that captures the specified
183
	 * {@link EntityConstraintViolation}? See
184
	 * {@link #markerCapturesProblem(IMarker, EntityConstraintViolation)}.
185
	 * 
186
	 * @param problem
187
	 * @param markers
188
	 * @return
189
	 * @throws CoreException
190
	 */
191
	private boolean markerExistsForProblem(EntityConstraintViolation problem, IMarker[] markers) throws CoreException
171
	private boolean isObsoleteMarker(IMarker marker) throws CoreException
172
	{
173
		for (EntityConstraintViolation problem : problems) {
174
			if (isMarkerForProblem(marker, problem)) {
175
				return false;
176
			}
177
		}
178
		return true;
179
	}
180

  
181

  
182
	private static boolean isNewProblem(EntityConstraintViolation problem, IMarker[] markers) throws CoreException
192 183
	{
193 184
		for (IMarker marker : markers) {
194
			if (markerCapturesProblem(marker, problem)) {
195
				return true;
185
			if (isMarkerForProblem(marker, problem)) {
186
				return false;
196 187
			}
197 188
		}
198
		return false;
189
		return true;
199 190
	}
200 191

  
201 192

  
202
	/**
203
	 * <p>
204
	 * This method determines whether the problem exposed by the specified
205
	 * marker is <b>de facto</b> equivalent to the specified
206
	 * {@code EntityConstraintViolation}. When the CVI validates an entity, it
207
	 * first deletes previous validation results for that entity and only then
208
	 * saves the new validation result. Thus you cannot rely on the database id
209
	 * of the {@code EntityConstraintViolation} to determine equivalence. Maybe
210
	 * later we can make the CVI more sophisticated in this respect. Or maybe
211
	 * see if solving it through the equals() method of
212
	 * {@code EntityValidation} and/or {@code EntityConstraintViolation}
213
	 * is possible. But for now this is the easiest solution.
214
	 * </p>
215
	 * <p>
216
	 * The reason we check for equivalence, is that we don't want to
217
	 * unnecessarily update the Problems view. If a marker is there, we don't
218
	 * want to replace it with an equivalent marker, because that might lead to
219
	 * strange click behaviour for end users (e.g. selected problems will
220
	 * disappear and re-appear unselected).
221
	 * </p>
222
	 * 
223
	 * @param marker
224
	 * @param problem
225
	 * @return
226
	 * @throws CoreException
227
	 */
228
	private static boolean markerCapturesProblem(IMarker marker, EntityConstraintViolation problem) throws CoreException
193
	private static boolean isMarkerForProblem(IMarker marker, EntityConstraintViolation problem) throws CoreException
229 194
	{
195
		if (problem.getId() == (Integer) marker.getAttribute(ATTRIB_DATABASE_ID)) {
196
			return true;
197
		}
230 198
		EntityValidation result = problem.getEntityValidation();
231
		if (!marker.getAttribute(ATTRIB_ENTITY_CLASS).equals(result.getValidatedEntityClass())) {
199
		if (!marker.getAttribute(ATTRIB_ENTITY_ID).equals(result.getValidatedEntityId())) {
232 200
			return false;
233 201
		}
234
		if (!marker.getAttribute(ATTRIB_ENTITY_ID).equals(result.getValidatedEntityId())) {
202
		if (!marker.getAttribute(ATTRIB_INVALID_VALUE).equals(problem.getInvalidValue())) {
235 203
			return false;
236 204
		}
237
		if (!marker.getAttribute(ATTRIB_USER_FRIENDLY_FIELD_NAME).equals(problem.getPropertyPath())) {
205
		if (!marker.getAttribute(ATTRIB_ENTITY_CLASS).equals(result.getValidatedEntityClass())) {
238 206
			return false;
239 207
		}
240
		if (!marker.getAttribute(ATTRIB_INVALID_VALUE).equals(problem.getInvalidValue())) {
208
		if (!marker.getAttribute(ATTRIB_USER_FRIENDLY_FIELD_NAME).equals(problem.getUserFriendlyFieldName())) {
241 209
			return false;
242 210
		}
243 211
		if (!marker.getAttribute(ATTRIB_VALIDATOR_CLASS).equals(problem.getValidator())) {
......
246 214
		return true;
247 215
	}
248 216

  
249

  
250
	/**
251
	 * Is this a marker without a corresponding database record (
252
	 * {@link EntityValidation})?
253
	 * 
254
	 * @param marker
255
	 * @return
256
	 * @throws CoreException
257
	 */
258
	private boolean isObsoleteMarker(IMarker marker) throws CoreException
259
	{
260
		return resultMap.get(getDatabaseId(marker)) == null;
261
	}
262

  
263

  
264
	/**
265
	 * Is this an {@link EntityValidation} for which no marker has been
266
	 * created yet?
267
	 * 
268
	 * @param result
269
	 * @return
270
	 */
271
	private boolean isNewResult(EntityValidation result)
272
	{
273
		return markerMap.get(result.getId()) == null;
274
	}
275

  
276

  
277
	/**
278
	 * Get the id of the {@link EntityValidation} that was stored as one
279
	 * of the marker's attributes.
280
	 * 
281
	 * @param marker
282
	 * @return
283
	 * @throws CoreException
284
	 */
285
	private static Integer getDatabaseId(IMarker marker) throws CoreException
286
	{
287
		return (Integer) marker.getAttribute(ATTRIB_DATABASE_ID);
288
	}
289

  
290 217
}
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/validation/ValidationDaemon.java
8 8
import org.eclipse.core.runtime.Status;
9 9
import org.eclipse.core.runtime.jobs.Job;
10 10

  
11
import eu.etaxonomy.cdm.api.service.IEntityConstraintViolationService;
12 11
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
13 12
import eu.etaxonomy.cdm.model.validation.EntityValidation;
14 13
import eu.etaxonomy.taxeditor.model.MessagingUtils;
......
27 26
	private static final Logger logger = Logger.getLogger(ValidationDaemon.class);
28 27

  
29 28
	private final IEntityValidationService validationResultService;
30
	
31
	@SuppressWarnings("unused")
32
	/* Not currently needed but present for future use if/when required */
33
	private final IEntityConstraintViolationService constraintViolationService;
34 29

  
35 30
	private boolean cancelRequested = false;
36 31

  
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);
32

  
33
	public ValidationDaemon()
34
	{
35
		super("");
42 36
		validationResultService = CdmStore.getService(IEntityValidationService.class);
43 37
	}
44 38

  
45
	
39

  
46 40
	@Override
47
	protected void canceling(){
41
	protected void canceling()
42
	{
48 43
		cancelRequested = true;
49 44
	}
50 45

  
......
54 49
	 * {@link Job#cancel()}, because that method does not have the desired
55 50
	 * effect.
56 51
	 */
57
	public void setCancelRequested(){
52
	public void setCancelRequested()
53
	{
58 54
		cancelRequested = true;
59 55
	}
60 56

  
61 57

  
62 58
	@Override
63
	protected IStatus run(IProgressMonitor monitor){
59
	protected IStatus run(IProgressMonitor monitor)
60
	{
64 61
		MarkerManager markerManager;
65 62
		List<EntityValidation> results;
66 63
		try {
......
72 69
				// Might want to make this configurable:
73 70
				Thread.sleep(5000);
74 71
			}
75
//			StoreUtil.info("Validation module stopped");
76 72
			MessagingUtils.info("Validation module stopped");
77 73
			return Status.OK_STATUS;
78 74
		}
79 75
		catch (Throwable t) {
80
//			StoreUtil.info("Validation module terminated unexpectedly: " + t.getMessage());
81 76
			MessagingUtils.info("Validation module terminated unexpectedly: " + t.getMessage());
82 77
			return Status.CANCEL_STATUS;
83 78
		}

Also available in: Unified diff