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;
20  
21  import java.util.HashMap;
22  import java.util.LinkedHashMap;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.logging.log4j.LogManager;
29  import org.apache.logging.log4j.Logger;
30  import org.jaxen.JaxenException;
31  import org.jdom2.JDOMException;
32  import org.mycore.common.xml.MCRXMLFunctions;
33  import org.mycore.common.xml.MCRXMLHelper;
34  import org.mycore.common.xml.MCRXPathBuilder;
35  
36  public class MCREditorSubmission {
37  
38      public static final String PREFIX_DEFAULT_VALUE = "_xed_default_";
39  
40      public static final String PREFIX_CHECK_RESUBMISSION = "_xed_check";
41  
42      private static final Logger LOGGER = LogManager.getLogger();
43  
44      private Set<String> xPaths2CheckResubmission = new LinkedHashSet<>();
45  
46      private Map<String, String> xPath2DefaultValue = new LinkedHashMap<>();
47  
48      private MCREditorSession session;
49  
50      public MCREditorSubmission(MCREditorSession session) {
51          this.session = session;
52      }
53  
54      public void clear() {
55          xPaths2CheckResubmission.clear();
56          xPath2DefaultValue.clear();
57      }
58  
59      public void mark2checkResubmission(MCRBinding binding) {
60          for (Object node : binding.getBoundNodes()) {
61              xPaths2CheckResubmission.add(MCRXPathBuilder.buildXPath(node));
62          }
63      }
64  
65      public String getXPaths2CheckResubmission() {
66          StringBuilder sb = new StringBuilder();
67          for (String xPath : xPaths2CheckResubmission) {
68              String path = xPath.substring(xPath.indexOf("/", 1) + 1);
69              sb.append(path).append(' ');
70          }
71          return sb.toString().trim();
72      }
73  
74      public void setXPaths2CheckResubmission(String xPaths) throws JDOMException {
75          xPaths2CheckResubmission.clear();
76          String rootXPath = MCRXPathBuilder.buildXPath(session.getEditedXML().getRootElement()) + "/";
77          if (xPaths != null) {
78              for (String xPath : xPaths.split(" ")) {
79                  xPaths2CheckResubmission.add(rootXPath + xPath);
80              }
81          }
82      }
83  
84      public void emptyNotResubmittedNodes() throws JDOMException, JaxenException {
85          for (String xPath : xPaths2CheckResubmission) {
86              MCRBinding binding = new MCRBinding(xPath, false, session.getRootBinding());
87              if (!binding.getBoundNodes().isEmpty()) {
88                  binding.setValue("");
89              } else {
90                  LOGGER.warn("Binding does not contain a bound node [MCR-2558]");
91              }
92              binding.detach();
93          }
94      }
95  
96      public void markDefaultValue(String xPath, String defaultValue) {
97          xPath2DefaultValue.put(xPath, defaultValue);
98      }
99  
100     public Map<String, String> getDefaultValues() {
101         return xPath2DefaultValue;
102     }
103 
104     public void setSubmittedValues(Map<String, String[]> values) throws JaxenException, JDOMException {
105         String[] xPaths2Check = values.get(PREFIX_CHECK_RESUBMISSION);
106         if ((xPaths2Check != null) && (xPaths2Check.length > 0)) {
107             setXPaths2CheckResubmission(xPaths2Check[0]);
108         }
109 
110         xPath2DefaultValue.clear();
111 
112         Map<MCRBinding, String[]> valuesToSet = new HashMap<>();
113 
114         for (String paramName : values.keySet()) {
115             if (paramName.startsWith("/")) {
116                 MCRBinding binding = new MCRBinding(paramName, true, session.getRootBinding());
117                 valuesToSet.put(binding, values.get(paramName));
118             } else if (paramName.startsWith(PREFIX_DEFAULT_VALUE)) {
119                 String xPath = paramName.substring(PREFIX_DEFAULT_VALUE.length());
120 
121                 MCRBinding binding = new MCRBinding(xPath, false, session.getRootBinding());
122                 boolean noSuchNode = binding.getBoundNodes().isEmpty();
123                 binding.detach();
124                 if (noSuchNode) {
125                     continue;
126                 }
127 
128                 String defaultValue = values.get(paramName)[0];
129                 markDefaultValue(xPath, defaultValue);
130             }
131         }
132 
133         for (MCRBinding binding : valuesToSet.keySet()) {
134             setSubmittedValues(binding, valuesToSet.get(binding));
135         }
136 
137         emptyNotResubmittedNodes();
138         setDefaultValues();
139 
140         session.setBreakpoint("After setting submitted values");
141     }
142 
143     private void setSubmittedValues(MCRBinding binding, String[] values) throws JDOMException, JaxenException {
144         List<Object> boundNodes = binding.getBoundNodes();
145 
146         while (boundNodes.size() < values.length) {
147             binding.cloneBoundElement(boundNodes.size() - 1);
148         }
149 
150         for (int i = 0; i < values.length; i++) {
151             String value = values[i] == null ? "" : values[i].trim();
152             value = MCRXMLFunctions.normalizeUnicode(value);
153             value = MCRXMLHelper.removeIllegalChars(value);
154             binding.setValue(i, value);
155 
156             Object node = binding.getBoundNodes().get(i);
157             xPaths2CheckResubmission.remove(MCRXPathBuilder.buildXPath(node));
158         }
159 
160         binding.detach();
161     }
162 
163     public void setDefaultValues() throws JDOMException, JaxenException {
164         MCRBinding rootBinding = session.getRootBinding();
165         for (String xPath : xPath2DefaultValue.keySet()) {
166             String defaultValue = xPath2DefaultValue.get(xPath);
167             MCRBinding binding = new MCRBinding(xPath, false, rootBinding);
168             binding.setDefault(defaultValue);
169             binding.detach();
170         }
171     }
172 }