Search in sources :

Example 11 with MCRPersistentIdentifierException

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

the class MCRDOICommands method updateMediaListForDOI.

@MCRCommand(syntax = REPAIR_MEDIALIST_OF_0_AND_SERVICE_1, help = "Sends new media list to Datacite. {0} is the DOI. The Service ID{1} is the id from the configuration.")
public static void updateMediaListForDOI(String doiString, String serviceID) {
    MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
    MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
    MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(doiString).orElseThrow(() -> new IllegalArgumentException("The String " + doiString + " is no valid DOI!"));
    try {
        URI uri = dataciteClient.resolveDOI(doi);
        if (uri.toString().startsWith(registrationService.getRegisterURL())) {
            String s = uri.toString();
            LOGGER.info("Checking DOI: {} / {}", doi.asString(), s);
            String idString = s.substring(s.lastIndexOf("/") + 1);
            MCRObjectID objectID = MCRObjectID.getInstance(idString);
            if (MCRMetadataManager.exists(objectID)) {
                MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
                List<Map.Entry<String, URI>> newMediaList = registrationService.getMediaList(obj);
                List<Map.Entry<String, URI>> oldMediaList;
                try {
                    oldMediaList = dataciteClient.getMediaList(doi);
                } catch (MCRIdentifierUnresolvableException e) {
                    LOGGER.warn("{} had no media list!", doi);
                    oldMediaList = new ArrayList<>();
                }
                HashMap<String, URI> newHashMap = new HashMap<>();
                newMediaList.forEach(e -> newHashMap.put(e.getKey(), e.getValue()));
                oldMediaList.forEach(e -> {
                    /*
                        Currently it is not possible to delete inserted values key values (mime types).
                        So we update old media mimetypes which are not present in new list to the same URL of the first
                        mimetype entry.
                        */
                    if (!newHashMap.containsKey(e.getKey())) {
                        newHashMap.put(e.getKey(), newMediaList.stream().findFirst().orElseThrow(() -> new MCRException("new media list is empty (this should not happen)")).getValue());
                    }
                });
                dataciteClient.setMediaList(doi, newHashMap.entrySet().stream().collect(Collectors.toList()));
                LOGGER.info("Updated media-list of {}", doiString);
            } else {
                LOGGER.info("Object {} does not exist in this application!", objectID);
            }
        } else {
            LOGGER.info("DOI is not from this application: ({}) {}", uri, registrationService.getRegisterURL());
        }
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.error("Error occurred for DOI: {}", doi, e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI) MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 12 with MCRPersistentIdentifierException

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

the class MCRPIRegistrationService method updateFlag.

public void updateFlag(MCRObjectID id, String additional, MCRPI mcrpi) {
    MCRBase obj = MCRMetadataManager.retrieve(id);
    MCRObjectService service = obj.getService();
    ArrayList<String> flags = service.getFlags(MCRPIRegistrationService.PI_FLAG);
    Gson gson = getGson();
    String stringFlag = flags.stream().filter(_stringFlag -> {
        MCRPI flag = gson.fromJson(_stringFlag, MCRPI.class);
        return flag.getAdditional().equals(additional) && flag.getIdentifier().equals(mcrpi.getIdentifier());
    }).findAny().orElseThrow(() -> new MCRException(new MCRPersistentIdentifierException("Could find flag to update (" + id + "," + additional + "," + mcrpi.getIdentifier() + ")")));
    int flagIndex = service.getFlagIndex(stringFlag);
    service.removeFlag(flagIndex);
    addFlagToObject(obj, mcrpi);
    try {
        MCRMetadataManager.update(obj);
    } catch (Exception e) {
        throw new MCRException("Could not update flags of object " + id, e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Gson(com.google.gson.Gson) MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 13 with MCRPersistentIdentifierException

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

the class MCRDOIRegistrationService method updateJob.

@Override
public void updateJob(Map<String, String> parameters) throws MCRPersistentIdentifierException {
    MCRDigitalObjectIdentifier doi = getDOIFromJob(parameters);
    String idString = parameters.get(CONTEXT_OBJ);
    MCRObjectID objectID = MCRObjectID.getInstance(idString);
    this.validateJobUserRights(objectID);
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    Document newDataciteMetadata = transformToDatacite(doi, object);
    MCRDataciteClient dataciteClient = getDataciteClient();
    try {
        URI uri = dataciteClient.resolveDOI(doi);
        URI registeredURI = getRegisteredURI(object);
        if (!uri.equals(registeredURI)) {
            LOGGER.info("Sending new URL({}) to Datacite!", registeredURI);
            dataciteClient.mintDOI(doi, registeredURI);
        }
    } catch (URISyntaxException e) {
        throw new MCRPersistentIdentifierException("Error while updating URL!", e);
    }
    Document oldDataciteMetadata = dataciteClient.resolveMetadata(doi);
    if (!MCRXMLHelper.deepEqual(newDataciteMetadata, oldDataciteMetadata)) {
        LOGGER.info("Sending new Metadata of {} to Datacite!", idString);
        dataciteClient.deleteMetadata(doi);
        dataciteClient.storeMetadata(newDataciteMetadata);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI)

Example 14 with MCRPersistentIdentifierException

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

the class MCRDOIRegistrationService method insertDOI.

private void insertDOI(Document datacite, MCRDigitalObjectIdentifier doi) throws MCRPersistentIdentifierException {
    XPathExpression<Element> compile = XPathFactory.instance().compile("//datacite:identifier[@identifierType='DOI']", Filters.element(), null, nameSpace);
    List<Element> doiList = compile.evaluate(datacite);
    if (doiList.size() > 1) {
        throw new MCRPersistentIdentifierException("There is more then one identifier with type DOI!");
    } else if (doiList.size() == 1) {
        Element doiElement = doiList.stream().findAny().get();
        LOGGER.warn("Found existing DOI({}) in Document will be replaced with {}", doiElement.getTextTrim(), doi.asString());
        doiElement.setText(doi.asString());
    } else {
        // must be 0
        Element doiElement = new Element("identifier", nameSpace);
        datacite.getRootElement().addContent(doiElement);
        doiElement.setAttribute("identifierType", "DOI");
        doiElement.setText(doi.asString());
    }
}
Also used : Element(org.jdom2.Element) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException)

Example 15 with MCRPersistentIdentifierException

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

the class MCRURNObjectXPathMetadataManager method insertIdentifier.

@Override
public void insertIdentifier(MCRDNBURN identifier, MCRBase obj, String additional) throws MCRPersistentIdentifierException {
    String xpath = getProperties().get("Xpath");
    Document xml = obj.createXML();
    MCRNodeBuilder nb = new MCRNodeBuilder();
    try {
        nb.buildElement(xpath, identifier.asString(), xml);
        MCRBase object = new MCRObject(xml);
        MCRMetadataManager.update(object);
    } catch (Exception e) {
        throw new MCRException("Error while inscribing URN to " + obj.getId(), e);
    }
}
Also used : MCRNodeBuilder(org.mycore.common.xml.MCRNodeBuilder) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRBase(org.mycore.datamodel.metadata.MCRBase) Document(org.jdom2.Document) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRException(org.mycore.common.MCRException)

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