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 |
}
|
EntitValidationResult -> EntityValidation