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.target;
20  
21  import java.util.Map;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.jaxen.JaxenException;
26  import org.jdom2.Document;
27  import org.jdom2.Element;
28  import org.jdom2.JDOMException;
29  import org.mycore.frontend.servlets.MCRServletJob;
30  import org.mycore.frontend.xeditor.MCRBinding;
31  import org.mycore.frontend.xeditor.MCREditorSession;
32  import org.mycore.frontend.xeditor.tracker.MCRChangeData;
33  
34  import jakarta.servlet.ServletContext;
35  
36  public class MCRSubselectReturnTarget implements MCREditorTarget {
37  
38      private static final Logger LOGGER = LogManager.getLogger(MCRSubselectReturnTarget.class);
39  
40      @Override
41      public void handleSubmission(ServletContext context, MCRServletJob job, MCREditorSession session, String parameter)
42          throws Exception {
43          String baseXPath = getBaseXPathForSubselect(session);
44          LOGGER.info("Returning from subselect for {}", baseXPath);
45  
46          if ("cancel".equals(parameter)) {
47              session.setBreakpoint("After canceling subselect for " + baseXPath);
48          } else {
49              Map<String, String[]> submittedValues = MCRTargetUtils.getSubmittedValues(job, baseXPath);
50              session.getSubmission().setSubmittedValues(submittedValues);
51              session.setBreakpoint("After returning from subselect for " + baseXPath);
52          }
53  
54          job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(session.getRedirectURL(null)));
55      }
56  
57      private String getBaseXPathForSubselect(MCREditorSession session) throws JaxenException, JDOMException {
58          Document doc = session.getEditedXML();
59          MCRChangeData change = session.getChangeTracker().findLastChange(doc);
60          String text = change.getText();
61          String xPath = text.substring(text.lastIndexOf(" ") + 1).trim();
62          return bindsFirstOrMoreThanOneElement(xPath, session) ? xPath + "[1]" : xPath;
63      }
64  
65      private boolean bindsFirstOrMoreThanOneElement(String xPath, MCREditorSession session)
66          throws JaxenException, JDOMException {
67          MCRBinding binding = new MCRBinding(xPath, false, session.getRootBinding());
68          boolean result = (binding.getBoundNode() instanceof Element) && !xPath.endsWith("]");
69          binding.detach();
70          return result;
71      }
72  }