1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.frontend.xeditor.validation;
20
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import org.mycore.common.config.MCRConfiguration2;
27
28 public class MCRValidationResults {
29
30 static final String MARKER_DEFAULT;
31
32 static final String MARKER_SUCCESS;
33
34 static final String MARKER_ERROR;
35
36 static {
37 String prefix = "MCR.XEditor.Validation.Marker.";
38 MARKER_DEFAULT = MCRConfiguration2.getString(prefix + "default").orElse("");
39 MARKER_SUCCESS = MCRConfiguration2.getString(prefix + "success").orElse("mcr-valid");
40 MARKER_ERROR = MCRConfiguration2.getString(prefix + "error").orElse("mcr-invalid");
41 }
42
43 private Map<String, String> xPath2Marker = new HashMap<>();
44
45 private LinkedHashMap<String, MCRValidator> xPath2FailedRule = new LinkedHashMap<>();
46
47 private boolean isValid = true;
48
49 public boolean hasError(String xPath) {
50 return MARKER_ERROR.equals(xPath2Marker.get(xPath));
51 }
52
53 public void mark(String xPath, boolean isValid, MCRValidator failedRule) {
54 if (hasError(xPath)) {
55 return;
56 }
57
58 if (isValid) {
59 xPath2Marker.put(xPath, MARKER_SUCCESS);
60 } else {
61 xPath2Marker.put(xPath, MARKER_ERROR);
62 xPath2FailedRule.put(xPath, failedRule);
63 this.isValid = false;
64 }
65 }
66
67 public String getValidationMarker(String xPath) {
68 return xPath2Marker.getOrDefault(xPath, MARKER_DEFAULT);
69 }
70
71 public MCRValidator getFailedRule(String xPath) {
72 return xPath2FailedRule.get(xPath);
73 }
74
75 public Collection<MCRValidator> getFailedRules() {
76 return xPath2FailedRule.values();
77 }
78
79 public boolean isValid() {
80 return isValid;
81 }
82 }