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 java.util.Collection;
22  import java.util.Map;
23  
24  import org.jdom2.Element;
25  import org.mycore.common.content.MCRJDOMContent;
26  import org.mycore.datamodel.common.MCRLinkTableManager;
27  import org.mycore.datamodel.metadata.MCRMetadataManager;
28  import org.mycore.datamodel.metadata.MCRObject;
29  import org.mycore.datamodel.metadata.MCRObjectID;
30  import org.mycore.frontend.MCRFrontendUtil;
31  
32  /**
33   * This servlet is used to display all parents of an mycore object and their
34   * containing derivates. It returns a xml document which will be transformed
35   * by derivateLinks-parentList.xsl.
36   *  
37   * @author Matthias Eichner
38   */
39  public class MCRDerivateLinkServlet extends MCRServlet {
40  
41      private static final long serialVersionUID = 1L;
42  
43      protected static String derivateLinkErrorPage = "error_derivatelink.xml";
44  
45      @Override
46      protected void doGetPost(MCRServletJob job) throws Exception {
47          @SuppressWarnings("unchecked")
48          Map<String, String[]> pMap = job.getRequest().getParameterMap();
49          String webpage = pMap.get("subselect.webpage")[0];
50          String mcrId = getSubParameterValueOfBrowserAddressParameter(webpage, "mcrid");
51          String parentId = getSubParameterValueOfBrowserAddressParameter(webpage, "parentID");
52  
53          // create a new root element
54          Element rootElement = new Element("derivateLinks-parentList");
55  
56          MCRObjectID objId = MCRObjectID.getInstance(mcrId);
57          if (MCRMetadataManager.exists(objId)) {
58              /* mcr object exists in datastore -> add all parent with their
59               * derivates to the jdom tree */
60              addParentsToElement(rootElement, objId);
61          } else if (parentId != null && MCRMetadataManager.exists(MCRObjectID.getInstance(parentId))) {
62              /* mcr object doesnt exists in datastore -> use the parent id
63               * to create the content */
64              Element firstParent = getMyCoReObjectElement(MCRObjectID.getInstance(parentId));
65              if (firstParent != null) {
66                  rootElement.addContent(firstParent);
67              }
68              addParentsToElement(rootElement, MCRObjectID.getInstance(parentId));
69          }
70  
71          // check if root element has content -> if not, show an error page
72          if (rootElement.getContentSize() == 0) {
73              job.getResponse().sendRedirect(
74                  job.getResponse().encodeRedirectURL(MCRFrontendUtil.getBaseURL() + derivateLinkErrorPage));
75              return;
76          }
77  
78          // set some important attributes to the root element
79          rootElement.setAttribute("session", pMap.get("subselect.session")[0]);
80          rootElement.setAttribute("varpath", pMap.get("subselect.varpath")[0]);
81          rootElement.setAttribute("webpage", webpage);
82  
83          // transform & display the generated xml document
84          getLayoutService().doLayout(job.getRequest(), job.getResponse(), new MCRJDOMContent(rootElement));
85      }
86  
87      /**
88       * This method adds all parents and their derivates
89       * iterative to the given element.
90       * 
91       * @param toElement the element where the parents and derivates will be added
92       * @param objId the source object id
93       */
94      private void addParentsToElement(Element toElement, MCRObjectID objId) {
95          MCRObjectID pId = objId;
96          while ((pId = getParentId(pId)) != null) {
97              Element parentElement = getMyCoReObjectElement(pId);
98              if (parentElement != null) {
99                  toElement.addContent(parentElement);
100             }
101         }
102     }
103 
104     /**
105      * Returns the parent object id of an mcr object.
106      * 
107      * @param objectId from which id
108      * @return the parent id
109      */
110     private MCRObjectID getParentId(MCRObjectID objectId) {
111         MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectId);
112         return obj.getStructure().getParentID();
113     }
114 
115     /**
116      * Creates a new mcrobject jdom element which contains all
117      * derivates with their ids as children.
118      * 
119      * @param objectId id of the mcr object
120      * @return a new jdom element
121      */
122     private Element getMyCoReObjectElement(MCRObjectID objectId) {
123         Collection<String> derivates = MCRLinkTableManager.instance().getDestinationOf(objectId, "derivate");
124         if (derivates.size() <= 0) {
125             return null;
126         }
127         Element objElement = new Element("mycoreobject");
128         objElement.setAttribute("id", objectId.toString());
129         for (String derivate : derivates) {
130             Element derElement = new Element("derivate");
131             derElement.setAttribute("id", derivate);
132             objElement.addContent(derElement);
133         }
134         return objElement;
135     }
136 
137     /**
138      * Returns the value of a parameter which is embedded in a parameter
139      * of the browser address.
140      * 
141      * @param browserAddressParameter the separated parameter from the browser address
142      * @param subParameter the sub parameter to search
143      * @return the value of the sub parameter
144      */
145     private String getSubParameterValueOfBrowserAddressParameter(String browserAddressParameter, String subParameter) {
146         String value = null;
147         int index = browserAddressParameter.indexOf(subParameter);
148         if (index != -1) {
149             int startIndex = index + subParameter.length() + 1;
150             int endIndex = browserAddressParameter.indexOf("&", index + 1);
151             if (endIndex == -1) {
152                 endIndex = browserAddressParameter.length();
153             }
154             value = browserAddressParameter.substring(startIndex, endIndex);
155         }
156         return value;
157     }
158 }