use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRDataciteClient method changeToTestDOI.
private void changeToTestDOI(Document metadata) {
XPathExpression<Element> compile = XPathFactory.instance().compile("//datacite:identifier[@identifierType='DOI']", Filters.element(), null, Namespace.getNamespace("datacite", "http://datacite.org/schema/kernel-3"));
Element element = compile.evaluateFirst(metadata);
MCRDigitalObjectIdentifier doi = new MCRDOIParser().parse(element.getText()).orElseThrow(() -> new MCRException("Datacite Document contains invalid DOI!"));
String testDOI = doi.toTestPrefix().asString();
element.setText(testDOI);
}
use of org.mycore.common.MCRException 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.common.MCRException 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);
}
}
use of org.mycore.common.MCRException 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);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRPIRegistrationService method getMetadataManager.
public MCRPersistentIdentifierMetadataManager<T> getMetadataManager() {
Map<String, String> properties = getProperties();
MCRConfiguration configuration = MCRConfiguration.instance();
String metadataManager;
if (properties.containsKey(METADATA_MANAGER_PROPERTY_KEY)) {
metadataManager = properties.get(METADATA_MANAGER_PROPERTY_KEY);
} else if (properties.containsKey(METADATA_MANAGER_DEPRECATED_PROPERTY_KEY)) {
LOGGER.warn(MessageFormat.format("You should use {0}{1}.{2} instead of {3}{4}.{5}", REGISTRATION_CONFIG_PREFIX, getRegistrationServiceID(), METADATA_MANAGER_PROPERTY_KEY, REGISTRATION_CONFIG_PREFIX, getRegistrationServiceID(), METADATA_MANAGER_DEPRECATED_PROPERTY_KEY));
metadataManager = properties.get(METADATA_MANAGER_DEPRECATED_PROPERTY_KEY);
} else {
throw new MCRConfigurationException(getRegistrationServiceID() + " has no MetadataManager(or legacy Inscriber)!");
}
String className;
String metadataManagerPropertyKey;
metadataManagerPropertyKey = METADATA_MANAGER_CONFIG_PREFIX + metadataManager;
className = configuration.getString(metadataManagerPropertyKey, null);
if (className == null) {
metadataManagerPropertyKey = METADATA_MANAGER_DEPRECATED_CONFIG_PREFIX + metadataManager;
className = configuration.getString(metadataManagerPropertyKey, null);
if (className == null) {
throw new MCRConfigurationException("Missing property: " + METADATA_MANAGER_CONFIG_PREFIX + metadataManager + " or " + metadataManagerPropertyKey);
}
LOGGER.warn("You should use {} instead of {}", METADATA_MANAGER_CONFIG_PREFIX + metadataManager, METADATA_MANAGER_DEPRECATED_PROPERTY_KEY + metadataManager);
}
className = repairDeprecatedClassNames(className, metadataManagerPropertyKey);
try {
@SuppressWarnings("unchecked") Class<MCRPersistentIdentifierMetadataManager<T>> classObject = (Class<MCRPersistentIdentifierMetadataManager<T>>) Class.forName(className);
Constructor<MCRPersistentIdentifierMetadataManager<T>> constructor = classObject.getConstructor(String.class);
return constructor.newInstance(metadataManager);
} catch (ClassNotFoundException e) {
throw new MCRConfigurationException("Configurated class (" + metadataManagerPropertyKey + ") not found: " + className, e);
} catch (NoSuchMethodException e) {
throw new MCRConfigurationException("Configurated class (" + metadataManagerPropertyKey + ") needs a string constructor: " + className);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new MCRException(e);
}
}
Aggregations