use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRMigrationCommands method fixDerivateLinks.
@MCRCommand(syntax = "fix invalid derivate links {0} for {1}", help = "Fixes the paths of all derivate links " + "({0} -> xpath -> e.g. /mycoreobject/metadata/derivateLinks/derivateLink) for object {1}. (MCR-1267)", order = 15)
public static void fixDerivateLinks(String xpath, String id) throws IOException, JDOMException, SAXException {
// get mcr object
MCRObjectID objectID = MCRObjectID.getInstance(id);
// find derivate links
Document xml = MCRXMLMetadataManager.instance().retrieveXML(objectID);
Element mcrObjectXML = xml.getRootElement();
XPathExpression<Element> expression = XPathFactory.instance().compile(xpath, Filters.element());
List<Element> derivateLinkElements = expression.evaluate(mcrObjectXML);
// check them
boolean changedObject = false;
for (Element derivateLinkElement : derivateLinkElements) {
String href = derivateLinkElement.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
MCRMetaDerivateLink link = new MCRMetaDerivateLink();
link.setReference(href, null, null);
String owner = link.getOwner();
try {
String path = link.getPath();
MCRPath mcrPath = MCRPath.getPath(owner, path);
if (!Files.exists(mcrPath)) {
// -> e.g. a?c.tif -> path (a), query (c.tif) which is obvious wrong
if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
changedObject = true;
} else {
LOGGER.warn("{} of {}cannot be found on file system. This is most likly a dead link.", href, objectID);
}
}
} catch (URISyntaxException uriExc) {
// not encoded properly
if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
changedObject = true;
} else {
LOGGER.warn("{} of {} isn't URI encoded and cannot be found on file system. This is most likly a dead link.", href, objectID);
}
}
}
// store the mcr object if its changed
if (changedObject) {
// we use MCRXMLMetadataMananger because we don't want to validate the old mcr object
MCRXMLMetadataManager.instance().update(objectID, xml, new Date());
// manually fire update event
MCRObject newObject = MCRMetadataManager.retrieveMCRObject(objectID);
newObject.setImportMode(true);
MCRMetadataManager.fireUpdateEvent(newObject);
}
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRMigrationCommands method fixMissingChildren.
@MCRCommand(syntax = "add missing children to {0}", help = "Adds missing children to structure of parent {0}. (MCR-1480)", order = 15)
public static void fixMissingChildren(String id) throws IOException, JDOMException, SAXException {
MCRObjectID parentId = MCRObjectID.getInstance(id);
Collection<String> children = MCRLinkTableManager.instance().getSourceOf(parentId, MCRLinkTableManager.ENTRY_TYPE_PARENT);
if (children.isEmpty()) {
return;
}
MCRObject parent = MCRMetadataManager.retrieveMCRObject(parentId);
MCRObjectStructure parentStructure = parent.getStructure();
int sizeBefore = parentStructure.getChildren().size();
children.stream().map(MCRObjectID::getInstance).filter(cid -> !parentStructure.getChildren().stream().anyMatch(candidate -> candidate.getXLinkHrefID().equals(cid))).sorted().map(MCRMigrationCommands::toLinkId).sequential().peek(lid -> LOGGER.info("Adding {} to {}", lid, parentId)).forEach(parentStructure::addChild);
if (parentStructure.getChildren().size() != sizeBefore) {
MCRMetadataManager.fireUpdateEvent(parent);
}
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRExtractRelatedItemsEventHandler method createRelatedObject.
private MCRObjectID createRelatedObject(Element relatedItem, MCRObjectID childID) throws MCRPersistenceException, MCRAccessException {
MCRMODSWrapper wrapper = new MCRMODSWrapper();
MCRObject object = wrapper.getMCRObject();
MCRObjectID oid = MCRObjectID.getNextFreeId(childID.getBase());
if (oid.equals(childID)) {
oid = MCRObjectID.getNextFreeId(childID.getBase());
}
object.setId(oid);
if (isHost(relatedItem)) {
object.getStructure().addChild(new MCRMetaLinkID("child", childID, childID.toString(), childID.toString()));
}
Element mods = cloneRelatedItem(relatedItem);
wrapper.setMODS(mods);
LOGGER.info("create object {}", oid);
MCRMetadataManager.create(object);
return oid;
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRMODSCommands method saveAsMyCoReObject.
private static MCRObjectID saveAsMyCoReObject(String projectID, Element modsRoot) throws MCRActiveLinkException, MCRPersistenceException, MCRAccessException {
MCRObject mcrObject = MCRMODSWrapper.wrapMODSDocument(modsRoot, projectID);
mcrObject.setId(MCRObjectID.getNextFreeId(mcrObject.getId().getBase()));
MCRMetadataManager.create(mcrObject);
return mcrObject.getId();
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRRSSFeedImporter method handleFeedEntry.
private MCRObject handleFeedEntry(SyndEntry entry, String projectID) throws MCRPersistenceException, MCRAccessException {
String publicationID = getPublicationID(entry);
if (publicationID == null) {
return null;
}
if (isAlreadyStored(publicationID)) {
LOGGER.info("publication with ID {} already existing, will not import.", publicationID);
return null;
}
LOGGER.info("publication with ID {} does not exist yet, retrieving data...", publicationID);
Element publicationXML = retrieveAndConvertPublication(publicationID);
if (shouldIgnore(publicationXML)) {
LOGGER.info("publication will be ignored, do not store.");
return null;
}
MCRObject obj = buildMCRObject(publicationXML, projectID);
MCRMetadataManager.create(obj);
return obj;
}
Aggregations