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.iview2.frontend;
20  
21  import java.nio.file.Files;
22  import java.util.Collection;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.Logger;
26  import org.mycore.access.MCRAccessManager;
27  import org.mycore.common.config.MCRConfiguration2;
28  import org.mycore.datamodel.common.MCRLinkTableManager;
29  import org.mycore.datamodel.niofs.MCRPath;
30  import org.mycore.frontend.MCRFrontendUtil;
31  import org.mycore.frontend.servlets.MCRServlet;
32  import org.mycore.iview2.services.MCRIView2Tools;
33  
34  /**
35   * Adapter that can be extended to work with different internal files systems.
36   * To get the extending class invoked, one need to define a MyCoRe property, which defaults to:
37   * <code>MCR.Module-iview2.MCRIView2XSLFunctionsAdapter=org.mycore.iview2.frontend.MCRIView2XSLFunctionsAdapter</code>
38   * @author Thomas Scheffler (yagee)
39   *
40   */
41  public class MCRIView2XSLFunctionsAdapter {
42      private static Logger LOGGER = LogManager.getLogger(MCRIView2XSLFunctionsAdapter.class);
43  
44      private static final MCRLinkTableManager LINK_TABLE_MANAGER = MCRLinkTableManager.instance();
45  
46      public static MCRIView2XSLFunctionsAdapter getInstance() {
47          return MCRConfiguration2
48              .<MCRIView2XSLFunctionsAdapter>getInstanceOf(MCRIView2Tools.CONFIG_PREFIX + "MCRIView2XSLFunctionsAdapter")
49              .orElseGet(MCRIView2XSLFunctionsAdapter::new);
50      }
51  
52      public boolean hasMETSFile(String derivateID) {
53          return Files.exists(MCRPath.getPath(derivateID, "/mets.xml"));
54      }
55  
56      public String getSupportedMainFile(String derivateID) {
57          return MCRIView2Tools.getSupportedMainFile(derivateID);
58      }
59  
60      public String getOptions(String derivateID, String extensions) {
61          final Collection<String> sources = LINK_TABLE_MANAGER.getSourceOf(derivateID, "derivate");
62          String objectID = (sources != null && !sources.isEmpty()) ? sources.iterator().next() : "";
63          StringBuilder options = new StringBuilder();
64          options.append('{');
65          options.append("\"derivateId\":").append('\"').append(derivateID).append("\",");
66          options.append("\"objectId\":").append('\"').append(objectID).append("\",");
67          options.append("\"webappBaseUri\":").append('\"').append(MCRFrontendUtil.getBaseURL()).append("\",");
68          String baseUris = MCRConfiguration2.getString("MCR.Module-iview2.BaseURL").orElse("");
69          if (baseUris.length() < 10) {
70              baseUris = MCRServlet.getServletBaseURL() + "MCRTileServlet";
71          }
72          options.append("\"baseUri\":").append('\"').append(baseUris).append("\".split(\",\")");
73          if (MCRAccessManager.checkPermission(derivateID, "create-pdf")) {
74              options.append(",\"pdfCreatorURI\":").append('\"')
75                  .append(MCRConfiguration2.getString("MCR.Module-iview2.PDFCreatorURI").orElse("")).append("\",");
76              options.append("\"pdfCreatorStyle\":").append('\"')
77                  .append(MCRConfiguration2.getString("MCR.Module-iview2.PDFCreatorStyle").orElse("")).append('\"');
78          }
79  
80          if (extensions != null && !extensions.equals("")) {
81              options.append(',');
82              options.append(extensions);
83          }
84  
85          options.append('}');
86          LOGGER.debug(options.toString());
87          return options.toString();
88      }
89  }