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;
}
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, "");
}
}
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;
}
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;
}
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);
}
Aggregations