Search in sources :

Example 31 with MCRObjectID

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

the class MCRAccessManager method checkPermissionForReadingDerivate.

/**
 * checks whether the current user has the permission to read/see a derivate check is also against the mcrobject,
 * the derivate belongs to both checks must return true <br>
 * it is needed in MCRFileNodeServlet and MCRZipServlet
 *
 * @param derID
 *            String ID of a MyCoRe-Derivate
 * @return true if the access is allowed otherwise it return false
 */
public static boolean checkPermissionForReadingDerivate(String derID) {
    // derID must be a derivate ID
    boolean accessAllowed = false;
    MCRObjectID objectId = MCRMetadataManager.getObjectId(MCRObjectID.getInstance(derID), 10, TimeUnit.MINUTES);
    if (objectId != null) {
        accessAllowed = checkPermission(objectId, PERMISSION_READ) && checkPermission(derID, PERMISSION_READ);
    } else {
        accessAllowed = checkPermission(derID, PERMISSION_READ);
        LogManager.getLogger("MCRAccessManager.class").warn("no mcrobject could be found for derivate: {}", derID);
    }
    return accessAllowed;
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 32 with MCRObjectID

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

the class MCRCreatorRuleStrategy method isCreatorRuleAvailable.

public boolean isCreatorRuleAvailable(String id, String permission) {
    if (MCRAccessManager.PERMISSION_WRITE.equals(permission)) {
        MCRObjectID mcrObjectId = null;
        try {
            mcrObjectId = MCRObjectID.getInstance(id);
            MCRUserInformation currentUser = MCRSessionMgr.getCurrentSession().getUserInformation();
            if (currentUser.isUserInRole(CREATOR_ROLE) && objectStatusIsSubmitted(mcrObjectId)) {
                if (isCurrentUserCreator(mcrObjectId, currentUser)) {
                    return true;
                }
            }
        } catch (RuntimeException e) {
            if (mcrObjectId == null) {
                LOGGER.debug("id is not a valid object ID", e);
            } else {
                LOGGER.warn("Eror while checking permission.", e);
            }
        }
    }
    return false;
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRUserInformation(org.mycore.common.MCRUserInformation)

Example 33 with MCRObjectID

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

the class MCRDerivateCommands method synchronizeDerivateForID.

/**
 * The method sychronize the xlink:label of the mycorederivate with the
 * xlink:label of the derivate reference of mycoreobject.
 *
 * @param id
 *            the MCRObjectID as String
 */
@MCRCommand(syntax = "synchronize derivate with ID {0}", help = "The command read a derivate with the MCRObjectID {0} and synchronize the xlink:label with the derivate entry of the mycoreobject.", order = 170)
public static void synchronizeDerivateForID(String id) {
    MCRObjectID mid = null;
    try {
        mid = MCRObjectID.getInstance(id);
    } catch (Exception e) {
        LOGGER.error("The String {} is not a MCRObjectID.", id);
        return;
    }
    // set mycoreobject
    MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(mid);
    String label = der.getLabel();
    String href = der.getDerivate().getMetaLink().getXLinkHref();
    MCRObject obj = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(href));
    int size = obj.getStructure().getDerivates().size();
    boolean isset = false;
    for (int i = 0; i < size; i++) {
        MCRMetaLinkID link = obj.getStructure().getDerivates().get(i);
        if (link.getXLinkHref().equals(mid.toString())) {
            String oldlabel = link.getXLinkLabel();
            if (oldlabel != null && !oldlabel.trim().equals(label)) {
                obj.getStructure().getDerivates().get(i).setXLinkTitle(label);
                isset = true;
            }
            break;
        }
    }
    // update mycoreobject
    if (isset) {
        MCRMetadataManager.fireUpdateEvent(obj);
        LOGGER.info("Synchronized {}", mid);
    }
}
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 34 with MCRObjectID

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

the class MCRDerivateCommands method exportDerivate.

/**
 * @param dir
 * @param trans
 * @param nid
 * @throws FileNotFoundException
 * @throws TransformerException
 * @throws IOException
 */
private static void exportDerivate(File dir, Transformer trans, String nid) throws TransformerException, IOException {
    // store the XML file
    Document xml = null;
    MCRDerivate obj;
    MCRObjectID derivateID = MCRObjectID.getInstance(nid);
    try {
        obj = MCRMetadataManager.retrieveMCRDerivate(derivateID);
        String path = obj.getDerivate().getInternals().getSourcePath();
        // reset from the absolute to relative path, for later reload
        LOGGER.info("Old Internal Path ====>{}", path);
        obj.getDerivate().getInternals().setSourcePath(nid);
        LOGGER.info("New Internal Path ====>{}", nid);
        // add ACL's
        Collection<String> l = ACCESS_IMPL.getPermissionsForID(nid);
        for (String permission : l) {
            Element rule = ACCESS_IMPL.getRule(nid, permission);
            obj.getService().addRule(permission, rule);
        }
        // build JDOM
        xml = obj.createXML();
    } catch (MCRException ex) {
        LOGGER.warn("Could not read {}, continue with next ID", nid);
        return;
    }
    File xmlOutput = new File(dir, derivateID + ".xml");
    FileOutputStream out = new FileOutputStream(xmlOutput);
    dir = new File(dir, derivateID.toString());
    if (trans != null) {
        trans.setParameter("dirname", dir.getPath());
        StreamResult sr = new StreamResult(out);
        trans.transform(new org.jdom2.transform.JDOMSource(xml), sr);
    } else {
        new org.jdom2.output.XMLOutputter().output(xml, out);
        out.flush();
        out.close();
    }
    LOGGER.info("Object {} stored under {}.", nid, xmlOutput);
    // store the derivate file under dirname
    if (!dir.isDirectory()) {
        dir.mkdir();
    }
    MCRPath rootPath = MCRPath.getPath(derivateID.toString(), "/");
    Files.walkFileTree(rootPath, new MCRTreeCopier(rootPath, dir.toPath()));
    LOGGER.info("Derivate {} saved under {} and {}.", nid, dir, xmlOutput);
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.jdom2.Element) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) FileOutputStream(java.io.FileOutputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File)

Example 35 with MCRObjectID

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

Aggregations

MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)144 IOException (java.io.IOException)37 MCRObject (org.mycore.datamodel.metadata.MCRObject)32 MCRException (org.mycore.common.MCRException)30 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)30 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)29 MCRPath (org.mycore.datamodel.niofs.MCRPath)25 MCRAccessException (org.mycore.access.MCRAccessException)22 Document (org.jdom2.Document)20 MCRPersistenceException (org.mycore.common.MCRPersistenceException)18 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)16 JDOMException (org.jdom2.JDOMException)15 MCRBase (org.mycore.datamodel.metadata.MCRBase)15 SAXException (org.xml.sax.SAXException)15 Date (java.util.Date)14 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)13 MCRSession (org.mycore.common.MCRSession)11 MCRContent (org.mycore.common.content.MCRContent)11 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)11 URISyntaxException (java.net.URISyntaxException)10