use of org.mycore.pi.doi.MCRDOIParser in project mycore by MyCoRe-Org.
the class MCRMODSDOIPersistentIdentifierMetadataManager method getIdentifier.
@Override
public Optional<MCRPersistentIdentifier> getIdentifier(MCRBase base, String additional) throws MCRPersistentIdentifierException {
MCRObject object = checkObject(base);
MCRMODSWrapper wrapper = new MCRMODSWrapper(object);
Element element = wrapper.getElement("mods:identifier[@type='doi']");
if (element == null) {
return Optional.empty();
}
String doiText = element.getTextNormalize();
return new MCRDOIParser().parse(doiText).filter(Objects::nonNull).map(MCRPersistentIdentifier.class::cast);
}
use of org.mycore.pi.doi.MCRDOIParser 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);
}
}
use of org.mycore.pi.doi.MCRDOIParser in project mycore by MyCoRe-Org.
the class MCRDOICommands method repairIncompleteRegisteredDOI.
@MCRCommand(syntax = "repair incomplete registered doi {0} with registration service {1}", help = "Use this method if a DOI is registered, but not inserted in the Database. {0} is the DOI and " + "{1} the registration service from configuration.")
public static void repairIncompleteRegisteredDOI(String doiString, String serviceID) throws MCRPersistentIdentifierException, MCRAccessException, MCRActiveLinkException {
MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(doiString).orElseThrow(() -> new MCRException("Invalid DOI: " + doiString));
URI uri = dataciteClient.resolveDOI(doi);
if (!uri.toString().startsWith(registrationService.getRegisterURL())) {
LOGGER.info("DOI/URL is not from this application: {}/{}", doi.asString(), uri);
return;
}
MCRObjectID objectID = getObjectID(uri);
if (!MCRMetadataManager.exists(objectID)) {
LOGGER.info("Could not find Object : {}", objectID);
return;
}
MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(objectID);
MCRPersistentIdentifierMetadataManager<MCRDigitalObjectIdentifier> synchronizer = registrationService.getMetadataManager();
if (!registrationService.isRegistered(objectID, doiString)) {
LOGGER.info("{} is not found in PI-Database. Insert it..", objectID);
registrationService.insertIdentifierToDatabase(mcrObject, "", doi);
}
if (!synchronizer.getIdentifier(mcrObject, "").isPresent()) {
LOGGER.info("Object doesn't have Identifier inscribed! Insert it..");
synchronizer.insertIdentifier(doi, mcrObject, "");
MCRMetadataManager.update(mcrObject);
}
}
Aggregations