View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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  }