Search in sources :

Example 16 with MCRDerivate

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

the class MCRXMLFunctions method isDisplayedEnabledDerivate.

public static boolean isDisplayedEnabledDerivate(String derivateId) {
    MCRObjectID derId = MCRObjectID.getInstance(derivateId);
    ModifiedHandle modifiedHandle = MCRXMLMetaDataManagerHolder.instance.getLastModifiedHandle(derId, 30, TimeUnit.SECONDS);
    Boolean result;
    try {
        result = DISPLAY_DERIVATE_CACHE.getIfUpToDate(derivateId, modifiedHandle);
    } catch (IOException e) {
        LOGGER.warn("Error while determining when {} was last modified.", derId, e);
        return false;
    }
    if (result != null) {
        return result;
    }
    MCRDerivate der;
    try {
        org.jdom2.Document derDoc = MCRXMLMetaDataManagerHolder.instance.retrieveXML(derId);
        if (derDoc == null) {
            LOGGER.error("Derivate \"{}\" does not exist", derId);
            return false;
        }
        der = new MCRDerivate(derDoc);
    } catch (SAXException | JDOMException | IOException | RuntimeException e) {
        LOGGER.warn("Error while loading derivate: {}", derId, e);
        return false;
    }
    org.jdom2.Element derivateElem = der.getDerivate().createXML();
    String display = derivateElem.getAttributeValue("display", "true");
    Boolean returnValue = Boolean.valueOf(display);
    DISPLAY_DERIVATE_CACHE.put(derivateId, returnValue);
    return returnValue;
}
Also used : ModifiedHandle(org.mycore.common.MCRCache.ModifiedHandle) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 17 with MCRDerivate

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

the class MCREventHandlerBase method undoHandleEvent.

/**
 * This method roll back all calls for EventHandler for the event types
 * MCRObject, MCRDerivate and MCRFile.
 *
 * @param evt
 *            The MCREvent object
 */
public void undoHandleEvent(MCREvent evt) {
    if (evt.getObjectType().equals(MCREvent.OBJECT_TYPE)) {
        MCRObject obj = (MCRObject) evt.get("object");
        if (obj != null) {
            logger.debug("{} handling {} {}", getClass().getName(), obj.getId(), evt.getEventType());
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    undoObjectCreated(evt, obj);
                    break;
                case MCREvent.UPDATE_EVENT:
                    undoObjectUpdated(evt, obj);
                    break;
                case MCREvent.DELETE_EVENT:
                    undoObjectDeleted(evt, obj);
                    break;
                case MCREvent.REPAIR_EVENT:
                    undoObjectRepaired(evt, obj);
                    break;
                default:
                    logger.warn("Can't find method for an object data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.OBJECT_TYPE + " for event type {}", evt.getEventType());
        return;
    }
    if (evt.getObjectType().equals(MCREvent.DERIVATE_TYPE)) {
        MCRDerivate der = (MCRDerivate) evt.get("derivate");
        if (der != null) {
            logger.debug("{} handling {}{}", getClass().getName(), der.getId(), evt.getEventType());
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    undoDerivateCreated(evt, der);
                    break;
                case MCREvent.UPDATE_EVENT:
                    undoDerivateUpdated(evt, der);
                    break;
                case MCREvent.DELETE_EVENT:
                    undoDerivateDeleted(evt, der);
                    break;
                case MCREvent.REPAIR_EVENT:
                    undoDerivateRepaired(evt, der);
                    break;
                default:
                    logger.warn("Can't find method for a derivate data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.DERIVATE_TYPE + " for event type {}", evt.getEventType());
        return;
    }
    if (evt.getObjectType().equals(MCREvent.PATH_TYPE)) {
        Path path = (Path) evt.get(MCREvent.PATH_KEY);
        if (path != null) {
            if (!path.isAbsolute()) {
                logger.warn("Cannot handle path events on non absolute paths: {}", path);
            }
            logger.debug("{} handling {} {}", getClass().getName(), path, evt.getEventType());
            BasicFileAttributes attrs = (BasicFileAttributes) evt.get(MCREvent.FILEATTR_KEY);
            if (attrs == null && !evt.getEventType().equals(MCREvent.DELETE_EVENT)) {
                logger.warn("BasicFileAttributes for {} was not given. Resolving now.", path);
                try {
                    attrs = Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
                } catch (IOException e) {
                    logger.error("Could not get BasicFileAttributes from path: {}", path, e);
                }
            }
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    undoPathCreated(evt, path, attrs);
                    break;
                case MCREvent.UPDATE_EVENT:
                    undoPathUpdated(evt, path, attrs);
                    break;
                case MCREvent.DELETE_EVENT:
                    undoPathDeleted(evt, path, attrs);
                    break;
                case MCREvent.REPAIR_EVENT:
                    undoPathRepaired(evt, path, attrs);
                    break;
                default:
                    logger.warn("Can't find method for Path data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.PATH_TYPE + " for event type {}", evt.getEventType());
        return;
    }
    if (evt.getObjectType().equals(MCREvent.CLASS_TYPE)) {
        MCRCategory obj = (MCRCategory) evt.get("class");
        if (obj != null) {
            logger.debug("{} handling {} {}", getClass().getName(), obj.getId(), evt.getEventType());
            switch(evt.getEventType()) {
                case MCREvent.CREATE_EVENT:
                    undoClassificationCreated(evt, obj);
                    break;
                case MCREvent.UPDATE_EVENT:
                    undoClassificationUpdated(evt, obj);
                    break;
                case MCREvent.DELETE_EVENT:
                    undoClassificationDeleted(evt, obj);
                    break;
                case MCREvent.REPAIR_EVENT:
                    undoClassificationRepaired(evt, obj);
                    break;
                default:
                    logger.warn("Can't find method for an classification data handler for event type {}", evt.getEventType());
                    break;
            }
            return;
        }
        logger.warn("Can't find method for " + MCREvent.CLASS_TYPE + " for event type {}", evt.getEventType());
    }
}
Also used : Path(java.nio.file.Path) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 18 with MCRDerivate

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

the class MCRUpdateDerivateServlet method updateDerivateXML.

/**
 * Updates derivate xml in the persistence backend
 * @param editorSubmission
 *  MyCoRe derivate as XML
 * @return
 *  MCRObjectID of the MyCoRe object
 * @throws SAXParseException
 * @throws MCRAccessException
 */
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
    MCRObjectID objectID;
    Element root = editorSubmission.getRootElement();
    root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
    root.addNamespaceDeclaration(XLINK_NAMESPACE);
    root.addNamespaceDeclaration(XSI_NAMESPACE);
    byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
    MCRDerivate der = new MCRDerivate(xml, true);
    MCRObjectID derivateID = der.getId();
    // store entry of derivate xlink:title in object
    objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
    MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
    MCRObjectStructure structure = obj.getStructure();
    MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
    linkID.setXLinkTitle(der.getLabel());
    try {
        MCRMetadataManager.update(obj);
        MCRMetadataManager.update(der);
    } catch (MCRPersistenceException | MCRAccessException e) {
        throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
    }
    return objectID;
}
Also used : MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 19 with MCRDerivate

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

the class MCRDerivateServlet method setMainFile.

/**
 * The method set the main file of a derivate object that is stored in the
 * server. The method use the input parameter: <b>type</b>,<b>step</b>
 * <b>se_mcrid</b> and <b>re_mcrid</b>. Access rights must be 'writedb'.
 */
private void setMainFile(String derivateId, String file, HttpServletResponse response) throws IOException {
    if (MCRAccessManager.checkPermission(derivateId, PERMISSION_WRITE)) {
        MCRObjectID mcrid = MCRObjectID.getInstance(derivateId);
        MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(mcrid);
        der.getDerivate().getInternals().setMainDoc(file);
        MCRMetadataManager.updateMCRDerivateXML(der);
    } else {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, MessageFormat.format("User has not the \"" + PERMISSION_WRITE + "\" permission on object {0}.", derivateId));
    }
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 20 with MCRDerivate

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

the class MCRIView2Tools method getSupportedMainFile.

/**
 * @param derivateID
 *            ID of derivate
 * @return empty String or absolute path to main file of derivate if file is supported.
 */
public static String getSupportedMainFile(String derivateID) {
    try {
        MCRDerivate deriv = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
        String nameOfMainFile = deriv.getDerivate().getInternals().getMainDoc();
        // verify support
        if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
            MCRPath mainFile = MCRPath.getPath(derivateID, '/' + nameOfMainFile);
            if (mainFile != null && isFileSupported(mainFile))
                return mainFile.getRoot().relativize(mainFile).toString();
        }
    } catch (Exception e) {
        LOGGER.warn("Could not get main file of derivate.", e);
    }
    return "";
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPath(org.mycore.datamodel.niofs.MCRPath) NoSuchFileException(java.nio.file.NoSuchFileException) JDOMException(org.jdom2.JDOMException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) IOException(java.io.IOException) FileSystemAlreadyExistsException(java.nio.file.FileSystemAlreadyExistsException)

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