Search in sources :

Example 1 with MCRMetaDerivateLink

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

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

Aggregations

MCRMetaDerivateLink (org.mycore.datamodel.metadata.MCRMetaDerivateLink)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Document (org.jdom2.Document)1 Element (org.jdom2.Element)1 MCRCategLinkReference (org.mycore.datamodel.classifications2.MCRCategLinkReference)1 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)1 MCRMetaClassification (org.mycore.datamodel.metadata.MCRMetaClassification)1 MCRMetaLink (org.mycore.datamodel.metadata.MCRMetaLink)1 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)1 MCRObject (org.mycore.datamodel.metadata.MCRObject)1 MCRObjectMetadata (org.mycore.datamodel.metadata.MCRObjectMetadata)1 MCRObjectStructure (org.mycore.datamodel.metadata.MCRObjectStructure)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)1