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