Search in sources :

Example 41 with MCRCommand

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);
}
Also used : MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 42 with MCRCommand

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);
    }
}
Also used : MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) Date(java.util.Date) HashMap(java.util.HashMap) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) Map(java.util.Map) URI(java.net.URI) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Collectors(java.util.stream.Collectors) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRPI(org.mycore.pi.backend.MCRPI) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) URI(java.net.URI) Date(java.util.Date) 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 43 with MCRCommand

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();
}
Also used : MCRDOIParser(org.mycore.pi.doi.MCRDOIParser) Date(java.util.Date) HashMap(java.util.HashMap) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRIdentifierUnresolvableException(org.mycore.pi.exceptions.MCRIdentifierUnresolvableException) Map(java.util.Map) URI(java.net.URI) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Collectors(java.util.stream.Collectors) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRPersistentIdentifierMetadataManager(org.mycore.pi.MCRPersistentIdentifierMetadataManager) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRDigitalObjectIdentifier(org.mycore.pi.doi.MCRDigitalObjectIdentifier) MCRDOIRegistrationService(org.mycore.pi.doi.MCRDOIRegistrationService) MCRDataciteClient(org.mycore.pi.doi.MCRDataciteClient) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 44 with MCRCommand

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);
}
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 45 with MCRCommand

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);
}
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)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11