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