Search in sources :

Example 31 with MCRObject

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

the class MCRDefaultDerivateTitleResolver method resolveTitle.

@Override
public String resolveTitle(String derivateIDString) {
    MCRObjectID derivateID = MCRObjectID.getInstance(derivateIDString);
    final MCRObjectID objectID = MCRMetadataManager.getObjectId(derivateID, EXPIRE_METADATA_CACHE_TIME, TimeUnit.SECONDS);
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    return object.getStructure().getDerivateLink(derivateID).getXLinkTitle();
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 32 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject 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 33 with MCRObject

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

the class MCRCreateObjectServlet method createObject.

/**
 * Adds a new mycore object to the persistence backend.
 * @param doc
 *  MyCoRe object as XML
 * @return
 *  MCRObjectID of the newly created object.
 * @throws MCRActiveLinkException
 *  If links from or to other objects will fail.
 * @throws JDOMException
 *  from {@link MCRPersistenceHelper#getMCRObject(Document)}
 * @throws IOException
 *  from {@link MCRPersistenceHelper#getMCRObject(Document)}
 * @throws SAXParseException
 * @throws MCRException
 * @throws MCRAccessException
 */
private MCRObjectID createObject(Document doc) throws MCRActiveLinkException, JDOMException, IOException, MCRException, SAXParseException, MCRAccessException {
    MCRObject mcrObject = MCRPersistenceHelper.getMCRObject(doc);
    MCRObjectID objectId = mcrObject.getId();
    // noinspection SynchronizeOnNonFinalField
    checkCreatePrivilege(objectId);
    synchronized (this) {
        if (objectId.getNumberAsInteger() == 0) {
            String objId = mcrObject.getId().toString();
            objectId = MCRObjectID.getNextFreeId(objectId.getBase());
            if (mcrObject.getLabel().equals(objId))
                mcrObject.setLabel(objectId.toString());
            mcrObject.setId(objectId);
        }
    }
    MCRMetadataManager.create(mcrObject);
    return objectId;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 34 with MCRObject

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

the class MCRPersistenceHelper method getMCRObject.

/**
 * creates a MCRObject instance on base of JDOM document
 * @param doc
 *  MyCoRe object as XML.
 * @return
 * @throws JDOMException
 *  exception from underlying {@link MCREditorOutValidator}
 * @throws IOException
 *  exception from underlying {@link MCREditorOutValidator} or {@link XMLOutputter}
 * @throws SAXParseException
 * @throws MCRException
 */
static MCRObject getMCRObject(Document doc) throws JDOMException, IOException, MCRException, SAXParseException {
    String id = doc.getRootElement().getAttributeValue("ID");
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    MCREditorOutValidator ev = new MCREditorOutValidator(doc, objectID);
    Document jdom_out = ev.generateValidMyCoReObject();
    if (ev.getErrorLog().size() > 0 && LOGGER.isDebugEnabled()) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        StringWriter swOrig = new StringWriter();
        xout.output(doc, swOrig);
        LOGGER.debug("Input document \n{}", swOrig);
        for (String logMsg : ev.getErrorLog()) {
            LOGGER.debug(logMsg);
        }
        StringWriter swClean = new StringWriter();
        xout.output(jdom_out, swClean);
        LOGGER.debug("Results in \n{}", swClean);
    }
    return new MCRObject(jdom_out);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRObject(org.mycore.datamodel.metadata.MCRObject) StringWriter(java.io.StringWriter) MCREditorOutValidator(org.mycore.datamodel.metadata.validator.MCREditorOutValidator) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Document(org.jdom2.Document)

Example 35 with MCRObject

use of org.mycore.datamodel.metadata.MCRObject 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)

Aggregations

MCRObject (org.mycore.datamodel.metadata.MCRObject)71 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)33 Document (org.jdom2.Document)18 Element (org.jdom2.Element)17 MCRException (org.mycore.common.MCRException)16 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)14 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)14 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)12 IOException (java.io.IOException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRMODSWrapper (org.mycore.mods.MCRMODSWrapper)9 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)6 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)6 SAXException (org.xml.sax.SAXException)6 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5