1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }