Search in sources :

Example 6 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method linkDerivateToObject.

/**
 * Links the given derivate to the given object.
 */
@MCRCommand(syntax = "link derivate {0} to {1}", help = "links the given derivate {0} to the given mycore object {1}", order = 180)
public static void linkDerivateToObject(String derivateId, String objectId) throws Exception {
    if (derivateId == null || objectId == null) {
        LOGGER.error("Either derivate id or object id is null. Derivate={}, object={}", derivateId, objectId);
        return;
    }
    MCRObjectID derID = MCRObjectID.getInstance(derivateId);
    MCRObjectID objID = MCRObjectID.getInstance(objectId);
    if (!MCRMetadataManager.exists(objID)) {
        throw new Exception("The object with id " + objID + " does not exist");
    }
    if (!MCRMetadataManager.exists(derID)) {
        throw new Exception("The derivate with id " + derID + " does not exist");
    }
    MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(derID);
    MCRMetaLinkID oldDerivateToObjectLink = derObj.getDerivate().getMetaLink();
    MCRObjectID oldOwnerId = oldDerivateToObjectLink.getXLinkHrefID();
    /* set link to new parent in the derivate object */
    LOGGER.info("Setting {} as parent for derivate {}", objID, derID);
    derObj.getDerivate().getMetaLink().setReference(objID, oldDerivateToObjectLink.getXLinkLabel(), oldDerivateToObjectLink.getXLinkTitle());
    derObj.setLabel("data object from " + objectId + " (prev. owner was " + oldOwnerId);
    MCRMetadataManager.updateMCRDerivateXML(derObj);
    /* set link to derivate in the new parent */
    MCRObject oldOwner = MCRMetadataManager.retrieveMCRObject(oldOwnerId);
    List<MCRMetaLinkID> derivates = oldOwner.getStructure().getDerivates();
    MCRMetaLinkID oldObjectToDerivateLink = null;
    for (MCRMetaLinkID derivate : derivates) {
        if (derivate.getXLinkHrefID().equals(derID)) {
            oldObjectToDerivateLink = derivate;
        }
    }
    if (oldObjectToDerivateLink == null) {
        oldObjectToDerivateLink = new MCRMetaLinkID();
    }
    LOGGER.info("Linking derivate {} to {}", derID, objID);
    MCRMetaLinkID derivateLink = new MCRMetaLinkID();
    derivateLink.setReference(derID, oldObjectToDerivateLink.getXLinkLabel(), oldObjectToDerivateLink.getXLinkTitle());
    derivateLink.setSubTag("derobject");
    MCRMetadataManager.addOrUpdateDerivateToObject(objID, derivateLink);
    /* removing link from old parent */
    boolean flag = oldOwner.getStructure().removeDerivate(derID);
    LOGGER.info("Unlinking derivate {} from object {}. Success={}", derID, oldOwnerId, flag);
    MCRMetadataManager.fireUpdateEvent(oldOwner);
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 7 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRObjectCommands method checkDerivatesInObjects.

/**
 * Check the derivate links in objects of MCR base ID for existing. It looks to the XML store on the disk to get all
 * object IDs.
 *
 * @param base_id
 *            the base part of a MCRObjectID e.g. DocPortal_document
 */
@MCRCommand(syntax = "check derivate entries in objects for base {0}", help = "check in all objects with MCR base ID {0} for existing linked derivates", order = 400)
public static void checkDerivatesInObjects(String base_id) throws IOException {
    if (base_id == null || base_id.length() == 0) {
        LOGGER.error("Base ID missed for check derivate entries in objects for base {0}");
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(base_id);
    int counter = 0;
    int maxresults = id_list.size();
    for (String objid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, objid);
        // get from data
        MCRObjectID mcrobjid = MCRObjectID.getInstance(objid);
        MCRObject obj = MCRMetadataManager.retrieveMCRObject(mcrobjid);
        List<MCRMetaLinkID> derivate_entries = obj.getStructure().getDerivates();
        for (MCRMetaLinkID derivate_entry : derivate_entries) {
            String derid = derivate_entry.getXLinkHref();
            if (!mgr.exists(MCRObjectID.getInstance(derid))) {
                LOGGER.error("   !!! Missing derivate {} in database for base ID {}", derid, base_id);
            }
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 8 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRBasketPersistence method createNewDerivate.

/**
 * Creates a new, empty derivate.
 *
 * @param ownerID the ID of the object owning the new derivate
 * @param derivateOID a free derivate ID to use for the newly created derivate
 * @return the empty derivate that was created.
 * @throws IOException
 * @throws MCRPersistenceException
 * @throws MCRAccessException see {@link MCRMetadataManager#create(MCRDerivate)}
 */
private static MCRDerivate createNewDerivate(MCRObjectID ownerID, MCRObjectID derivateOID) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(derivateOID);
    derivate.setLabel("Saved basket data for " + ownerID);
    String schema = MCRConfiguration.instance().getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml");
    derivate.setSchema(schema.replaceAll(".xml", ".xsd"));
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    MCRMetaLinkID linkId = new MCRMetaLinkID();
    linkId.setSubTag("linkmeta");
    linkId.setReference(ownerID, null, null);
    derivate.getDerivate().setLinkMeta(linkId);
    MCRMetadataManager.create(derivate);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 9 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRTransferPackageUtil method importObjectCLI.

/**
 * Same as {@link #importObject(Path, String)} but returns a list of derivates which
 * should be imported afterwards.
 *
 * @param targetDirectory
 *                the directory where the *.tar was unpacked
 * @param objectId
 *                object id to import
 * @throws JDOMException
 *                coulnd't parse the import order xml
 * @throws IOException
 *                when an I/O error prevents a document from being fully parsed
 * @throws MCRActiveLinkException
 *                if object is created (no real update), see {@link MCRMetadataManager#create(MCRObject)}
 * @throws MCRAccessException
 *                if write permission is missing or see {@link MCRMetadataManager#create(MCRObject)}
 */
public static List<String> importObjectCLI(Path targetDirectory, String objectId) throws JDOMException, IOException, MCRActiveLinkException, MCRAccessException {
    SAXBuilder sax = new SAXBuilder();
    Path targetXML = targetDirectory.resolve(CONTENT_DIRECTORY).resolve(objectId + ".xml");
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(MessageFormat.format("Importing {0}", targetXML.toAbsolutePath().toString()));
    }
    Document objXML = sax.build(targetXML.toFile());
    MCRObject mcr = new MCRObject(objXML);
    mcr.setImportMode(true);
    List<String> derivates = new LinkedList<>();
    // one must copy the ids before updating the mcr objects
    for (MCRMetaLinkID id : mcr.getStructure().getDerivates()) {
        derivates.add(id.getXLinkHref());
    }
    // delete children & derivate -> will be added later
    mcr.getStructure().clearChildren();
    mcr.getStructure().clearDerivates();
    // update
    MCRMetadataManager.update(mcr);
    // return list of derivates
    return derivates;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SAXBuilder(org.jdom2.input.SAXBuilder) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) Document(org.jdom2.Document) LinkedList(java.util.LinkedList)

Example 10 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.

the class MCRUpdateDerivateServlet method updateDerivateXML.

/**
 * Updates derivate xml in the persistence backend
 * @param editorSubmission
 *  MyCoRe derivate as XML
 * @return
 *  MCRObjectID of the MyCoRe object
 * @throws SAXParseException
 * @throws MCRAccessException
 */
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
    MCRObjectID objectID;
    Element root = editorSubmission.getRootElement();
    root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
    root.addNamespaceDeclaration(XLINK_NAMESPACE);
    root.addNamespaceDeclaration(XSI_NAMESPACE);
    byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
    MCRDerivate der = new MCRDerivate(xml, true);
    MCRObjectID derivateID = der.getId();
    // store entry of derivate xlink:title in object
    objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
    MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
    MCRObjectStructure structure = obj.getStructure();
    MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
    linkID.setXLinkTitle(der.getLabel());
    try {
        MCRMetadataManager.update(obj);
        MCRMetadataManager.update(der);
    } catch (MCRPersistenceException | MCRAccessException e) {
        throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
    }
    return objectID;
}
Also used : MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Aggregations

MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)22 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)15 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)10 MCRAccessException (org.mycore.access.MCRAccessException)7 IOException (java.io.IOException)6 MCRPersistenceException (org.mycore.common.MCRPersistenceException)6 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 Element (org.jdom2.Element)4 MCRException (org.mycore.common.MCRException)4 TransformerException (javax.xml.transform.TransformerException)3 JDOMException (org.jdom2.JDOMException)3 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 FileNotFoundException (java.io.FileNotFoundException)2 Document (org.jdom2.Document)2