Search in sources :

Example 41 with MCRObject

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

the class MCRMetadataHistoryCommands method buildObjectHistory.

private static Stream<MCRMetaHistoryItem> buildObjectHistory(MCRObjectID objId, List<MCRMetadataVersion> versions) throws IOException {
    boolean exist = false;
    LogManager.getLogger().debug("Complete history rebuild of {} should be possible", objId);
    ArrayList<MCRMetaHistoryItem> items = new ArrayList<>(100);
    for (MCRMetadataVersion version : versions) {
        String user = version.getUser();
        Instant revDate = version.getDate().toInstant();
        if (version.getType() == MCRMetadataVersion.DELETED) {
            if (exist) {
                items.add(delete(objId, user, revDate));
                exist = false;
            }
        } else {
            // created or updated
            int timeOffset = 0;
            if (version.getType() == MCRMetadataVersion.CREATED && !exist) {
                items.add(create(objId, user, revDate));
                timeOffset = 1;
                exist = true;
            }
            try {
                MCRObject obj = new MCRObject(version.retrieve().asXML());
                boolean objectIsHidden = MCRMetadataHistoryManager.objectIsHidden(obj);
                if (objectIsHidden && exist) {
                    items.add(delete(objId, user, revDate.plusMillis(timeOffset)));
                    exist = false;
                } else if (!objectIsHidden && !exist) {
                    items.add(create(objId, user, revDate.plusMillis(timeOffset)));
                    exist = true;
                }
            } catch (JDOMException | SAXException e) {
                LogManager.getLogger().error("Error while reading revision {} of {}", version.getRevision(), objId, e);
            }
        }
    }
    return items.stream();
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Instant(java.time.Instant) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException)

Example 42 with MCRObject

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

the class MCRDefaultMetadataShareAgent method receiveMetadata.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject, org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public void receiveMetadata(MCRObject child) {
    MCRObjectID parentID = child.getStructure().getParentID();
    if (parentID == null) {
        return;
    }
    LOGGER.debug("Parent ID = {}", parentID);
    MCRObject parent = MCRMetadataManager.retrieveMCRObject(parentID);
    // remove already embedded inherited tags
    child.getMetadata().removeInheritedMetadata();
    // insert heritable tags
    child.getMetadata().appendMetadata(parent.getMetadata().getHeritableMetadata());
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 43 with MCRObject

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

the class MCRDefaultMetadataShareAgent method distributeMetadata.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public void distributeMetadata(MCRObject parent) throws MCRPersistenceException, MCRAccessException {
    for (MCRMetaLinkID childId : parent.getStructure().getChildren()) {
        LOGGER.debug("Update metadata from Child {}", childId);
        final MCRObject child = MCRMetadataManager.retrieveMCRObject(childId.getXLinkHrefID());
        MCRMetadataManager.update(child);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID)

Example 44 with MCRObject

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

the class MCREditorOutValidator method generateValidMyCoReObject.

/**
 * tries to generate a valid MCRObject as JDOM Document.
 *
 * @return MCRObject
 */
public Document generateValidMyCoReObject() throws JDOMException, SAXParseException, IOException {
    MCRObject obj;
    // load the JDOM object
    XPathFactory.instance().compile("/mycoreobject/*/*/*/@editor.output", Filters.attribute()).evaluate(input).forEach(Attribute::detach);
    try {
        byte[] xml = new MCRJDOMContent(input).asByteArray();
        obj = new MCRObject(xml, true);
    } catch (SAXParseException e) {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        LOGGER.warn("Failure while parsing document:\n{}", xout.outputString(input));
        throw e;
    }
    Date curTime = new Date();
    obj.getService().setDate("modifydate", curTime);
    // return the XML tree
    input = obj.createXML();
    return input;
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRObject(org.mycore.datamodel.metadata.MCRObject) Attribute(org.jdom2.Attribute) SAXParseException(org.xml.sax.SAXParseException) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRMetaISO8601Date(org.mycore.datamodel.metadata.MCRMetaISO8601Date) Date(java.util.Date) MCRMetaHistoryDate(org.mycore.datamodel.metadata.MCRMetaHistoryDate)

Example 45 with MCRObject

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

the class MCRObjectCommands method processFromFile.

/**
 * Load or update an MCRObject's from an XML file.
 *
 * @param file
 *            the location of the xml file
 * @param update
 *            if true, object will be updated, else object is created
 * @param importMode
 *            if true, servdates are taken from xml file
 * @throws SAXParseException
 *            unable to build the mycore object from the file's URI
 * @throws MCRException
 *            the parent of the given object does not exists
 * @throws MCRAccessException
 *            if write permission is missing
 */
private static boolean processFromFile(File file, boolean update, boolean importMode) throws MCRException, SAXParseException, IOException, MCRAccessException {
    if (!file.getName().endsWith(".xml")) {
        LOGGER.warn("{} ignored, does not end with *.xml", file);
        return false;
    }
    if (!file.isFile()) {
        LOGGER.warn("{} ignored, is not a file.", file);
        return false;
    }
    LOGGER.info("Reading file {} ...", file);
    MCRObject mcrObject = new MCRObject(file.toURI());
    if (mcrObject.hasParent()) {
        MCRObjectID parentID = mcrObject.getStructure().getParentID();
        if (!MCRMetadataManager.exists(mcrObject.getStructure().getParentID())) {
            throw new MCRException("The parent object " + parentID + "does not exist for " + mcrObject + ".");
        }
    }
    mcrObject.setImportMode(importMode);
    LOGGER.debug("Label --> {}", mcrObject.getLabel());
    if (update) {
        MCRMetadataManager.update(mcrObject);
        LOGGER.info("{} updated.", mcrObject.getId());
    } else {
        MCRMetadataManager.create(mcrObject);
        LOGGER.info("{} loaded.", mcrObject.getId());
    }
    return true;
}
Also used : MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

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