Search in sources :

Example 1 with MCRMetaElement

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

the class MCRObjectMerger method mergeMetadata.

/**
 * Merges the metadata of the given source into the target object. Be aware that
 * performance isn't that good when validation is activated, due checking against
 * the schema each time a change is made.
 *
 * @param source the source which is merged into the target
 * @param validate If true, every change is tracked and validated against the
 *          xml schema of the mycore object. When a change is invalid it will be
 *          canceled and the merging continues.
 *          When set to false the mycore object will be merged without validation.
 *          This can result in an invalid object.
 *
 * @return true if something was merged
 */
public boolean mergeMetadata(MCRObject source, boolean validate) {
    MCRObjectMetadata targetMetadata = this.target.getMetadata();
    boolean merged = false;
    for (MCRMetaElement metaElementSource : source.getMetadata()) {
        MCRMetaElement metaElementTarget = targetMetadata.getMetadataElement(metaElementSource.getTag());
        if (metaElementTarget == null) {
            targetMetadata.setMetadataElement(metaElementSource.clone());
            if (validate && !validate(this.target)) {
                targetMetadata.removeMetadataElement(metaElementSource.getTag());
            } else {
                merged = true;
            }
        } else {
            for (MCRMetaInterface metaInterfaceSource : metaElementSource) {
                boolean equal = false;
                for (MCRMetaInterface metaInterfaceTarget : metaElementTarget) {
                    if (metaInterfaceSource.equals(metaInterfaceTarget)) {
                        equal = true;
                        break;
                    }
                }
                if (!equal) {
                    metaElementTarget.addMetaObject(metaInterfaceSource.clone());
                    if (validate && !validate(this.target)) {
                        metaElementTarget.removeMetaObject(metaInterfaceSource);
                    } else {
                        merged = true;
                    }
                }
            }
        }
    }
    return merged;
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata) MCRMetaInterface(org.mycore.datamodel.metadata.MCRMetaInterface)

Example 2 with MCRMetaElement

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

the class MCRDefaultMetadataShareAgent method shareableMetadataChanged.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritableMetadataChanged(org.mycore.datamodel.metadata.MCRObject, org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public boolean shareableMetadataChanged(MCRObject oldVersion, MCRObject newVersion) {
    final MCRObjectMetadata md = newVersion.getMetadata();
    final MCRObjectMetadata mdold = oldVersion.getMetadata();
    final Element newXML = md.createXML();
    Element oldXML = null;
    try {
        oldXML = mdold.createXML();
    } catch (MCRException exc) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("The old metadata of the object {} was invalid.", oldVersion.getId(), exc);
        }
    }
    // TODO: handle inheritance of mycore-mods in that component
    if (oldXML != null && MCRXMLHelper.deepEqual(newXML, oldXML)) {
        return false;
    }
    int numheritablemd = 0;
    int numheritablemdold;
    for (int i = 0; i < md.size(); i++) {
        final MCRMetaElement melm = md.getMetadataElement(i);
        if (melm.isHeritable()) {
            numheritablemd++;
            try {
                final MCRMetaElement melmold = mdold.getMetadataElement(melm.getTag());
                final Element jelm = melm.createXML(true);
                Element jelmold = null;
                try {
                    jelmold = melmold.createXML(true);
                } catch (MCRException exc) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("One of the old metadata elements is invalid.", exc);
                    }
                }
                if (jelmold == null || !MCRXMLHelper.deepEqual(jelmold, jelm)) {
                    return true;
                }
            } catch (final RuntimeException e) {
                return true;
            }
        }
    }
    numheritablemdold = (int) StreamSupport.stream(mdold.spliterator(), false).filter(MCRMetaElement::isHeritable).count();
    return numheritablemd != numheritablemdold;
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRException(org.mycore.common.MCRException) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Element(org.jdom2.Element) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata)

Example 3 with MCRMetaElement

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

the class MCRClassificationMappingEventHandler method createMapping.

private void createMapping(MCRObject obj) {
    MCRMetaElement mappings = obj.getMetadata().getMetadataElement("mappings");
    if (mappings != null) {
        oldMappings = mappings.clone();
        obj.getMetadata().removeMetadataElement("mappings");
    }
    Element currentClassElement = null;
    try {
        Document doc = new Document(obj.getMetadata().createXML().detach());
        XPathExpression<Element> classElementPath = XPathFactory.instance().compile("//*[@categid]", Filters.element());
        List<Element> classList = classElementPath.evaluate(doc);
        if (classList.size() > 0) {
            mappings = new MCRMetaElement();
            mappings.setTag("mappings");
            mappings.setClass(MCRMetaClassification.class);
            mappings.setHeritable(false);
            mappings.setNotInherit(true);
            obj.getMetadata().setMetadataElement(mappings);
        }
        for (Element classElement : classList) {
            currentClassElement = classElement;
            MCRCategory categ = DAO.getCategory(new MCRCategoryID(classElement.getAttributeValue("classid"), classElement.getAttributeValue("categid")), 0);
            addMappings(mappings, categ);
        }
    } catch (Exception je) {
        if (currentClassElement == null) {
            LOGGER.error("Error while finding classification elements", je);
        } else {
            LOGGER.error("Error while finding classification elements for {}", new XMLOutputter().outputString(currentClassElement), je);
        }
    } finally {
        if (mappings == null || mappings.size() == 0) {
            obj.getMetadata().removeMetadataElement("mappings");
        }
    }
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) XMLOutputter(org.jdom2.output.XMLOutputter) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 4 with MCRMetaElement

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

the class MCRMODSWrapper method setMODS.

public void setMODS(Element mods) {
    MCRObjectMetadata om = object.getMetadata();
    if (om.getMetadataElement(DEF_MODS_CONTAINER) != null) {
        om.removeMetadataElement(DEF_MODS_CONTAINER);
    }
    MCRMetaXML modsContainer = new MCRMetaXML(MODS_CONTAINER, null, 0);
    List<MCRMetaXML> list = Collections.nCopies(1, modsContainer);
    MCRMetaElement defModsContainer = new MCRMetaElement(MCRMetaXML.class, DEF_MODS_CONTAINER, false, true, list);
    om.setMetadataElement(defModsContainer);
    modsContainer.addContent(mods);
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata) MCRMetaXML(org.mycore.datamodel.metadata.MCRMetaXML)

Aggregations

MCRMetaElement (org.mycore.datamodel.metadata.MCRMetaElement)4 MCRObjectMetadata (org.mycore.datamodel.metadata.MCRObjectMetadata)3 Element (org.jdom2.Element)2 Document (org.jdom2.Document)1 XMLOutputter (org.jdom2.output.XMLOutputter)1 MCRException (org.mycore.common.MCRException)1 MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)1 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)1 MCRMetaInterface (org.mycore.datamodel.metadata.MCRMetaInterface)1 MCRMetaXML (org.mycore.datamodel.metadata.MCRMetaXML)1