Search in sources :

Example 31 with MCRDerivate

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

the class MCRUploadHandlerIFS method createDerivate.

private MCRDerivate createDerivate(MCRObjectID derivateID) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(derivateID);
    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(null);
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", this.derivateID);
    MCRMetadataManager.create(derivate);
    setDefaultPermissions(derivateID);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 32 with MCRDerivate

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

the class MCRDisplayHideDerivateServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    String derivate = job.getRequest().getParameter("derivate");
    if (derivate == null || (!derivate.contains("_derivate_"))) {
        LOGGER.error("Cannot toogle display attribute. No derivate id provided.");
        job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "You must provide a proper derivate id");
        return;
    }
    if (!MCRAccessManager.checkPermission(MCRObjectID.getInstance(derivate), PERMISSION_WRITE)) {
        job.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN, "You have to be logged in.");
        return;
    }
    LOGGER.info("Toggling display attribute of {}", derivate);
    MCRDerivate obj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
    toggleDisplay(obj);
    String url = MCRFrontendUtil.getBaseURL() + "receive/" + getParentHref(obj);
    job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(url));
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate)

Example 33 with MCRDerivate

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

the class MCRDisplayHideDerivateServlet method toggleDisplay.

/**
 * Toggles the display attribute value of the derivate element.
 */
private void toggleDisplay(MCRDerivate derObj) throws Exception {
    Document xml = derObj.createXML();
    Element derivateNode = xml.getRootElement().getChild("derivate");
    Attribute displayAttr = derivateNode.getAttribute("display");
    /* the attributs is not existing, user wants to hide derivate */
    if (displayAttr == null) {
        displayAttr = new Attribute("display", "false");
        derivateNode.setAttribute(displayAttr);
    } else /* attribute exists, thus toggle the attribute value */
    {
        String oldVal = displayAttr.getValue();
        String newVal = oldVal.equals(String.valueOf(true)) ? String.valueOf(false) : String.valueOf(true);
        displayAttr.setValue(newVal);
        LOGGER.info("Setting display attribute of derivate with id {} to {}", derObj.getId(), newVal);
    }
    MCRDerivate updated = new MCRDerivate(xml);
    MCRMetadataManager.updateMCRDerivateXML(updated);
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document)

Example 34 with MCRDerivate

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

the class MCRSwordMediaHandler method deleteMediaResource.

public void deleteMediaResource(String derivateId, String requestFilePath) throws SwordError, SwordServerException {
    if (!MCRAccessManager.checkPermission(derivateId, MCRAccessManager.PERMISSION_DELETE)) {
        throw new SwordError(UriRegistry.ERROR_METHOD_NOT_ALLOWED, "You dont have the right to delete (from) the derivate!");
    }
    if (requestFilePath == null || requestFilePath.equals("/")) {
        final MCRObjectID derivateID = MCRObjectID.getInstance(derivateId);
        if (MCRMetadataManager.exists(derivateID)) {
            final MCRDerivate mcrDerivate = MCRMetadataManager.retrieveMCRDerivate(derivateID);
            try {
                MCRMetadataManager.delete(mcrDerivate);
            } catch (MCRAccessException e) {
                throw new SwordError(UriRegistry.ERROR_METHOD_NOT_ALLOWED, e);
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_NOT_FOUND, "The requested Object '" + requestFilePath + "' does not exists.");
        }
    } else {
        final MCRPath path = MCRPath.getPath(derivateId, requestFilePath);
        checkFile(path);
        try {
            if (Files.isDirectory(path)) {
                Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.delete(file);
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                        Files.delete(dir);
                        return FileVisitResult.CONTINUE;
                    }
                });
            } else {
                Files.delete(path);
            }
        } catch (IOException e) {
            throw new SwordServerException("Error while deleting media resource!", e);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SwordServerException(org.swordapp.server.SwordServerException) SwordError(org.swordapp.server.SwordError) MCRAccessException(org.mycore.access.MCRAccessException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) FileVisitResult(java.nio.file.FileVisitResult) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 35 with MCRDerivate

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

the class MCRSwordUtil method createDerivate.

public static MCRDerivate createDerivate(String documentID) throws MCRPersistenceException, IOException, MCRAccessException {
    final String projectId = MCRObjectID.getInstance(documentID).getProjectId();
    MCRObjectID oid = MCRObjectID.getNextFreeId(projectId, "derivate");
    final String derivateID = oid.toString();
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(oid);
    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(null);
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", derivateID);
    MCRMetadataManager.create(derivate);
    if (CONFIG.getBoolean("MCR.Access.AddDerivateDefaultRule", true)) {
        MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
        Collection<String> configuredPermissions = AI.getAccessPermissionsFromConfiguration();
        for (String permission : configuredPermissions) {
            MCRAccessManager.addRule(derivateID, permission, MCRAccessManager.getTrueRule(), "default derivate rule");
        }
    }
    final MCRPath rootDir = MCRPath.getPath(derivateID, "/");
    if (Files.notExists(rootDir)) {
        rootDir.getFileSystem().createRoot(derivateID);
    }
    return derivate;
}
Also used : MCRAccessInterface(org.mycore.access.MCRAccessInterface) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) MCRPath(org.mycore.datamodel.niofs.MCRPath)

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