use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRGoogleSitemapCommands method buildSitemap.
/**
* The build and store method.
*/
@MCRCommand(syntax = "build google sitemap", help = "Creates the google sitemap(s) in webapps directory.", order = 10)
public static void buildSitemap() throws Exception {
// check time
LOGGER.debug("Build Google sitemap start.");
final long start = System.currentTimeMillis();
// init
File webappBaseDir = new File(MCRConfiguration.instance().getString("MCR.WebApplication.basedir"));
MCRGoogleSitemapCommon common = new MCRGoogleSitemapCommon(webappBaseDir);
// remove old files
common.removeSitemapFiles();
// compute number of files
int number = common.checkSitemapFile();
LOGGER.debug("Build Google number of URL files {}.", Integer.toString(number));
if (number == 1) {
String fn = common.getFileName(1, true);
File xml = new File(fn);
Document jdom = common.buildSingleSitemap();
LOGGER.info("Write Google sitemap file {}.", fn);
new MCRJDOMContent(jdom).sendTo(xml);
} else {
String fn = common.getFileName(1, true);
File xml = new File(fn);
Document jdom = common.buildSitemapIndex(number);
LOGGER.info("Write Google sitemap file {}.", fn);
new MCRJDOMContent(jdom).sendTo(xml);
for (int i = 0; i < number; i++) {
fn = common.getFileName(i + 2, true);
xml = new File(fn);
jdom = common.buildPartSitemap(i);
LOGGER.info("Write Google sitemap file {}.", fn);
new MCRJDOMContent(jdom).sendTo(xml);
}
}
// check time
LOGGER.debug("Google sitemap request took {}ms.", System.currentTimeMillis() - start);
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRDOICommands method synchronizeDatabase.
@MCRCommand(syntax = "repair registered dois {0}", help = "Contacts the Registration Service and inserts all registered DOIs to the Database. It also updates all media files. The Service ID{0} is the id from the configuration.", order = 10)
public static void synchronizeDatabase(String serviceID) {
MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
try {
MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
List<MCRDigitalObjectIdentifier> doiList = dataciteClient.getDOIList();
doiList.stream().filter(doi -> {
boolean isTestDOI = doi.getPrefix().equals(MCRDigitalObjectIdentifier.TEST_DOI_PREFIX);
return !isTestDOI || registrationService.usesTestPrefix();
}).forEach(doi -> {
try {
URI uri = dataciteClient.resolveDOI(doi);
if (uri.toString().startsWith(registrationService.getRegisterURL())) {
LOGGER.info("Checking DOI: {}", doi.asString());
MCRObjectID objectID = getObjectID(uri);
if (MCRMetadataManager.exists(objectID)) {
if (!registrationService.isRegistered(objectID, "")) {
LOGGER.info("DOI is not registered in MyCoRe. Add to Database: {}", doi.asString());
MCRPI databaseEntry = new MCRPI(doi.asString(), registrationService.getType(), objectID.toString(), "", serviceID, new Date());
MCRHIBConnection.instance().getSession().save(databaseEntry);
}
// Update main files
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
List<Map.Entry<String, URI>> entryList = registrationService.getMediaList(obj);
dataciteClient.setMediaList(doi, entryList);
} else {
LOGGER.info("Could not find Object : {}", objectID);
}
} else {
LOGGER.info("DOI/URL is not from this application: {}/{}", doi.asString(), uri);
}
} catch (MCRPersistentIdentifierException e) {
LOGGER.error("Error occurred for DOI: {}", doi, e);
}
});
} catch (MCRPersistentIdentifierException e) {
LOGGER.error("Error while receiving DOI list from Registration-Service!", e);
}
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRDOICommands method updateMediaListOfAllDOI.
@MCRCommand(syntax = "repair media list of {0}", help = "Sends new media lists to Datacite. The Service ID{0} is the id from the configuration.")
public static List<String> updateMediaListOfAllDOI(String serviceID) {
MCRDOIRegistrationService registrationService = new MCRDOIRegistrationService(serviceID);
try {
MCRDataciteClient dataciteClient = registrationService.getDataciteClient();
List<MCRDigitalObjectIdentifier> doiList = dataciteClient.getDOIList();
return doiList.stream().filter(doi -> {
boolean isTestDOI = doi.getPrefix().equals(MCRDigitalObjectIdentifier.TEST_DOI_PREFIX);
return !isTestDOI || registrationService.usesTestPrefix();
}).map(MCRDigitalObjectIdentifier::asString).map(doiStr -> MessageFormat.format(REPAIR_MEDIALIST_OF_0_AND_SERVICE_1, doiStr, serviceID)).collect(Collectors.toList());
} catch (MCRPersistentIdentifierException e) {
LOGGER.error("Error while receiving DOI list from Registration-Service!", e);
}
return Collections.emptyList();
}
use of org.mycore.frontend.cli.annotation.MCRCommand 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);
}
use of org.mycore.frontend.cli.annotation.MCRCommand 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);
}
Aggregations