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.servlets;
20  
21  import static org.mycore.access.MCRAccessManager.PERMISSION_WRITE;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.jdom2.Attribute;
26  import org.jdom2.Document;
27  import org.jdom2.Element;
28  import org.mycore.access.MCRAccessManager;
29  import org.mycore.datamodel.metadata.MCRDerivate;
30  import org.mycore.datamodel.metadata.MCRMetadataManager;
31  import org.mycore.datamodel.metadata.MCRObjectID;
32  import org.mycore.frontend.MCRFrontendUtil;
33  
34  import jakarta.servlet.http.HttpServletResponse;
35  
36  /**
37   * @author shermann
38   */
39  public class MCRDisplayHideDerivateServlet extends MCRServlet {
40      private static final long serialVersionUID = 1L;
41  
42      private static final Logger LOGGER = LogManager.getLogger(MCRDisplayHideDerivateServlet.class);
43  
44      @Override
45      protected void doGetPost(MCRServletJob job) throws Exception {
46          String derivate = job.getRequest().getParameter("derivate");
47  
48          if (derivate == null || (!derivate.contains("_derivate_"))) {
49              LOGGER.error("Cannot toogle display attribute. No derivate id provided.");
50              job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "You must provide a proper derivate id");
51              return;
52          }
53  
54          if (!MCRAccessManager.checkPermission(MCRObjectID.getInstance(derivate), PERMISSION_WRITE)) {
55              job.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN, "You have to be logged in.");
56              return;
57          }
58  
59          LOGGER.info("Toggling display attribute of {}", derivate);
60          MCRDerivate obj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
61          toggleDisplay(obj);
62  
63          String url = MCRFrontendUtil.getBaseURL() + "receive/" + getParentHref(obj);
64          job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(url));
65      }
66  
67      private String getParentHref(MCRDerivate obj) {
68          return obj.getDerivate().getMetaLink().getXLinkHref();
69      }
70  
71      /**
72       * Toggles the display attribute value of the derivate element.
73       */
74      private void toggleDisplay(MCRDerivate derObj) throws Exception {
75          Document xml = derObj.createXML();
76          Element derivateNode = xml.getRootElement().getChild("derivate");
77  
78          Attribute displayAttr = derivateNode.getAttribute("display");
79  
80          if (displayAttr == null) {
81              /* the attributs is not existing, user wants to hide derivate */
82              displayAttr = new Attribute("display", "false");
83              derivateNode.setAttribute(displayAttr);
84          } else {
85              /* attribute exists, thus toggle the attribute value */
86              String oldVal = displayAttr.getValue();
87              String newVal = oldVal.equals(String.valueOf(true)) ? String.valueOf(false) : String.valueOf(true);
88              displayAttr.setValue(newVal);
89              LOGGER.info("Setting display attribute of derivate with id {} to {}", derObj.getId(), newVal);
90          }
91          MCRDerivate updated = new MCRDerivate(xml);
92          MCRMetadataManager.update(updated);
93      }
94  }