Search in sources :

Example 21 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRMODSMetadataShareAgent method distributeMetadata.

/* (non-Javadoc)
     * @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
     */
@Override
public void distributeMetadata(MCRObject holder) throws MCRPersistenceException, MCRAccessException {
    MCRMODSWrapper holderWrapper = new MCRMODSWrapper(holder);
    List<MCRMetaLinkID> children = holder.getStructure().getChildren();
    if (!children.isEmpty()) {
        LOGGER.info("Update inherited metadata");
        for (MCRMetaLinkID childIdRef : children) {
            MCRObjectID childId = childIdRef.getXLinkHrefID();
            if (MCRMODSWrapper.isSupported(childId)) {
                LOGGER.info("Update: {}", childIdRef);
                MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
                MCRMODSWrapper childWrapper = new MCRMODSWrapper(child);
                inheritToChild(holderWrapper, childWrapper);
                LOGGER.info("Saving: {}", childIdRef);
                try {
                    MCRMetadataManager.update(child);
                } catch (MCRPersistenceException | MCRAccessException e) {
                    throw new MCRPersistenceException("Error while updating inherited metadata", e);
                }
            }
        }
    }
    Collection<String> recipientIds = MCRLinkTableManager.instance().getSourceOf(holder.getId(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
    for (String rId : recipientIds) {
        MCRObjectID recipientId = MCRObjectID.getInstance(rId);
        if (MCRMODSWrapper.isSupported(recipientId)) {
            LOGGER.info("distribute metadata to {}", rId);
            MCRObject recipient = MCRMetadataManager.retrieveMCRObject(recipientId);
            MCRMODSWrapper recipientWrapper = new MCRMODSWrapper(recipient);
            for (Element relatedItem : recipientWrapper.getLinkedRelatedItems()) {
                String holderId = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
                if (holder.getId().toString().equals(holderId)) {
                    @SuppressWarnings("unchecked") Filter<Content> sharedMetadata = (Filter<Content>) Filters.element("part", MCRConstants.MODS_NAMESPACE).negate();
                    relatedItem.removeContent(sharedMetadata);
                    relatedItem.addContent(holderWrapper.getMODS().cloneContent());
                    LOGGER.info("Saving: {}", recipientId);
                    try {
                        MCRMetadataManager.update(recipient);
                    } catch (MCRPersistenceException | MCRAccessException e) {
                        throw new MCRPersistenceException("Error while updating shared metadata", e);
                    }
                }
            }
        }
    }
}
Also used : Element(org.jdom2.Element) MCRAccessException(org.mycore.access.MCRAccessException) MCRObject(org.mycore.datamodel.metadata.MCRObject) Filter(org.jdom2.filter.Filter) Content(org.jdom2.Content) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 22 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRRestAPIUploadHelper method deleteDerivate.

/**
 * deletes a whole derivate
 * @param info - the Jersey UriInfo object
 * @param request - the HTTPServletRequest object
 * @param pathParamMcrObjID - the MyCoRe Object ID
 * @param pathParamMcrDerID - the MyCoRe Derivate ID
 * @return a Jersey Response Object
 * @throws MCRRestAPIException
 */
public static Response deleteDerivate(UriInfo info, HttpServletRequest request, String pathParamMcrObjID, String pathParamMcrDerID) throws MCRRestAPIException {
    Response response = Response.status(Status.INTERNAL_SERVER_ERROR).build();
    SignedJWT signedJWT = MCRJSONWebTokenUtil.retrieveAuthenticationToken(request);
    String base64Signature = request.getHeader("X-MyCoRe-RestAPI-Signature");
    if (base64Signature == null) {
    // ToDo error handling
    }
    try (MCRJPATransactionWrapper mtw = new MCRJPATransactionWrapper()) {
        // MCRSession session = MCRServlet.getSession(request);
        MCRSession session = MCRSessionMgr.getCurrentSession();
        MCRUserInformation currentUser = session.getUserInformation();
        session.setUserInformation(MCRUserManager.getUser(MCRJSONWebTokenUtil.retrieveUsernameFromAuthenticationToken(signedJWT)));
        MCRObjectID objID = MCRObjectID.getInstance(pathParamMcrObjID);
        MCRObjectID derID = MCRObjectID.getInstance(pathParamMcrDerID);
        // MCRAccessManager.checkPermission() uses CACHE, which seems to be dirty from other calls????
        MCRAccessManager.invalidPermissionCache(derID.toString(), PERMISSION_DELETE);
        if (MCRAccessManager.checkPermission(derID.toString(), PERMISSION_DELETE)) {
            try {
                MCRMetadataManager.deleteMCRDerivate(derID);
            } catch (MCRPersistenceException pe) {
            // dir does not exist - do nothing
            } catch (MCRAccessException e) {
                LOGGER.error(e);
            }
        }
        session.setUserInformation(currentUser);
        response = Response.created(info.getBaseUriBuilder().path("v1/objects/" + objID + "/derivates").build()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, "Bearer " + MCRJSONWebTokenUtil.createJWTAuthorizationHeader(signedJWT)).build();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) MCRSession(org.mycore.common.MCRSession) MCRAccessException(org.mycore.access.MCRAccessException) SignedJWT(com.nimbusds.jwt.SignedJWT) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRUserInformation(org.mycore.common.MCRUserInformation) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 23 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRURNGranularOAIRegistrationService method register.

@Override
public MCRDNBURN register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    this.validateRegistration(obj, additional);
    MCRObjectDerivate derivate = ((MCRDerivate) obj).getDerivate();
    MCRDNBURN newURN;
    if (additional.equals("")) {
        /* Multiple URN for entire Derivate...  */
        newURN = registerURNsDerivate(obj, additional, derivate);
    } else {
        /* Single URN to one File... */
        newURN = registerSingleURN(obj, additional, derivate);
    }
    try {
        MCRMetadataManager.update(obj);
    } catch (Exception e) {
        throw new MCRPersistentIdentifierException("Error while updating derivate " + obj.getId(), e);
    }
    return newURN;
}
Also used : MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) IOException(java.io.IOException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Aggregations

MCRAccessException (org.mycore.access.MCRAccessException)23 MCRPersistenceException (org.mycore.common.MCRPersistenceException)12 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)12 IOException (java.io.IOException)11 MCRException (org.mycore.common.MCRException)9 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)9 MCRPath (org.mycore.datamodel.niofs.MCRPath)7 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)6 MCRObject (org.mycore.datamodel.metadata.MCRObject)6 Path (java.nio.file.Path)4 JDOMException (org.jdom2.JDOMException)4 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)4 SAXException (org.xml.sax.SAXException)4 List (java.util.List)3 Stream (java.util.stream.Stream)3 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)3 SignedJWT (com.nimbusds.jwt.SignedJWT)2 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2