Search in sources :

Example 41 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRTransferPackageUtil method importDerivate.

/**
 * Imports a derivate from the given target directory path.
 *
 * @param targetDirectory path to the extracted *.tar archive
 * @param derivateId the derivate to import
 * @throws JDOMException derivate xml couldn't be read
 * @throws IOException some file system stuff went wrong
 * @throws MCRAccessException you do not have the permissions to do the import
 */
public static void importDerivate(Path targetDirectory, String derivateId) throws JDOMException, IOException, MCRAccessException {
    SAXBuilder sax = new SAXBuilder();
    Path derivateDirectory = targetDirectory.resolve(CONTENT_DIRECTORY).resolve(derivateId);
    Path derivatePath = derivateDirectory.resolve(derivateId + ".xml");
    LOGGER.info(MessageFormat.format("Importing {0}", derivatePath.toAbsolutePath().toString()));
    MCRDerivate der = new MCRDerivate(sax.build(derivatePath.toFile()));
    MCRMetadataManager.update(der);
    MCRDirectory dir = MCRDirectory.getRootDirectory(der.getId().toString());
    if (dir == null) {
        LOGGER.info("Creating missing {} {}", MCRFilesystemNode.class.getSimpleName(), der.getId());
        final MCRDirectory difs = new MCRDirectory(der.getId().toString());
        der.getDerivate().getInternals().setIFSID(difs.getID());
        MCRMetadataManager.update(der);
    }
    try (Stream<Path> stream = Files.find(derivateDirectory, 5, filterDerivateDirectory(derivateId, derivateDirectory))) {
        stream.forEach(path -> {
            String targetPath = derivateDirectory.relativize(path).toString();
            try (InputStream in = Files.newInputStream(path)) {
                Files.copy(in, MCRPath.getPath(derivateId, targetPath), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException ioExc) {
                throw new MCRException("Unable to add file " + path.toAbsolutePath() + " to derivate " + derivateId, ioExc);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) MCRException(org.mycore.common.MCRException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) InputStream(java.io.InputStream) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException)

Example 42 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRMetsIIIFPresentationImpl method getMets.

public Document getMets(String id) throws IOException, JDOMException, SAXException {
    String objectid = MCRLinkTableManager.instance().getSourceOf(id).iterator().next();
    MCRContentTransformer transformer = getTransformer();
    MCRParameterCollector parameter = new MCRParameterCollector();
    if (objectid != null && objectid.length() != 0) {
        MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(id));
        MCRObjectID ownerID = derObj.getOwnerID();
        objectid = ownerID.toString();
        parameter.setParameter("objectID", objectid);
        parameter.setParameter("derivateID", id);
    }
    MCRPath metsPath = MCRPath.getPath(id, "mets.xml");
    if (!Files.exists(metsPath)) {
        throw new MCRException("File not found: " + id);
    }
    MCRPathContent source = new MCRPathContent(metsPath);
    MCRContent content = transformer instanceof MCRParameterizedTransformer ? ((MCRParameterizedTransformer) transformer).transform(source, parameter) : transformer.transform(source);
    return content.asXML();
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 43 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRIFSTest method createDerivate.

public static MCRDerivate createDerivate(MCRObjectID objectHrefId) {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(MCRObjectID.getNextFreeId("mycore_derivate"));
    derivate.setSchema("datamodel-derivate.xsd");
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    MCRMetaLinkID mcrMetaLinkID = new MCRMetaLinkID();
    mcrMetaLinkID.setReference(objectHrefId.toString(), null, null);
    derivate.getDerivate().setLinkMeta(mcrMetaLinkID);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 44 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRMODSCommands method createDerivate.

private static MCRDerivate createDerivate(MCRObjectID documentID, File fileDir) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(MCRObjectID.getNextFreeId(documentID.getProjectId(), "derivate"));
    derivate.setLabel("data object from " + documentID);
    String schema = CONFIG.getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml").replaceAll(".xml", ".xsd");
    derivate.setSchema(schema);
    MCRMetaLinkID linkId = new MCRMetaLinkID();
    linkId.setSubTag("linkmeta");
    linkId.setReference(documentID, null, null);
    derivate.getDerivate().setLinkMeta(linkId);
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(fileDir.getAbsolutePath());
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", derivate.getId());
    MCRMetadataManager.create(derivate);
    setDefaultPermissions(derivate.getId());
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 45 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRDFGLinkServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    HttpServletResponse response = job.getResponse();
    String filePath = request.getParameter("file") == null ? "" : request.getParameter("file");
    String derivateID = request.getParameter("deriv") == null ? "" : request.getParameter("deriv");
    if (derivateID.equals("")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Derivate is not set");
    }
    String encodedMetsURL = URLEncoder.encode(MCRServlet.getServletBaseURL() + "MCRMETSServlet/" + derivateID + "?XSL.Style=dfg", "UTF-8");
    LOGGER.info(request.getPathInfo());
    MCRPath rootPath = MCRPath.getPath(derivateID, "/");
    if (!Files.isDirectory(rootPath)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Derivate {0} does not exist.", derivateID));
        return;
    }
    request.setAttribute("XSL.derivateID", derivateID);
    Collection<String> linkList = MCRLinkTableManager.instance().getSourceOf(derivateID);
    if (linkList.isEmpty()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageFormat.format("Derivate {0} is not linked with a MCRObject. Please contact an administrator.", derivateID));
        return;
    }
    // TODO: this seems very very wrong
    if (filePath == "") {
        MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
        filePath = derivate.getDerivate().getInternals().getMainDoc();
    }
    MCRPath metsPath = (MCRPath) rootPath.resolve("mets.xml");
    int imageNumber = -2;
    if (Files.exists(metsPath)) {
        imageNumber = getOrderNumber(new MCRPathContent(metsPath).asXML(), filePath);
    } else {
        MCRContent metsContent = getMetsSource(job, useExistingMets(request), derivateID);
        imageNumber = getOrderNumber(metsContent.asXML(), filePath);
    }
    String dfgURL = "";
    switch(imageNumber) {
        case -1:
            response.sendError(HttpServletResponse.SC_CONFLICT, MessageFormat.format("Image \"{0}\" not found in the MCRDerivate. Please contact an administrator.", filePath));
            return;
        case -2:
            dfgURL = "http://dfg-viewer.de/show/?set[mets]=" + encodedMetsURL;
            break;
        default:
            dfgURL = "http://dfg-viewer.de/show/?set[mets]=" + encodedMetsURL + "&set[image]=" + imageNumber;
            break;
    }
    response.sendRedirect(dfgURL);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRPathContent(org.mycore.common.content.MCRPathContent) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Aggregations

MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)53 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)27 IOException (java.io.IOException)22 MCRPath (org.mycore.datamodel.niofs.MCRPath)19 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)10 MCRAccessException (org.mycore.access.MCRAccessException)9 Path (java.nio.file.Path)8 MCRException (org.mycore.common.MCRException)8 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)8 Document (org.jdom2.Document)7 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)6 ArrayList (java.util.ArrayList)5 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)5 MCRObjectDerivate (org.mycore.datamodel.metadata.MCRObjectDerivate)5 SAXException (org.xml.sax.SAXException)5 StringWriter (java.io.StringWriter)4