Search in sources :

Example 1 with MCRObject

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);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) Element(org.jdom2.Element) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) Document(org.jdom2.Document) MCRPath(org.mycore.datamodel.niofs.MCRPath) Date(java.util.Date) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 2 with MCRObject

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);
    }
}
Also used : MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) Document(org.jdom2.Document) MCRPI(org.mycore.pi.backend.MCRPI) JDOMException(org.jdom2.JDOMException) MCRTileJob(org.mycore.iview2.services.MCRTileJob) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) MCRLinkTableManager(org.mycore.datamodel.common.MCRLinkTableManager) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) XPathExpression(org.jdom2.xpath.XPathExpression) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) Element(org.jdom2.Element) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) XPathFactory(org.jdom2.xpath.XPathFactory) MCRConstants(org.mycore.common.MCRConstants) TypedQuery(javax.persistence.TypedQuery) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) MCRLINKHREFPK_(org.mycore.backend.jpa.links.MCRLINKHREFPK_) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) Root(javax.persistence.criteria.Root) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Files(java.nio.file.Files) IOException(java.io.IOException) MCRLINKHREF_(org.mycore.backend.jpa.links.MCRLINKHREF_) EntityManager(javax.persistence.EntityManager) Paths(java.nio.file.Paths) EntityTransaction(javax.persistence.EntityTransaction) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) LogManager(org.apache.logging.log4j.LogManager) Filters(org.jdom2.filter.Filters) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 3 with MCRObject

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;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 4 with MCRObject

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();
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject)

Example 5 with MCRObject

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;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element)

Aggregations

MCRObject (org.mycore.datamodel.metadata.MCRObject)71 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)33 Document (org.jdom2.Document)18 Element (org.jdom2.Element)17 MCRException (org.mycore.common.MCRException)16 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)14 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)14 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)12 IOException (java.io.IOException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRMODSWrapper (org.mycore.mods.MCRMODSWrapper)9 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)6 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)6 SAXException (org.xml.sax.SAXException)6 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5