Search in sources :

Example 6 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierRegistrationResource method register.

@POST
@Path("service/{serviceName}/{mycoreId}")
@Produces(MediaType.APPLICATION_JSON)
public Response register(@PathParam("serviceName") String serviceName, @PathParam("mycoreId") String mycoreId, @DefaultValue("") @QueryParam("additional") String additional) {
    if (!MCRPIRegistrationServiceManager.getInstance().getServiceIDList().contains(serviceName)) {
        return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("No Registration Service found for " + serviceName)).build();
    }
    MCRPIRegistrationService<MCRPersistentIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceName);
    MCRObjectID mycoreIDObject;
    try {
        mycoreIDObject = MCRObjectID.getInstance(mycoreId);
    } catch (MCRException e) {
        LOGGER.error(e);
        return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("The provided id " + mycoreId + " seems to be invalid!", e)).build();
    }
    MCRPersistentIdentifier identifier;
    MCRBase object = MCRMetadataManager.retrieve(mycoreIDObject);
    try {
        identifier = registrationService.register(object, additional, true);
    } catch (MCRPersistentIdentifierException | MCRActiveLinkException e) {
        LOGGER.error(e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(buildErrorJSON("Error while register new identifier!", e)).build();
    } catch (MCRAccessException e) {
        LOGGER.error(e);
        return Response.status(Response.Status.FORBIDDEN).entity(buildErrorJSON("Error while register new identifier!", e)).build();
    }
    return Response.status(Response.Status.CREATED).entity(buildIdentifierObject(identifier)).build();
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 7 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRPICommands method removeControlFromObject.

@MCRCommand(syntax = "remove control {0} with service {1} with additional {2}", help = "This commands removes the " + "pi control from the object {0}(object id) with the serivce {1}(service id) and the additional {2}")
public static void removeControlFromObject(String objectIDString, String serviceID, String additional) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    MCRObjectID objectID = MCRObjectID.getInstance(objectIDString);
    MCRPI mcrpi = MCRPersistentIdentifierManager.getInstance().get(serviceID, objectIDString, additional != null ? additional.trim() : null);
    MCRPersistentIdentifierManager.getInstance().delete(mcrpi.getMycoreID(), mcrpi.getAdditional(), mcrpi.getType(), mcrpi.getService());
    MCRBase base = MCRMetadataManager.retrieve(objectID);
    if (MCRPIRegistrationService.removeFlagFromObject(base, mcrpi) == null) {
        throw new MCRPersistentIdentifierException("Could not delete Flag of object (flag not found)!");
    }
    MCRMetadataManager.update(base);
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 8 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRPICommands method controlObjectWithServiceAndAdditional.

@MCRCommand(syntax = "try to control {0} with service {1} with additional {2}", help = "This command tries to" + " read a pi from the object {0} with the MetadataManager from the specified service {1}." + " If the service configuration is right then the pi is under control of MyCoRe.")
public static void controlObjectWithServiceAndAdditional(String objectIDString, String serviceID, final String additional) throws MCRAccessException, MCRActiveLinkException, IOException {
    String trimAdditional = additional.trim();
    MCRPIRegistrationService<MCRPersistentIdentifier> service = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceID);
    MCRPersistentIdentifierMetadataManager<MCRPersistentIdentifier> metadataManager = service.getMetadataManager();
    MCRObjectID objectID = MCRObjectID.getInstance(objectIDString);
    MCRBase mcrBase = MCRMetadataManager.retrieve(objectID);
    Optional<MCRPersistentIdentifier> identifier;
    try {
        identifier = metadataManager.getIdentifier(mcrBase, trimAdditional);
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.info("Could not detect any identifier with service {}", serviceID, e);
        return;
    }
    if (!identifier.isPresent()) {
        LOGGER.info("Could not detect any identifier with service {}", serviceID);
        return;
    }
    MCRPersistentIdentifier persistentIdentifier = identifier.get();
    if (service.isRegistered(objectID, trimAdditional)) {
        LOGGER.info("Already present in Database: {}", serviceID);
        return;
    }
    MCRPI mcrpi = service.insertIdentifierToDatabase(mcrBase, trimAdditional, persistentIdentifier);
    MCRPIRegistrationService.addFlagToObject(mcrBase, mcrpi);
    MCRMetadataManager.update(mcrBase);
    LOGGER.info("{}:{} is now under control of {}", objectID, trimAdditional, serviceID);
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 9 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRDOIRegistrationService method transformToDatacite.

protected Document transformToDatacite(MCRDigitalObjectIdentifier doi, MCRBase mcrBase) throws MCRPersistentIdentifierException {
    MCRObjectID id = mcrBase.getId();
    MCRBaseContent content = new MCRBaseContent(mcrBase);
    try {
        MCRContent transform = MCRContentTransformerFactory.getTransformer(this.transformer).transform(content);
        Document dataciteDocument = transform.asXML();
        insertDOI(dataciteDocument, doi);
        Schema dataciteSchema = loadDataciteSchema();
        try {
            dataciteSchema.newValidator().validate(new JDOMSource(dataciteDocument));
        } catch (SAXException e) {
            String translatedInformation = MCRTranslation.translate(TRANSLATE_PREFIX + ERR_CODE_1_2);
            throw new MCRPersistentIdentifierException("The document " + id + " does not generate well formed Datacite!", translatedInformation, ERR_CODE_1_2, e);
        }
        return dataciteDocument;
    } catch (IOException | JDOMException | SAXException e) {
        throw new MCRPersistentIdentifierException("Could not transform the content of " + id + " with the transformer " + transformer, e);
    }
}
Also used : Schema(javax.xml.validation.Schema) MCRBaseContent(org.mycore.common.content.MCRBaseContent) JDOMSource(org.jdom2.transform.JDOMSource) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) JDOMException(org.jdom2.JDOMException) MCRContent(org.mycore.common.content.MCRContent) SAXException(org.xml.sax.SAXException)

Example 10 with MCRPersistentIdentifierException

use of org.mycore.pi.exceptions.MCRPersistentIdentifierException in project mycore by MyCoRe-Org.

the class MCRIdentifierXSLUtils method getPIServiceInformation.

/**
 * Gets all available services which are configured.
 * e.g.
 *   <ul>
 *     <li>&lt;service id="service1" inscribed="false" permission="true" type="urn" /&gt;</li>
 *     <li>&lt;service id="service2" inscribed="true" permission="false" type="doi" /&gt;</li>
 *   </ul>
 *
 * @param objectID the object
 * @return a Nodelist
 * @throws JDOMException
 */
public static NodeList getPIServiceInformation(String objectID) throws JDOMException {
    Element e = new Element("list");
    MCRBase obj = MCRMetadataManager.retrieve(MCRObjectID.getInstance(objectID));
    MCRConfiguration.instance().getPropertiesMap("MCR.PI.Registration.").keySet().stream().map(s -> s.substring("MCR.PI.Registration.".length())).filter(id -> !id.contains(".")).map((serviceID) -> MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceID)).map((rs -> {
        Element service = new Element("service");
        service.setAttribute("id", rs.getRegistrationServiceID());
        // Check if the inscriber of this service can read a PI
        try {
            if (rs.getMetadataManager().getIdentifier(obj, "").isPresent()) {
                service.setAttribute("inscribed", "true");
            } else {
                service.setAttribute("inscribed", "false");
            }
        } catch (MCRPersistentIdentifierException e1) {
            LOGGER.warn("Error happened while try to read PI from object: {}", objectID, e1);
            service.setAttribute("inscribed", "false");
        }
        // rights
        String permission = "register-" + rs.getRegistrationServiceID();
        Boolean canRegister = MCRAccessManager.checkPermission(objectID, "writedb") && MCRAccessManager.checkPermission(obj.getId(), permission);
        service.setAttribute("permission", canRegister.toString().toLowerCase(Locale.ROOT));
        // add the type
        service.setAttribute("type", rs.getType());
        return service;
    })).forEach(e::addContent);
    return new DOMOutputter().output(e).getElementsByTagName("service");
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) NodeList(org.w3c.dom.NodeList) DOMOutputter(org.jdom2.output.DOMOutputter) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRAccessManager(org.mycore.access.MCRAccessManager) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) Logger(org.apache.logging.log4j.Logger) JDOMException(org.jdom2.JDOMException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Locale(java.util.Locale) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) LogManager(org.apache.logging.log4j.LogManager) MCRPIRegistrationServiceManager(org.mycore.pi.MCRPIRegistrationServiceManager) Element(org.jdom2.Element) DOMOutputter(org.jdom2.output.DOMOutputter) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) Locale(java.util.Locale) Element(org.jdom2.Element) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException)

Aggregations

MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)20 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)11 MCRPI (org.mycore.pi.backend.MCRPI)8 MCRAccessException (org.mycore.access.MCRAccessException)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 MCRBase (org.mycore.datamodel.metadata.MCRBase)7 MCRException (org.mycore.common.MCRException)6 MCRObject (org.mycore.datamodel.metadata.MCRObject)6 Date (java.util.Date)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 LogManager (org.apache.logging.log4j.LogManager)4 Logger (org.apache.logging.log4j.Logger)4 MCRHIBConnection (org.mycore.backend.hibernate.MCRHIBConnection)4 MCRMetadataManager (org.mycore.datamodel.metadata.MCRMetadataManager)4 MCRPersistentIdentifier (org.mycore.pi.MCRPersistentIdentifier)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 List (java.util.List)3