Search in sources :

Example 1 with MCRMetaInterface

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

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

the class MCREditorOutValidator method checkMetaObject.

public static String checkMetaObject(Element datasubtag, Class<? extends MCRMetaInterface> metaClass, boolean keepLang) {
    if (!keepLang) {
        datasubtag.removeAttribute("lang", XML_NAMESPACE);
    }
    MCRMetaInterface test = null;
    try {
        test = metaClass.getDeclaredConstructor().newInstance();
    } catch (Exception e) {
        throw new MCRException("Could not instantiate " + metaClass.getCanonicalName());
    }
    test.setFromDOM(datasubtag);
    test.validate();
    return null;
}
Also used : MCRException(org.mycore.common.MCRException) MCRMetaInterface(org.mycore.datamodel.metadata.MCRMetaInterface) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Aggregations

MCRMetaInterface (org.mycore.datamodel.metadata.MCRMetaInterface)2 IOException (java.io.IOException)1 JDOMException (org.jdom2.JDOMException)1 MCRException (org.mycore.common.MCRException)1 MCRMetaElement (org.mycore.datamodel.metadata.MCRMetaElement)1 MCRObjectMetadata (org.mycore.datamodel.metadata.MCRObjectMetadata)1 SAXParseException (org.xml.sax.SAXParseException)1