Search in sources :

Example 1 with MCRObjectMetadata

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

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

the class MCRLinkTableManager method create.

/**
 * Creates all references for the given object. You should call {@link #delete(MCRObjectID)} before using this
 * method otherwise doublets could occur.
 *
 * @param obj the object to create the references
 */
public void create(MCRObject obj) {
    MCRObjectID mcrId = obj.getId();
    // set new entries
    MCRObjectMetadata meta = obj.getMetadata();
    // use Set for category collection to remove duplicates if there are any
    Collection<MCRCategoryID> categories = new HashSet<>();
    meta.stream().flatMap(MCRMetaElement::stream).forEach(inf -> {
        if (inf instanceof MCRMetaClassification) {
            String classId = ((MCRMetaClassification) inf).getClassId();
            String categId = ((MCRMetaClassification) inf).getCategId();
            categories.add(new MCRCategoryID(classId, categId));
        } else if (inf instanceof MCRMetaLinkID) {
            addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE, "");
        } else if (inf instanceof MCRMetaDerivateLink) {
            addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE_LINK, "");
        }
    });
    MCRCategoryID state = obj.getService().getState();
    if (state != null) {
        categories.add(state);
    }
    if (categories.size() > 0) {
        MCRCategLinkReference objectReference = new MCRCategLinkReference(mcrId);
        MCRCategLinkServiceFactory.getInstance().setLinks(objectReference, categories);
    }
    // add derivate reference
    MCRObjectStructure structure = obj.getStructure();
    for (int i = 0; i < structure.getDerivates().size(); i++) {
        MCRMetaLinkID lid = structure.getDerivates().get(i);
        addReferenceLink(obj.getId(), lid.getXLinkHrefID(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE, "");
    }
    // add parent reference
    if (structure.getParentID() != null) {
        addReferenceLink(mcrId, structure.getParentID(), MCRLinkTableManager.ENTRY_TYPE_PARENT, "");
    }
}
Also used : MCRMetaClassification(org.mycore.datamodel.metadata.MCRMetaClassification) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata) MCRMetaLink(org.mycore.datamodel.metadata.MCRMetaLink) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) HashSet(java.util.HashSet)

Example 3 with MCRObjectMetadata

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

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

the class MCRMODSMetadataShareAgent 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();
    // if any metadata changed we need to update children
    boolean metadataChanged = !MCRXMLHelper.deepEqual(md.createXML(), mdold.createXML());
    if (!metadataChanged) {
        LOGGER.info("Metadata did not change on update of {}", newVersion.getId());
    }
    return metadataChanged;
}
Also used : MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata)

Example 5 with MCRObjectMetadata

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

MCRObjectMetadata (org.mycore.datamodel.metadata.MCRObjectMetadata)5 MCRMetaElement (org.mycore.datamodel.metadata.MCRMetaElement)3 HashSet (java.util.HashSet)1 Element (org.jdom2.Element)1 MCRException (org.mycore.common.MCRException)1 MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)1 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)1 MCRMetaClassification (org.mycore.datamodel.metadata.MCRMetaClassification)1 MCRMetaDerivateLink (org.mycore.datamodel.metadata.MCRMetaDerivateLink)1 MCRMetaInterface (org.mycore.datamodel.metadata.MCRMetaInterface)1 MCRMetaLink (org.mycore.datamodel.metadata.MCRMetaLink)1 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)1 MCRMetaXML (org.mycore.datamodel.metadata.MCRMetaXML)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1 MCRObjectStructure (org.mycore.datamodel.metadata.MCRObjectStructure)1